ppc64 out-of-line register save/restore functions
[external/binutils.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2    Copyright (C) 1999-2015 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_can_gc_sections 1
76 #define elf_backend_can_refcount 1
77 #define elf_backend_rela_normal 1
78 #define elf_backend_default_execstack 0
79
80 #define bfd_elf64_mkobject                    ppc64_elf_mkobject
81 #define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
82 #define bfd_elf64_bfd_reloc_name_lookup       ppc64_elf_reloc_name_lookup
83 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
84 #define bfd_elf64_bfd_print_private_bfd_data  ppc64_elf_print_private_bfd_data
85 #define bfd_elf64_new_section_hook            ppc64_elf_new_section_hook
86 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
87 #define bfd_elf64_get_synthetic_symtab        ppc64_elf_get_synthetic_symtab
88 #define bfd_elf64_bfd_link_just_syms          ppc64_elf_link_just_syms
89
90 #define elf_backend_object_p                  ppc64_elf_object_p
91 #define elf_backend_grok_prstatus             ppc64_elf_grok_prstatus
92 #define elf_backend_grok_psinfo               ppc64_elf_grok_psinfo
93 #define elf_backend_write_core_note           ppc64_elf_write_core_note
94 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
95 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
96 #define elf_backend_add_symbol_hook           ppc64_elf_add_symbol_hook
97 #define elf_backend_check_directives          ppc64_elf_before_check_relocs
98 #define elf_backend_notice_as_needed          ppc64_elf_notice_as_needed
99 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
100 #define elf_backend_check_relocs              ppc64_elf_check_relocs
101 #define elf_backend_gc_keep                   ppc64_elf_gc_keep
102 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
103 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
104 #define elf_backend_gc_sweep_hook             ppc64_elf_gc_sweep_hook
105 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
106 #define elf_backend_hide_symbol               ppc64_elf_hide_symbol
107 #define elf_backend_maybe_function_sym        ppc64_elf_maybe_function_sym
108 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
109 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
110 #define elf_backend_hash_symbol               ppc64_elf_hash_symbol
111 #define elf_backend_init_index_section        _bfd_elf_init_2_index_sections
112 #define elf_backend_action_discarded          ppc64_elf_action_discarded
113 #define elf_backend_relocate_section          ppc64_elf_relocate_section
114 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
115 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
116 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
117 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
118 #define elf_backend_special_sections          ppc64_elf_special_sections
119 #define elf_backend_merge_symbol_attribute    ppc64_elf_merge_symbol_attribute
120
121 /* The name of the dynamic interpreter.  This is put in the .interp
122    section.  */
123 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
124
125 /* The size in bytes of an entry in the procedure linkage table.  */
126 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
127
128 /* The initial size of the plt reserved for the dynamic linker.  */
129 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
130
131 /* Offsets to some stack save slots.  */
132 #define STK_LR 16
133 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
134 /* This one is dodgy.  ELFv2 does not have a linker word, so use the
135    CR save slot.  Used only by optimised __tls_get_addr call stub,
136    relying on __tls_get_addr_opt not saving CR..  */
137 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
138
139 /* TOC base pointers offset from start of TOC.  */
140 #define TOC_BASE_OFF    0x8000
141 /* TOC base alignment.  */
142 #define TOC_BASE_ALIGN  256
143
144 /* Offset of tp and dtp pointers from start of TLS block.  */
145 #define TP_OFFSET       0x7000
146 #define DTP_OFFSET      0x8000
147
148 /* .plt call stub instructions.  The normal stub is like this, but
149    sometimes the .plt entry crosses a 64k boundary and we need to
150    insert an addi to adjust r11.  */
151 #define STD_R2_0R1      0xf8410000      /* std   %r2,0+40(%r1)       */
152 #define ADDIS_R11_R2    0x3d620000      /* addis %r11,%r2,xxx@ha     */
153 #define LD_R12_0R11     0xe98b0000      /* ld    %r12,xxx+0@l(%r11)  */
154 #define MTCTR_R12       0x7d8903a6      /* mtctr %r12                */
155 #define LD_R2_0R11      0xe84b0000      /* ld    %r2,xxx+8@l(%r11)   */
156 #define LD_R11_0R11     0xe96b0000      /* ld    %r11,xxx+16@l(%r11) */
157 #define BCTR            0x4e800420      /* bctr                      */
158
159 #define ADDI_R11_R11    0x396b0000      /* addi %r11,%r11,off@l  */
160 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
161 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
162
163 #define XOR_R2_R12_R12  0x7d826278      /* xor   %r2,%r12,%r12   */
164 #define ADD_R11_R11_R2  0x7d6b1214      /* add   %r11,%r11,%r2   */
165 #define XOR_R11_R12_R12 0x7d8b6278      /* xor   %r11,%r12,%r12  */
166 #define ADD_R2_R2_R11   0x7c425a14      /* add   %r2,%r2,%r11    */
167 #define CMPLDI_R2_0     0x28220000      /* cmpldi %r2,0          */
168 #define BNECTR          0x4ca20420      /* bnectr+               */
169 #define BNECTR_P4       0x4ce20420      /* bnectr+               */
170
171 #define LD_R12_0R2      0xe9820000      /* ld    %r12,xxx+0(%r2) */
172 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
173 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
174
175 #define LD_R2_0R1       0xe8410000      /* ld    %r2,0(%r1)      */
176
177 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha     */
178 #define ADDIS_R12_R12   0x3d8c0000      /* addis %r12,%r12,xxx@ha */
179 #define LD_R12_0R12     0xe98c0000      /* ld    %r12,xxx@l(%r12) */
180
181 /* glink call stub instructions.  We enter with the index in R0.  */
182 #define GLINK_CALL_STUB_SIZE (16*4)
183                                         /* 0:                           */
184                                         /*  .quad plt0-1f               */
185                                         /* __glink:                     */
186 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
187 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
188                                         /* 1:                           */
189 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
190                                         /*  ld %2,(0b-1b)(%11)          */
191 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
192 #define ADD_R11_R2_R11  0x7d625a14      /*  add %11,%2,%11              */
193                                         /*  ld %12,0(%11)               */
194                                         /*  ld %2,8(%11)                */
195                                         /*  mtctr %12                   */
196                                         /*  ld %11,16(%11)              */
197                                         /*  bctr                        */
198 #define MFLR_R0         0x7c0802a6      /*  mflr %r0                    */
199 #define MTLR_R0         0x7c0803a6      /*  mtlr %r0                    */
200 #define SUB_R12_R12_R11 0x7d8b6050      /*  subf %r12,%r11,%r12         */
201 #define ADDI_R0_R12     0x380c0000      /*  addi %r0,%r12,0             */
202 #define SRDI_R0_R0_2    0x7800f082      /*  rldicl %r0,%r0,62,2         */
203
204 /* Pad with this.  */
205 #define NOP             0x60000000
206
207 /* Some other nops.  */
208 #define CROR_151515     0x4def7b82
209 #define CROR_313131     0x4ffffb82
210
211 /* .glink entries for the first 32k functions are two instructions.  */
212 #define LI_R0_0         0x38000000      /* li    %r0,0          */
213 #define B_DOT           0x48000000      /* b     .              */
214
215 /* After that, we need two instructions to load the index, followed by
216    a branch.  */
217 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
218 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
219
220 /* Instructions used by the save and restore reg functions.  */
221 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
222 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
223 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
224 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
225 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
226 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
227 #define LI_R12_0        0x39800000      /* li    %r12,0         */
228 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
229 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
230 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
231 #define BLR             0x4e800020      /* blr                  */
232
233 /* Since .opd is an array of descriptors and each entry will end up
234    with identical R_PPC64_RELATIVE relocs, there is really no need to
235    propagate .opd relocs;  The dynamic linker should be taught to
236    relocate .opd without reloc entries.  */
237 #ifndef NO_OPD_RELOCS
238 #define NO_OPD_RELOCS 0
239 #endif
240
241 #ifndef ARRAY_SIZE
242 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
243 #endif
244
245 static inline int
246 abiversion (bfd *abfd)
247 {
248   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
249 }
250
251 static inline void
252 set_abiversion (bfd *abfd, int ver)
253 {
254   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
255   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
256 }
257 \f
258 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
259
260 /* Relocation HOWTO's.  */
261 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
262
263 static reloc_howto_type ppc64_elf_howto_raw[] = {
264   /* This reloc does nothing.  */
265   HOWTO (R_PPC64_NONE,          /* type */
266          0,                     /* rightshift */
267          3,                     /* size (0 = byte, 1 = short, 2 = long) */
268          0,                     /* bitsize */
269          FALSE,                 /* pc_relative */
270          0,                     /* bitpos */
271          complain_overflow_dont, /* complain_on_overflow */
272          bfd_elf_generic_reloc, /* special_function */
273          "R_PPC64_NONE",        /* name */
274          FALSE,                 /* partial_inplace */
275          0,                     /* src_mask */
276          0,                     /* dst_mask */
277          FALSE),                /* pcrel_offset */
278
279   /* A standard 32 bit relocation.  */
280   HOWTO (R_PPC64_ADDR32,        /* type */
281          0,                     /* rightshift */
282          2,                     /* size (0 = byte, 1 = short, 2 = long) */
283          32,                    /* bitsize */
284          FALSE,                 /* pc_relative */
285          0,                     /* bitpos */
286          complain_overflow_bitfield, /* complain_on_overflow */
287          bfd_elf_generic_reloc, /* special_function */
288          "R_PPC64_ADDR32",      /* name */
289          FALSE,                 /* partial_inplace */
290          0,                     /* src_mask */
291          0xffffffff,            /* dst_mask */
292          FALSE),                /* pcrel_offset */
293
294   /* An absolute 26 bit branch; the lower two bits must be zero.
295      FIXME: we don't check that, we just clear them.  */
296   HOWTO (R_PPC64_ADDR24,        /* type */
297          0,                     /* rightshift */
298          2,                     /* size (0 = byte, 1 = short, 2 = long) */
299          26,                    /* bitsize */
300          FALSE,                 /* pc_relative */
301          0,                     /* bitpos */
302          complain_overflow_bitfield, /* complain_on_overflow */
303          bfd_elf_generic_reloc, /* special_function */
304          "R_PPC64_ADDR24",      /* name */
305          FALSE,                 /* partial_inplace */
306          0,                     /* src_mask */
307          0x03fffffc,            /* dst_mask */
308          FALSE),                /* pcrel_offset */
309
310   /* A standard 16 bit relocation.  */
311   HOWTO (R_PPC64_ADDR16,        /* type */
312          0,                     /* rightshift */
313          1,                     /* size (0 = byte, 1 = short, 2 = long) */
314          16,                    /* bitsize */
315          FALSE,                 /* pc_relative */
316          0,                     /* bitpos */
317          complain_overflow_bitfield, /* complain_on_overflow */
318          bfd_elf_generic_reloc, /* special_function */
319          "R_PPC64_ADDR16",      /* name */
320          FALSE,                 /* partial_inplace */
321          0,                     /* src_mask */
322          0xffff,                /* dst_mask */
323          FALSE),                /* pcrel_offset */
324
325   /* A 16 bit relocation without overflow.  */
326   HOWTO (R_PPC64_ADDR16_LO,     /* type */
327          0,                     /* rightshift */
328          1,                     /* size (0 = byte, 1 = short, 2 = long) */
329          16,                    /* bitsize */
330          FALSE,                 /* pc_relative */
331          0,                     /* bitpos */
332          complain_overflow_dont,/* complain_on_overflow */
333          bfd_elf_generic_reloc, /* special_function */
334          "R_PPC64_ADDR16_LO",   /* name */
335          FALSE,                 /* partial_inplace */
336          0,                     /* src_mask */
337          0xffff,                /* dst_mask */
338          FALSE),                /* pcrel_offset */
339
340   /* Bits 16-31 of an address.  */
341   HOWTO (R_PPC64_ADDR16_HI,     /* type */
342          16,                    /* rightshift */
343          1,                     /* size (0 = byte, 1 = short, 2 = long) */
344          16,                    /* bitsize */
345          FALSE,                 /* pc_relative */
346          0,                     /* bitpos */
347          complain_overflow_signed, /* complain_on_overflow */
348          bfd_elf_generic_reloc, /* special_function */
349          "R_PPC64_ADDR16_HI",   /* name */
350          FALSE,                 /* partial_inplace */
351          0,                     /* src_mask */
352          0xffff,                /* dst_mask */
353          FALSE),                /* pcrel_offset */
354
355   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
356      bits, treated as a signed number, is negative.  */
357   HOWTO (R_PPC64_ADDR16_HA,     /* type */
358          16,                    /* rightshift */
359          1,                     /* size (0 = byte, 1 = short, 2 = long) */
360          16,                    /* bitsize */
361          FALSE,                 /* pc_relative */
362          0,                     /* bitpos */
363          complain_overflow_signed, /* complain_on_overflow */
364          ppc64_elf_ha_reloc,    /* special_function */
365          "R_PPC64_ADDR16_HA",   /* name */
366          FALSE,                 /* partial_inplace */
367          0,                     /* src_mask */
368          0xffff,                /* dst_mask */
369          FALSE),                /* pcrel_offset */
370
371   /* An absolute 16 bit branch; the lower two bits must be zero.
372      FIXME: we don't check that, we just clear them.  */
373   HOWTO (R_PPC64_ADDR14,        /* type */
374          0,                     /* rightshift */
375          2,                     /* size (0 = byte, 1 = short, 2 = long) */
376          16,                    /* bitsize */
377          FALSE,                 /* pc_relative */
378          0,                     /* bitpos */
379          complain_overflow_signed, /* complain_on_overflow */
380          ppc64_elf_branch_reloc, /* special_function */
381          "R_PPC64_ADDR14",      /* name */
382          FALSE,                 /* partial_inplace */
383          0,                     /* src_mask */
384          0x0000fffc,            /* dst_mask */
385          FALSE),                /* pcrel_offset */
386
387   /* An absolute 16 bit branch, for which bit 10 should be set to
388      indicate that the branch is expected to be taken.  The lower two
389      bits must be zero.  */
390   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
391          0,                     /* rightshift */
392          2,                     /* size (0 = byte, 1 = short, 2 = long) */
393          16,                    /* bitsize */
394          FALSE,                 /* pc_relative */
395          0,                     /* bitpos */
396          complain_overflow_signed, /* complain_on_overflow */
397          ppc64_elf_brtaken_reloc, /* special_function */
398          "R_PPC64_ADDR14_BRTAKEN",/* name */
399          FALSE,                 /* partial_inplace */
400          0,                     /* src_mask */
401          0x0000fffc,            /* dst_mask */
402          FALSE),                /* pcrel_offset */
403
404   /* An absolute 16 bit branch, for which bit 10 should be set to
405      indicate that the branch is not expected to be taken.  The lower
406      two bits must be zero.  */
407   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
408          0,                     /* rightshift */
409          2,                     /* size (0 = byte, 1 = short, 2 = long) */
410          16,                    /* bitsize */
411          FALSE,                 /* pc_relative */
412          0,                     /* bitpos */
413          complain_overflow_signed, /* complain_on_overflow */
414          ppc64_elf_brtaken_reloc, /* special_function */
415          "R_PPC64_ADDR14_BRNTAKEN",/* name */
416          FALSE,                 /* partial_inplace */
417          0,                     /* src_mask */
418          0x0000fffc,            /* dst_mask */
419          FALSE),                /* pcrel_offset */
420
421   /* A relative 26 bit branch; the lower two bits must be zero.  */
422   HOWTO (R_PPC64_REL24,         /* type */
423          0,                     /* rightshift */
424          2,                     /* size (0 = byte, 1 = short, 2 = long) */
425          26,                    /* bitsize */
426          TRUE,                  /* pc_relative */
427          0,                     /* bitpos */
428          complain_overflow_signed, /* complain_on_overflow */
429          ppc64_elf_branch_reloc, /* special_function */
430          "R_PPC64_REL24",       /* name */
431          FALSE,                 /* partial_inplace */
432          0,                     /* src_mask */
433          0x03fffffc,            /* dst_mask */
434          TRUE),                 /* pcrel_offset */
435
436   /* A relative 16 bit branch; the lower two bits must be zero.  */
437   HOWTO (R_PPC64_REL14,         /* type */
438          0,                     /* rightshift */
439          2,                     /* size (0 = byte, 1 = short, 2 = long) */
440          16,                    /* bitsize */
441          TRUE,                  /* pc_relative */
442          0,                     /* bitpos */
443          complain_overflow_signed, /* complain_on_overflow */
444          ppc64_elf_branch_reloc, /* special_function */
445          "R_PPC64_REL14",       /* name */
446          FALSE,                 /* partial_inplace */
447          0,                     /* src_mask */
448          0x0000fffc,            /* dst_mask */
449          TRUE),                 /* pcrel_offset */
450
451   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
452      the branch is expected to be taken.  The lower two bits must be
453      zero.  */
454   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
455          0,                     /* rightshift */
456          2,                     /* size (0 = byte, 1 = short, 2 = long) */
457          16,                    /* bitsize */
458          TRUE,                  /* pc_relative */
459          0,                     /* bitpos */
460          complain_overflow_signed, /* complain_on_overflow */
461          ppc64_elf_brtaken_reloc, /* special_function */
462          "R_PPC64_REL14_BRTAKEN", /* name */
463          FALSE,                 /* partial_inplace */
464          0,                     /* src_mask */
465          0x0000fffc,            /* dst_mask */
466          TRUE),                 /* pcrel_offset */
467
468   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
469      the branch is not expected to be taken.  The lower two bits must
470      be zero.  */
471   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
472          0,                     /* rightshift */
473          2,                     /* size (0 = byte, 1 = short, 2 = long) */
474          16,                    /* bitsize */
475          TRUE,                  /* pc_relative */
476          0,                     /* bitpos */
477          complain_overflow_signed, /* complain_on_overflow */
478          ppc64_elf_brtaken_reloc, /* special_function */
479          "R_PPC64_REL14_BRNTAKEN",/* name */
480          FALSE,                 /* partial_inplace */
481          0,                     /* src_mask */
482          0x0000fffc,            /* dst_mask */
483          TRUE),                 /* pcrel_offset */
484
485   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
486      symbol.  */
487   HOWTO (R_PPC64_GOT16,         /* type */
488          0,                     /* rightshift */
489          1,                     /* size (0 = byte, 1 = short, 2 = long) */
490          16,                    /* bitsize */
491          FALSE,                 /* pc_relative */
492          0,                     /* bitpos */
493          complain_overflow_signed, /* complain_on_overflow */
494          ppc64_elf_unhandled_reloc, /* special_function */
495          "R_PPC64_GOT16",       /* name */
496          FALSE,                 /* partial_inplace */
497          0,                     /* src_mask */
498          0xffff,                /* dst_mask */
499          FALSE),                /* pcrel_offset */
500
501   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
502      the symbol.  */
503   HOWTO (R_PPC64_GOT16_LO,      /* type */
504          0,                     /* rightshift */
505          1,                     /* size (0 = byte, 1 = short, 2 = long) */
506          16,                    /* bitsize */
507          FALSE,                 /* pc_relative */
508          0,                     /* bitpos */
509          complain_overflow_dont, /* complain_on_overflow */
510          ppc64_elf_unhandled_reloc, /* special_function */
511          "R_PPC64_GOT16_LO",    /* name */
512          FALSE,                 /* partial_inplace */
513          0,                     /* src_mask */
514          0xffff,                /* dst_mask */
515          FALSE),                /* pcrel_offset */
516
517   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
518      the symbol.  */
519   HOWTO (R_PPC64_GOT16_HI,      /* type */
520          16,                    /* rightshift */
521          1,                     /* size (0 = byte, 1 = short, 2 = long) */
522          16,                    /* bitsize */
523          FALSE,                 /* pc_relative */
524          0,                     /* bitpos */
525          complain_overflow_signed,/* complain_on_overflow */
526          ppc64_elf_unhandled_reloc, /* special_function */
527          "R_PPC64_GOT16_HI",    /* name */
528          FALSE,                 /* partial_inplace */
529          0,                     /* src_mask */
530          0xffff,                /* dst_mask */
531          FALSE),                /* pcrel_offset */
532
533   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
534      the symbol.  */
535   HOWTO (R_PPC64_GOT16_HA,      /* type */
536          16,                    /* rightshift */
537          1,                     /* size (0 = byte, 1 = short, 2 = long) */
538          16,                    /* bitsize */
539          FALSE,                 /* pc_relative */
540          0,                     /* bitpos */
541          complain_overflow_signed,/* complain_on_overflow */
542          ppc64_elf_unhandled_reloc, /* special_function */
543          "R_PPC64_GOT16_HA",    /* name */
544          FALSE,                 /* partial_inplace */
545          0,                     /* src_mask */
546          0xffff,                /* dst_mask */
547          FALSE),                /* pcrel_offset */
548
549   /* This is used only by the dynamic linker.  The symbol should exist
550      both in the object being run and in some shared library.  The
551      dynamic linker copies the data addressed by the symbol from the
552      shared library into the object, because the object being
553      run has to have the data at some particular address.  */
554   HOWTO (R_PPC64_COPY,          /* type */
555          0,                     /* rightshift */
556          0,                     /* this one is variable size */
557          0,                     /* bitsize */
558          FALSE,                 /* pc_relative */
559          0,                     /* bitpos */
560          complain_overflow_dont, /* complain_on_overflow */
561          ppc64_elf_unhandled_reloc, /* special_function */
562          "R_PPC64_COPY",        /* name */
563          FALSE,                 /* partial_inplace */
564          0,                     /* src_mask */
565          0,                     /* dst_mask */
566          FALSE),                /* pcrel_offset */
567
568   /* Like R_PPC64_ADDR64, but used when setting global offset table
569      entries.  */
570   HOWTO (R_PPC64_GLOB_DAT,      /* type */
571          0,                     /* rightshift */
572          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
573          64,                    /* bitsize */
574          FALSE,                 /* pc_relative */
575          0,                     /* bitpos */
576          complain_overflow_dont, /* complain_on_overflow */
577          ppc64_elf_unhandled_reloc,  /* special_function */
578          "R_PPC64_GLOB_DAT",    /* name */
579          FALSE,                 /* partial_inplace */
580          0,                     /* src_mask */
581          ONES (64),             /* dst_mask */
582          FALSE),                /* pcrel_offset */
583
584   /* Created by the link editor.  Marks a procedure linkage table
585      entry for a symbol.  */
586   HOWTO (R_PPC64_JMP_SLOT,      /* type */
587          0,                     /* rightshift */
588          0,                     /* size (0 = byte, 1 = short, 2 = long) */
589          0,                     /* bitsize */
590          FALSE,                 /* pc_relative */
591          0,                     /* bitpos */
592          complain_overflow_dont, /* complain_on_overflow */
593          ppc64_elf_unhandled_reloc, /* special_function */
594          "R_PPC64_JMP_SLOT",    /* name */
595          FALSE,                 /* partial_inplace */
596          0,                     /* src_mask */
597          0,                     /* dst_mask */
598          FALSE),                /* pcrel_offset */
599
600   /* Used only by the dynamic linker.  When the object is run, this
601      doubleword64 is set to the load address of the object, plus the
602      addend.  */
603   HOWTO (R_PPC64_RELATIVE,      /* type */
604          0,                     /* rightshift */
605          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
606          64,                    /* bitsize */
607          FALSE,                 /* pc_relative */
608          0,                     /* bitpos */
609          complain_overflow_dont, /* complain_on_overflow */
610          bfd_elf_generic_reloc, /* special_function */
611          "R_PPC64_RELATIVE",    /* name */
612          FALSE,                 /* partial_inplace */
613          0,                     /* src_mask */
614          ONES (64),             /* dst_mask */
615          FALSE),                /* pcrel_offset */
616
617   /* Like R_PPC64_ADDR32, but may be unaligned.  */
618   HOWTO (R_PPC64_UADDR32,       /* type */
619          0,                     /* rightshift */
620          2,                     /* size (0 = byte, 1 = short, 2 = long) */
621          32,                    /* bitsize */
622          FALSE,                 /* pc_relative */
623          0,                     /* bitpos */
624          complain_overflow_bitfield, /* complain_on_overflow */
625          bfd_elf_generic_reloc, /* special_function */
626          "R_PPC64_UADDR32",     /* name */
627          FALSE,                 /* partial_inplace */
628          0,                     /* src_mask */
629          0xffffffff,            /* dst_mask */
630          FALSE),                /* pcrel_offset */
631
632   /* Like R_PPC64_ADDR16, but may be unaligned.  */
633   HOWTO (R_PPC64_UADDR16,       /* type */
634          0,                     /* rightshift */
635          1,                     /* size (0 = byte, 1 = short, 2 = long) */
636          16,                    /* bitsize */
637          FALSE,                 /* pc_relative */
638          0,                     /* bitpos */
639          complain_overflow_bitfield, /* complain_on_overflow */
640          bfd_elf_generic_reloc, /* special_function */
641          "R_PPC64_UADDR16",     /* name */
642          FALSE,                 /* partial_inplace */
643          0,                     /* src_mask */
644          0xffff,                /* dst_mask */
645          FALSE),                /* pcrel_offset */
646
647   /* 32-bit PC relative.  */
648   HOWTO (R_PPC64_REL32,         /* type */
649          0,                     /* rightshift */
650          2,                     /* size (0 = byte, 1 = short, 2 = long) */
651          32,                    /* bitsize */
652          TRUE,                  /* pc_relative */
653          0,                     /* bitpos */
654          complain_overflow_signed, /* complain_on_overflow */
655          bfd_elf_generic_reloc, /* special_function */
656          "R_PPC64_REL32",       /* name */
657          FALSE,                 /* partial_inplace */
658          0,                     /* src_mask */
659          0xffffffff,            /* dst_mask */
660          TRUE),                 /* pcrel_offset */
661
662   /* 32-bit relocation to the symbol's procedure linkage table.  */
663   HOWTO (R_PPC64_PLT32,         /* type */
664          0,                     /* rightshift */
665          2,                     /* size (0 = byte, 1 = short, 2 = long) */
666          32,                    /* bitsize */
667          FALSE,                 /* pc_relative */
668          0,                     /* bitpos */
669          complain_overflow_bitfield, /* complain_on_overflow */
670          ppc64_elf_unhandled_reloc, /* special_function */
671          "R_PPC64_PLT32",       /* name */
672          FALSE,                 /* partial_inplace */
673          0,                     /* src_mask */
674          0xffffffff,            /* dst_mask */
675          FALSE),                /* pcrel_offset */
676
677   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
678      FIXME: R_PPC64_PLTREL32 not supported.  */
679   HOWTO (R_PPC64_PLTREL32,      /* type */
680          0,                     /* rightshift */
681          2,                     /* size (0 = byte, 1 = short, 2 = long) */
682          32,                    /* bitsize */
683          TRUE,                  /* pc_relative */
684          0,                     /* bitpos */
685          complain_overflow_signed, /* complain_on_overflow */
686          bfd_elf_generic_reloc, /* special_function */
687          "R_PPC64_PLTREL32",    /* name */
688          FALSE,                 /* partial_inplace */
689          0,                     /* src_mask */
690          0xffffffff,            /* dst_mask */
691          TRUE),                 /* pcrel_offset */
692
693   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
694      the symbol.  */
695   HOWTO (R_PPC64_PLT16_LO,      /* type */
696          0,                     /* rightshift */
697          1,                     /* size (0 = byte, 1 = short, 2 = long) */
698          16,                    /* bitsize */
699          FALSE,                 /* pc_relative */
700          0,                     /* bitpos */
701          complain_overflow_dont, /* complain_on_overflow */
702          ppc64_elf_unhandled_reloc, /* special_function */
703          "R_PPC64_PLT16_LO",    /* name */
704          FALSE,                 /* partial_inplace */
705          0,                     /* src_mask */
706          0xffff,                /* dst_mask */
707          FALSE),                /* pcrel_offset */
708
709   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
710      the symbol.  */
711   HOWTO (R_PPC64_PLT16_HI,      /* type */
712          16,                    /* rightshift */
713          1,                     /* size (0 = byte, 1 = short, 2 = long) */
714          16,                    /* bitsize */
715          FALSE,                 /* pc_relative */
716          0,                     /* bitpos */
717          complain_overflow_signed, /* complain_on_overflow */
718          ppc64_elf_unhandled_reloc, /* special_function */
719          "R_PPC64_PLT16_HI",    /* name */
720          FALSE,                 /* partial_inplace */
721          0,                     /* src_mask */
722          0xffff,                /* dst_mask */
723          FALSE),                /* pcrel_offset */
724
725   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
726      the symbol.  */
727   HOWTO (R_PPC64_PLT16_HA,      /* type */
728          16,                    /* rightshift */
729          1,                     /* size (0 = byte, 1 = short, 2 = long) */
730          16,                    /* bitsize */
731          FALSE,                 /* pc_relative */
732          0,                     /* bitpos */
733          complain_overflow_signed, /* complain_on_overflow */
734          ppc64_elf_unhandled_reloc, /* special_function */
735          "R_PPC64_PLT16_HA",    /* name */
736          FALSE,                 /* partial_inplace */
737          0,                     /* src_mask */
738          0xffff,                /* dst_mask */
739          FALSE),                /* pcrel_offset */
740
741   /* 16-bit section relative relocation.  */
742   HOWTO (R_PPC64_SECTOFF,       /* type */
743          0,                     /* rightshift */
744          1,                     /* size (0 = byte, 1 = short, 2 = long) */
745          16,                    /* bitsize */
746          FALSE,                 /* pc_relative */
747          0,                     /* bitpos */
748          complain_overflow_signed, /* complain_on_overflow */
749          ppc64_elf_sectoff_reloc, /* special_function */
750          "R_PPC64_SECTOFF",     /* name */
751          FALSE,                 /* partial_inplace */
752          0,                     /* src_mask */
753          0xffff,                /* dst_mask */
754          FALSE),                /* pcrel_offset */
755
756   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
757   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
758          0,                     /* rightshift */
759          1,                     /* size (0 = byte, 1 = short, 2 = long) */
760          16,                    /* bitsize */
761          FALSE,                 /* pc_relative */
762          0,                     /* bitpos */
763          complain_overflow_dont, /* complain_on_overflow */
764          ppc64_elf_sectoff_reloc, /* special_function */
765          "R_PPC64_SECTOFF_LO",  /* name */
766          FALSE,                 /* partial_inplace */
767          0,                     /* src_mask */
768          0xffff,                /* dst_mask */
769          FALSE),                /* pcrel_offset */
770
771   /* 16-bit upper half section relative relocation.  */
772   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
773          16,                    /* rightshift */
774          1,                     /* size (0 = byte, 1 = short, 2 = long) */
775          16,                    /* bitsize */
776          FALSE,                 /* pc_relative */
777          0,                     /* bitpos */
778          complain_overflow_signed, /* complain_on_overflow */
779          ppc64_elf_sectoff_reloc, /* special_function */
780          "R_PPC64_SECTOFF_HI",  /* name */
781          FALSE,                 /* partial_inplace */
782          0,                     /* src_mask */
783          0xffff,                /* dst_mask */
784          FALSE),                /* pcrel_offset */
785
786   /* 16-bit upper half adjusted section relative relocation.  */
787   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
788          16,                    /* rightshift */
789          1,                     /* size (0 = byte, 1 = short, 2 = long) */
790          16,                    /* bitsize */
791          FALSE,                 /* pc_relative */
792          0,                     /* bitpos */
793          complain_overflow_signed, /* complain_on_overflow */
794          ppc64_elf_sectoff_ha_reloc, /* special_function */
795          "R_PPC64_SECTOFF_HA",  /* name */
796          FALSE,                 /* partial_inplace */
797          0,                     /* src_mask */
798          0xffff,                /* dst_mask */
799          FALSE),                /* pcrel_offset */
800
801   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
802   HOWTO (R_PPC64_REL30,         /* type */
803          2,                     /* rightshift */
804          2,                     /* size (0 = byte, 1 = short, 2 = long) */
805          30,                    /* bitsize */
806          TRUE,                  /* pc_relative */
807          0,                     /* bitpos */
808          complain_overflow_dont, /* complain_on_overflow */
809          bfd_elf_generic_reloc, /* special_function */
810          "R_PPC64_REL30",       /* name */
811          FALSE,                 /* partial_inplace */
812          0,                     /* src_mask */
813          0xfffffffc,            /* dst_mask */
814          TRUE),                 /* pcrel_offset */
815
816   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
817
818   /* A standard 64-bit relocation.  */
819   HOWTO (R_PPC64_ADDR64,        /* type */
820          0,                     /* rightshift */
821          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
822          64,                    /* bitsize */
823          FALSE,                 /* pc_relative */
824          0,                     /* bitpos */
825          complain_overflow_dont, /* complain_on_overflow */
826          bfd_elf_generic_reloc, /* special_function */
827          "R_PPC64_ADDR64",      /* name */
828          FALSE,                 /* partial_inplace */
829          0,                     /* src_mask */
830          ONES (64),             /* dst_mask */
831          FALSE),                /* pcrel_offset */
832
833   /* The bits 32-47 of an address.  */
834   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
835          32,                    /* rightshift */
836          1,                     /* size (0 = byte, 1 = short, 2 = long) */
837          16,                    /* bitsize */
838          FALSE,                 /* pc_relative */
839          0,                     /* bitpos */
840          complain_overflow_dont, /* complain_on_overflow */
841          bfd_elf_generic_reloc, /* special_function */
842          "R_PPC64_ADDR16_HIGHER", /* name */
843          FALSE,                 /* partial_inplace */
844          0,                     /* src_mask */
845          0xffff,                /* dst_mask */
846          FALSE),                /* pcrel_offset */
847
848   /* The bits 32-47 of an address, plus 1 if the contents of the low
849      16 bits, treated as a signed number, is negative.  */
850   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
851          32,                    /* rightshift */
852          1,                     /* size (0 = byte, 1 = short, 2 = long) */
853          16,                    /* bitsize */
854          FALSE,                 /* pc_relative */
855          0,                     /* bitpos */
856          complain_overflow_dont, /* complain_on_overflow */
857          ppc64_elf_ha_reloc,    /* special_function */
858          "R_PPC64_ADDR16_HIGHERA", /* name */
859          FALSE,                 /* partial_inplace */
860          0,                     /* src_mask */
861          0xffff,                /* dst_mask */
862          FALSE),                /* pcrel_offset */
863
864   /* The bits 48-63 of an address.  */
865   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
866          48,                    /* rightshift */
867          1,                     /* size (0 = byte, 1 = short, 2 = long) */
868          16,                    /* bitsize */
869          FALSE,                 /* pc_relative */
870          0,                     /* bitpos */
871          complain_overflow_dont, /* complain_on_overflow */
872          bfd_elf_generic_reloc, /* special_function */
873          "R_PPC64_ADDR16_HIGHEST", /* name */
874          FALSE,                 /* partial_inplace */
875          0,                     /* src_mask */
876          0xffff,                /* dst_mask */
877          FALSE),                /* pcrel_offset */
878
879   /* The bits 48-63 of an address, plus 1 if the contents of the low
880      16 bits, treated as a signed number, is negative.  */
881   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
882          48,                    /* rightshift */
883          1,                     /* size (0 = byte, 1 = short, 2 = long) */
884          16,                    /* bitsize */
885          FALSE,                 /* pc_relative */
886          0,                     /* bitpos */
887          complain_overflow_dont, /* complain_on_overflow */
888          ppc64_elf_ha_reloc,    /* special_function */
889          "R_PPC64_ADDR16_HIGHESTA", /* name */
890          FALSE,                 /* partial_inplace */
891          0,                     /* src_mask */
892          0xffff,                /* dst_mask */
893          FALSE),                /* pcrel_offset */
894
895   /* Like ADDR64, but may be unaligned.  */
896   HOWTO (R_PPC64_UADDR64,       /* type */
897          0,                     /* rightshift */
898          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
899          64,                    /* bitsize */
900          FALSE,                 /* pc_relative */
901          0,                     /* bitpos */
902          complain_overflow_dont, /* complain_on_overflow */
903          bfd_elf_generic_reloc, /* special_function */
904          "R_PPC64_UADDR64",     /* name */
905          FALSE,                 /* partial_inplace */
906          0,                     /* src_mask */
907          ONES (64),             /* dst_mask */
908          FALSE),                /* pcrel_offset */
909
910   /* 64-bit relative relocation.  */
911   HOWTO (R_PPC64_REL64,         /* type */
912          0,                     /* rightshift */
913          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
914          64,                    /* bitsize */
915          TRUE,                  /* pc_relative */
916          0,                     /* bitpos */
917          complain_overflow_dont, /* complain_on_overflow */
918          bfd_elf_generic_reloc, /* special_function */
919          "R_PPC64_REL64",       /* name */
920          FALSE,                 /* partial_inplace */
921          0,                     /* src_mask */
922          ONES (64),             /* dst_mask */
923          TRUE),                 /* pcrel_offset */
924
925   /* 64-bit relocation to the symbol's procedure linkage table.  */
926   HOWTO (R_PPC64_PLT64,         /* type */
927          0,                     /* rightshift */
928          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
929          64,                    /* bitsize */
930          FALSE,                 /* pc_relative */
931          0,                     /* bitpos */
932          complain_overflow_dont, /* complain_on_overflow */
933          ppc64_elf_unhandled_reloc, /* special_function */
934          "R_PPC64_PLT64",       /* name */
935          FALSE,                 /* partial_inplace */
936          0,                     /* src_mask */
937          ONES (64),             /* dst_mask */
938          FALSE),                /* pcrel_offset */
939
940   /* 64-bit PC relative relocation to the symbol's procedure linkage
941      table.  */
942   /* FIXME: R_PPC64_PLTREL64 not supported.  */
943   HOWTO (R_PPC64_PLTREL64,      /* type */
944          0,                     /* rightshift */
945          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
946          64,                    /* bitsize */
947          TRUE,                  /* pc_relative */
948          0,                     /* bitpos */
949          complain_overflow_dont, /* complain_on_overflow */
950          ppc64_elf_unhandled_reloc, /* special_function */
951          "R_PPC64_PLTREL64",    /* name */
952          FALSE,                 /* partial_inplace */
953          0,                     /* src_mask */
954          ONES (64),             /* dst_mask */
955          TRUE),                 /* pcrel_offset */
956
957   /* 16 bit TOC-relative relocation.  */
958
959   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
960   HOWTO (R_PPC64_TOC16,         /* type */
961          0,                     /* rightshift */
962          1,                     /* size (0 = byte, 1 = short, 2 = long) */
963          16,                    /* bitsize */
964          FALSE,                 /* pc_relative */
965          0,                     /* bitpos */
966          complain_overflow_signed, /* complain_on_overflow */
967          ppc64_elf_toc_reloc,   /* special_function */
968          "R_PPC64_TOC16",       /* name */
969          FALSE,                 /* partial_inplace */
970          0,                     /* src_mask */
971          0xffff,                /* dst_mask */
972          FALSE),                /* pcrel_offset */
973
974   /* 16 bit TOC-relative relocation without overflow.  */
975
976   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
977   HOWTO (R_PPC64_TOC16_LO,      /* type */
978          0,                     /* rightshift */
979          1,                     /* size (0 = byte, 1 = short, 2 = long) */
980          16,                    /* bitsize */
981          FALSE,                 /* pc_relative */
982          0,                     /* bitpos */
983          complain_overflow_dont, /* complain_on_overflow */
984          ppc64_elf_toc_reloc,   /* special_function */
985          "R_PPC64_TOC16_LO",    /* name */
986          FALSE,                 /* partial_inplace */
987          0,                     /* src_mask */
988          0xffff,                /* dst_mask */
989          FALSE),                /* pcrel_offset */
990
991   /* 16 bit TOC-relative relocation, high 16 bits.  */
992
993   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
994   HOWTO (R_PPC64_TOC16_HI,      /* type */
995          16,                    /* rightshift */
996          1,                     /* size (0 = byte, 1 = short, 2 = long) */
997          16,                    /* bitsize */
998          FALSE,                 /* pc_relative */
999          0,                     /* bitpos */
1000          complain_overflow_signed, /* complain_on_overflow */
1001          ppc64_elf_toc_reloc,   /* special_function */
1002          "R_PPC64_TOC16_HI",    /* name */
1003          FALSE,                 /* partial_inplace */
1004          0,                     /* src_mask */
1005          0xffff,                /* dst_mask */
1006          FALSE),                /* pcrel_offset */
1007
1008   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1009      contents of the low 16 bits, treated as a signed number, is
1010      negative.  */
1011
1012   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
1013   HOWTO (R_PPC64_TOC16_HA,      /* type */
1014          16,                    /* rightshift */
1015          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1016          16,                    /* bitsize */
1017          FALSE,                 /* pc_relative */
1018          0,                     /* bitpos */
1019          complain_overflow_signed, /* complain_on_overflow */
1020          ppc64_elf_toc_ha_reloc, /* special_function */
1021          "R_PPC64_TOC16_HA",    /* name */
1022          FALSE,                 /* partial_inplace */
1023          0,                     /* src_mask */
1024          0xffff,                /* dst_mask */
1025          FALSE),                /* pcrel_offset */
1026
1027   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
1028
1029   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
1030   HOWTO (R_PPC64_TOC,           /* type */
1031          0,                     /* rightshift */
1032          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1033          64,                    /* bitsize */
1034          FALSE,                 /* pc_relative */
1035          0,                     /* bitpos */
1036          complain_overflow_dont, /* complain_on_overflow */
1037          ppc64_elf_toc64_reloc, /* special_function */
1038          "R_PPC64_TOC",         /* name */
1039          FALSE,                 /* partial_inplace */
1040          0,                     /* src_mask */
1041          ONES (64),             /* dst_mask */
1042          FALSE),                /* pcrel_offset */
1043
1044   /* Like R_PPC64_GOT16, but also informs the link editor that the
1045      value to relocate may (!) refer to a PLT entry which the link
1046      editor (a) may replace with the symbol value.  If the link editor
1047      is unable to fully resolve the symbol, it may (b) create a PLT
1048      entry and store the address to the new PLT entry in the GOT.
1049      This permits lazy resolution of function symbols at run time.
1050      The link editor may also skip all of this and just (c) emit a
1051      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
1052   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
1053     HOWTO (R_PPC64_PLTGOT16,    /* type */
1054          0,                     /* rightshift */
1055          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1056          16,                    /* bitsize */
1057          FALSE,                 /* pc_relative */
1058          0,                     /* bitpos */
1059          complain_overflow_signed, /* complain_on_overflow */
1060          ppc64_elf_unhandled_reloc, /* special_function */
1061          "R_PPC64_PLTGOT16",    /* name */
1062          FALSE,                 /* partial_inplace */
1063          0,                     /* src_mask */
1064          0xffff,                /* dst_mask */
1065          FALSE),                /* pcrel_offset */
1066
1067   /* Like R_PPC64_PLTGOT16, but without overflow.  */
1068   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1069   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1070          0,                     /* rightshift */
1071          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1072          16,                    /* bitsize */
1073          FALSE,                 /* pc_relative */
1074          0,                     /* bitpos */
1075          complain_overflow_dont, /* complain_on_overflow */
1076          ppc64_elf_unhandled_reloc, /* special_function */
1077          "R_PPC64_PLTGOT16_LO", /* name */
1078          FALSE,                 /* partial_inplace */
1079          0,                     /* src_mask */
1080          0xffff,                /* dst_mask */
1081          FALSE),                /* pcrel_offset */
1082
1083   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1084   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1085   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1086          16,                    /* rightshift */
1087          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1088          16,                    /* bitsize */
1089          FALSE,                 /* pc_relative */
1090          0,                     /* bitpos */
1091          complain_overflow_signed, /* complain_on_overflow */
1092          ppc64_elf_unhandled_reloc, /* special_function */
1093          "R_PPC64_PLTGOT16_HI", /* name */
1094          FALSE,                 /* partial_inplace */
1095          0,                     /* src_mask */
1096          0xffff,                /* dst_mask */
1097          FALSE),                /* pcrel_offset */
1098
1099   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1100      1 if the contents of the low 16 bits, treated as a signed number,
1101      is negative.  */
1102   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1103   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1104          16,                    /* rightshift */
1105          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1106          16,                    /* bitsize */
1107          FALSE,                 /* pc_relative */
1108          0,                     /* bitpos */
1109          complain_overflow_signed, /* complain_on_overflow */
1110          ppc64_elf_unhandled_reloc, /* special_function */
1111          "R_PPC64_PLTGOT16_HA", /* name */
1112          FALSE,                 /* partial_inplace */
1113          0,                     /* src_mask */
1114          0xffff,                /* dst_mask */
1115          FALSE),                /* pcrel_offset */
1116
1117   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1118   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1119          0,                     /* rightshift */
1120          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1121          16,                    /* bitsize */
1122          FALSE,                 /* pc_relative */
1123          0,                     /* bitpos */
1124          complain_overflow_signed, /* complain_on_overflow */
1125          bfd_elf_generic_reloc, /* special_function */
1126          "R_PPC64_ADDR16_DS",   /* name */
1127          FALSE,                 /* partial_inplace */
1128          0,                     /* src_mask */
1129          0xfffc,                /* dst_mask */
1130          FALSE),                /* pcrel_offset */
1131
1132   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1133   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1134          0,                     /* rightshift */
1135          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1136          16,                    /* bitsize */
1137          FALSE,                 /* pc_relative */
1138          0,                     /* bitpos */
1139          complain_overflow_dont,/* complain_on_overflow */
1140          bfd_elf_generic_reloc, /* special_function */
1141          "R_PPC64_ADDR16_LO_DS",/* name */
1142          FALSE,                 /* partial_inplace */
1143          0,                     /* src_mask */
1144          0xfffc,                /* dst_mask */
1145          FALSE),                /* pcrel_offset */
1146
1147   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1148   HOWTO (R_PPC64_GOT16_DS,      /* type */
1149          0,                     /* rightshift */
1150          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1151          16,                    /* bitsize */
1152          FALSE,                 /* pc_relative */
1153          0,                     /* bitpos */
1154          complain_overflow_signed, /* complain_on_overflow */
1155          ppc64_elf_unhandled_reloc, /* special_function */
1156          "R_PPC64_GOT16_DS",    /* name */
1157          FALSE,                 /* partial_inplace */
1158          0,                     /* src_mask */
1159          0xfffc,                /* dst_mask */
1160          FALSE),                /* pcrel_offset */
1161
1162   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1163   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1164          0,                     /* rightshift */
1165          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1166          16,                    /* bitsize */
1167          FALSE,                 /* pc_relative */
1168          0,                     /* bitpos */
1169          complain_overflow_dont, /* complain_on_overflow */
1170          ppc64_elf_unhandled_reloc, /* special_function */
1171          "R_PPC64_GOT16_LO_DS", /* name */
1172          FALSE,                 /* partial_inplace */
1173          0,                     /* src_mask */
1174          0xfffc,                /* dst_mask */
1175          FALSE),                /* pcrel_offset */
1176
1177   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1178   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1179          0,                     /* rightshift */
1180          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1181          16,                    /* bitsize */
1182          FALSE,                 /* pc_relative */
1183          0,                     /* bitpos */
1184          complain_overflow_dont, /* complain_on_overflow */
1185          ppc64_elf_unhandled_reloc, /* special_function */
1186          "R_PPC64_PLT16_LO_DS", /* name */
1187          FALSE,                 /* partial_inplace */
1188          0,                     /* src_mask */
1189          0xfffc,                /* dst_mask */
1190          FALSE),                /* pcrel_offset */
1191
1192   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1193   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1194          0,                     /* rightshift */
1195          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1196          16,                    /* bitsize */
1197          FALSE,                 /* pc_relative */
1198          0,                     /* bitpos */
1199          complain_overflow_signed, /* complain_on_overflow */
1200          ppc64_elf_sectoff_reloc, /* special_function */
1201          "R_PPC64_SECTOFF_DS",  /* name */
1202          FALSE,                 /* partial_inplace */
1203          0,                     /* src_mask */
1204          0xfffc,                /* dst_mask */
1205          FALSE),                /* pcrel_offset */
1206
1207   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1208   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1209          0,                     /* rightshift */
1210          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1211          16,                    /* bitsize */
1212          FALSE,                 /* pc_relative */
1213          0,                     /* bitpos */
1214          complain_overflow_dont, /* complain_on_overflow */
1215          ppc64_elf_sectoff_reloc, /* special_function */
1216          "R_PPC64_SECTOFF_LO_DS",/* name */
1217          FALSE,                 /* partial_inplace */
1218          0,                     /* src_mask */
1219          0xfffc,                /* dst_mask */
1220          FALSE),                /* pcrel_offset */
1221
1222   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1223   HOWTO (R_PPC64_TOC16_DS,      /* type */
1224          0,                     /* rightshift */
1225          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1226          16,                    /* bitsize */
1227          FALSE,                 /* pc_relative */
1228          0,                     /* bitpos */
1229          complain_overflow_signed, /* complain_on_overflow */
1230          ppc64_elf_toc_reloc,   /* special_function */
1231          "R_PPC64_TOC16_DS",    /* name */
1232          FALSE,                 /* partial_inplace */
1233          0,                     /* src_mask */
1234          0xfffc,                /* dst_mask */
1235          FALSE),                /* pcrel_offset */
1236
1237   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1238   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1239          0,                     /* rightshift */
1240          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1241          16,                    /* bitsize */
1242          FALSE,                 /* pc_relative */
1243          0,                     /* bitpos */
1244          complain_overflow_dont, /* complain_on_overflow */
1245          ppc64_elf_toc_reloc,   /* special_function */
1246          "R_PPC64_TOC16_LO_DS", /* name */
1247          FALSE,                 /* partial_inplace */
1248          0,                     /* src_mask */
1249          0xfffc,                /* dst_mask */
1250          FALSE),                /* pcrel_offset */
1251
1252   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1253   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1254   HOWTO (R_PPC64_PLTGOT16_DS,   /* type */
1255          0,                     /* rightshift */
1256          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1257          16,                    /* bitsize */
1258          FALSE,                 /* pc_relative */
1259          0,                     /* bitpos */
1260          complain_overflow_signed, /* complain_on_overflow */
1261          ppc64_elf_unhandled_reloc, /* special_function */
1262          "R_PPC64_PLTGOT16_DS", /* name */
1263          FALSE,                 /* partial_inplace */
1264          0,                     /* src_mask */
1265          0xfffc,                /* dst_mask */
1266          FALSE),                /* pcrel_offset */
1267
1268   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1269   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1270   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1271          0,                     /* rightshift */
1272          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1273          16,                    /* bitsize */
1274          FALSE,                 /* pc_relative */
1275          0,                     /* bitpos */
1276          complain_overflow_dont, /* complain_on_overflow */
1277          ppc64_elf_unhandled_reloc, /* special_function */
1278          "R_PPC64_PLTGOT16_LO_DS",/* name */
1279          FALSE,                 /* partial_inplace */
1280          0,                     /* src_mask */
1281          0xfffc,                /* dst_mask */
1282          FALSE),                /* pcrel_offset */
1283
1284   /* Marker relocs for TLS.  */
1285   HOWTO (R_PPC64_TLS,
1286          0,                     /* rightshift */
1287          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1288          32,                    /* bitsize */
1289          FALSE,                 /* pc_relative */
1290          0,                     /* bitpos */
1291          complain_overflow_dont, /* complain_on_overflow */
1292          bfd_elf_generic_reloc, /* special_function */
1293          "R_PPC64_TLS",         /* name */
1294          FALSE,                 /* partial_inplace */
1295          0,                     /* src_mask */
1296          0,                     /* dst_mask */
1297          FALSE),                /* pcrel_offset */
1298
1299   HOWTO (R_PPC64_TLSGD,
1300          0,                     /* rightshift */
1301          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1302          32,                    /* bitsize */
1303          FALSE,                 /* pc_relative */
1304          0,                     /* bitpos */
1305          complain_overflow_dont, /* complain_on_overflow */
1306          bfd_elf_generic_reloc, /* special_function */
1307          "R_PPC64_TLSGD",       /* name */
1308          FALSE,                 /* partial_inplace */
1309          0,                     /* src_mask */
1310          0,                     /* dst_mask */
1311          FALSE),                /* pcrel_offset */
1312
1313   HOWTO (R_PPC64_TLSLD,
1314          0,                     /* rightshift */
1315          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1316          32,                    /* bitsize */
1317          FALSE,                 /* pc_relative */
1318          0,                     /* bitpos */
1319          complain_overflow_dont, /* complain_on_overflow */
1320          bfd_elf_generic_reloc, /* special_function */
1321          "R_PPC64_TLSLD",       /* name */
1322          FALSE,                 /* partial_inplace */
1323          0,                     /* src_mask */
1324          0,                     /* dst_mask */
1325          FALSE),                /* pcrel_offset */
1326
1327   HOWTO (R_PPC64_TOCSAVE,
1328          0,                     /* rightshift */
1329          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1330          32,                    /* bitsize */
1331          FALSE,                 /* pc_relative */
1332          0,                     /* bitpos */
1333          complain_overflow_dont, /* complain_on_overflow */
1334          bfd_elf_generic_reloc, /* special_function */
1335          "R_PPC64_TOCSAVE",     /* name */
1336          FALSE,                 /* partial_inplace */
1337          0,                     /* src_mask */
1338          0,                     /* dst_mask */
1339          FALSE),                /* pcrel_offset */
1340
1341   /* Computes the load module index of the load module that contains the
1342      definition of its TLS sym.  */
1343   HOWTO (R_PPC64_DTPMOD64,
1344          0,                     /* rightshift */
1345          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1346          64,                    /* bitsize */
1347          FALSE,                 /* pc_relative */
1348          0,                     /* bitpos */
1349          complain_overflow_dont, /* complain_on_overflow */
1350          ppc64_elf_unhandled_reloc, /* special_function */
1351          "R_PPC64_DTPMOD64",    /* name */
1352          FALSE,                 /* partial_inplace */
1353          0,                     /* src_mask */
1354          ONES (64),             /* dst_mask */
1355          FALSE),                /* pcrel_offset */
1356
1357   /* Computes a dtv-relative displacement, the difference between the value
1358      of sym+add and the base address of the thread-local storage block that
1359      contains the definition of sym, minus 0x8000.  */
1360   HOWTO (R_PPC64_DTPREL64,
1361          0,                     /* rightshift */
1362          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1363          64,                    /* bitsize */
1364          FALSE,                 /* pc_relative */
1365          0,                     /* bitpos */
1366          complain_overflow_dont, /* complain_on_overflow */
1367          ppc64_elf_unhandled_reloc, /* special_function */
1368          "R_PPC64_DTPREL64",    /* name */
1369          FALSE,                 /* partial_inplace */
1370          0,                     /* src_mask */
1371          ONES (64),             /* dst_mask */
1372          FALSE),                /* pcrel_offset */
1373
1374   /* A 16 bit dtprel reloc.  */
1375   HOWTO (R_PPC64_DTPREL16,
1376          0,                     /* rightshift */
1377          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1378          16,                    /* bitsize */
1379          FALSE,                 /* pc_relative */
1380          0,                     /* bitpos */
1381          complain_overflow_signed, /* complain_on_overflow */
1382          ppc64_elf_unhandled_reloc, /* special_function */
1383          "R_PPC64_DTPREL16",    /* name */
1384          FALSE,                 /* partial_inplace */
1385          0,                     /* src_mask */
1386          0xffff,                /* dst_mask */
1387          FALSE),                /* pcrel_offset */
1388
1389   /* Like DTPREL16, but no overflow.  */
1390   HOWTO (R_PPC64_DTPREL16_LO,
1391          0,                     /* rightshift */
1392          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1393          16,                    /* bitsize */
1394          FALSE,                 /* pc_relative */
1395          0,                     /* bitpos */
1396          complain_overflow_dont, /* complain_on_overflow */
1397          ppc64_elf_unhandled_reloc, /* special_function */
1398          "R_PPC64_DTPREL16_LO", /* name */
1399          FALSE,                 /* partial_inplace */
1400          0,                     /* src_mask */
1401          0xffff,                /* dst_mask */
1402          FALSE),                /* pcrel_offset */
1403
1404   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1405   HOWTO (R_PPC64_DTPREL16_HI,
1406          16,                    /* rightshift */
1407          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1408          16,                    /* bitsize */
1409          FALSE,                 /* pc_relative */
1410          0,                     /* bitpos */
1411          complain_overflow_signed, /* complain_on_overflow */
1412          ppc64_elf_unhandled_reloc, /* special_function */
1413          "R_PPC64_DTPREL16_HI", /* name */
1414          FALSE,                 /* partial_inplace */
1415          0,                     /* src_mask */
1416          0xffff,                /* dst_mask */
1417          FALSE),                /* pcrel_offset */
1418
1419   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1420   HOWTO (R_PPC64_DTPREL16_HA,
1421          16,                    /* rightshift */
1422          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1423          16,                    /* bitsize */
1424          FALSE,                 /* pc_relative */
1425          0,                     /* bitpos */
1426          complain_overflow_signed, /* complain_on_overflow */
1427          ppc64_elf_unhandled_reloc, /* special_function */
1428          "R_PPC64_DTPREL16_HA", /* name */
1429          FALSE,                 /* partial_inplace */
1430          0,                     /* src_mask */
1431          0xffff,                /* dst_mask */
1432          FALSE),                /* pcrel_offset */
1433
1434   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1435   HOWTO (R_PPC64_DTPREL16_HIGHER,
1436          32,                    /* rightshift */
1437          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1438          16,                    /* bitsize */
1439          FALSE,                 /* pc_relative */
1440          0,                     /* bitpos */
1441          complain_overflow_dont, /* complain_on_overflow */
1442          ppc64_elf_unhandled_reloc, /* special_function */
1443          "R_PPC64_DTPREL16_HIGHER", /* name */
1444          FALSE,                 /* partial_inplace */
1445          0,                     /* src_mask */
1446          0xffff,                /* dst_mask */
1447          FALSE),                /* pcrel_offset */
1448
1449   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1450   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1451          32,                    /* rightshift */
1452          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1453          16,                    /* bitsize */
1454          FALSE,                 /* pc_relative */
1455          0,                     /* bitpos */
1456          complain_overflow_dont, /* complain_on_overflow */
1457          ppc64_elf_unhandled_reloc, /* special_function */
1458          "R_PPC64_DTPREL16_HIGHERA", /* name */
1459          FALSE,                 /* partial_inplace */
1460          0,                     /* src_mask */
1461          0xffff,                /* dst_mask */
1462          FALSE),                /* pcrel_offset */
1463
1464   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1465   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1466          48,                    /* rightshift */
1467          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1468          16,                    /* bitsize */
1469          FALSE,                 /* pc_relative */
1470          0,                     /* bitpos */
1471          complain_overflow_dont, /* complain_on_overflow */
1472          ppc64_elf_unhandled_reloc, /* special_function */
1473          "R_PPC64_DTPREL16_HIGHEST", /* name */
1474          FALSE,                 /* partial_inplace */
1475          0,                     /* src_mask */
1476          0xffff,                /* dst_mask */
1477          FALSE),                /* pcrel_offset */
1478
1479   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1480   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1481          48,                    /* rightshift */
1482          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1483          16,                    /* bitsize */
1484          FALSE,                 /* pc_relative */
1485          0,                     /* bitpos */
1486          complain_overflow_dont, /* complain_on_overflow */
1487          ppc64_elf_unhandled_reloc, /* special_function */
1488          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1489          FALSE,                 /* partial_inplace */
1490          0,                     /* src_mask */
1491          0xffff,                /* dst_mask */
1492          FALSE),                /* pcrel_offset */
1493
1494   /* Like DTPREL16, but for insns with a DS field.  */
1495   HOWTO (R_PPC64_DTPREL16_DS,
1496          0,                     /* rightshift */
1497          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1498          16,                    /* bitsize */
1499          FALSE,                 /* pc_relative */
1500          0,                     /* bitpos */
1501          complain_overflow_signed, /* complain_on_overflow */
1502          ppc64_elf_unhandled_reloc, /* special_function */
1503          "R_PPC64_DTPREL16_DS", /* name */
1504          FALSE,                 /* partial_inplace */
1505          0,                     /* src_mask */
1506          0xfffc,                /* dst_mask */
1507          FALSE),                /* pcrel_offset */
1508
1509   /* Like DTPREL16_DS, but no overflow.  */
1510   HOWTO (R_PPC64_DTPREL16_LO_DS,
1511          0,                     /* rightshift */
1512          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1513          16,                    /* bitsize */
1514          FALSE,                 /* pc_relative */
1515          0,                     /* bitpos */
1516          complain_overflow_dont, /* complain_on_overflow */
1517          ppc64_elf_unhandled_reloc, /* special_function */
1518          "R_PPC64_DTPREL16_LO_DS", /* name */
1519          FALSE,                 /* partial_inplace */
1520          0,                     /* src_mask */
1521          0xfffc,                /* dst_mask */
1522          FALSE),                /* pcrel_offset */
1523
1524   /* Computes a tp-relative displacement, the difference between the value of
1525      sym+add and the value of the thread pointer (r13).  */
1526   HOWTO (R_PPC64_TPREL64,
1527          0,                     /* rightshift */
1528          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1529          64,                    /* bitsize */
1530          FALSE,                 /* pc_relative */
1531          0,                     /* bitpos */
1532          complain_overflow_dont, /* complain_on_overflow */
1533          ppc64_elf_unhandled_reloc, /* special_function */
1534          "R_PPC64_TPREL64",     /* name */
1535          FALSE,                 /* partial_inplace */
1536          0,                     /* src_mask */
1537          ONES (64),             /* dst_mask */
1538          FALSE),                /* pcrel_offset */
1539
1540   /* A 16 bit tprel reloc.  */
1541   HOWTO (R_PPC64_TPREL16,
1542          0,                     /* rightshift */
1543          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1544          16,                    /* bitsize */
1545          FALSE,                 /* pc_relative */
1546          0,                     /* bitpos */
1547          complain_overflow_signed, /* complain_on_overflow */
1548          ppc64_elf_unhandled_reloc, /* special_function */
1549          "R_PPC64_TPREL16",     /* name */
1550          FALSE,                 /* partial_inplace */
1551          0,                     /* src_mask */
1552          0xffff,                /* dst_mask */
1553          FALSE),                /* pcrel_offset */
1554
1555   /* Like TPREL16, but no overflow.  */
1556   HOWTO (R_PPC64_TPREL16_LO,
1557          0,                     /* rightshift */
1558          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1559          16,                    /* bitsize */
1560          FALSE,                 /* pc_relative */
1561          0,                     /* bitpos */
1562          complain_overflow_dont, /* complain_on_overflow */
1563          ppc64_elf_unhandled_reloc, /* special_function */
1564          "R_PPC64_TPREL16_LO",  /* name */
1565          FALSE,                 /* partial_inplace */
1566          0,                     /* src_mask */
1567          0xffff,                /* dst_mask */
1568          FALSE),                /* pcrel_offset */
1569
1570   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1571   HOWTO (R_PPC64_TPREL16_HI,
1572          16,                    /* rightshift */
1573          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1574          16,                    /* bitsize */
1575          FALSE,                 /* pc_relative */
1576          0,                     /* bitpos */
1577          complain_overflow_signed, /* complain_on_overflow */
1578          ppc64_elf_unhandled_reloc, /* special_function */
1579          "R_PPC64_TPREL16_HI",  /* name */
1580          FALSE,                 /* partial_inplace */
1581          0,                     /* src_mask */
1582          0xffff,                /* dst_mask */
1583          FALSE),                /* pcrel_offset */
1584
1585   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1586   HOWTO (R_PPC64_TPREL16_HA,
1587          16,                    /* rightshift */
1588          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1589          16,                    /* bitsize */
1590          FALSE,                 /* pc_relative */
1591          0,                     /* bitpos */
1592          complain_overflow_signed, /* complain_on_overflow */
1593          ppc64_elf_unhandled_reloc, /* special_function */
1594          "R_PPC64_TPREL16_HA",  /* name */
1595          FALSE,                 /* partial_inplace */
1596          0,                     /* src_mask */
1597          0xffff,                /* dst_mask */
1598          FALSE),                /* pcrel_offset */
1599
1600   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1601   HOWTO (R_PPC64_TPREL16_HIGHER,
1602          32,                    /* rightshift */
1603          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1604          16,                    /* bitsize */
1605          FALSE,                 /* pc_relative */
1606          0,                     /* bitpos */
1607          complain_overflow_dont, /* complain_on_overflow */
1608          ppc64_elf_unhandled_reloc, /* special_function */
1609          "R_PPC64_TPREL16_HIGHER",      /* name */
1610          FALSE,                 /* partial_inplace */
1611          0,                     /* src_mask */
1612          0xffff,                /* dst_mask */
1613          FALSE),                /* pcrel_offset */
1614
1615   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1616   HOWTO (R_PPC64_TPREL16_HIGHERA,
1617          32,                    /* rightshift */
1618          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1619          16,                    /* bitsize */
1620          FALSE,                 /* pc_relative */
1621          0,                     /* bitpos */
1622          complain_overflow_dont, /* complain_on_overflow */
1623          ppc64_elf_unhandled_reloc, /* special_function */
1624          "R_PPC64_TPREL16_HIGHERA", /* name */
1625          FALSE,                 /* partial_inplace */
1626          0,                     /* src_mask */
1627          0xffff,                /* dst_mask */
1628          FALSE),                /* pcrel_offset */
1629
1630   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1631   HOWTO (R_PPC64_TPREL16_HIGHEST,
1632          48,                    /* rightshift */
1633          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1634          16,                    /* bitsize */
1635          FALSE,                 /* pc_relative */
1636          0,                     /* bitpos */
1637          complain_overflow_dont, /* complain_on_overflow */
1638          ppc64_elf_unhandled_reloc, /* special_function */
1639          "R_PPC64_TPREL16_HIGHEST", /* name */
1640          FALSE,                 /* partial_inplace */
1641          0,                     /* src_mask */
1642          0xffff,                /* dst_mask */
1643          FALSE),                /* pcrel_offset */
1644
1645   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1646   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1647          48,                    /* rightshift */
1648          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1649          16,                    /* bitsize */
1650          FALSE,                 /* pc_relative */
1651          0,                     /* bitpos */
1652          complain_overflow_dont, /* complain_on_overflow */
1653          ppc64_elf_unhandled_reloc, /* special_function */
1654          "R_PPC64_TPREL16_HIGHESTA", /* name */
1655          FALSE,                 /* partial_inplace */
1656          0,                     /* src_mask */
1657          0xffff,                /* dst_mask */
1658          FALSE),                /* pcrel_offset */
1659
1660   /* Like TPREL16, but for insns with a DS field.  */
1661   HOWTO (R_PPC64_TPREL16_DS,
1662          0,                     /* rightshift */
1663          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1664          16,                    /* bitsize */
1665          FALSE,                 /* pc_relative */
1666          0,                     /* bitpos */
1667          complain_overflow_signed, /* complain_on_overflow */
1668          ppc64_elf_unhandled_reloc, /* special_function */
1669          "R_PPC64_TPREL16_DS",  /* name */
1670          FALSE,                 /* partial_inplace */
1671          0,                     /* src_mask */
1672          0xfffc,                /* dst_mask */
1673          FALSE),                /* pcrel_offset */
1674
1675   /* Like TPREL16_DS, but no overflow.  */
1676   HOWTO (R_PPC64_TPREL16_LO_DS,
1677          0,                     /* rightshift */
1678          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1679          16,                    /* bitsize */
1680          FALSE,                 /* pc_relative */
1681          0,                     /* bitpos */
1682          complain_overflow_dont, /* complain_on_overflow */
1683          ppc64_elf_unhandled_reloc, /* special_function */
1684          "R_PPC64_TPREL16_LO_DS", /* name */
1685          FALSE,                 /* partial_inplace */
1686          0,                     /* src_mask */
1687          0xfffc,                /* dst_mask */
1688          FALSE),                /* pcrel_offset */
1689
1690   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1691      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1692      to the first entry relative to the TOC base (r2).  */
1693   HOWTO (R_PPC64_GOT_TLSGD16,
1694          0,                     /* rightshift */
1695          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1696          16,                    /* bitsize */
1697          FALSE,                 /* pc_relative */
1698          0,                     /* bitpos */
1699          complain_overflow_signed, /* complain_on_overflow */
1700          ppc64_elf_unhandled_reloc, /* special_function */
1701          "R_PPC64_GOT_TLSGD16", /* name */
1702          FALSE,                 /* partial_inplace */
1703          0,                     /* src_mask */
1704          0xffff,                /* dst_mask */
1705          FALSE),                /* pcrel_offset */
1706
1707   /* Like GOT_TLSGD16, but no overflow.  */
1708   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1709          0,                     /* rightshift */
1710          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1711          16,                    /* bitsize */
1712          FALSE,                 /* pc_relative */
1713          0,                     /* bitpos */
1714          complain_overflow_dont, /* complain_on_overflow */
1715          ppc64_elf_unhandled_reloc, /* special_function */
1716          "R_PPC64_GOT_TLSGD16_LO", /* name */
1717          FALSE,                 /* partial_inplace */
1718          0,                     /* src_mask */
1719          0xffff,                /* dst_mask */
1720          FALSE),                /* pcrel_offset */
1721
1722   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1723   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1724          16,                    /* rightshift */
1725          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1726          16,                    /* bitsize */
1727          FALSE,                 /* pc_relative */
1728          0,                     /* bitpos */
1729          complain_overflow_signed, /* complain_on_overflow */
1730          ppc64_elf_unhandled_reloc, /* special_function */
1731          "R_PPC64_GOT_TLSGD16_HI", /* name */
1732          FALSE,                 /* partial_inplace */
1733          0,                     /* src_mask */
1734          0xffff,                /* dst_mask */
1735          FALSE),                /* pcrel_offset */
1736
1737   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1738   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1739          16,                    /* rightshift */
1740          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1741          16,                    /* bitsize */
1742          FALSE,                 /* pc_relative */
1743          0,                     /* bitpos */
1744          complain_overflow_signed, /* complain_on_overflow */
1745          ppc64_elf_unhandled_reloc, /* special_function */
1746          "R_PPC64_GOT_TLSGD16_HA", /* name */
1747          FALSE,                 /* partial_inplace */
1748          0,                     /* src_mask */
1749          0xffff,                /* dst_mask */
1750          FALSE),                /* pcrel_offset */
1751
1752   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1753      with values (sym+add)@dtpmod and zero, and computes the offset to the
1754      first entry relative to the TOC base (r2).  */
1755   HOWTO (R_PPC64_GOT_TLSLD16,
1756          0,                     /* rightshift */
1757          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1758          16,                    /* bitsize */
1759          FALSE,                 /* pc_relative */
1760          0,                     /* bitpos */
1761          complain_overflow_signed, /* complain_on_overflow */
1762          ppc64_elf_unhandled_reloc, /* special_function */
1763          "R_PPC64_GOT_TLSLD16", /* name */
1764          FALSE,                 /* partial_inplace */
1765          0,                     /* src_mask */
1766          0xffff,                /* dst_mask */
1767          FALSE),                /* pcrel_offset */
1768
1769   /* Like GOT_TLSLD16, but no overflow.  */
1770   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1771          0,                     /* rightshift */
1772          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1773          16,                    /* bitsize */
1774          FALSE,                 /* pc_relative */
1775          0,                     /* bitpos */
1776          complain_overflow_dont, /* complain_on_overflow */
1777          ppc64_elf_unhandled_reloc, /* special_function */
1778          "R_PPC64_GOT_TLSLD16_LO", /* name */
1779          FALSE,                 /* partial_inplace */
1780          0,                     /* src_mask */
1781          0xffff,                /* dst_mask */
1782          FALSE),                /* pcrel_offset */
1783
1784   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1785   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1786          16,                    /* rightshift */
1787          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1788          16,                    /* bitsize */
1789          FALSE,                 /* pc_relative */
1790          0,                     /* bitpos */
1791          complain_overflow_signed, /* complain_on_overflow */
1792          ppc64_elf_unhandled_reloc, /* special_function */
1793          "R_PPC64_GOT_TLSLD16_HI", /* name */
1794          FALSE,                 /* partial_inplace */
1795          0,                     /* src_mask */
1796          0xffff,                /* dst_mask */
1797          FALSE),                /* pcrel_offset */
1798
1799   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1800   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1801          16,                    /* rightshift */
1802          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1803          16,                    /* bitsize */
1804          FALSE,                 /* pc_relative */
1805          0,                     /* bitpos */
1806          complain_overflow_signed, /* complain_on_overflow */
1807          ppc64_elf_unhandled_reloc, /* special_function */
1808          "R_PPC64_GOT_TLSLD16_HA", /* name */
1809          FALSE,                 /* partial_inplace */
1810          0,                     /* src_mask */
1811          0xffff,                /* dst_mask */
1812          FALSE),                /* pcrel_offset */
1813
1814   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1815      the offset to the entry relative to the TOC base (r2).  */
1816   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1817          0,                     /* rightshift */
1818          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1819          16,                    /* bitsize */
1820          FALSE,                 /* pc_relative */
1821          0,                     /* bitpos */
1822          complain_overflow_signed, /* complain_on_overflow */
1823          ppc64_elf_unhandled_reloc, /* special_function */
1824          "R_PPC64_GOT_DTPREL16_DS", /* name */
1825          FALSE,                 /* partial_inplace */
1826          0,                     /* src_mask */
1827          0xfffc,                /* dst_mask */
1828          FALSE),                /* pcrel_offset */
1829
1830   /* Like GOT_DTPREL16_DS, but no overflow.  */
1831   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1832          0,                     /* rightshift */
1833          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1834          16,                    /* bitsize */
1835          FALSE,                 /* pc_relative */
1836          0,                     /* bitpos */
1837          complain_overflow_dont, /* complain_on_overflow */
1838          ppc64_elf_unhandled_reloc, /* special_function */
1839          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1840          FALSE,                 /* partial_inplace */
1841          0,                     /* src_mask */
1842          0xfffc,                /* dst_mask */
1843          FALSE),                /* pcrel_offset */
1844
1845   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1846   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1847          16,                    /* rightshift */
1848          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1849          16,                    /* bitsize */
1850          FALSE,                 /* pc_relative */
1851          0,                     /* bitpos */
1852          complain_overflow_signed, /* complain_on_overflow */
1853          ppc64_elf_unhandled_reloc, /* special_function */
1854          "R_PPC64_GOT_DTPREL16_HI", /* name */
1855          FALSE,                 /* partial_inplace */
1856          0,                     /* src_mask */
1857          0xffff,                /* dst_mask */
1858          FALSE),                /* pcrel_offset */
1859
1860   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1861   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1862          16,                    /* rightshift */
1863          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1864          16,                    /* bitsize */
1865          FALSE,                 /* pc_relative */
1866          0,                     /* bitpos */
1867          complain_overflow_signed, /* complain_on_overflow */
1868          ppc64_elf_unhandled_reloc, /* special_function */
1869          "R_PPC64_GOT_DTPREL16_HA", /* name */
1870          FALSE,                 /* partial_inplace */
1871          0,                     /* src_mask */
1872          0xffff,                /* dst_mask */
1873          FALSE),                /* pcrel_offset */
1874
1875   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1876      offset to the entry relative to the TOC base (r2).  */
1877   HOWTO (R_PPC64_GOT_TPREL16_DS,
1878          0,                     /* rightshift */
1879          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1880          16,                    /* bitsize */
1881          FALSE,                 /* pc_relative */
1882          0,                     /* bitpos */
1883          complain_overflow_signed, /* complain_on_overflow */
1884          ppc64_elf_unhandled_reloc, /* special_function */
1885          "R_PPC64_GOT_TPREL16_DS", /* name */
1886          FALSE,                 /* partial_inplace */
1887          0,                     /* src_mask */
1888          0xfffc,                /* dst_mask */
1889          FALSE),                /* pcrel_offset */
1890
1891   /* Like GOT_TPREL16_DS, but no overflow.  */
1892   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1893          0,                     /* rightshift */
1894          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1895          16,                    /* bitsize */
1896          FALSE,                 /* pc_relative */
1897          0,                     /* bitpos */
1898          complain_overflow_dont, /* complain_on_overflow */
1899          ppc64_elf_unhandled_reloc, /* special_function */
1900          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1901          FALSE,                 /* partial_inplace */
1902          0,                     /* src_mask */
1903          0xfffc,                /* dst_mask */
1904          FALSE),                /* pcrel_offset */
1905
1906   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1907   HOWTO (R_PPC64_GOT_TPREL16_HI,
1908          16,                    /* rightshift */
1909          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1910          16,                    /* bitsize */
1911          FALSE,                 /* pc_relative */
1912          0,                     /* bitpos */
1913          complain_overflow_signed, /* complain_on_overflow */
1914          ppc64_elf_unhandled_reloc, /* special_function */
1915          "R_PPC64_GOT_TPREL16_HI", /* name */
1916          FALSE,                 /* partial_inplace */
1917          0,                     /* src_mask */
1918          0xffff,                /* dst_mask */
1919          FALSE),                /* pcrel_offset */
1920
1921   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1922   HOWTO (R_PPC64_GOT_TPREL16_HA,
1923          16,                    /* rightshift */
1924          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1925          16,                    /* bitsize */
1926          FALSE,                 /* pc_relative */
1927          0,                     /* bitpos */
1928          complain_overflow_signed, /* complain_on_overflow */
1929          ppc64_elf_unhandled_reloc, /* special_function */
1930          "R_PPC64_GOT_TPREL16_HA", /* name */
1931          FALSE,                 /* partial_inplace */
1932          0,                     /* src_mask */
1933          0xffff,                /* dst_mask */
1934          FALSE),                /* pcrel_offset */
1935
1936   HOWTO (R_PPC64_JMP_IREL,      /* type */
1937          0,                     /* rightshift */
1938          0,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1939          0,                     /* bitsize */
1940          FALSE,                 /* pc_relative */
1941          0,                     /* bitpos */
1942          complain_overflow_dont, /* complain_on_overflow */
1943          ppc64_elf_unhandled_reloc, /* special_function */
1944          "R_PPC64_JMP_IREL",    /* name */
1945          FALSE,                 /* partial_inplace */
1946          0,                     /* src_mask */
1947          0,                     /* dst_mask */
1948          FALSE),                /* pcrel_offset */
1949
1950   HOWTO (R_PPC64_IRELATIVE,     /* type */
1951          0,                     /* rightshift */
1952          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1953          64,                    /* bitsize */
1954          FALSE,                 /* pc_relative */
1955          0,                     /* bitpos */
1956          complain_overflow_dont, /* complain_on_overflow */
1957          bfd_elf_generic_reloc, /* special_function */
1958          "R_PPC64_IRELATIVE",   /* name */
1959          FALSE,                 /* partial_inplace */
1960          0,                     /* src_mask */
1961          ONES (64),             /* dst_mask */
1962          FALSE),                /* pcrel_offset */
1963
1964   /* A 16 bit relative relocation.  */
1965   HOWTO (R_PPC64_REL16,         /* type */
1966          0,                     /* rightshift */
1967          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1968          16,                    /* bitsize */
1969          TRUE,                  /* pc_relative */
1970          0,                     /* bitpos */
1971          complain_overflow_signed, /* complain_on_overflow */
1972          bfd_elf_generic_reloc, /* special_function */
1973          "R_PPC64_REL16",       /* name */
1974          FALSE,                 /* partial_inplace */
1975          0,                     /* src_mask */
1976          0xffff,                /* dst_mask */
1977          TRUE),                 /* pcrel_offset */
1978
1979   /* A 16 bit relative relocation without overflow.  */
1980   HOWTO (R_PPC64_REL16_LO,      /* type */
1981          0,                     /* rightshift */
1982          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1983          16,                    /* bitsize */
1984          TRUE,                  /* pc_relative */
1985          0,                     /* bitpos */
1986          complain_overflow_dont,/* complain_on_overflow */
1987          bfd_elf_generic_reloc, /* special_function */
1988          "R_PPC64_REL16_LO",    /* name */
1989          FALSE,                 /* partial_inplace */
1990          0,                     /* src_mask */
1991          0xffff,                /* dst_mask */
1992          TRUE),                 /* pcrel_offset */
1993
1994   /* The high order 16 bits of a relative address.  */
1995   HOWTO (R_PPC64_REL16_HI,      /* type */
1996          16,                    /* rightshift */
1997          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1998          16,                    /* bitsize */
1999          TRUE,                  /* pc_relative */
2000          0,                     /* bitpos */
2001          complain_overflow_signed, /* complain_on_overflow */
2002          bfd_elf_generic_reloc, /* special_function */
2003          "R_PPC64_REL16_HI",    /* name */
2004          FALSE,                 /* partial_inplace */
2005          0,                     /* src_mask */
2006          0xffff,                /* dst_mask */
2007          TRUE),                 /* pcrel_offset */
2008
2009   /* The high order 16 bits of a relative address, plus 1 if the contents of
2010      the low 16 bits, treated as a signed number, is negative.  */
2011   HOWTO (R_PPC64_REL16_HA,      /* type */
2012          16,                    /* rightshift */
2013          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2014          16,                    /* bitsize */
2015          TRUE,                  /* pc_relative */
2016          0,                     /* bitpos */
2017          complain_overflow_signed, /* complain_on_overflow */
2018          ppc64_elf_ha_reloc,    /* special_function */
2019          "R_PPC64_REL16_HA",    /* name */
2020          FALSE,                 /* partial_inplace */
2021          0,                     /* src_mask */
2022          0xffff,                /* dst_mask */
2023          TRUE),                 /* pcrel_offset */
2024
2025   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
2026   HOWTO (R_PPC64_ADDR16_HIGH,   /* type */
2027          16,                    /* rightshift */
2028          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2029          16,                    /* bitsize */
2030          FALSE,                 /* pc_relative */
2031          0,                     /* bitpos */
2032          complain_overflow_dont, /* complain_on_overflow */
2033          bfd_elf_generic_reloc, /* special_function */
2034          "R_PPC64_ADDR16_HIGH", /* name */
2035          FALSE,                 /* partial_inplace */
2036          0,                     /* src_mask */
2037          0xffff,                /* dst_mask */
2038          FALSE),                /* pcrel_offset */
2039
2040   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
2041   HOWTO (R_PPC64_ADDR16_HIGHA,  /* type */
2042          16,                    /* rightshift */
2043          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2044          16,                    /* bitsize */
2045          FALSE,                 /* pc_relative */
2046          0,                     /* bitpos */
2047          complain_overflow_dont, /* complain_on_overflow */
2048          ppc64_elf_ha_reloc,    /* special_function */
2049          "R_PPC64_ADDR16_HIGHA",        /* name */
2050          FALSE,                 /* partial_inplace */
2051          0,                     /* src_mask */
2052          0xffff,                /* dst_mask */
2053          FALSE),                /* pcrel_offset */
2054
2055   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
2056   HOWTO (R_PPC64_DTPREL16_HIGH,
2057          16,                    /* rightshift */
2058          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2059          16,                    /* bitsize */
2060          FALSE,                 /* pc_relative */
2061          0,                     /* bitpos */
2062          complain_overflow_dont, /* complain_on_overflow */
2063          ppc64_elf_unhandled_reloc, /* special_function */
2064          "R_PPC64_DTPREL16_HIGH", /* name */
2065          FALSE,                 /* partial_inplace */
2066          0,                     /* src_mask */
2067          0xffff,                /* dst_mask */
2068          FALSE),                /* pcrel_offset */
2069
2070   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
2071   HOWTO (R_PPC64_DTPREL16_HIGHA,
2072          16,                    /* rightshift */
2073          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2074          16,                    /* bitsize */
2075          FALSE,                 /* pc_relative */
2076          0,                     /* bitpos */
2077          complain_overflow_dont, /* complain_on_overflow */
2078          ppc64_elf_unhandled_reloc, /* special_function */
2079          "R_PPC64_DTPREL16_HIGHA", /* name */
2080          FALSE,                 /* partial_inplace */
2081          0,                     /* src_mask */
2082          0xffff,                /* dst_mask */
2083          FALSE),                /* pcrel_offset */
2084
2085   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
2086   HOWTO (R_PPC64_TPREL16_HIGH,
2087          16,                    /* rightshift */
2088          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2089          16,                    /* bitsize */
2090          FALSE,                 /* pc_relative */
2091          0,                     /* bitpos */
2092          complain_overflow_dont, /* complain_on_overflow */
2093          ppc64_elf_unhandled_reloc, /* special_function */
2094          "R_PPC64_TPREL16_HIGH",        /* name */
2095          FALSE,                 /* partial_inplace */
2096          0,                     /* src_mask */
2097          0xffff,                /* dst_mask */
2098          FALSE),                /* pcrel_offset */
2099
2100   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
2101   HOWTO (R_PPC64_TPREL16_HIGHA,
2102          16,                    /* rightshift */
2103          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2104          16,                    /* bitsize */
2105          FALSE,                 /* pc_relative */
2106          0,                     /* bitpos */
2107          complain_overflow_dont, /* complain_on_overflow */
2108          ppc64_elf_unhandled_reloc, /* special_function */
2109          "R_PPC64_TPREL16_HIGHA",       /* name */
2110          FALSE,                 /* partial_inplace */
2111          0,                     /* src_mask */
2112          0xffff,                /* dst_mask */
2113          FALSE),                /* pcrel_offset */
2114
2115   /* Like ADDR64, but use local entry point of function.  */
2116   HOWTO (R_PPC64_ADDR64_LOCAL,  /* type */
2117          0,                     /* rightshift */
2118          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2119          64,                    /* bitsize */
2120          FALSE,                 /* pc_relative */
2121          0,                     /* bitpos */
2122          complain_overflow_dont, /* complain_on_overflow */
2123          bfd_elf_generic_reloc, /* special_function */
2124          "R_PPC64_ADDR64_LOCAL", /* name */
2125          FALSE,                 /* partial_inplace */
2126          0,                     /* src_mask */
2127          ONES (64),             /* dst_mask */
2128          FALSE),                /* pcrel_offset */
2129
2130   /* GNU extension to record C++ vtable hierarchy.  */
2131   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2132          0,                     /* rightshift */
2133          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2134          0,                     /* bitsize */
2135          FALSE,                 /* pc_relative */
2136          0,                     /* bitpos */
2137          complain_overflow_dont, /* complain_on_overflow */
2138          NULL,                  /* special_function */
2139          "R_PPC64_GNU_VTINHERIT", /* name */
2140          FALSE,                 /* partial_inplace */
2141          0,                     /* src_mask */
2142          0,                     /* dst_mask */
2143          FALSE),                /* pcrel_offset */
2144
2145   /* GNU extension to record C++ vtable member usage.  */
2146   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
2147          0,                     /* rightshift */
2148          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2149          0,                     /* bitsize */
2150          FALSE,                 /* pc_relative */
2151          0,                     /* bitpos */
2152          complain_overflow_dont, /* complain_on_overflow */
2153          NULL,                  /* special_function */
2154          "R_PPC64_GNU_VTENTRY", /* name */
2155          FALSE,                 /* partial_inplace */
2156          0,                     /* src_mask */
2157          0,                     /* dst_mask */
2158          FALSE),                /* pcrel_offset */
2159 };
2160
2161 \f
2162 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2163    be done.  */
2164
2165 static void
2166 ppc_howto_init (void)
2167 {
2168   unsigned int i, type;
2169
2170   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2171     {
2172       type = ppc64_elf_howto_raw[i].type;
2173       BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
2174       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2175     }
2176 }
2177
2178 static reloc_howto_type *
2179 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2180                              bfd_reloc_code_real_type code)
2181 {
2182   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2183
2184   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2185     /* Initialize howto table if needed.  */
2186     ppc_howto_init ();
2187
2188   switch (code)
2189     {
2190     default:
2191       return NULL;
2192
2193     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
2194       break;
2195     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
2196       break;
2197     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
2198       break;
2199     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
2200       break;
2201     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
2202       break;
2203     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
2204       break;
2205     case BFD_RELOC_PPC64_ADDR16_HIGH:           r = R_PPC64_ADDR16_HIGH;
2206       break;
2207     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
2208       break;
2209     case BFD_RELOC_PPC64_ADDR16_HIGHA:          r = R_PPC64_ADDR16_HIGHA;
2210       break;
2211     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
2212       break;
2213     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
2214       break;
2215     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
2216       break;
2217     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
2218       break;
2219     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
2220       break;
2221     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
2222       break;
2223     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
2224       break;
2225     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
2226       break;
2227     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
2228       break;
2229     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
2230       break;
2231     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
2232       break;
2233     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
2234       break;
2235     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
2236       break;
2237     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
2238       break;
2239     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
2240       break;
2241     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
2242       break;
2243     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
2244       break;
2245     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
2246       break;
2247     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
2248       break;
2249     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
2250       break;
2251     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
2252       break;
2253     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
2254       break;
2255     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
2256       break;
2257     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
2258       break;
2259     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
2260       break;
2261     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
2262       break;
2263     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
2264       break;
2265     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
2266       break;
2267     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
2268       break;
2269     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
2270       break;
2271     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
2272       break;
2273     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
2274       break;
2275     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
2276       break;
2277     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
2278       break;
2279     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
2280       break;
2281     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
2282       break;
2283     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
2284       break;
2285     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
2286       break;
2287     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
2288       break;
2289     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
2290       break;
2291     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
2292       break;
2293     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
2294       break;
2295     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
2296       break;
2297     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
2298       break;
2299     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
2300       break;
2301     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
2302       break;
2303     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
2304       break;
2305     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2306       break;
2307     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2308       break;
2309     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2310       break;
2311     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2312       break;
2313     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2314       break;
2315     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2316       break;
2317     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
2318       break;
2319     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
2320       break;
2321     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2322       break;
2323     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2324       break;
2325     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2326       break;
2327     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2328       break;
2329     case BFD_RELOC_PPC64_TPREL16_HIGH:          r = R_PPC64_TPREL16_HIGH;
2330       break;
2331     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2332       break;
2333     case BFD_RELOC_PPC64_TPREL16_HIGHA:         r = R_PPC64_TPREL16_HIGHA;
2334       break;
2335     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2336       break;
2337     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2338       break;
2339     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2340       break;
2341     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2342       break;
2343     case BFD_RELOC_PPC64_DTPREL16_HIGH:         r = R_PPC64_DTPREL16_HIGH;
2344       break;
2345     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2346       break;
2347     case BFD_RELOC_PPC64_DTPREL16_HIGHA:        r = R_PPC64_DTPREL16_HIGHA;
2348       break;
2349     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2350       break;
2351     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2352       break;
2353     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2354       break;
2355     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2356       break;
2357     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2358       break;
2359     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2360       break;
2361     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2362       break;
2363     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2364       break;
2365     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2366       break;
2367     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2368       break;
2369     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2370       break;
2371     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2372       break;
2373     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2374       break;
2375     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2376       break;
2377     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2378       break;
2379     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2380       break;
2381     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2382       break;
2383     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2384       break;
2385     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2386       break;
2387     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2388       break;
2389     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2390       break;
2391     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2392       break;
2393     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2394       break;
2395     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2396       break;
2397     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2398       break;
2399     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2400       break;
2401     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2402       break;
2403     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2404       break;
2405     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2406       break;
2407     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
2408       break;
2409     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
2410       break;
2411     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
2412       break;
2413     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
2414       break;
2415     case BFD_RELOC_PPC64_ADDR64_LOCAL:          r = R_PPC64_ADDR64_LOCAL;
2416       break;
2417     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2418       break;
2419     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2420       break;
2421     }
2422
2423   return ppc64_elf_howto_table[r];
2424 };
2425
2426 static reloc_howto_type *
2427 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2428                              const char *r_name)
2429 {
2430   unsigned int i;
2431
2432   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2433     if (ppc64_elf_howto_raw[i].name != NULL
2434         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2435       return &ppc64_elf_howto_raw[i];
2436
2437   return NULL;
2438 }
2439
2440 /* Set the howto pointer for a PowerPC ELF reloc.  */
2441
2442 static void
2443 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2444                          Elf_Internal_Rela *dst)
2445 {
2446   unsigned int type;
2447
2448   /* Initialize howto table if needed.  */
2449   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2450     ppc_howto_init ();
2451
2452   type = ELF64_R_TYPE (dst->r_info);
2453   if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
2454     {
2455       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2456                              abfd, (int) type);
2457       type = R_PPC64_NONE;
2458     }
2459   cache_ptr->howto = ppc64_elf_howto_table[type];
2460 }
2461
2462 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2463
2464 static bfd_reloc_status_type
2465 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2466                     void *data, asection *input_section,
2467                     bfd *output_bfd, char **error_message)
2468 {
2469   /* If this is a relocatable link (output_bfd test tells us), just
2470      call the generic function.  Any adjustment will be done at final
2471      link time.  */
2472   if (output_bfd != NULL)
2473     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2474                                   input_section, output_bfd, error_message);
2475
2476   /* Adjust the addend for sign extension of the low 16 bits.
2477      We won't actually be using the low 16 bits, so trashing them
2478      doesn't matter.  */
2479   reloc_entry->addend += 0x8000;
2480   return bfd_reloc_continue;
2481 }
2482
2483 static bfd_reloc_status_type
2484 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2485                         void *data, asection *input_section,
2486                         bfd *output_bfd, char **error_message)
2487 {
2488   if (output_bfd != NULL)
2489     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2490                                   input_section, output_bfd, error_message);
2491
2492   if (strcmp (symbol->section->name, ".opd") == 0
2493       && (symbol->section->owner->flags & DYNAMIC) == 0)
2494     {
2495       bfd_vma dest = opd_entry_value (symbol->section,
2496                                       symbol->value + reloc_entry->addend,
2497                                       NULL, NULL, FALSE);
2498       if (dest != (bfd_vma) -1)
2499         reloc_entry->addend = dest - (symbol->value
2500                                       + symbol->section->output_section->vma
2501                                       + symbol->section->output_offset);
2502     }
2503   else
2504     {
2505       elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2506
2507       if (symbol->section->owner != abfd
2508           && abiversion (symbol->section->owner) >= 2)
2509         {
2510           unsigned int i;
2511
2512           for (i = 0; i < symbol->section->owner->symcount; ++i)
2513             {
2514               asymbol *symdef = symbol->section->owner->outsymbols[i];
2515
2516               if (strcmp (symdef->name, symbol->name) == 0)
2517                 {
2518                   elfsym = (elf_symbol_type *) symdef;
2519                   break;
2520                 }
2521             }
2522         }
2523       reloc_entry->addend
2524         += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2525     }
2526   return bfd_reloc_continue;
2527 }
2528
2529 static bfd_reloc_status_type
2530 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2531                          void *data, asection *input_section,
2532                          bfd *output_bfd, char **error_message)
2533 {
2534   long insn;
2535   enum elf_ppc64_reloc_type r_type;
2536   bfd_size_type octets;
2537   /* Assume 'at' branch hints.  */
2538   bfd_boolean is_isa_v2 = TRUE;
2539
2540   /* If this is a relocatable link (output_bfd test tells us), just
2541      call the generic function.  Any adjustment will be done at final
2542      link time.  */
2543   if (output_bfd != NULL)
2544     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2545                                   input_section, output_bfd, error_message);
2546
2547   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2548   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2549   insn &= ~(0x01 << 21);
2550   r_type = reloc_entry->howto->type;
2551   if (r_type == R_PPC64_ADDR14_BRTAKEN
2552       || r_type == R_PPC64_REL14_BRTAKEN)
2553     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2554
2555   if (is_isa_v2)
2556     {
2557       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2558          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2559          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2560       if ((insn & (0x14 << 21)) == (0x04 << 21))
2561         insn |= 0x02 << 21;
2562       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2563         insn |= 0x08 << 21;
2564       else
2565         goto out;
2566     }
2567   else
2568     {
2569       bfd_vma target = 0;
2570       bfd_vma from;
2571
2572       if (!bfd_is_com_section (symbol->section))
2573         target = symbol->value;
2574       target += symbol->section->output_section->vma;
2575       target += symbol->section->output_offset;
2576       target += reloc_entry->addend;
2577
2578       from = (reloc_entry->address
2579               + input_section->output_offset
2580               + input_section->output_section->vma);
2581
2582       /* Invert 'y' bit if not the default.  */
2583       if ((bfd_signed_vma) (target - from) < 0)
2584         insn ^= 0x01 << 21;
2585     }
2586   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2587  out:
2588   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2589                                  input_section, output_bfd, error_message);
2590 }
2591
2592 static bfd_reloc_status_type
2593 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2594                          void *data, asection *input_section,
2595                          bfd *output_bfd, char **error_message)
2596 {
2597   /* If this is a relocatable link (output_bfd test tells us), just
2598      call the generic function.  Any adjustment will be done at final
2599      link time.  */
2600   if (output_bfd != NULL)
2601     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2602                                   input_section, output_bfd, error_message);
2603
2604   /* Subtract the symbol section base address.  */
2605   reloc_entry->addend -= symbol->section->output_section->vma;
2606   return bfd_reloc_continue;
2607 }
2608
2609 static bfd_reloc_status_type
2610 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2611                             void *data, asection *input_section,
2612                             bfd *output_bfd, char **error_message)
2613 {
2614   /* If this is a relocatable link (output_bfd test tells us), just
2615      call the generic function.  Any adjustment will be done at final
2616      link time.  */
2617   if (output_bfd != NULL)
2618     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2619                                   input_section, output_bfd, error_message);
2620
2621   /* Subtract the symbol section base address.  */
2622   reloc_entry->addend -= symbol->section->output_section->vma;
2623
2624   /* Adjust the addend for sign extension of the low 16 bits.  */
2625   reloc_entry->addend += 0x8000;
2626   return bfd_reloc_continue;
2627 }
2628
2629 static bfd_reloc_status_type
2630 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2631                      void *data, asection *input_section,
2632                      bfd *output_bfd, char **error_message)
2633 {
2634   bfd_vma TOCstart;
2635
2636   /* If this is a relocatable link (output_bfd test tells us), just
2637      call the generic function.  Any adjustment will be done at final
2638      link time.  */
2639   if (output_bfd != NULL)
2640     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2641                                   input_section, output_bfd, error_message);
2642
2643   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2644   if (TOCstart == 0)
2645     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2646
2647   /* Subtract the TOC base address.  */
2648   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2649   return bfd_reloc_continue;
2650 }
2651
2652 static bfd_reloc_status_type
2653 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2654                         void *data, asection *input_section,
2655                         bfd *output_bfd, char **error_message)
2656 {
2657   bfd_vma TOCstart;
2658
2659   /* If this is a relocatable link (output_bfd test tells us), just
2660      call the generic function.  Any adjustment will be done at final
2661      link time.  */
2662   if (output_bfd != NULL)
2663     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2664                                   input_section, output_bfd, error_message);
2665
2666   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2667   if (TOCstart == 0)
2668     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2669
2670   /* Subtract the TOC base address.  */
2671   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2672
2673   /* Adjust the addend for sign extension of the low 16 bits.  */
2674   reloc_entry->addend += 0x8000;
2675   return bfd_reloc_continue;
2676 }
2677
2678 static bfd_reloc_status_type
2679 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2680                        void *data, asection *input_section,
2681                        bfd *output_bfd, char **error_message)
2682 {
2683   bfd_vma TOCstart;
2684   bfd_size_type octets;
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   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2694   if (TOCstart == 0)
2695     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2696
2697   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2698   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2699   return bfd_reloc_ok;
2700 }
2701
2702 static bfd_reloc_status_type
2703 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2704                            void *data, asection *input_section,
2705                            bfd *output_bfd, char **error_message)
2706 {
2707   /* If this is a relocatable link (output_bfd test tells us), just
2708      call the generic function.  Any adjustment will be done at final
2709      link time.  */
2710   if (output_bfd != NULL)
2711     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2712                                   input_section, output_bfd, error_message);
2713
2714   if (error_message != NULL)
2715     {
2716       static char buf[60];
2717       sprintf (buf, "generic linker can't handle %s",
2718                reloc_entry->howto->name);
2719       *error_message = buf;
2720     }
2721   return bfd_reloc_dangerous;
2722 }
2723
2724 /* Track GOT entries needed for a given symbol.  We might need more
2725    than one got entry per symbol.  */
2726 struct got_entry
2727 {
2728   struct got_entry *next;
2729
2730   /* The symbol addend that we'll be placing in the GOT.  */
2731   bfd_vma addend;
2732
2733   /* Unlike other ELF targets, we use separate GOT entries for the same
2734      symbol referenced from different input files.  This is to support
2735      automatic multiple TOC/GOT sections, where the TOC base can vary
2736      from one input file to another.  After partitioning into TOC groups
2737      we merge entries within the group.
2738
2739      Point to the BFD owning this GOT entry.  */
2740   bfd *owner;
2741
2742   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2743      TLS_TPREL or TLS_DTPREL for tls entries.  */
2744   unsigned char tls_type;
2745
2746   /* Non-zero if got.ent points to real entry.  */
2747   unsigned char is_indirect;
2748
2749   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2750   union
2751     {
2752       bfd_signed_vma refcount;
2753       bfd_vma offset;
2754       struct got_entry *ent;
2755     } got;
2756 };
2757
2758 /* The same for PLT.  */
2759 struct plt_entry
2760 {
2761   struct plt_entry *next;
2762
2763   bfd_vma addend;
2764
2765   union
2766     {
2767       bfd_signed_vma refcount;
2768       bfd_vma offset;
2769     } plt;
2770 };
2771
2772 struct ppc64_elf_obj_tdata
2773 {
2774   struct elf_obj_tdata elf;
2775
2776   /* Shortcuts to dynamic linker sections.  */
2777   asection *got;
2778   asection *relgot;
2779
2780   /* Used during garbage collection.  We attach global symbols defined
2781      on removed .opd entries to this section so that the sym is removed.  */
2782   asection *deleted_section;
2783
2784   /* TLS local dynamic got entry handling.  Support for multiple GOT
2785      sections means we potentially need one of these for each input bfd.  */
2786   struct got_entry tlsld_got;
2787
2788   union {
2789     /* A copy of relocs before they are modified for --emit-relocs.  */
2790     Elf_Internal_Rela *relocs;
2791
2792     /* Section contents.  */
2793     bfd_byte *contents;
2794   } opd;
2795
2796   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2797      the reloc to be in the range -32768 to 32767.  */
2798   unsigned int has_small_toc_reloc : 1;
2799
2800   /* Set if toc/got ha relocs detected not using r2, or lo reloc
2801      instruction not one we handle.  */
2802   unsigned int unexpected_toc_insn : 1;
2803 };
2804
2805 #define ppc64_elf_tdata(bfd) \
2806   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2807
2808 #define ppc64_tlsld_got(bfd) \
2809   (&ppc64_elf_tdata (bfd)->tlsld_got)
2810
2811 #define is_ppc64_elf(bfd) \
2812   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2813    && elf_object_id (bfd) == PPC64_ELF_DATA)
2814
2815 /* Override the generic function because we store some extras.  */
2816
2817 static bfd_boolean
2818 ppc64_elf_mkobject (bfd *abfd)
2819 {
2820   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2821                                   PPC64_ELF_DATA);
2822 }
2823
2824 /* Fix bad default arch selected for a 64 bit input bfd when the
2825    default is 32 bit.  */
2826
2827 static bfd_boolean
2828 ppc64_elf_object_p (bfd *abfd)
2829 {
2830   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2831     {
2832       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2833
2834       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2835         {
2836           /* Relies on arch after 32 bit default being 64 bit default.  */
2837           abfd->arch_info = abfd->arch_info->next;
2838           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2839         }
2840     }
2841   return TRUE;
2842 }
2843
2844 /* Support for core dump NOTE sections.  */
2845
2846 static bfd_boolean
2847 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2848 {
2849   size_t offset, size;
2850
2851   if (note->descsz != 504)
2852     return FALSE;
2853
2854   /* pr_cursig */
2855   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2856
2857   /* pr_pid */
2858   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2859
2860   /* pr_reg */
2861   offset = 112;
2862   size = 384;
2863
2864   /* Make a ".reg/999" section.  */
2865   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2866                                           size, note->descpos + offset);
2867 }
2868
2869 static bfd_boolean
2870 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2871 {
2872   if (note->descsz != 136)
2873     return FALSE;
2874
2875   elf_tdata (abfd)->core->pid
2876     = bfd_get_32 (abfd, note->descdata + 24);
2877   elf_tdata (abfd)->core->program
2878     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2879   elf_tdata (abfd)->core->command
2880     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2881
2882   return TRUE;
2883 }
2884
2885 static char *
2886 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2887                            ...)
2888 {
2889   switch (note_type)
2890     {
2891     default:
2892       return NULL;
2893
2894     case NT_PRPSINFO:
2895       {
2896         char data[136];
2897         va_list ap;
2898
2899         va_start (ap, note_type);
2900         memset (data, 0, sizeof (data));
2901         strncpy (data + 40, va_arg (ap, const char *), 16);
2902         strncpy (data + 56, va_arg (ap, const char *), 80);
2903         va_end (ap);
2904         return elfcore_write_note (abfd, buf, bufsiz,
2905                                    "CORE", note_type, data, sizeof (data));
2906       }
2907
2908     case NT_PRSTATUS:
2909       {
2910         char data[504];
2911         va_list ap;
2912         long pid;
2913         int cursig;
2914         const void *greg;
2915
2916         va_start (ap, note_type);
2917         memset (data, 0, 112);
2918         pid = va_arg (ap, long);
2919         bfd_put_32 (abfd, pid, data + 32);
2920         cursig = va_arg (ap, int);
2921         bfd_put_16 (abfd, cursig, data + 12);
2922         greg = va_arg (ap, const void *);
2923         memcpy (data + 112, greg, 384);
2924         memset (data + 496, 0, 8);
2925         va_end (ap);
2926         return elfcore_write_note (abfd, buf, bufsiz,
2927                                    "CORE", note_type, data, sizeof (data));
2928       }
2929     }
2930 }
2931
2932 /* Add extra PPC sections.  */
2933
2934 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2935 {
2936   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
2937   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2938   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2939   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2940   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2941   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2942   { NULL,                     0,  0, 0,            0 }
2943 };
2944
2945 enum _ppc64_sec_type {
2946   sec_normal = 0,
2947   sec_opd = 1,
2948   sec_toc = 2
2949 };
2950
2951 struct _ppc64_elf_section_data
2952 {
2953   struct bfd_elf_section_data elf;
2954
2955   union
2956   {
2957     /* An array with one entry for each opd function descriptor,
2958        and some spares since opd entries may be either 16 or 24 bytes.  */
2959 #define OPD_NDX(OFF) ((OFF) >> 4)
2960     struct _opd_sec_data
2961     {
2962       /* Points to the function code section for local opd entries.  */
2963       asection **func_sec;
2964
2965       /* After editing .opd, adjust references to opd local syms.  */
2966       long *adjust;
2967     } opd;
2968
2969     /* An array for toc sections, indexed by offset/8.  */
2970     struct _toc_sec_data
2971     {
2972       /* Specifies the relocation symbol index used at a given toc offset.  */
2973       unsigned *symndx;
2974
2975       /* And the relocation addend.  */
2976       bfd_vma *add;
2977     } toc;
2978   } u;
2979
2980   enum _ppc64_sec_type sec_type:2;
2981
2982   /* Flag set when small branches are detected.  Used to
2983      select suitable defaults for the stub group size.  */
2984   unsigned int has_14bit_branch:1;
2985 };
2986
2987 #define ppc64_elf_section_data(sec) \
2988   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2989
2990 static bfd_boolean
2991 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2992 {
2993   if (!sec->used_by_bfd)
2994     {
2995       struct _ppc64_elf_section_data *sdata;
2996       bfd_size_type amt = sizeof (*sdata);
2997
2998       sdata = bfd_zalloc (abfd, amt);
2999       if (sdata == NULL)
3000         return FALSE;
3001       sec->used_by_bfd = sdata;
3002     }
3003
3004   return _bfd_elf_new_section_hook (abfd, sec);
3005 }
3006
3007 static struct _opd_sec_data *
3008 get_opd_info (asection * sec)
3009 {
3010   if (sec != NULL
3011       && ppc64_elf_section_data (sec) != NULL
3012       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3013     return &ppc64_elf_section_data (sec)->u.opd;
3014   return NULL;
3015 }
3016 \f
3017 /* Parameters for the qsort hook.  */
3018 static bfd_boolean synthetic_relocatable;
3019
3020 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
3021
3022 static int
3023 compare_symbols (const void *ap, const void *bp)
3024 {
3025   const asymbol *a = * (const asymbol **) ap;
3026   const asymbol *b = * (const asymbol **) bp;
3027
3028   /* Section symbols first.  */
3029   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3030     return -1;
3031   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3032     return 1;
3033
3034   /* then .opd symbols.  */
3035   if (strcmp (a->section->name, ".opd") == 0
3036       && strcmp (b->section->name, ".opd") != 0)
3037     return -1;
3038   if (strcmp (a->section->name, ".opd") != 0
3039       && strcmp (b->section->name, ".opd") == 0)
3040     return 1;
3041
3042   /* then other code symbols.  */
3043   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3044       == (SEC_CODE | SEC_ALLOC)
3045       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3046          != (SEC_CODE | SEC_ALLOC))
3047     return -1;
3048
3049   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3050       != (SEC_CODE | SEC_ALLOC)
3051       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3052          == (SEC_CODE | SEC_ALLOC))
3053     return 1;
3054
3055   if (synthetic_relocatable)
3056     {
3057       if (a->section->id < b->section->id)
3058         return -1;
3059
3060       if (a->section->id > b->section->id)
3061         return 1;
3062     }
3063
3064   if (a->value + a->section->vma < b->value + b->section->vma)
3065     return -1;
3066
3067   if (a->value + a->section->vma > b->value + b->section->vma)
3068     return 1;
3069
3070   /* For syms with the same value, prefer strong dynamic global function
3071      syms over other syms.  */
3072   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3073     return -1;
3074
3075   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3076     return 1;
3077
3078   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3079     return -1;
3080
3081   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3082     return 1;
3083
3084   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3085     return -1;
3086
3087   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3088     return 1;
3089
3090   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3091     return -1;
3092
3093   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3094     return 1;
3095
3096   return 0;
3097 }
3098
3099 /* Search SYMS for a symbol of the given VALUE.  */
3100
3101 static asymbol *
3102 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3103 {
3104   long mid;
3105
3106   if (id == (unsigned) -1)
3107     {
3108       while (lo < hi)
3109         {
3110           mid = (lo + hi) >> 1;
3111           if (syms[mid]->value + syms[mid]->section->vma < value)
3112             lo = mid + 1;
3113           else if (syms[mid]->value + syms[mid]->section->vma > value)
3114             hi = mid;
3115           else
3116             return syms[mid];
3117         }
3118     }
3119   else
3120     {
3121       while (lo < hi)
3122         {
3123           mid = (lo + hi) >> 1;
3124           if (syms[mid]->section->id < id)
3125             lo = mid + 1;
3126           else if (syms[mid]->section->id > id)
3127             hi = mid;
3128           else if (syms[mid]->value < value)
3129             lo = mid + 1;
3130           else if (syms[mid]->value > value)
3131             hi = mid;
3132           else
3133             return syms[mid];
3134         }
3135     }
3136   return NULL;
3137 }
3138
3139 static bfd_boolean
3140 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3141 {
3142   bfd_vma vma = *(bfd_vma *) ptr;
3143   return ((section->flags & SEC_ALLOC) != 0
3144           && section->vma <= vma
3145           && vma < section->vma + section->size);
3146 }
3147
3148 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3149    entry syms.  Also generate @plt symbols for the glink branch table.
3150    Returns count of synthetic symbols in RET or -1 on error.  */
3151
3152 static long
3153 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3154                                 long static_count, asymbol **static_syms,
3155                                 long dyn_count, asymbol **dyn_syms,
3156                                 asymbol **ret)
3157 {
3158   asymbol *s;
3159   long i;
3160   long count;
3161   char *names;
3162   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3163   asection *opd = NULL;
3164   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3165   asymbol **syms;
3166   int abi = abiversion (abfd);
3167
3168   *ret = NULL;
3169
3170   if (abi < 2)
3171     {
3172       opd = bfd_get_section_by_name (abfd, ".opd");
3173       if (opd == NULL && abi == 1)
3174         return 0;
3175     }
3176
3177   symcount = static_count;
3178   if (!relocatable)
3179     symcount += dyn_count;
3180   if (symcount == 0)
3181     return 0;
3182
3183   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3184   if (syms == NULL)
3185     return -1;
3186
3187   if (!relocatable && static_count != 0 && dyn_count != 0)
3188     {
3189       /* Use both symbol tables.  */
3190       memcpy (syms, static_syms, static_count * sizeof (*syms));
3191       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3192     }
3193   else if (!relocatable && static_count == 0)
3194     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3195   else
3196     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3197
3198   synthetic_relocatable = relocatable;
3199   qsort (syms, symcount, sizeof (*syms), compare_symbols);
3200
3201   if (!relocatable && symcount > 1)
3202     {
3203       long j;
3204       /* Trim duplicate syms, since we may have merged the normal and
3205          dynamic symbols.  Actually, we only care about syms that have
3206          different values, so trim any with the same value.  */
3207       for (i = 1, j = 1; i < symcount; ++i)
3208         if (syms[i - 1]->value + syms[i - 1]->section->vma
3209             != syms[i]->value + syms[i]->section->vma)
3210           syms[j++] = syms[i];
3211       symcount = j;
3212     }
3213
3214   i = 0;
3215   if (strcmp (syms[i]->section->name, ".opd") == 0)
3216     ++i;
3217   codesecsym = i;
3218
3219   for (; i < symcount; ++i)
3220     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3221          != (SEC_CODE | SEC_ALLOC))
3222         || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3223       break;
3224   codesecsymend = i;
3225
3226   for (; i < symcount; ++i)
3227     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3228       break;
3229   secsymend = i;
3230
3231   for (; i < symcount; ++i)
3232     if (strcmp (syms[i]->section->name, ".opd") != 0)
3233       break;
3234   opdsymend = i;
3235
3236   for (; i < symcount; ++i)
3237     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3238         != (SEC_CODE | SEC_ALLOC))
3239       break;
3240   symcount = i;
3241
3242   count = 0;
3243
3244   if (relocatable)
3245     {
3246       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3247       arelent *r;
3248       size_t size;
3249       long relcount;
3250
3251       if (opdsymend == secsymend)
3252         goto done;
3253
3254       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3255       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3256       if (relcount == 0)
3257         goto done;
3258
3259       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3260         {
3261           count = -1;
3262           goto done;
3263         }
3264
3265       size = 0;
3266       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3267         {
3268           asymbol *sym;
3269
3270           while (r < opd->relocation + relcount
3271                  && r->address < syms[i]->value + opd->vma)
3272             ++r;
3273
3274           if (r == opd->relocation + relcount)
3275             break;
3276
3277           if (r->address != syms[i]->value + opd->vma)
3278             continue;
3279
3280           if (r->howto->type != R_PPC64_ADDR64)
3281             continue;
3282
3283           sym = *r->sym_ptr_ptr;
3284           if (!sym_exists_at (syms, opdsymend, symcount,
3285                               sym->section->id, sym->value + r->addend))
3286             {
3287               ++count;
3288               size += sizeof (asymbol);
3289               size += strlen (syms[i]->name) + 2;
3290             }
3291         }
3292
3293       if (size == 0)
3294         goto done;
3295       s = *ret = bfd_malloc (size);
3296       if (s == NULL)
3297         {
3298           count = -1;
3299           goto done;
3300         }
3301
3302       names = (char *) (s + count);
3303
3304       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3305         {
3306           asymbol *sym;
3307
3308           while (r < opd->relocation + relcount
3309                  && r->address < syms[i]->value + opd->vma)
3310             ++r;
3311
3312           if (r == opd->relocation + relcount)
3313             break;
3314
3315           if (r->address != syms[i]->value + opd->vma)
3316             continue;
3317
3318           if (r->howto->type != R_PPC64_ADDR64)
3319             continue;
3320
3321           sym = *r->sym_ptr_ptr;
3322           if (!sym_exists_at (syms, opdsymend, symcount,
3323                               sym->section->id, sym->value + r->addend))
3324             {
3325               size_t len;
3326
3327               *s = *syms[i];
3328               s->flags |= BSF_SYNTHETIC;
3329               s->section = sym->section;
3330               s->value = sym->value + r->addend;
3331               s->name = names;
3332               *names++ = '.';
3333               len = strlen (syms[i]->name);
3334               memcpy (names, syms[i]->name, len + 1);
3335               names += len + 1;
3336               /* Have udata.p point back to the original symbol this
3337                  synthetic symbol was derived from.  */
3338               s->udata.p = syms[i];
3339               s++;
3340             }
3341         }
3342     }
3343   else
3344     {
3345       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3346       bfd_byte *contents = NULL;
3347       size_t size;
3348       long plt_count = 0;
3349       bfd_vma glink_vma = 0, resolv_vma = 0;
3350       asection *dynamic, *glink = NULL, *relplt = NULL;
3351       arelent *p;
3352
3353       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3354         {
3355         free_contents_and_exit_err:
3356           count = -1;
3357         free_contents_and_exit:
3358           if (contents)
3359             free (contents);
3360           goto done;
3361         }
3362
3363       size = 0;
3364       for (i = secsymend; i < opdsymend; ++i)
3365         {
3366           bfd_vma ent;
3367
3368           /* Ignore bogus symbols.  */
3369           if (syms[i]->value > opd->size - 8)
3370             continue;
3371
3372           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3373           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3374             {
3375               ++count;
3376               size += sizeof (asymbol);
3377               size += strlen (syms[i]->name) + 2;
3378             }
3379         }
3380
3381       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
3382       if (dyn_count != 0
3383           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3384         {
3385           bfd_byte *dynbuf, *extdyn, *extdynend;
3386           size_t extdynsize;
3387           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3388
3389           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3390             goto free_contents_and_exit_err;
3391
3392           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3393           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3394
3395           extdyn = dynbuf;
3396           extdynend = extdyn + dynamic->size;
3397           for (; extdyn < extdynend; extdyn += extdynsize)
3398             {
3399               Elf_Internal_Dyn dyn;
3400               (*swap_dyn_in) (abfd, extdyn, &dyn);
3401
3402               if (dyn.d_tag == DT_NULL)
3403                 break;
3404
3405               if (dyn.d_tag == DT_PPC64_GLINK)
3406                 {
3407                   /* The first glink stub starts at offset 32; see
3408                      comment in ppc64_elf_finish_dynamic_sections. */
3409                   glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
3410                   /* The .glink section usually does not survive the final
3411                      link; search for the section (usually .text) where the
3412                      glink stubs now reside.  */
3413                   glink = bfd_sections_find_if (abfd, section_covers_vma,
3414                                                 &glink_vma);
3415                   break;
3416                 }
3417             }
3418
3419           free (dynbuf);
3420         }
3421
3422       if (glink != NULL)
3423         {
3424           /* Determine __glink trampoline by reading the relative branch
3425              from the first glink stub.  */
3426           bfd_byte buf[4];
3427           unsigned int off = 0;
3428
3429           while (bfd_get_section_contents (abfd, glink, buf,
3430                                            glink_vma + off - glink->vma, 4))
3431             {
3432               unsigned int insn = bfd_get_32 (abfd, buf);
3433               insn ^= B_DOT;
3434               if ((insn & ~0x3fffffc) == 0)
3435                 {
3436                   resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3437                   break;
3438                 }
3439               off += 4;
3440               if (off > 4)
3441                 break;
3442             }
3443
3444           if (resolv_vma)
3445             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3446
3447           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3448           if (relplt != NULL)
3449             {
3450               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3451               if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3452                 goto free_contents_and_exit_err;
3453
3454               plt_count = relplt->size / sizeof (Elf64_External_Rela);
3455               size += plt_count * sizeof (asymbol);
3456
3457               p = relplt->relocation;
3458               for (i = 0; i < plt_count; i++, p++)
3459                 {
3460                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3461                   if (p->addend != 0)
3462                     size += sizeof ("+0x") - 1 + 16;
3463                 }
3464             }
3465         }
3466
3467       if (size == 0)
3468         goto free_contents_and_exit;
3469       s = *ret = bfd_malloc (size);
3470       if (s == NULL)
3471         goto free_contents_and_exit_err;
3472
3473       names = (char *) (s + count + plt_count + (resolv_vma != 0));
3474
3475       for (i = secsymend; i < opdsymend; ++i)
3476         {
3477           bfd_vma ent;
3478
3479           if (syms[i]->value > opd->size - 8)
3480             continue;
3481
3482           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3483           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3484             {
3485               long lo, hi;
3486               size_t len;
3487               asection *sec = abfd->sections;
3488
3489               *s = *syms[i];
3490               lo = codesecsym;
3491               hi = codesecsymend;
3492               while (lo < hi)
3493                 {
3494                   long mid = (lo + hi) >> 1;
3495                   if (syms[mid]->section->vma < ent)
3496                     lo = mid + 1;
3497                   else if (syms[mid]->section->vma > ent)
3498                     hi = mid;
3499                   else
3500                     {
3501                       sec = syms[mid]->section;
3502                       break;
3503                     }
3504                 }
3505
3506               if (lo >= hi && lo > codesecsym)
3507                 sec = syms[lo - 1]->section;
3508
3509               for (; sec != NULL; sec = sec->next)
3510                 {
3511                   if (sec->vma > ent)
3512                     break;
3513                   /* SEC_LOAD may not be set if SEC is from a separate debug
3514                      info file.  */
3515                   if ((sec->flags & SEC_ALLOC) == 0)
3516                     break;
3517                   if ((sec->flags & SEC_CODE) != 0)
3518                     s->section = sec;
3519                 }
3520               s->flags |= BSF_SYNTHETIC;
3521               s->value = ent - s->section->vma;
3522               s->name = names;
3523               *names++ = '.';
3524               len = strlen (syms[i]->name);
3525               memcpy (names, syms[i]->name, len + 1);
3526               names += len + 1;
3527               /* Have udata.p point back to the original symbol this
3528                  synthetic symbol was derived from.  */
3529               s->udata.p = syms[i];
3530               s++;
3531             }
3532         }
3533       free (contents);
3534
3535       if (glink != NULL && relplt != NULL)
3536         {
3537           if (resolv_vma)
3538             {
3539               /* Add a symbol for the main glink trampoline.  */
3540               memset (s, 0, sizeof *s);
3541               s->the_bfd = abfd;
3542               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3543               s->section = glink;
3544               s->value = resolv_vma - glink->vma;
3545               s->name = names;
3546               memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3547               names += sizeof ("__glink_PLTresolve");
3548               s++;
3549               count++;
3550             }
3551
3552           /* FIXME: It would be very much nicer to put sym@plt on the
3553              stub rather than on the glink branch table entry.  The
3554              objdump disassembler would then use a sensible symbol
3555              name on plt calls.  The difficulty in doing so is
3556              a) finding the stubs, and,
3557              b) matching stubs against plt entries, and,
3558              c) there can be multiple stubs for a given plt entry.
3559
3560              Solving (a) could be done by code scanning, but older
3561              ppc64 binaries used different stubs to current code.
3562              (b) is the tricky one since you need to known the toc
3563              pointer for at least one function that uses a pic stub to
3564              be able to calculate the plt address referenced.
3565              (c) means gdb would need to set multiple breakpoints (or
3566              find the glink branch itself) when setting breakpoints
3567              for pending shared library loads.  */
3568           p = relplt->relocation;
3569           for (i = 0; i < plt_count; i++, p++)
3570             {
3571               size_t len;
3572
3573               *s = **p->sym_ptr_ptr;
3574               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
3575                  we are defining a symbol, ensure one of them is set.  */
3576               if ((s->flags & BSF_LOCAL) == 0)
3577                 s->flags |= BSF_GLOBAL;
3578               s->flags |= BSF_SYNTHETIC;
3579               s->section = glink;
3580               s->value = glink_vma - glink->vma;
3581               s->name = names;
3582               s->udata.p = NULL;
3583               len = strlen ((*p->sym_ptr_ptr)->name);
3584               memcpy (names, (*p->sym_ptr_ptr)->name, len);
3585               names += len;
3586               if (p->addend != 0)
3587                 {
3588                   memcpy (names, "+0x", sizeof ("+0x") - 1);
3589                   names += sizeof ("+0x") - 1;
3590                   bfd_sprintf_vma (abfd, names, p->addend);
3591                   names += strlen (names);
3592                 }
3593               memcpy (names, "@plt", sizeof ("@plt"));
3594               names += sizeof ("@plt");
3595               s++;
3596               if (abi < 2)
3597                 {
3598                   glink_vma += 8;
3599                   if (i >= 0x8000)
3600                     glink_vma += 4;
3601                 }
3602               else
3603                 glink_vma += 4;
3604             }
3605           count += plt_count;
3606         }
3607     }
3608
3609  done:
3610   free (syms);
3611   return count;
3612 }
3613 \f
3614 /* The following functions are specific to the ELF linker, while
3615    functions above are used generally.  Those named ppc64_elf_* are
3616    called by the main ELF linker code.  They appear in this file more
3617    or less in the order in which they are called.  eg.
3618    ppc64_elf_check_relocs is called early in the link process,
3619    ppc64_elf_finish_dynamic_sections is one of the last functions
3620    called.
3621
3622    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3623    functions have both a function code symbol and a function descriptor
3624    symbol.  A call to foo in a relocatable object file looks like:
3625
3626    .            .text
3627    .    x:
3628    .            bl      .foo
3629    .            nop
3630
3631    The function definition in another object file might be:
3632
3633    .            .section .opd
3634    .    foo:    .quad   .foo
3635    .            .quad   .TOC.@tocbase
3636    .            .quad   0
3637    .
3638    .            .text
3639    .    .foo:   blr
3640
3641    When the linker resolves the call during a static link, the branch
3642    unsurprisingly just goes to .foo and the .opd information is unused.
3643    If the function definition is in a shared library, things are a little
3644    different:  The call goes via a plt call stub, the opd information gets
3645    copied to the plt, and the linker patches the nop.
3646
3647    .    x:
3648    .            bl      .foo_stub
3649    .            ld      2,40(1)
3650    .
3651    .
3652    .    .foo_stub:
3653    .            std     2,40(1)                 # in practice, the call stub
3654    .            addis   11,2,Lfoo@toc@ha        # is slightly optimized, but
3655    .            addi    11,11,Lfoo@toc@l        # this is the general idea
3656    .            ld      12,0(11)
3657    .            ld      2,8(11)
3658    .            mtctr   12
3659    .            ld      11,16(11)
3660    .            bctr
3661    .
3662    .            .section .plt
3663    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
3664
3665    The "reloc ()" notation is supposed to indicate that the linker emits
3666    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3667    copying.
3668
3669    What are the difficulties here?  Well, firstly, the relocations
3670    examined by the linker in check_relocs are against the function code
3671    sym .foo, while the dynamic relocation in the plt is emitted against
3672    the function descriptor symbol, foo.  Somewhere along the line, we need
3673    to carefully copy dynamic link information from one symbol to the other.
3674    Secondly, the generic part of the elf linker will make .foo a dynamic
3675    symbol as is normal for most other backends.  We need foo dynamic
3676    instead, at least for an application final link.  However, when
3677    creating a shared library containing foo, we need to have both symbols
3678    dynamic so that references to .foo are satisfied during the early
3679    stages of linking.  Otherwise the linker might decide to pull in a
3680    definition from some other object, eg. a static library.
3681
3682    Update: As of August 2004, we support a new convention.  Function
3683    calls may use the function descriptor symbol, ie. "bl foo".  This
3684    behaves exactly as "bl .foo".  */
3685
3686 /* Of those relocs that might be copied as dynamic relocs, this function
3687    selects those that must be copied when linking a shared library,
3688    even when the symbol is local.  */
3689
3690 static int
3691 must_be_dyn_reloc (struct bfd_link_info *info,
3692                    enum elf_ppc64_reloc_type r_type)
3693 {
3694   switch (r_type)
3695     {
3696     default:
3697       return 1;
3698
3699     case R_PPC64_REL32:
3700     case R_PPC64_REL64:
3701     case R_PPC64_REL30:
3702       return 0;
3703
3704     case R_PPC64_TPREL16:
3705     case R_PPC64_TPREL16_LO:
3706     case R_PPC64_TPREL16_HI:
3707     case R_PPC64_TPREL16_HA:
3708     case R_PPC64_TPREL16_DS:
3709     case R_PPC64_TPREL16_LO_DS:
3710     case R_PPC64_TPREL16_HIGH:
3711     case R_PPC64_TPREL16_HIGHA:
3712     case R_PPC64_TPREL16_HIGHER:
3713     case R_PPC64_TPREL16_HIGHERA:
3714     case R_PPC64_TPREL16_HIGHEST:
3715     case R_PPC64_TPREL16_HIGHESTA:
3716     case R_PPC64_TPREL64:
3717       return !bfd_link_executable (info);
3718     }
3719 }
3720
3721 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3722    copying dynamic variables from a shared lib into an app's dynbss
3723    section, and instead use a dynamic relocation to point into the
3724    shared lib.  With code that gcc generates, it's vital that this be
3725    enabled;  In the PowerPC64 ABI, the address of a function is actually
3726    the address of a function descriptor, which resides in the .opd
3727    section.  gcc uses the descriptor directly rather than going via the
3728    GOT as some other ABI's do, which means that initialized function
3729    pointers must reference the descriptor.  Thus, a function pointer
3730    initialized to the address of a function in a shared library will
3731    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3732    redefines the function descriptor symbol to point to the copy.  This
3733    presents a problem as a plt entry for that function is also
3734    initialized from the function descriptor symbol and the copy reloc
3735    may not be initialized first.  */
3736 #define ELIMINATE_COPY_RELOCS 1
3737
3738 /* Section name for stubs is the associated section name plus this
3739    string.  */
3740 #define STUB_SUFFIX ".stub"
3741
3742 /* Linker stubs.
3743    ppc_stub_long_branch:
3744    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3745    destination, but a 24 bit branch in a stub section will reach.
3746    .    b       dest
3747
3748    ppc_stub_plt_branch:
3749    Similar to the above, but a 24 bit branch in the stub section won't
3750    reach its destination.
3751    .    addis   %r11,%r2,xxx@toc@ha
3752    .    ld      %r12,xxx@toc@l(%r11)
3753    .    mtctr   %r12
3754    .    bctr
3755
3756    ppc_stub_plt_call:
3757    Used to call a function in a shared library.  If it so happens that
3758    the plt entry referenced crosses a 64k boundary, then an extra
3759    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3760    .    std     %r2,40(%r1)
3761    .    addis   %r11,%r2,xxx@toc@ha
3762    .    ld      %r12,xxx+0@toc@l(%r11)
3763    .    mtctr   %r12
3764    .    ld      %r2,xxx+8@toc@l(%r11)
3765    .    ld      %r11,xxx+16@toc@l(%r11)
3766    .    bctr
3767
3768    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3769    code to adjust the value and save r2 to support multiple toc sections.
3770    A ppc_stub_long_branch with an r2 offset looks like:
3771    .    std     %r2,40(%r1)
3772    .    addis   %r2,%r2,off@ha
3773    .    addi    %r2,%r2,off@l
3774    .    b       dest
3775
3776    A ppc_stub_plt_branch with an r2 offset looks like:
3777    .    std     %r2,40(%r1)
3778    .    addis   %r11,%r2,xxx@toc@ha
3779    .    ld      %r12,xxx@toc@l(%r11)
3780    .    addis   %r2,%r2,off@ha
3781    .    addi    %r2,%r2,off@l
3782    .    mtctr   %r12
3783    .    bctr
3784
3785    In cases where the "addis" instruction would add zero, the "addis" is
3786    omitted and following instructions modified slightly in some cases.
3787 */
3788
3789 enum ppc_stub_type {
3790   ppc_stub_none,
3791   ppc_stub_long_branch,
3792   ppc_stub_long_branch_r2off,
3793   ppc_stub_plt_branch,
3794   ppc_stub_plt_branch_r2off,
3795   ppc_stub_plt_call,
3796   ppc_stub_plt_call_r2save,
3797   ppc_stub_global_entry,
3798   ppc_stub_save_res
3799 };
3800
3801 /* Information on stub grouping.  */
3802 struct map_stub
3803 {
3804   /* The stub section.  */
3805   asection *stub_sec;
3806   /* This is the section to which stubs in the group will be attached.  */
3807   asection *link_sec;
3808   /* Next group.  */
3809   struct map_stub *next;
3810   /* Whether to emit a copy of register save/restore functions in this
3811      group.  */
3812   int needs_save_res;
3813 };
3814
3815 struct ppc_stub_hash_entry {
3816
3817   /* Base hash table entry structure.  */
3818   struct bfd_hash_entry root;
3819
3820   enum ppc_stub_type stub_type;
3821
3822   /* Group information.  */
3823   struct map_stub *group;
3824
3825   /* Offset within stub_sec of the beginning of this stub.  */
3826   bfd_vma stub_offset;
3827
3828   /* Given the symbol's value and its section we can determine its final
3829      value when building the stubs (so the stub knows where to jump.  */
3830   bfd_vma target_value;
3831   asection *target_section;
3832
3833   /* The symbol table entry, if any, that this was derived from.  */
3834   struct ppc_link_hash_entry *h;
3835   struct plt_entry *plt_ent;
3836
3837   /* Symbol st_other.  */
3838   unsigned char other;
3839 };
3840
3841 struct ppc_branch_hash_entry {
3842
3843   /* Base hash table entry structure.  */
3844   struct bfd_hash_entry root;
3845
3846   /* Offset within branch lookup table.  */
3847   unsigned int offset;
3848
3849   /* Generation marker.  */
3850   unsigned int iter;
3851 };
3852
3853 /* Used to track dynamic relocations for local symbols.  */
3854 struct ppc_dyn_relocs
3855 {
3856   struct ppc_dyn_relocs *next;
3857
3858   /* The input section of the reloc.  */
3859   asection *sec;
3860
3861   /* Total number of relocs copied for the input section.  */
3862   unsigned int count : 31;
3863
3864   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
3865   unsigned int ifunc : 1;
3866 };
3867
3868 struct ppc_link_hash_entry
3869 {
3870   struct elf_link_hash_entry elf;
3871
3872   union {
3873     /* A pointer to the most recently used stub hash entry against this
3874        symbol.  */
3875     struct ppc_stub_hash_entry *stub_cache;
3876
3877     /* A pointer to the next symbol starting with a '.'  */
3878     struct ppc_link_hash_entry *next_dot_sym;
3879   } u;
3880
3881   /* Track dynamic relocs copied for this symbol.  */
3882   struct elf_dyn_relocs *dyn_relocs;
3883
3884   /* Link between function code and descriptor symbols.  */
3885   struct ppc_link_hash_entry *oh;
3886
3887   /* Flag function code and descriptor symbols.  */
3888   unsigned int is_func:1;
3889   unsigned int is_func_descriptor:1;
3890   unsigned int fake:1;
3891
3892   /* Whether global opd/toc sym has been adjusted or not.
3893      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3894      should be set for all globals defined in any opd/toc section.  */
3895   unsigned int adjust_done:1;
3896
3897   /* Set if we twiddled this symbol to weak at some stage.  */
3898   unsigned int was_undefined:1;
3899
3900   /* Set if this is an out-of-line register save/restore function,
3901      with non-standard calling convention.  */
3902   unsigned int save_res:1;
3903
3904   /* Contexts in which symbol is used in the GOT (or TOC).
3905      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3906      corresponding relocs are encountered during check_relocs.
3907      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3908      indicate the corresponding GOT entry type is not needed.
3909      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3910      a TPREL one.  We use a separate flag rather than setting TPREL
3911      just for convenience in distinguishing the two cases.  */
3912 #define TLS_GD           1      /* GD reloc. */
3913 #define TLS_LD           2      /* LD reloc. */
3914 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
3915 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
3916 #define TLS_TLS         16      /* Any TLS reloc.  */
3917 #define TLS_EXPLICIT    32      /* Marks TOC section TLS relocs. */
3918 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
3919 #define PLT_IFUNC      128      /* STT_GNU_IFUNC.  */
3920   unsigned char tls_mask;
3921 };
3922
3923 /* ppc64 ELF linker hash table.  */
3924
3925 struct ppc_link_hash_table
3926 {
3927   struct elf_link_hash_table elf;
3928
3929   /* The stub hash table.  */
3930   struct bfd_hash_table stub_hash_table;
3931
3932   /* Another hash table for plt_branch stubs.  */
3933   struct bfd_hash_table branch_hash_table;
3934
3935   /* Hash table for function prologue tocsave.  */
3936   htab_t tocsave_htab;
3937
3938   /* Various options and other info passed from the linker.  */
3939   struct ppc64_elf_params *params;
3940
3941   /* The size of sec_info below.  */
3942   unsigned int sec_info_arr_size;
3943
3944   /* Per-section array of extra section info.  Done this way rather
3945      than as part of ppc64_elf_section_data so we have the info for
3946      non-ppc64 sections.  */
3947   struct
3948   {
3949     /* Along with elf_gp, specifies the TOC pointer used by this section.  */
3950     bfd_vma toc_off;
3951
3952     union
3953     {
3954       /* The section group that this section belongs to.  */
3955       struct map_stub *group;
3956       /* A temp section list pointer.  */
3957       asection *list;
3958     } u;
3959   } *sec_info;
3960
3961   /* Linked list of groups.  */
3962   struct map_stub *group;
3963
3964   /* Temp used when calculating TOC pointers.  */
3965   bfd_vma toc_curr;
3966   bfd *toc_bfd;
3967   asection *toc_first_sec;
3968
3969   /* Used when adding symbols.  */
3970   struct ppc_link_hash_entry *dot_syms;
3971
3972   /* Shortcuts to get to dynamic linker sections.  */
3973   asection *dynbss;
3974   asection *relbss;
3975   asection *glink;
3976   asection *sfpr;
3977   asection *brlt;
3978   asection *relbrlt;
3979   asection *glink_eh_frame;
3980
3981   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
3982   struct ppc_link_hash_entry *tls_get_addr;
3983   struct ppc_link_hash_entry *tls_get_addr_fd;
3984
3985   /* The size of reliplt used by got entry relocs.  */
3986   bfd_size_type got_reli_size;
3987
3988   /* Statistics.  */
3989   unsigned long stub_count[ppc_stub_global_entry];
3990
3991   /* Number of stubs against global syms.  */
3992   unsigned long stub_globals;
3993
3994   /* Set if we're linking code with function descriptors.  */
3995   unsigned int opd_abi:1;
3996
3997   /* Support for multiple toc sections.  */
3998   unsigned int do_multi_toc:1;
3999   unsigned int multi_toc_needed:1;
4000   unsigned int second_toc_pass:1;
4001   unsigned int do_toc_opt:1;
4002
4003   /* Set on error.  */
4004   unsigned int stub_error:1;
4005
4006   /* Temp used by ppc64_elf_before_check_relocs.  */
4007   unsigned int twiddled_syms:1;
4008
4009   /* Incremented every time we size stubs.  */
4010   unsigned int stub_iteration;
4011
4012   /* Small local sym cache.  */
4013   struct sym_cache sym_cache;
4014 };
4015
4016 /* Rename some of the generic section flags to better document how they
4017    are used here.  */
4018
4019 /* Nonzero if this section has TLS related relocations.  */
4020 #define has_tls_reloc sec_flg0
4021
4022 /* Nonzero if this section has a call to __tls_get_addr.  */
4023 #define has_tls_get_addr_call sec_flg1
4024
4025 /* Nonzero if this section has any toc or got relocs.  */
4026 #define has_toc_reloc sec_flg2
4027
4028 /* Nonzero if this section has a call to another section that uses
4029    the toc or got.  */
4030 #define makes_toc_func_call sec_flg3
4031
4032 /* Recursion protection when determining above flag.  */
4033 #define call_check_in_progress sec_flg4
4034 #define call_check_done sec_flg5
4035
4036 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
4037
4038 #define ppc_hash_table(p) \
4039   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4040   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4041
4042 #define ppc_stub_hash_lookup(table, string, create, copy) \
4043   ((struct ppc_stub_hash_entry *) \
4044    bfd_hash_lookup ((table), (string), (create), (copy)))
4045
4046 #define ppc_branch_hash_lookup(table, string, create, copy) \
4047   ((struct ppc_branch_hash_entry *) \
4048    bfd_hash_lookup ((table), (string), (create), (copy)))
4049
4050 /* Create an entry in the stub hash table.  */
4051
4052 static struct bfd_hash_entry *
4053 stub_hash_newfunc (struct bfd_hash_entry *entry,
4054                    struct bfd_hash_table *table,
4055                    const char *string)
4056 {
4057   /* Allocate the structure if it has not already been allocated by a
4058      subclass.  */
4059   if (entry == NULL)
4060     {
4061       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4062       if (entry == NULL)
4063         return entry;
4064     }
4065
4066   /* Call the allocation method of the superclass.  */
4067   entry = bfd_hash_newfunc (entry, table, string);
4068   if (entry != NULL)
4069     {
4070       struct ppc_stub_hash_entry *eh;
4071
4072       /* Initialize the local fields.  */
4073       eh = (struct ppc_stub_hash_entry *) entry;
4074       eh->stub_type = ppc_stub_none;
4075       eh->group = NULL;
4076       eh->stub_offset = 0;
4077       eh->target_value = 0;
4078       eh->target_section = NULL;
4079       eh->h = NULL;
4080       eh->plt_ent = NULL;
4081       eh->other = 0;
4082     }
4083
4084   return entry;
4085 }
4086
4087 /* Create an entry in the branch hash table.  */
4088
4089 static struct bfd_hash_entry *
4090 branch_hash_newfunc (struct bfd_hash_entry *entry,
4091                      struct bfd_hash_table *table,
4092                      const char *string)
4093 {
4094   /* Allocate the structure if it has not already been allocated by a
4095      subclass.  */
4096   if (entry == NULL)
4097     {
4098       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4099       if (entry == NULL)
4100         return entry;
4101     }
4102
4103   /* Call the allocation method of the superclass.  */
4104   entry = bfd_hash_newfunc (entry, table, string);
4105   if (entry != NULL)
4106     {
4107       struct ppc_branch_hash_entry *eh;
4108
4109       /* Initialize the local fields.  */
4110       eh = (struct ppc_branch_hash_entry *) entry;
4111       eh->offset = 0;
4112       eh->iter = 0;
4113     }
4114
4115   return entry;
4116 }
4117
4118 /* Create an entry in a ppc64 ELF linker hash table.  */
4119
4120 static struct bfd_hash_entry *
4121 link_hash_newfunc (struct bfd_hash_entry *entry,
4122                    struct bfd_hash_table *table,
4123                    const char *string)
4124 {
4125   /* Allocate the structure if it has not already been allocated by a
4126      subclass.  */
4127   if (entry == NULL)
4128     {
4129       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4130       if (entry == NULL)
4131         return entry;
4132     }
4133
4134   /* Call the allocation method of the superclass.  */
4135   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4136   if (entry != NULL)
4137     {
4138       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4139
4140       memset (&eh->u.stub_cache, 0,
4141               (sizeof (struct ppc_link_hash_entry)
4142                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4143
4144       /* When making function calls, old ABI code references function entry
4145          points (dot symbols), while new ABI code references the function
4146          descriptor symbol.  We need to make any combination of reference and
4147          definition work together, without breaking archive linking.
4148
4149          For a defined function "foo" and an undefined call to "bar":
4150          An old object defines "foo" and ".foo", references ".bar" (possibly
4151          "bar" too).
4152          A new object defines "foo" and references "bar".
4153
4154          A new object thus has no problem with its undefined symbols being
4155          satisfied by definitions in an old object.  On the other hand, the
4156          old object won't have ".bar" satisfied by a new object.
4157
4158          Keep a list of newly added dot-symbols.  */
4159
4160       if (string[0] == '.')
4161         {
4162           struct ppc_link_hash_table *htab;
4163
4164           htab = (struct ppc_link_hash_table *) table;
4165           eh->u.next_dot_sym = htab->dot_syms;
4166           htab->dot_syms = eh;
4167         }
4168     }
4169
4170   return entry;
4171 }
4172
4173 struct tocsave_entry {
4174   asection *sec;
4175   bfd_vma offset;
4176 };
4177
4178 static hashval_t
4179 tocsave_htab_hash (const void *p)
4180 {
4181   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4182   return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4183 }
4184
4185 static int
4186 tocsave_htab_eq (const void *p1, const void *p2)
4187 {
4188   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4189   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4190   return e1->sec == e2->sec && e1->offset == e2->offset;
4191 }
4192
4193 /* Destroy a ppc64 ELF linker hash table.  */
4194
4195 static void
4196 ppc64_elf_link_hash_table_free (bfd *obfd)
4197 {
4198   struct ppc_link_hash_table *htab;
4199
4200   htab = (struct ppc_link_hash_table *) obfd->link.hash;
4201   if (htab->tocsave_htab)
4202     htab_delete (htab->tocsave_htab);
4203   bfd_hash_table_free (&htab->branch_hash_table);
4204   bfd_hash_table_free (&htab->stub_hash_table);
4205   _bfd_elf_link_hash_table_free (obfd);
4206 }
4207
4208 /* Create a ppc64 ELF linker hash table.  */
4209
4210 static struct bfd_link_hash_table *
4211 ppc64_elf_link_hash_table_create (bfd *abfd)
4212 {
4213   struct ppc_link_hash_table *htab;
4214   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4215
4216   htab = bfd_zmalloc (amt);
4217   if (htab == NULL)
4218     return NULL;
4219
4220   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4221                                       sizeof (struct ppc_link_hash_entry),
4222                                       PPC64_ELF_DATA))
4223     {
4224       free (htab);
4225       return NULL;
4226     }
4227
4228   /* Init the stub hash table too.  */
4229   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4230                             sizeof (struct ppc_stub_hash_entry)))
4231     {
4232       _bfd_elf_link_hash_table_free (abfd);
4233       return NULL;
4234     }
4235
4236   /* And the branch hash table.  */
4237   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4238                             sizeof (struct ppc_branch_hash_entry)))
4239     {
4240       bfd_hash_table_free (&htab->stub_hash_table);
4241       _bfd_elf_link_hash_table_free (abfd);
4242       return NULL;
4243     }
4244
4245   htab->tocsave_htab = htab_try_create (1024,
4246                                         tocsave_htab_hash,
4247                                         tocsave_htab_eq,
4248                                         NULL);
4249   if (htab->tocsave_htab == NULL)
4250     {
4251       ppc64_elf_link_hash_table_free (abfd);
4252       return NULL;
4253     }
4254   htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4255
4256   /* Initializing two fields of the union is just cosmetic.  We really
4257      only care about glist, but when compiled on a 32-bit host the
4258      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
4259      debugger inspection of these fields look nicer.  */
4260   htab->elf.init_got_refcount.refcount = 0;
4261   htab->elf.init_got_refcount.glist = NULL;
4262   htab->elf.init_plt_refcount.refcount = 0;
4263   htab->elf.init_plt_refcount.glist = NULL;
4264   htab->elf.init_got_offset.offset = 0;
4265   htab->elf.init_got_offset.glist = NULL;
4266   htab->elf.init_plt_offset.offset = 0;
4267   htab->elf.init_plt_offset.glist = NULL;
4268
4269   return &htab->elf.root;
4270 }
4271
4272 /* Create sections for linker generated code.  */
4273
4274 static bfd_boolean
4275 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4276 {
4277   struct ppc_link_hash_table *htab;
4278   flagword flags;
4279
4280   htab = ppc_hash_table (info);
4281
4282   /* Create .sfpr for code to save and restore fp regs.  */
4283   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4284            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4285   htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4286                                                    flags);
4287   if (htab->sfpr == NULL
4288       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4289     return FALSE;
4290
4291   /* Create .glink for lazy dynamic linking support.  */
4292   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4293                                                     flags);
4294   if (htab->glink == NULL
4295       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4296     return FALSE;
4297
4298   if (!info->no_ld_generated_unwind_info)
4299     {
4300       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4301                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4302       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4303                                                                  ".eh_frame",
4304                                                                  flags);
4305       if (htab->glink_eh_frame == NULL
4306           || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4307         return FALSE;
4308     }
4309
4310   flags = SEC_ALLOC | SEC_LINKER_CREATED;
4311   htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4312   if (htab->elf.iplt == NULL
4313       || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4314     return FALSE;
4315
4316   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4317            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4318   htab->elf.irelplt
4319     = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4320   if (htab->elf.irelplt == NULL
4321       || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4322     return FALSE;
4323
4324   /* Create branch lookup table for plt_branch stubs.  */
4325   flags = (SEC_ALLOC | SEC_LOAD
4326            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4327   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4328                                                    flags);
4329   if (htab->brlt == NULL
4330       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4331     return FALSE;
4332
4333   if (!bfd_link_pic (info))
4334     return TRUE;
4335
4336   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4337            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4338   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4339                                                       ".rela.branch_lt",
4340                                                       flags);
4341   if (htab->relbrlt == NULL
4342       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4343     return FALSE;
4344
4345   return TRUE;
4346 }
4347
4348 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
4349
4350 bfd_boolean
4351 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4352                          struct ppc64_elf_params *params)
4353 {
4354   struct ppc_link_hash_table *htab;
4355
4356   elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4357
4358 /* Always hook our dynamic sections into the first bfd, which is the
4359    linker created stub bfd.  This ensures that the GOT header is at
4360    the start of the output TOC section.  */
4361   htab = ppc_hash_table (info);
4362   if (htab == NULL)
4363     return FALSE;
4364   htab->elf.dynobj = params->stub_bfd;
4365   htab->params = params;
4366
4367   if (bfd_link_relocatable (info))
4368     return TRUE;
4369
4370   return create_linkage_sections (htab->elf.dynobj, info);
4371 }
4372
4373 /* Build a name for an entry in the stub hash table.  */
4374
4375 static char *
4376 ppc_stub_name (const asection *input_section,
4377                const asection *sym_sec,
4378                const struct ppc_link_hash_entry *h,
4379                const Elf_Internal_Rela *rel)
4380 {
4381   char *stub_name;
4382   ssize_t len;
4383
4384   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4385      offsets from a sym as a branch target?  In fact, we could
4386      probably assume the addend is always zero.  */
4387   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4388
4389   if (h)
4390     {
4391       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4392       stub_name = bfd_malloc (len);
4393       if (stub_name == NULL)
4394         return stub_name;
4395
4396       len = sprintf (stub_name, "%08x.%s+%x",
4397                      input_section->id & 0xffffffff,
4398                      h->elf.root.root.string,
4399                      (int) rel->r_addend & 0xffffffff);
4400     }
4401   else
4402     {
4403       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4404       stub_name = bfd_malloc (len);
4405       if (stub_name == NULL)
4406         return stub_name;
4407
4408       len = sprintf (stub_name, "%08x.%x:%x+%x",
4409                      input_section->id & 0xffffffff,
4410                      sym_sec->id & 0xffffffff,
4411                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4412                      (int) rel->r_addend & 0xffffffff);
4413     }
4414   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4415     stub_name[len - 2] = 0;
4416   return stub_name;
4417 }
4418
4419 /* Look up an entry in the stub hash.  Stub entries are cached because
4420    creating the stub name takes a bit of time.  */
4421
4422 static struct ppc_stub_hash_entry *
4423 ppc_get_stub_entry (const asection *input_section,
4424                     const asection *sym_sec,
4425                     struct ppc_link_hash_entry *h,
4426                     const Elf_Internal_Rela *rel,
4427                     struct ppc_link_hash_table *htab)
4428 {
4429   struct ppc_stub_hash_entry *stub_entry;
4430   struct map_stub *group;
4431
4432   /* If this input section is part of a group of sections sharing one
4433      stub section, then use the id of the first section in the group.
4434      Stub names need to include a section id, as there may well be
4435      more than one stub used to reach say, printf, and we need to
4436      distinguish between them.  */
4437   group = htab->sec_info[input_section->id].u.group;
4438
4439   if (h != NULL && h->u.stub_cache != NULL
4440       && h->u.stub_cache->h == h
4441       && h->u.stub_cache->group == group)
4442     {
4443       stub_entry = h->u.stub_cache;
4444     }
4445   else
4446     {
4447       char *stub_name;
4448
4449       stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4450       if (stub_name == NULL)
4451         return NULL;
4452
4453       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4454                                          stub_name, FALSE, FALSE);
4455       if (h != NULL)
4456         h->u.stub_cache = stub_entry;
4457
4458       free (stub_name);
4459     }
4460
4461   return stub_entry;
4462 }
4463
4464 /* Add a new stub entry to the stub hash.  Not all fields of the new
4465    stub entry are initialised.  */
4466
4467 static struct ppc_stub_hash_entry *
4468 ppc_add_stub (const char *stub_name,
4469               asection *section,
4470               struct bfd_link_info *info)
4471 {
4472   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4473   struct map_stub *group;
4474   asection *link_sec;
4475   asection *stub_sec;
4476   struct ppc_stub_hash_entry *stub_entry;
4477
4478   group = htab->sec_info[section->id].u.group;
4479   link_sec = group->link_sec;
4480   stub_sec = group->stub_sec;
4481   if (stub_sec == NULL)
4482     {
4483       size_t namelen;
4484       bfd_size_type len;
4485       char *s_name;
4486
4487       namelen = strlen (link_sec->name);
4488       len = namelen + sizeof (STUB_SUFFIX);
4489       s_name = bfd_alloc (htab->params->stub_bfd, len);
4490       if (s_name == NULL)
4491         return NULL;
4492
4493       memcpy (s_name, link_sec->name, namelen);
4494       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4495       stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4496       if (stub_sec == NULL)
4497         return NULL;
4498       group->stub_sec = stub_sec;
4499     }
4500
4501   /* Enter this entry into the linker stub hash table.  */
4502   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4503                                      TRUE, FALSE);
4504   if (stub_entry == NULL)
4505     {
4506       info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4507                               section->owner, stub_name);
4508       return NULL;
4509     }
4510
4511   stub_entry->group = group;
4512   stub_entry->stub_offset = 0;
4513   return stub_entry;
4514 }
4515
4516 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4517    not already done.  */
4518
4519 static bfd_boolean
4520 create_got_section (bfd *abfd, struct bfd_link_info *info)
4521 {
4522   asection *got, *relgot;
4523   flagword flags;
4524   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4525
4526   if (!is_ppc64_elf (abfd))
4527     return FALSE;
4528   if (htab == NULL)
4529     return FALSE;
4530
4531   if (!htab->elf.sgot
4532       && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4533     return FALSE;
4534
4535   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4536            | SEC_LINKER_CREATED);
4537
4538   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4539   if (!got
4540       || !bfd_set_section_alignment (abfd, got, 3))
4541     return FALSE;
4542
4543   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4544                                                flags | SEC_READONLY);
4545   if (!relgot
4546       || ! bfd_set_section_alignment (abfd, relgot, 3))
4547     return FALSE;
4548
4549   ppc64_elf_tdata (abfd)->got = got;
4550   ppc64_elf_tdata (abfd)->relgot = relgot;
4551   return TRUE;
4552 }
4553
4554 /* Create the dynamic sections, and set up shortcuts.  */
4555
4556 static bfd_boolean
4557 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4558 {
4559   struct ppc_link_hash_table *htab;
4560
4561   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4562     return FALSE;
4563
4564   htab = ppc_hash_table (info);
4565   if (htab == NULL)
4566     return FALSE;
4567
4568   htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
4569   if (!bfd_link_pic (info))
4570     htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
4571
4572   if (!htab->elf.sgot || !htab->elf.splt || !htab->elf.srelplt || !htab->dynbss
4573       || (!bfd_link_pic (info) && !htab->relbss))
4574     abort ();
4575
4576   return TRUE;
4577 }
4578
4579 /* Follow indirect and warning symbol links.  */
4580
4581 static inline struct bfd_link_hash_entry *
4582 follow_link (struct bfd_link_hash_entry *h)
4583 {
4584   while (h->type == bfd_link_hash_indirect
4585          || h->type == bfd_link_hash_warning)
4586     h = h->u.i.link;
4587   return h;
4588 }
4589
4590 static inline struct elf_link_hash_entry *
4591 elf_follow_link (struct elf_link_hash_entry *h)
4592 {
4593   return (struct elf_link_hash_entry *) follow_link (&h->root);
4594 }
4595
4596 static inline struct ppc_link_hash_entry *
4597 ppc_follow_link (struct ppc_link_hash_entry *h)
4598 {
4599   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4600 }
4601
4602 /* Merge PLT info on FROM with that on TO.  */
4603
4604 static void
4605 move_plt_plist (struct ppc_link_hash_entry *from,
4606                 struct ppc_link_hash_entry *to)
4607 {
4608   if (from->elf.plt.plist != NULL)
4609     {
4610       if (to->elf.plt.plist != NULL)
4611         {
4612           struct plt_entry **entp;
4613           struct plt_entry *ent;
4614
4615           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4616             {
4617               struct plt_entry *dent;
4618
4619               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4620                 if (dent->addend == ent->addend)
4621                   {
4622                     dent->plt.refcount += ent->plt.refcount;
4623                     *entp = ent->next;
4624                     break;
4625                   }
4626               if (dent == NULL)
4627                 entp = &ent->next;
4628             }
4629           *entp = to->elf.plt.plist;
4630         }
4631
4632       to->elf.plt.plist = from->elf.plt.plist;
4633       from->elf.plt.plist = NULL;
4634     }
4635 }
4636
4637 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
4638
4639 static void
4640 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4641                                 struct elf_link_hash_entry *dir,
4642                                 struct elf_link_hash_entry *ind)
4643 {
4644   struct ppc_link_hash_entry *edir, *eind;
4645
4646   edir = (struct ppc_link_hash_entry *) dir;
4647   eind = (struct ppc_link_hash_entry *) ind;
4648
4649   edir->is_func |= eind->is_func;
4650   edir->is_func_descriptor |= eind->is_func_descriptor;
4651   edir->tls_mask |= eind->tls_mask;
4652   if (eind->oh != NULL)
4653     edir->oh = ppc_follow_link (eind->oh);
4654
4655   /* If called to transfer flags for a weakdef during processing
4656      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4657      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
4658   if (!(ELIMINATE_COPY_RELOCS
4659         && eind->elf.root.type != bfd_link_hash_indirect
4660         && edir->elf.dynamic_adjusted))
4661     edir->elf.non_got_ref |= eind->elf.non_got_ref;
4662
4663   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4664   edir->elf.ref_regular |= eind->elf.ref_regular;
4665   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4666   edir->elf.needs_plt |= eind->elf.needs_plt;
4667   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4668
4669   /* Copy over any dynamic relocs we may have on the indirect sym.  */
4670   if (eind->dyn_relocs != NULL)
4671     {
4672       if (edir->dyn_relocs != NULL)
4673         {
4674           struct elf_dyn_relocs **pp;
4675           struct elf_dyn_relocs *p;
4676
4677           /* Add reloc counts against the indirect sym to the direct sym
4678              list.  Merge any entries against the same section.  */
4679           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4680             {
4681               struct elf_dyn_relocs *q;
4682
4683               for (q = edir->dyn_relocs; q != NULL; q = q->next)
4684                 if (q->sec == p->sec)
4685                   {
4686                     q->pc_count += p->pc_count;
4687                     q->count += p->count;
4688                     *pp = p->next;
4689                     break;
4690                   }
4691               if (q == NULL)
4692                 pp = &p->next;
4693             }
4694           *pp = edir->dyn_relocs;
4695         }
4696
4697       edir->dyn_relocs = eind->dyn_relocs;
4698       eind->dyn_relocs = NULL;
4699     }
4700
4701   /* If we were called to copy over info for a weak sym, that's all.
4702      You might think dyn_relocs need not be copied over;  After all,
4703      both syms will be dynamic or both non-dynamic so we're just
4704      moving reloc accounting around.  However, ELIMINATE_COPY_RELOCS
4705      code in ppc64_elf_adjust_dynamic_symbol needs to check for
4706      dyn_relocs in read-only sections, and it does so on what is the
4707      DIR sym here.  */
4708   if (eind->elf.root.type != bfd_link_hash_indirect)
4709     return;
4710
4711   /* Copy over got entries that we may have already seen to the
4712      symbol which just became indirect.  */
4713   if (eind->elf.got.glist != NULL)
4714     {
4715       if (edir->elf.got.glist != NULL)
4716         {
4717           struct got_entry **entp;
4718           struct got_entry *ent;
4719
4720           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4721             {
4722               struct got_entry *dent;
4723
4724               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4725                 if (dent->addend == ent->addend
4726                     && dent->owner == ent->owner
4727                     && dent->tls_type == ent->tls_type)
4728                   {
4729                     dent->got.refcount += ent->got.refcount;
4730                     *entp = ent->next;
4731                     break;
4732                   }
4733               if (dent == NULL)
4734                 entp = &ent->next;
4735             }
4736           *entp = edir->elf.got.glist;
4737         }
4738
4739       edir->elf.got.glist = eind->elf.got.glist;
4740       eind->elf.got.glist = NULL;
4741     }
4742
4743   /* And plt entries.  */
4744   move_plt_plist (eind, edir);
4745
4746   if (eind->elf.dynindx != -1)
4747     {
4748       if (edir->elf.dynindx != -1)
4749         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4750                                 edir->elf.dynstr_index);
4751       edir->elf.dynindx = eind->elf.dynindx;
4752       edir->elf.dynstr_index = eind->elf.dynstr_index;
4753       eind->elf.dynindx = -1;
4754       eind->elf.dynstr_index = 0;
4755     }
4756 }
4757
4758 /* Find the function descriptor hash entry from the given function code
4759    hash entry FH.  Link the entries via their OH fields.  */
4760
4761 static struct ppc_link_hash_entry *
4762 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4763 {
4764   struct ppc_link_hash_entry *fdh = fh->oh;
4765
4766   if (fdh == NULL)
4767     {
4768       const char *fd_name = fh->elf.root.root.string + 1;
4769
4770       fdh = (struct ppc_link_hash_entry *)
4771         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4772       if (fdh == NULL)
4773         return fdh;
4774
4775       fdh->is_func_descriptor = 1;
4776       fdh->oh = fh;
4777       fh->is_func = 1;
4778       fh->oh = fdh;
4779     }
4780
4781   return ppc_follow_link (fdh);
4782 }
4783
4784 /* Make a fake function descriptor sym for the code sym FH.  */
4785
4786 static struct ppc_link_hash_entry *
4787 make_fdh (struct bfd_link_info *info,
4788           struct ppc_link_hash_entry *fh)
4789 {
4790   bfd *abfd;
4791   asymbol *newsym;
4792   struct bfd_link_hash_entry *bh;
4793   struct ppc_link_hash_entry *fdh;
4794
4795   abfd = fh->elf.root.u.undef.abfd;
4796   newsym = bfd_make_empty_symbol (abfd);
4797   newsym->name = fh->elf.root.root.string + 1;
4798   newsym->section = bfd_und_section_ptr;
4799   newsym->value = 0;
4800   newsym->flags = BSF_WEAK;
4801
4802   bh = NULL;
4803   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4804                                          newsym->flags, newsym->section,
4805                                          newsym->value, NULL, FALSE, FALSE,
4806                                          &bh))
4807     return NULL;
4808
4809   fdh = (struct ppc_link_hash_entry *) bh;
4810   fdh->elf.non_elf = 0;
4811   fdh->fake = 1;
4812   fdh->is_func_descriptor = 1;
4813   fdh->oh = fh;
4814   fh->is_func = 1;
4815   fh->oh = fdh;
4816   return fdh;
4817 }
4818
4819 /* Fix function descriptor symbols defined in .opd sections to be
4820    function type.  */
4821
4822 static bfd_boolean
4823 ppc64_elf_add_symbol_hook (bfd *ibfd,
4824                            struct bfd_link_info *info,
4825                            Elf_Internal_Sym *isym,
4826                            const char **name,
4827                            flagword *flags ATTRIBUTE_UNUSED,
4828                            asection **sec,
4829                            bfd_vma *value)
4830 {
4831   if ((ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4832        || ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4833       && (ibfd->flags & DYNAMIC) == 0
4834       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4835     elf_tdata (info->output_bfd)->has_gnu_symbols = elf_gnu_symbol_any;
4836
4837   if (*sec != NULL
4838       && strcmp ((*sec)->name, ".opd") == 0)
4839     {
4840       asection *code_sec;
4841
4842       if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4843             || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4844         isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4845
4846       /* If the symbol is a function defined in .opd, and the function
4847          code is in a discarded group, let it appear to be undefined.  */
4848       if (!bfd_link_relocatable (info)
4849           && (*sec)->reloc_count != 0
4850           && opd_entry_value (*sec, *value, &code_sec, NULL,
4851                               FALSE) != (bfd_vma) -1
4852           && discarded_section (code_sec))
4853         {
4854           *sec = bfd_und_section_ptr;
4855           isym->st_shndx = SHN_UNDEF;
4856         }
4857     }
4858   else if (*sec != NULL
4859            && strcmp ((*sec)->name, ".toc") == 0
4860            && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4861     {
4862       struct ppc_link_hash_table *htab = ppc_hash_table (info);
4863       if (htab != NULL)
4864         htab->params->object_in_toc = 1;
4865     }
4866
4867   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4868     {
4869       if (abiversion (ibfd) == 0)
4870         set_abiversion (ibfd, 2);
4871       else if (abiversion (ibfd) == 1)
4872         {
4873           info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4874                                     " for ABI version 1\n"), name);
4875           bfd_set_error (bfd_error_bad_value);
4876           return FALSE;
4877         }
4878     }
4879
4880   return TRUE;
4881 }
4882
4883 /* Merge non-visibility st_other attributes: local entry point.  */
4884
4885 static void
4886 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4887                                   const Elf_Internal_Sym *isym,
4888                                   bfd_boolean definition,
4889                                   bfd_boolean dynamic)
4890 {
4891   if (definition && !dynamic)
4892     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4893                 | ELF_ST_VISIBILITY (h->other));
4894 }
4895
4896 /* This function makes an old ABI object reference to ".bar" cause the
4897    inclusion of a new ABI object archive that defines "bar".
4898    NAME is a symbol defined in an archive.  Return a symbol in the hash
4899    table that might be satisfied by the archive symbols.  */
4900
4901 static struct elf_link_hash_entry *
4902 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4903                                  struct bfd_link_info *info,
4904                                  const char *name)
4905 {
4906   struct elf_link_hash_entry *h;
4907   char *dot_name;
4908   size_t len;
4909
4910   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4911   if (h != NULL
4912       /* Don't return this sym if it is a fake function descriptor
4913          created by add_symbol_adjust.  */
4914       && !(h->root.type == bfd_link_hash_undefweak
4915            && ((struct ppc_link_hash_entry *) h)->fake))
4916     return h;
4917
4918   if (name[0] == '.')
4919     return h;
4920
4921   len = strlen (name);
4922   dot_name = bfd_alloc (abfd, len + 2);
4923   if (dot_name == NULL)
4924     return (struct elf_link_hash_entry *) 0 - 1;
4925   dot_name[0] = '.';
4926   memcpy (dot_name + 1, name, len + 1);
4927   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4928   bfd_release (abfd, dot_name);
4929   return h;
4930 }
4931
4932 /* This function satisfies all old ABI object references to ".bar" if a
4933    new ABI object defines "bar".  Well, at least, undefined dot symbols
4934    are made weak.  This stops later archive searches from including an
4935    object if we already have a function descriptor definition.  It also
4936    prevents the linker complaining about undefined symbols.
4937    We also check and correct mismatched symbol visibility here.  The
4938    most restrictive visibility of the function descriptor and the
4939    function entry symbol is used.  */
4940
4941 static bfd_boolean
4942 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4943 {
4944   struct ppc_link_hash_table *htab;
4945   struct ppc_link_hash_entry *fdh;
4946
4947   if (eh->elf.root.type == bfd_link_hash_indirect)
4948     return TRUE;
4949
4950   if (eh->elf.root.type == bfd_link_hash_warning)
4951     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4952
4953   if (eh->elf.root.root.string[0] != '.')
4954     abort ();
4955
4956   htab = ppc_hash_table (info);
4957   if (htab == NULL)
4958     return FALSE;
4959
4960   fdh = lookup_fdh (eh, htab);
4961   if (fdh == NULL)
4962     {
4963       if (!bfd_link_relocatable (info)
4964           && (eh->elf.root.type == bfd_link_hash_undefined
4965               || eh->elf.root.type == bfd_link_hash_undefweak)
4966           && eh->elf.ref_regular)
4967         {
4968           /* Make an undefweak function descriptor sym, which is enough to
4969              pull in an --as-needed shared lib, but won't cause link
4970              errors.  Archives are handled elsewhere.  */
4971           fdh = make_fdh (info, eh);
4972           if (fdh == NULL)
4973             return FALSE;
4974           fdh->elf.ref_regular = 1;
4975         }
4976     }
4977   else
4978     {
4979       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4980       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4981       if (entry_vis < descr_vis)
4982         fdh->elf.other += entry_vis - descr_vis;
4983       else if (entry_vis > descr_vis)
4984         eh->elf.other += descr_vis - entry_vis;
4985
4986       if ((fdh->elf.root.type == bfd_link_hash_defined
4987            || fdh->elf.root.type == bfd_link_hash_defweak)
4988           && eh->elf.root.type == bfd_link_hash_undefined)
4989         {
4990           eh->elf.root.type = bfd_link_hash_undefweak;
4991           eh->was_undefined = 1;
4992           htab->twiddled_syms = 1;
4993         }
4994     }
4995
4996   return TRUE;
4997 }
4998
4999 /* Set up opd section info and abiversion for IBFD, and process list
5000    of dot-symbols we made in link_hash_newfunc.  */
5001
5002 static bfd_boolean
5003 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5004 {
5005   struct ppc_link_hash_table *htab;
5006   struct ppc_link_hash_entry **p, *eh;
5007   asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5008
5009   if (opd != NULL && opd->size != 0)
5010     {
5011       if (abiversion (ibfd) == 0)
5012         set_abiversion (ibfd, 1);
5013       else if (abiversion (ibfd) == 2)
5014         {
5015           info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5016                                     " version %d\n"),
5017                                   ibfd, abiversion (ibfd));
5018           bfd_set_error (bfd_error_bad_value);
5019           return FALSE;
5020         }
5021
5022       if ((ibfd->flags & DYNAMIC) == 0
5023           && (opd->flags & SEC_RELOC) != 0
5024           && opd->reloc_count != 0
5025           && !bfd_is_abs_section (opd->output_section))
5026         {
5027           /* Garbage collection needs some extra help with .opd sections.
5028              We don't want to necessarily keep everything referenced by
5029              relocs in .opd, as that would keep all functions.  Instead,
5030              if we reference an .opd symbol (a function descriptor), we
5031              want to keep the function code symbol's section.  This is
5032              easy for global symbols, but for local syms we need to keep
5033              information about the associated function section.  */
5034           bfd_size_type amt;
5035           asection **opd_sym_map;
5036
5037           amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5038           opd_sym_map = bfd_zalloc (ibfd, amt);
5039           if (opd_sym_map == NULL)
5040             return FALSE;
5041           ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5042           BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5043           ppc64_elf_section_data (opd)->sec_type = sec_opd;
5044         }
5045     }
5046
5047   if (!is_ppc64_elf (info->output_bfd))
5048     return TRUE;
5049   htab = ppc_hash_table (info);
5050   if (htab == NULL)
5051     return FALSE;
5052
5053   /* For input files without an explicit abiversion in e_flags
5054      we should have flagged any with symbol st_other bits set
5055      as ELFv1 and above flagged those with .opd as ELFv2.
5056      Set the output abiversion if not yet set, and for any input
5057      still ambiguous, take its abiversion from the output.
5058      Differences in ABI are reported later.  */
5059   if (abiversion (info->output_bfd) == 0)
5060     set_abiversion (info->output_bfd, abiversion (ibfd));
5061   else if (abiversion (ibfd) == 0)
5062     set_abiversion (ibfd, abiversion (info->output_bfd));
5063
5064   p = &htab->dot_syms;
5065   while ((eh = *p) != NULL)
5066     {
5067       *p = NULL;
5068       if (&eh->elf == htab->elf.hgot)
5069         ;
5070       else if (htab->elf.hgot == NULL
5071                && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5072         htab->elf.hgot = &eh->elf;
5073       else if (!add_symbol_adjust (eh, info))
5074         return FALSE;
5075       p = &eh->u.next_dot_sym;
5076     }
5077
5078   /* Clear the list for non-ppc64 input files.  */
5079   p = &htab->dot_syms;
5080   while ((eh = *p) != NULL)
5081     {
5082       *p = NULL;
5083       p = &eh->u.next_dot_sym;
5084     }
5085
5086   /* We need to fix the undefs list for any syms we have twiddled to
5087      undef_weak.  */
5088   if (htab->twiddled_syms)
5089     {
5090       bfd_link_repair_undef_list (&htab->elf.root);
5091       htab->twiddled_syms = 0;
5092     }
5093   return TRUE;
5094 }
5095
5096 /* Undo hash table changes when an --as-needed input file is determined
5097    not to be needed.  */
5098
5099 static bfd_boolean
5100 ppc64_elf_notice_as_needed (bfd *ibfd,
5101                             struct bfd_link_info *info,
5102                             enum notice_asneeded_action act)
5103 {
5104   if (act == notice_not_needed)
5105     {
5106       struct ppc_link_hash_table *htab = ppc_hash_table (info);
5107
5108       if (htab == NULL)
5109         return FALSE;
5110
5111       htab->dot_syms = NULL;
5112     }
5113   return _bfd_elf_notice_as_needed (ibfd, info, act);
5114 }
5115
5116 /* If --just-symbols against a final linked binary, then assume we need
5117    toc adjusting stubs when calling functions defined there.  */
5118
5119 static void
5120 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5121 {
5122   if ((sec->flags & SEC_CODE) != 0
5123       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5124       && is_ppc64_elf (sec->owner))
5125     {
5126       if (abiversion (sec->owner) >= 2
5127           || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5128         sec->has_toc_reloc = 1;
5129     }
5130   _bfd_elf_link_just_syms (sec, info);
5131 }
5132
5133 static struct plt_entry **
5134 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5135                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5136 {
5137   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5138   struct plt_entry **local_plt;
5139   unsigned char *local_got_tls_masks;
5140
5141   if (local_got_ents == NULL)
5142     {
5143       bfd_size_type size = symtab_hdr->sh_info;
5144
5145       size *= (sizeof (*local_got_ents)
5146                + sizeof (*local_plt)
5147                + sizeof (*local_got_tls_masks));
5148       local_got_ents = bfd_zalloc (abfd, size);
5149       if (local_got_ents == NULL)
5150         return NULL;
5151       elf_local_got_ents (abfd) = local_got_ents;
5152     }
5153
5154   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5155     {
5156       struct got_entry *ent;
5157
5158       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5159         if (ent->addend == r_addend
5160             && ent->owner == abfd
5161             && ent->tls_type == tls_type)
5162           break;
5163       if (ent == NULL)
5164         {
5165           bfd_size_type amt = sizeof (*ent);
5166           ent = bfd_alloc (abfd, amt);
5167           if (ent == NULL)
5168             return FALSE;
5169           ent->next = local_got_ents[r_symndx];
5170           ent->addend = r_addend;
5171           ent->owner = abfd;
5172           ent->tls_type = tls_type;
5173           ent->is_indirect = FALSE;
5174           ent->got.refcount = 0;
5175           local_got_ents[r_symndx] = ent;
5176         }
5177       ent->got.refcount += 1;
5178     }
5179
5180   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5181   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5182   local_got_tls_masks[r_symndx] |= tls_type;
5183
5184   return local_plt + r_symndx;
5185 }
5186
5187 static bfd_boolean
5188 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5189 {
5190   struct plt_entry *ent;
5191
5192   for (ent = *plist; ent != NULL; ent = ent->next)
5193     if (ent->addend == addend)
5194       break;
5195   if (ent == NULL)
5196     {
5197       bfd_size_type amt = sizeof (*ent);
5198       ent = bfd_alloc (abfd, amt);
5199       if (ent == NULL)
5200         return FALSE;
5201       ent->next = *plist;
5202       ent->addend = addend;
5203       ent->plt.refcount = 0;
5204       *plist = ent;
5205     }
5206   ent->plt.refcount += 1;
5207   return TRUE;
5208 }
5209
5210 static bfd_boolean
5211 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5212 {
5213   return (r_type == R_PPC64_REL24
5214           || r_type == R_PPC64_REL14
5215           || r_type == R_PPC64_REL14_BRTAKEN
5216           || r_type == R_PPC64_REL14_BRNTAKEN
5217           || r_type == R_PPC64_ADDR24
5218           || r_type == R_PPC64_ADDR14
5219           || r_type == R_PPC64_ADDR14_BRTAKEN
5220           || r_type == R_PPC64_ADDR14_BRNTAKEN);
5221 }
5222
5223 /* Look through the relocs for a section during the first phase, and
5224    calculate needed space in the global offset table, procedure
5225    linkage table, and dynamic reloc sections.  */
5226
5227 static bfd_boolean
5228 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5229                         asection *sec, const Elf_Internal_Rela *relocs)
5230 {
5231   struct ppc_link_hash_table *htab;
5232   Elf_Internal_Shdr *symtab_hdr;
5233   struct elf_link_hash_entry **sym_hashes;
5234   const Elf_Internal_Rela *rel;
5235   const Elf_Internal_Rela *rel_end;
5236   asection *sreloc;
5237   asection **opd_sym_map;
5238   struct elf_link_hash_entry *tga, *dottga;
5239
5240   if (bfd_link_relocatable (info))
5241     return TRUE;
5242
5243   /* Don't do anything special with non-loaded, non-alloced sections.
5244      In particular, any relocs in such sections should not affect GOT
5245      and PLT reference counting (ie. we don't allow them to create GOT
5246      or PLT entries), there's no possibility or desire to optimize TLS
5247      relocs, and there's not much point in propagating relocs to shared
5248      libs that the dynamic linker won't relocate.  */
5249   if ((sec->flags & SEC_ALLOC) == 0)
5250     return TRUE;
5251
5252   BFD_ASSERT (is_ppc64_elf (abfd));
5253
5254   htab = ppc_hash_table (info);
5255   if (htab == NULL)
5256     return FALSE;
5257
5258   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5259                               FALSE, FALSE, TRUE);
5260   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5261                                  FALSE, FALSE, TRUE);
5262   symtab_hdr = &elf_symtab_hdr (abfd);
5263   sym_hashes = elf_sym_hashes (abfd);
5264   sreloc = NULL;
5265   opd_sym_map = NULL;
5266   if (ppc64_elf_section_data (sec) != NULL
5267       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5268     opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5269
5270   rel_end = relocs + sec->reloc_count;
5271   for (rel = relocs; rel < rel_end; rel++)
5272     {
5273       unsigned long r_symndx;
5274       struct elf_link_hash_entry *h;
5275       enum elf_ppc64_reloc_type r_type;
5276       int tls_type;
5277       struct _ppc64_elf_section_data *ppc64_sec;
5278       struct plt_entry **ifunc;
5279
5280       r_symndx = ELF64_R_SYM (rel->r_info);
5281       if (r_symndx < symtab_hdr->sh_info)
5282         h = NULL;
5283       else
5284         {
5285           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5286           h = elf_follow_link (h);
5287
5288           /* PR15323, ref flags aren't set for references in the same
5289              object.  */
5290           h->root.non_ir_ref = 1;
5291
5292           if (h == htab->elf.hgot)
5293             sec->has_toc_reloc = 1;
5294         }
5295
5296       tls_type = 0;
5297       ifunc = NULL;
5298       if (h != NULL)
5299         {
5300           if (h->type == STT_GNU_IFUNC)
5301             {
5302               h->needs_plt = 1;
5303               ifunc = &h->plt.plist;
5304             }
5305         }
5306       else
5307         {
5308           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5309                                                           abfd, r_symndx);
5310           if (isym == NULL)
5311             return FALSE;
5312
5313           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5314             {
5315               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5316                                              rel->r_addend, PLT_IFUNC);
5317               if (ifunc == NULL)
5318                 return FALSE;
5319             }
5320         }
5321       r_type = ELF64_R_TYPE (rel->r_info);
5322       if (is_branch_reloc (r_type))
5323         {
5324           if (h != NULL && (h == tga || h == dottga))
5325             {
5326               if (rel != relocs
5327                   && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5328                       || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5329                 /* We have a new-style __tls_get_addr call with a marker
5330                    reloc.  */
5331                 ;
5332               else
5333                 /* Mark this section as having an old-style call.  */
5334                 sec->has_tls_get_addr_call = 1;
5335             }
5336
5337           /* STT_GNU_IFUNC symbols must have a PLT entry.  */
5338           if (ifunc != NULL
5339               && !update_plt_info (abfd, ifunc, rel->r_addend))
5340             return FALSE;
5341         }
5342
5343       switch (r_type)
5344         {
5345         case R_PPC64_TLSGD:
5346         case R_PPC64_TLSLD:
5347           /* These special tls relocs tie a call to __tls_get_addr with
5348              its parameter symbol.  */
5349           break;
5350
5351         case R_PPC64_GOT_TLSLD16:
5352         case R_PPC64_GOT_TLSLD16_LO:
5353         case R_PPC64_GOT_TLSLD16_HI:
5354         case R_PPC64_GOT_TLSLD16_HA:
5355           tls_type = TLS_TLS | TLS_LD;
5356           goto dogottls;
5357
5358         case R_PPC64_GOT_TLSGD16:
5359         case R_PPC64_GOT_TLSGD16_LO:
5360         case R_PPC64_GOT_TLSGD16_HI:
5361         case R_PPC64_GOT_TLSGD16_HA:
5362           tls_type = TLS_TLS | TLS_GD;
5363           goto dogottls;
5364
5365         case R_PPC64_GOT_TPREL16_DS:
5366         case R_PPC64_GOT_TPREL16_LO_DS:
5367         case R_PPC64_GOT_TPREL16_HI:
5368         case R_PPC64_GOT_TPREL16_HA:
5369           if (bfd_link_pic (info))
5370             info->flags |= DF_STATIC_TLS;
5371           tls_type = TLS_TLS | TLS_TPREL;
5372           goto dogottls;
5373
5374         case R_PPC64_GOT_DTPREL16_DS:
5375         case R_PPC64_GOT_DTPREL16_LO_DS:
5376         case R_PPC64_GOT_DTPREL16_HI:
5377         case R_PPC64_GOT_DTPREL16_HA:
5378           tls_type = TLS_TLS | TLS_DTPREL;
5379         dogottls:
5380           sec->has_tls_reloc = 1;
5381           /* Fall thru */
5382
5383         case R_PPC64_GOT16:
5384         case R_PPC64_GOT16_DS:
5385         case R_PPC64_GOT16_HA:
5386         case R_PPC64_GOT16_HI:
5387         case R_PPC64_GOT16_LO:
5388         case R_PPC64_GOT16_LO_DS:
5389           /* This symbol requires a global offset table entry.  */
5390           sec->has_toc_reloc = 1;
5391           if (r_type == R_PPC64_GOT_TLSLD16
5392               || r_type == R_PPC64_GOT_TLSGD16
5393               || r_type == R_PPC64_GOT_TPREL16_DS
5394               || r_type == R_PPC64_GOT_DTPREL16_DS
5395               || r_type == R_PPC64_GOT16
5396               || r_type == R_PPC64_GOT16_DS)
5397             {
5398               htab->do_multi_toc = 1;
5399               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5400             }
5401
5402           if (ppc64_elf_tdata (abfd)->got == NULL
5403               && !create_got_section (abfd, info))
5404             return FALSE;
5405
5406           if (h != NULL)
5407             {
5408               struct ppc_link_hash_entry *eh;
5409               struct got_entry *ent;
5410
5411               eh = (struct ppc_link_hash_entry *) h;
5412               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5413                 if (ent->addend == rel->r_addend
5414                     && ent->owner == abfd
5415                     && ent->tls_type == tls_type)
5416                   break;
5417               if (ent == NULL)
5418                 {
5419                   bfd_size_type amt = sizeof (*ent);
5420                   ent = bfd_alloc (abfd, amt);
5421                   if (ent == NULL)
5422                     return FALSE;
5423                   ent->next = eh->elf.got.glist;
5424                   ent->addend = rel->r_addend;
5425                   ent->owner = abfd;
5426                   ent->tls_type = tls_type;
5427                   ent->is_indirect = FALSE;
5428                   ent->got.refcount = 0;
5429                   eh->elf.got.glist = ent;
5430                 }
5431               ent->got.refcount += 1;
5432               eh->tls_mask |= tls_type;
5433             }
5434           else
5435             /* This is a global offset table entry for a local symbol.  */
5436             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5437                                         rel->r_addend, tls_type))
5438               return FALSE;
5439
5440           /* We may also need a plt entry if the symbol turns out to be
5441              an ifunc.  */
5442           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5443             {
5444               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5445                 return FALSE;
5446             }
5447           break;
5448
5449         case R_PPC64_PLT16_HA:
5450         case R_PPC64_PLT16_HI:
5451         case R_PPC64_PLT16_LO:
5452         case R_PPC64_PLT32:
5453         case R_PPC64_PLT64:
5454           /* This symbol requires a procedure linkage table entry.  We
5455              actually build the entry in adjust_dynamic_symbol,
5456              because this might be a case of linking PIC code without
5457              linking in any dynamic objects, in which case we don't
5458              need to generate a procedure linkage table after all.  */
5459           if (h == NULL)
5460             {
5461               /* It does not make sense to have a procedure linkage
5462                  table entry for a local symbol.  */
5463               bfd_set_error (bfd_error_bad_value);
5464               return FALSE;
5465             }
5466           else
5467             {
5468               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5469                 return FALSE;
5470               h->needs_plt = 1;
5471               if (h->root.root.string[0] == '.'
5472                   && h->root.root.string[1] != '\0')
5473                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5474             }
5475           break;
5476
5477           /* The following relocations don't need to propagate the
5478              relocation if linking a shared object since they are
5479              section relative.  */
5480         case R_PPC64_SECTOFF:
5481         case R_PPC64_SECTOFF_LO:
5482         case R_PPC64_SECTOFF_HI:
5483         case R_PPC64_SECTOFF_HA:
5484         case R_PPC64_SECTOFF_DS:
5485         case R_PPC64_SECTOFF_LO_DS:
5486         case R_PPC64_DTPREL16:
5487         case R_PPC64_DTPREL16_LO:
5488         case R_PPC64_DTPREL16_HI:
5489         case R_PPC64_DTPREL16_HA:
5490         case R_PPC64_DTPREL16_DS:
5491         case R_PPC64_DTPREL16_LO_DS:
5492         case R_PPC64_DTPREL16_HIGH:
5493         case R_PPC64_DTPREL16_HIGHA:
5494         case R_PPC64_DTPREL16_HIGHER:
5495         case R_PPC64_DTPREL16_HIGHERA:
5496         case R_PPC64_DTPREL16_HIGHEST:
5497         case R_PPC64_DTPREL16_HIGHESTA:
5498           break;
5499
5500           /* Nor do these.  */
5501         case R_PPC64_REL16:
5502         case R_PPC64_REL16_LO:
5503         case R_PPC64_REL16_HI:
5504         case R_PPC64_REL16_HA:
5505           break;
5506
5507           /* Not supported as a dynamic relocation.  */
5508         case R_PPC64_ADDR64_LOCAL:
5509           if (bfd_link_pic (info))
5510             {
5511               if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5512                 ppc_howto_init ();
5513               info->callbacks->einfo (_("%P: %H: %s reloc unsupported "
5514                                         "in shared libraries and PIEs.\n"),
5515                                       abfd, sec, rel->r_offset,
5516                                       ppc64_elf_howto_table[r_type]->name);
5517               bfd_set_error (bfd_error_bad_value);
5518               return FALSE;
5519             }
5520           break;
5521
5522         case R_PPC64_TOC16:
5523         case R_PPC64_TOC16_DS:
5524           htab->do_multi_toc = 1;
5525           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5526         case R_PPC64_TOC16_LO:
5527         case R_PPC64_TOC16_HI:
5528         case R_PPC64_TOC16_HA:
5529         case R_PPC64_TOC16_LO_DS:
5530           sec->has_toc_reloc = 1;
5531           break;
5532
5533           /* This relocation describes the C++ object vtable hierarchy.
5534              Reconstruct it for later use during GC.  */
5535         case R_PPC64_GNU_VTINHERIT:
5536           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5537             return FALSE;
5538           break;
5539
5540           /* This relocation describes which C++ vtable entries are actually
5541              used.  Record for later use during GC.  */
5542         case R_PPC64_GNU_VTENTRY:
5543           BFD_ASSERT (h != NULL);
5544           if (h != NULL
5545               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5546             return FALSE;
5547           break;
5548
5549         case R_PPC64_REL14:
5550         case R_PPC64_REL14_BRTAKEN:
5551         case R_PPC64_REL14_BRNTAKEN:
5552           {
5553             asection *dest = NULL;
5554
5555             /* Heuristic: If jumping outside our section, chances are
5556                we are going to need a stub.  */
5557             if (h != NULL)
5558               {
5559                 /* If the sym is weak it may be overridden later, so
5560                    don't assume we know where a weak sym lives.  */
5561                 if (h->root.type == bfd_link_hash_defined)
5562                   dest = h->root.u.def.section;
5563               }
5564             else
5565               {
5566                 Elf_Internal_Sym *isym;
5567
5568                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5569                                               abfd, r_symndx);
5570                 if (isym == NULL)
5571                   return FALSE;
5572
5573                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5574               }
5575
5576             if (dest != sec)
5577               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5578           }
5579           /* Fall through.  */
5580
5581         case R_PPC64_REL24:
5582           if (h != NULL && ifunc == NULL)
5583             {
5584               /* We may need a .plt entry if the function this reloc
5585                  refers to is in a shared lib.  */
5586               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5587                 return FALSE;
5588               h->needs_plt = 1;
5589               if (h->root.root.string[0] == '.'
5590                   && h->root.root.string[1] != '\0')
5591                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5592               if (h == tga || h == dottga)
5593                 sec->has_tls_reloc = 1;
5594             }
5595           break;
5596
5597         case R_PPC64_TPREL64:
5598           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5599           if (bfd_link_pic (info))
5600             info->flags |= DF_STATIC_TLS;
5601           goto dotlstoc;
5602
5603         case R_PPC64_DTPMOD64:
5604           if (rel + 1 < rel_end
5605               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5606               && rel[1].r_offset == rel->r_offset + 8)
5607             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5608           else
5609             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5610           goto dotlstoc;
5611
5612         case R_PPC64_DTPREL64:
5613           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5614           if (rel != relocs
5615               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5616               && rel[-1].r_offset == rel->r_offset - 8)
5617             /* This is the second reloc of a dtpmod, dtprel pair.
5618                Don't mark with TLS_DTPREL.  */
5619             goto dodyn;
5620
5621         dotlstoc:
5622           sec->has_tls_reloc = 1;
5623           if (h != NULL)
5624             {
5625               struct ppc_link_hash_entry *eh;
5626               eh = (struct ppc_link_hash_entry *) h;
5627               eh->tls_mask |= tls_type;
5628             }
5629           else
5630             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5631                                         rel->r_addend, tls_type))
5632               return FALSE;
5633
5634           ppc64_sec = ppc64_elf_section_data (sec);
5635           if (ppc64_sec->sec_type != sec_toc)
5636             {
5637               bfd_size_type amt;
5638
5639               /* One extra to simplify get_tls_mask.  */
5640               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5641               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5642               if (ppc64_sec->u.toc.symndx == NULL)
5643                 return FALSE;
5644               amt = sec->size * sizeof (bfd_vma) / 8;
5645               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5646               if (ppc64_sec->u.toc.add == NULL)
5647                 return FALSE;
5648               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5649               ppc64_sec->sec_type = sec_toc;
5650             }
5651           BFD_ASSERT (rel->r_offset % 8 == 0);
5652           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5653           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5654
5655           /* Mark the second slot of a GD or LD entry.
5656              -1 to indicate GD and -2 to indicate LD.  */
5657           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5658             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5659           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5660             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5661           goto dodyn;
5662
5663         case R_PPC64_TPREL16:
5664         case R_PPC64_TPREL16_LO:
5665         case R_PPC64_TPREL16_HI:
5666         case R_PPC64_TPREL16_HA:
5667         case R_PPC64_TPREL16_DS:
5668         case R_PPC64_TPREL16_LO_DS:
5669         case R_PPC64_TPREL16_HIGH:
5670         case R_PPC64_TPREL16_HIGHA:
5671         case R_PPC64_TPREL16_HIGHER:
5672         case R_PPC64_TPREL16_HIGHERA:
5673         case R_PPC64_TPREL16_HIGHEST:
5674         case R_PPC64_TPREL16_HIGHESTA:
5675           if (bfd_link_pic (info))
5676             {
5677               info->flags |= DF_STATIC_TLS;
5678               goto dodyn;
5679             }
5680           break;
5681
5682         case R_PPC64_ADDR64:
5683           if (opd_sym_map != NULL
5684               && rel + 1 < rel_end
5685               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5686             {
5687               if (h != NULL)
5688                 {
5689                   if (h->root.root.string[0] == '.'
5690                       && h->root.root.string[1] != 0
5691                       && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5692                     ;
5693                   else
5694                     ((struct ppc_link_hash_entry *) h)->is_func = 1;
5695                 }
5696               else
5697                 {
5698                   asection *s;
5699                   Elf_Internal_Sym *isym;
5700
5701                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5702                                                 abfd, r_symndx);
5703                   if (isym == NULL)
5704                     return FALSE;
5705
5706                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5707                   if (s != NULL && s != sec)
5708                     opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5709                 }
5710             }
5711           /* Fall through.  */
5712
5713         case R_PPC64_ADDR16:
5714         case R_PPC64_ADDR16_DS:
5715         case R_PPC64_ADDR16_HA:
5716         case R_PPC64_ADDR16_HI:
5717         case R_PPC64_ADDR16_HIGH:
5718         case R_PPC64_ADDR16_HIGHA:
5719         case R_PPC64_ADDR16_HIGHER:
5720         case R_PPC64_ADDR16_HIGHERA:
5721         case R_PPC64_ADDR16_HIGHEST:
5722         case R_PPC64_ADDR16_HIGHESTA:
5723         case R_PPC64_ADDR16_LO:
5724         case R_PPC64_ADDR16_LO_DS:
5725           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5726               && rel->r_addend == 0)
5727             {
5728               /* We may need a .plt entry if this reloc refers to a
5729                  function in a shared lib.  */
5730               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5731                 return FALSE;
5732               h->pointer_equality_needed = 1;
5733             }
5734           /* Fall through.  */
5735
5736         case R_PPC64_REL30:
5737         case R_PPC64_REL32:
5738         case R_PPC64_REL64:
5739         case R_PPC64_ADDR14:
5740         case R_PPC64_ADDR14_BRNTAKEN:
5741         case R_PPC64_ADDR14_BRTAKEN:
5742         case R_PPC64_ADDR24:
5743         case R_PPC64_ADDR32:
5744         case R_PPC64_UADDR16:
5745         case R_PPC64_UADDR32:
5746         case R_PPC64_UADDR64:
5747         case R_PPC64_TOC:
5748           if (h != NULL && !bfd_link_pic (info))
5749             /* We may need a copy reloc.  */
5750             h->non_got_ref = 1;
5751
5752           /* Don't propagate .opd relocs.  */
5753           if (NO_OPD_RELOCS && opd_sym_map != NULL)
5754             break;
5755
5756           /* If we are creating a shared library, and this is a reloc
5757              against a global symbol, or a non PC relative reloc
5758              against a local symbol, then we need to copy the reloc
5759              into the shared library.  However, if we are linking with
5760              -Bsymbolic, we do not need to copy a reloc against a
5761              global symbol which is defined in an object we are
5762              including in the link (i.e., DEF_REGULAR is set).  At
5763              this point we have not seen all the input files, so it is
5764              possible that DEF_REGULAR is not set now but will be set
5765              later (it is never cleared).  In case of a weak definition,
5766              DEF_REGULAR may be cleared later by a strong definition in
5767              a shared library.  We account for that possibility below by
5768              storing information in the dyn_relocs field of the hash
5769              table entry.  A similar situation occurs when creating
5770              shared libraries and symbol visibility changes render the
5771              symbol local.
5772
5773              If on the other hand, we are creating an executable, we
5774              may need to keep relocations for symbols satisfied by a
5775              dynamic library if we manage to avoid copy relocs for the
5776              symbol.  */
5777         dodyn:
5778           if ((bfd_link_pic (info)
5779                && (must_be_dyn_reloc (info, r_type)
5780                    || (h != NULL
5781                        && (!SYMBOLIC_BIND (info, h)
5782                            || h->root.type == bfd_link_hash_defweak
5783                            || !h->def_regular))))
5784               || (ELIMINATE_COPY_RELOCS
5785                   && !bfd_link_pic (info)
5786                   && h != NULL
5787                   && (h->root.type == bfd_link_hash_defweak
5788                       || !h->def_regular))
5789               || (!bfd_link_pic (info)
5790                   && ifunc != NULL))
5791             {
5792               /* We must copy these reloc types into the output file.
5793                  Create a reloc section in dynobj and make room for
5794                  this reloc.  */
5795               if (sreloc == NULL)
5796                 {
5797                   sreloc = _bfd_elf_make_dynamic_reloc_section
5798                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5799
5800                   if (sreloc == NULL)
5801                     return FALSE;
5802                 }
5803
5804               /* If this is a global symbol, we count the number of
5805                  relocations we need for this symbol.  */
5806               if (h != NULL)
5807                 {
5808                   struct elf_dyn_relocs *p;
5809                   struct elf_dyn_relocs **head;
5810
5811                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5812                   p = *head;
5813                   if (p == NULL || p->sec != sec)
5814                     {
5815                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5816                       if (p == NULL)
5817                         return FALSE;
5818                       p->next = *head;
5819                       *head = p;
5820                       p->sec = sec;
5821                       p->count = 0;
5822                       p->pc_count = 0;
5823                     }
5824                   p->count += 1;
5825                   if (!must_be_dyn_reloc (info, r_type))
5826                     p->pc_count += 1;
5827                 }
5828               else
5829                 {
5830                   /* Track dynamic relocs needed for local syms too.
5831                      We really need local syms available to do this
5832                      easily.  Oh well.  */
5833                   struct ppc_dyn_relocs *p;
5834                   struct ppc_dyn_relocs **head;
5835                   bfd_boolean is_ifunc;
5836                   asection *s;
5837                   void *vpp;
5838                   Elf_Internal_Sym *isym;
5839
5840                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5841                                                 abfd, r_symndx);
5842                   if (isym == NULL)
5843                     return FALSE;
5844
5845                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5846                   if (s == NULL)
5847                     s = sec;
5848
5849                   vpp = &elf_section_data (s)->local_dynrel;
5850                   head = (struct ppc_dyn_relocs **) vpp;
5851                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5852                   p = *head;
5853                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5854                     p = p->next;
5855                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5856                     {
5857                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5858                       if (p == NULL)
5859                         return FALSE;
5860                       p->next = *head;
5861                       *head = p;
5862                       p->sec = sec;
5863                       p->ifunc = is_ifunc;
5864                       p->count = 0;
5865                     }
5866                   p->count += 1;
5867                 }
5868             }
5869           break;
5870
5871         default:
5872           break;
5873         }
5874     }
5875
5876   return TRUE;
5877 }
5878
5879 /* Merge backend specific data from an object file to the output
5880    object file when linking.  */
5881
5882 static bfd_boolean
5883 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5884 {
5885   unsigned long iflags, oflags;
5886
5887   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5888     return TRUE;
5889
5890   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5891     return TRUE;
5892
5893   if (!_bfd_generic_verify_endian_match (ibfd, obfd))
5894     return FALSE;
5895
5896   iflags = elf_elfheader (ibfd)->e_flags;
5897   oflags = elf_elfheader (obfd)->e_flags;
5898
5899   if (iflags & ~EF_PPC64_ABI)
5900     {
5901       (*_bfd_error_handler)
5902         (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
5903       bfd_set_error (bfd_error_bad_value);
5904       return FALSE;
5905     }
5906   else if (iflags != oflags && iflags != 0)
5907     {
5908       (*_bfd_error_handler)
5909         (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
5910          ibfd, iflags, oflags);
5911       bfd_set_error (bfd_error_bad_value);
5912       return FALSE;
5913     }
5914
5915   /* Merge Tag_compatibility attributes and any common GNU ones.  */
5916   _bfd_elf_merge_object_attributes (ibfd, obfd);
5917
5918   return TRUE;
5919 }
5920
5921 static bfd_boolean
5922 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5923 {
5924   /* Print normal ELF private data.  */
5925   _bfd_elf_print_private_bfd_data (abfd, ptr);
5926
5927   if (elf_elfheader (abfd)->e_flags != 0)
5928     {
5929       FILE *file = ptr;
5930
5931       /* xgettext:c-format */
5932       fprintf (file, _("private flags = 0x%lx:"),
5933                elf_elfheader (abfd)->e_flags);
5934
5935       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5936         fprintf (file, _(" [abiv%ld]"),
5937                  elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5938       fputc ('\n', file);
5939     }
5940
5941   return TRUE;
5942 }
5943
5944 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
5945    of the code entry point, and its section, which must be in the same
5946    object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
5947
5948 static bfd_vma
5949 opd_entry_value (asection *opd_sec,
5950                  bfd_vma offset,
5951                  asection **code_sec,
5952                  bfd_vma *code_off,
5953                  bfd_boolean in_code_sec)
5954 {
5955   bfd *opd_bfd = opd_sec->owner;
5956   Elf_Internal_Rela *relocs;
5957   Elf_Internal_Rela *lo, *hi, *look;
5958   bfd_vma val;
5959
5960   /* No relocs implies we are linking a --just-symbols object, or looking
5961      at a final linked executable with addr2line or somesuch.  */
5962   if (opd_sec->reloc_count == 0)
5963     {
5964       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5965
5966       if (contents == NULL)
5967         {
5968           if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5969             return (bfd_vma) -1;
5970           ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5971         }
5972
5973       /* PR 17512: file: 64b9dfbb.  */
5974       if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5975         return (bfd_vma) -1;
5976
5977       val = bfd_get_64 (opd_bfd, contents + offset);
5978       if (code_sec != NULL)
5979         {
5980           asection *sec, *likely = NULL;
5981
5982           if (in_code_sec)
5983             {
5984               sec = *code_sec;
5985               if (sec->vma <= val
5986                   && val < sec->vma + sec->size)
5987                 likely = sec;
5988               else
5989                 val = -1;
5990             }
5991           else
5992             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5993               if (sec->vma <= val
5994                   && (sec->flags & SEC_LOAD) != 0
5995                   && (sec->flags & SEC_ALLOC) != 0)
5996                 likely = sec;
5997           if (likely != NULL)
5998             {
5999               *code_sec = likely;
6000               if (code_off != NULL)
6001                 *code_off = val - likely->vma;
6002             }
6003         }
6004       return val;
6005     }
6006
6007   BFD_ASSERT (is_ppc64_elf (opd_bfd));
6008
6009   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6010   if (relocs == NULL)
6011     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6012   /* PR 17512: file: df8e1fd6.  */
6013   if (relocs == NULL)
6014     return (bfd_vma) -1;
6015
6016   /* Go find the opd reloc at the sym address.  */
6017   lo = relocs;
6018   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6019   val = (bfd_vma) -1;
6020   while (lo < hi)
6021     {
6022       look = lo + (hi - lo) / 2;
6023       if (look->r_offset < offset)
6024         lo = look + 1;
6025       else if (look->r_offset > offset)
6026         hi = look;
6027       else
6028         {
6029           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6030
6031           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6032               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6033             {
6034               unsigned long symndx = ELF64_R_SYM (look->r_info);
6035               asection *sec = NULL;
6036
6037               if (symndx >= symtab_hdr->sh_info
6038                   && elf_sym_hashes (opd_bfd) != NULL)
6039                 {
6040                   struct elf_link_hash_entry **sym_hashes;
6041                   struct elf_link_hash_entry *rh;
6042
6043                   sym_hashes = elf_sym_hashes (opd_bfd);
6044                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
6045                   if (rh != NULL)
6046                     {
6047                       rh = elf_follow_link (rh);
6048                       if (rh->root.type != bfd_link_hash_defined
6049                           && rh->root.type != bfd_link_hash_defweak)
6050                         break;
6051                       if (rh->root.u.def.section->owner == opd_bfd)
6052                         {
6053                           val = rh->root.u.def.value;
6054                           sec = rh->root.u.def.section;
6055                         }
6056                     }
6057                 }
6058
6059               if (sec == NULL)
6060                 {
6061                   Elf_Internal_Sym *sym;
6062
6063                   if (symndx < symtab_hdr->sh_info)
6064                     {
6065                       sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6066                       if (sym == NULL)
6067                         {
6068                           size_t symcnt = symtab_hdr->sh_info;
6069                           sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6070                                                       symcnt, 0,
6071                                                       NULL, NULL, NULL);
6072                           if (sym == NULL)
6073                             break;
6074                           symtab_hdr->contents = (bfd_byte *) sym;
6075                         }
6076                       sym += symndx;
6077                     }
6078                   else
6079                     {
6080                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6081                                                   1, symndx,
6082                                                   NULL, NULL, NULL);
6083                       if (sym == NULL)
6084                         break;
6085                     }
6086                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6087                   if (sec == NULL)
6088                     break;
6089                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6090                   val = sym->st_value;
6091                 }
6092
6093               val += look->r_addend;
6094               if (code_off != NULL)
6095                 *code_off = val;
6096               if (code_sec != NULL)
6097                 {
6098                   if (in_code_sec && *code_sec != sec)
6099                     return -1;
6100                   else
6101                     *code_sec = sec;
6102                 }
6103               if (sec->output_section != NULL)
6104                 val += sec->output_section->vma + sec->output_offset;
6105             }
6106           break;
6107         }
6108     }
6109
6110   return val;
6111 }
6112
6113 /* If the ELF symbol SYM might be a function in SEC, return the
6114    function size and set *CODE_OFF to the function's entry point,
6115    otherwise return zero.  */
6116
6117 static bfd_size_type
6118 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6119                               bfd_vma *code_off)
6120 {
6121   bfd_size_type size;
6122
6123   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6124                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6125     return 0;
6126
6127   size = 0;
6128   if (!(sym->flags & BSF_SYNTHETIC))
6129     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6130
6131   if (strcmp (sym->section->name, ".opd") == 0)
6132     {
6133       if (opd_entry_value (sym->section, sym->value,
6134                            &sec, code_off, TRUE) == (bfd_vma) -1)
6135         return 0;
6136       /* An old ABI binary with dot-syms has a size of 24 on the .opd
6137          symbol.  This size has nothing to do with the code size of the
6138          function, which is what we're supposed to return, but the
6139          code size isn't available without looking up the dot-sym.
6140          However, doing that would be a waste of time particularly
6141          since elf_find_function will look at the dot-sym anyway.
6142          Now, elf_find_function will keep the largest size of any
6143          function sym found at the code address of interest, so return
6144          1 here to avoid it incorrectly caching a larger function size
6145          for a small function.  This does mean we return the wrong
6146          size for a new-ABI function of size 24, but all that does is
6147          disable caching for such functions.  */
6148       if (size == 24)
6149         size = 1;
6150     }
6151   else
6152     {
6153       if (sym->section != sec)
6154         return 0;
6155       *code_off = sym->value;
6156     }
6157   if (size == 0)
6158     size = 1;
6159   return size;
6160 }
6161
6162 /* Return true if symbol is defined in a regular object file.  */
6163
6164 static bfd_boolean
6165 is_static_defined (struct elf_link_hash_entry *h)
6166 {
6167   return ((h->root.type == bfd_link_hash_defined
6168            || h->root.type == bfd_link_hash_defweak)
6169           && h->root.u.def.section != NULL
6170           && h->root.u.def.section->output_section != NULL);
6171 }
6172
6173 /* If FDH is a function descriptor symbol, return the associated code
6174    entry symbol if it is defined.  Return NULL otherwise.  */
6175
6176 static struct ppc_link_hash_entry *
6177 defined_code_entry (struct ppc_link_hash_entry *fdh)
6178 {
6179   if (fdh->is_func_descriptor)
6180     {
6181       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6182       if (fh->elf.root.type == bfd_link_hash_defined
6183           || fh->elf.root.type == bfd_link_hash_defweak)
6184         return fh;
6185     }
6186   return NULL;
6187 }
6188
6189 /* If FH is a function code entry symbol, return the associated
6190    function descriptor symbol if it is defined.  Return NULL otherwise.  */
6191
6192 static struct ppc_link_hash_entry *
6193 defined_func_desc (struct ppc_link_hash_entry *fh)
6194 {
6195   if (fh->oh != NULL
6196       && fh->oh->is_func_descriptor)
6197     {
6198       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6199       if (fdh->elf.root.type == bfd_link_hash_defined
6200           || fdh->elf.root.type == bfd_link_hash_defweak)
6201         return fdh;
6202     }
6203   return NULL;
6204 }
6205
6206 /* Mark all our entry sym sections, both opd and code section.  */
6207
6208 static void
6209 ppc64_elf_gc_keep (struct bfd_link_info *info)
6210 {
6211   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6212   struct bfd_sym_chain *sym;
6213
6214   if (htab == NULL)
6215     return;
6216
6217   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6218     {
6219       struct ppc_link_hash_entry *eh, *fh;
6220       asection *sec;
6221
6222       eh = (struct ppc_link_hash_entry *)
6223         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6224       if (eh == NULL)
6225         continue;
6226       if (eh->elf.root.type != bfd_link_hash_defined
6227           && eh->elf.root.type != bfd_link_hash_defweak)
6228         continue;
6229
6230       fh = defined_code_entry (eh);
6231       if (fh != NULL)
6232         {
6233           sec = fh->elf.root.u.def.section;
6234           sec->flags |= SEC_KEEP;
6235         }
6236       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6237                && opd_entry_value (eh->elf.root.u.def.section,
6238                                    eh->elf.root.u.def.value,
6239                                    &sec, NULL, FALSE) != (bfd_vma) -1)
6240         sec->flags |= SEC_KEEP;
6241
6242       sec = eh->elf.root.u.def.section;
6243       sec->flags |= SEC_KEEP;
6244     }
6245 }
6246
6247 /* Mark sections containing dynamically referenced symbols.  When
6248    building shared libraries, we must assume that any visible symbol is
6249    referenced.  */
6250
6251 static bfd_boolean
6252 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6253 {
6254   struct bfd_link_info *info = (struct bfd_link_info *) inf;
6255   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6256   struct ppc_link_hash_entry *fdh;
6257   struct bfd_elf_dynamic_list *d = info->dynamic_list;
6258
6259   /* Dynamic linking info is on the func descriptor sym.  */
6260   fdh = defined_func_desc (eh);
6261   if (fdh != NULL)
6262     eh = fdh;
6263
6264   if ((eh->elf.root.type == bfd_link_hash_defined
6265        || eh->elf.root.type == bfd_link_hash_defweak)
6266       && (eh->elf.ref_dynamic
6267           || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6268               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6269               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6270               && (!bfd_link_executable (info)
6271                   || info->export_dynamic
6272                   || (eh->elf.dynamic
6273                       && d != NULL
6274                       && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6275               && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6276                   || !bfd_hide_sym_by_version (info->version_info,
6277                                                eh->elf.root.root.string)))))
6278     {
6279       asection *code_sec;
6280       struct ppc_link_hash_entry *fh;
6281
6282       eh->elf.root.u.def.section->flags |= SEC_KEEP;
6283
6284       /* Function descriptor syms cause the associated
6285          function code sym section to be marked.  */
6286       fh = defined_code_entry (eh);
6287       if (fh != NULL)
6288         {
6289           code_sec = fh->elf.root.u.def.section;
6290           code_sec->flags |= SEC_KEEP;
6291         }
6292       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6293                && opd_entry_value (eh->elf.root.u.def.section,
6294                                    eh->elf.root.u.def.value,
6295                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
6296         code_sec->flags |= SEC_KEEP;
6297     }
6298
6299   return TRUE;
6300 }
6301
6302 /* Return the section that should be marked against GC for a given
6303    relocation.  */
6304
6305 static asection *
6306 ppc64_elf_gc_mark_hook (asection *sec,
6307                         struct bfd_link_info *info,
6308                         Elf_Internal_Rela *rel,
6309                         struct elf_link_hash_entry *h,
6310                         Elf_Internal_Sym *sym)
6311 {
6312   asection *rsec;
6313
6314   /* Syms return NULL if we're marking .opd, so we avoid marking all
6315      function sections, as all functions are referenced in .opd.  */
6316   rsec = NULL;
6317   if (get_opd_info (sec) != NULL)
6318     return rsec;
6319
6320   if (h != NULL)
6321     {
6322       enum elf_ppc64_reloc_type r_type;
6323       struct ppc_link_hash_entry *eh, *fh, *fdh;
6324
6325       r_type = ELF64_R_TYPE (rel->r_info);
6326       switch (r_type)
6327         {
6328         case R_PPC64_GNU_VTINHERIT:
6329         case R_PPC64_GNU_VTENTRY:
6330           break;
6331
6332         default:
6333           switch (h->root.type)
6334             {
6335             case bfd_link_hash_defined:
6336             case bfd_link_hash_defweak:
6337               eh = (struct ppc_link_hash_entry *) h;
6338               fdh = defined_func_desc (eh);
6339               if (fdh != NULL)
6340                 eh = fdh;
6341
6342               /* Function descriptor syms cause the associated
6343                  function code sym section to be marked.  */
6344               fh = defined_code_entry (eh);
6345               if (fh != NULL)
6346                 {
6347                   /* They also mark their opd section.  */
6348                   eh->elf.root.u.def.section->gc_mark = 1;
6349
6350                   rsec = fh->elf.root.u.def.section;
6351                 }
6352               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6353                        && opd_entry_value (eh->elf.root.u.def.section,
6354                                            eh->elf.root.u.def.value,
6355                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
6356                 eh->elf.root.u.def.section->gc_mark = 1;
6357               else
6358                 rsec = h->root.u.def.section;
6359               break;
6360
6361             case bfd_link_hash_common:
6362               rsec = h->root.u.c.p->section;
6363               break;
6364
6365             default:
6366               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6367             }
6368         }
6369     }
6370   else
6371     {
6372       struct _opd_sec_data *opd;
6373
6374       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6375       opd = get_opd_info (rsec);
6376       if (opd != NULL && opd->func_sec != NULL)
6377         {
6378           rsec->gc_mark = 1;
6379
6380           rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6381         }
6382     }
6383
6384   return rsec;
6385 }
6386
6387 /* Update the .got, .plt. and dynamic reloc reference counts for the
6388    section being removed.  */
6389
6390 static bfd_boolean
6391 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6392                          asection *sec, const Elf_Internal_Rela *relocs)
6393 {
6394   struct ppc_link_hash_table *htab;
6395   Elf_Internal_Shdr *symtab_hdr;
6396   struct elf_link_hash_entry **sym_hashes;
6397   struct got_entry **local_got_ents;
6398   const Elf_Internal_Rela *rel, *relend;
6399
6400   if (bfd_link_relocatable (info))
6401     return TRUE;
6402
6403   if ((sec->flags & SEC_ALLOC) == 0)
6404     return TRUE;
6405
6406   elf_section_data (sec)->local_dynrel = NULL;
6407
6408   htab = ppc_hash_table (info);
6409   if (htab == NULL)
6410     return FALSE;
6411
6412   symtab_hdr = &elf_symtab_hdr (abfd);
6413   sym_hashes = elf_sym_hashes (abfd);
6414   local_got_ents = elf_local_got_ents (abfd);
6415
6416   relend = relocs + sec->reloc_count;
6417   for (rel = relocs; rel < relend; rel++)
6418     {
6419       unsigned long r_symndx;
6420       enum elf_ppc64_reloc_type r_type;
6421       struct elf_link_hash_entry *h = NULL;
6422       unsigned char tls_type = 0;
6423
6424       r_symndx = ELF64_R_SYM (rel->r_info);
6425       r_type = ELF64_R_TYPE (rel->r_info);
6426       if (r_symndx >= symtab_hdr->sh_info)
6427         {
6428           struct ppc_link_hash_entry *eh;
6429           struct elf_dyn_relocs **pp;
6430           struct elf_dyn_relocs *p;
6431
6432           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6433           h = elf_follow_link (h);
6434           eh = (struct ppc_link_hash_entry *) h;
6435
6436           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6437             if (p->sec == sec)
6438               {
6439                 /* Everything must go for SEC.  */
6440                 *pp = p->next;
6441                 break;
6442               }
6443         }
6444
6445       if (is_branch_reloc (r_type))
6446         {
6447           struct plt_entry **ifunc = NULL;
6448           if (h != NULL)
6449             {
6450               if (h->type == STT_GNU_IFUNC)
6451                 ifunc = &h->plt.plist;
6452             }
6453           else if (local_got_ents != NULL)
6454             {
6455               struct plt_entry **local_plt = (struct plt_entry **)
6456                 (local_got_ents + symtab_hdr->sh_info);
6457               unsigned char *local_got_tls_masks = (unsigned char *)
6458                 (local_plt + symtab_hdr->sh_info);
6459               if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6460                 ifunc = local_plt + r_symndx;
6461             }
6462           if (ifunc != NULL)
6463             {
6464               struct plt_entry *ent;
6465
6466               for (ent = *ifunc; ent != NULL; ent = ent->next)
6467                 if (ent->addend == rel->r_addend)
6468                   break;
6469               if (ent == NULL)
6470                 abort ();
6471               if (ent->plt.refcount > 0)
6472                 ent->plt.refcount -= 1;
6473               continue;
6474             }
6475         }
6476
6477       switch (r_type)
6478         {
6479         case R_PPC64_GOT_TLSLD16:
6480         case R_PPC64_GOT_TLSLD16_LO:
6481         case R_PPC64_GOT_TLSLD16_HI:
6482         case R_PPC64_GOT_TLSLD16_HA:
6483           tls_type = TLS_TLS | TLS_LD;
6484           goto dogot;
6485
6486         case R_PPC64_GOT_TLSGD16:
6487         case R_PPC64_GOT_TLSGD16_LO:
6488         case R_PPC64_GOT_TLSGD16_HI:
6489         case R_PPC64_GOT_TLSGD16_HA:
6490           tls_type = TLS_TLS | TLS_GD;
6491           goto dogot;
6492
6493         case R_PPC64_GOT_TPREL16_DS:
6494         case R_PPC64_GOT_TPREL16_LO_DS:
6495         case R_PPC64_GOT_TPREL16_HI:
6496         case R_PPC64_GOT_TPREL16_HA:
6497           tls_type = TLS_TLS | TLS_TPREL;
6498           goto dogot;
6499
6500         case R_PPC64_GOT_DTPREL16_DS:
6501         case R_PPC64_GOT_DTPREL16_LO_DS:
6502         case R_PPC64_GOT_DTPREL16_HI:
6503         case R_PPC64_GOT_DTPREL16_HA:
6504           tls_type = TLS_TLS | TLS_DTPREL;
6505           goto dogot;
6506
6507         case R_PPC64_GOT16:
6508         case R_PPC64_GOT16_DS:
6509         case R_PPC64_GOT16_HA:
6510         case R_PPC64_GOT16_HI:
6511         case R_PPC64_GOT16_LO:
6512         case R_PPC64_GOT16_LO_DS:
6513         dogot:
6514           {
6515             struct got_entry *ent;
6516
6517             if (h != NULL)
6518               ent = h->got.glist;
6519             else
6520               ent = local_got_ents[r_symndx];
6521
6522             for (; ent != NULL; ent = ent->next)
6523               if (ent->addend == rel->r_addend
6524                   && ent->owner == abfd
6525                   && ent->tls_type == tls_type)
6526                 break;
6527             if (ent == NULL)
6528               abort ();
6529             if (ent->got.refcount > 0)
6530               ent->got.refcount -= 1;
6531           }
6532           break;
6533
6534         case R_PPC64_PLT16_HA:
6535         case R_PPC64_PLT16_HI:
6536         case R_PPC64_PLT16_LO:
6537         case R_PPC64_PLT32:
6538         case R_PPC64_PLT64:
6539         case R_PPC64_REL14:
6540         case R_PPC64_REL14_BRNTAKEN:
6541         case R_PPC64_REL14_BRTAKEN:
6542         case R_PPC64_REL24:
6543           if (h != NULL)
6544             {
6545               struct plt_entry *ent;
6546
6547               for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6548                 if (ent->addend == rel->r_addend)
6549                   break;
6550               if (ent != NULL && ent->plt.refcount > 0)
6551                 ent->plt.refcount -= 1;
6552             }
6553           break;
6554
6555         default:
6556           break;
6557         }
6558     }
6559   return TRUE;
6560 }
6561
6562 /* The maximum size of .sfpr.  */
6563 #define SFPR_MAX (218*4)
6564
6565 struct sfpr_def_parms
6566 {
6567   const char name[12];
6568   unsigned char lo, hi;
6569   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6570   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6571 };
6572
6573 /* Auto-generate _save*, _rest* functions in .sfpr.
6574    If STUB_SEC is non-null, define alias symbols in STUB_SEC
6575    instead.  */
6576
6577 static bfd_boolean
6578 sfpr_define (struct bfd_link_info *info,
6579              const struct sfpr_def_parms *parm,
6580              asection *stub_sec)
6581 {
6582   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6583   unsigned int i;
6584   size_t len = strlen (parm->name);
6585   bfd_boolean writing = FALSE;
6586   char sym[16];
6587
6588   if (htab == NULL)
6589     return FALSE;
6590
6591   memcpy (sym, parm->name, len);
6592   sym[len + 2] = 0;
6593
6594   for (i = parm->lo; i <= parm->hi; i++)
6595     {
6596       struct ppc_link_hash_entry *h;
6597
6598       sym[len + 0] = i / 10 + '0';
6599       sym[len + 1] = i % 10 + '0';
6600       h = (struct ppc_link_hash_entry *)
6601         elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6602       if (stub_sec != NULL)
6603         {
6604           if (h != NULL
6605               && h->elf.root.type == bfd_link_hash_defined
6606               && h->elf.root.u.def.section == htab->sfpr)
6607             {
6608               struct elf_link_hash_entry *s;
6609               char buf[32];
6610               sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6611               s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6612               if (s == NULL)
6613                 return FALSE;
6614               if (s->root.type == bfd_link_hash_new
6615                   || (s->root.type = bfd_link_hash_defined
6616                       && s->root.u.def.section == stub_sec))
6617                 {
6618                   s->root.type = bfd_link_hash_defined;
6619                   s->root.u.def.section = stub_sec;
6620                   s->root.u.def.value = (stub_sec->size
6621                                          + h->elf.root.u.def.value);
6622                   s->ref_regular = 1;
6623                   s->def_regular = 1;
6624                   s->ref_regular_nonweak = 1;
6625                   s->forced_local = 1;
6626                   s->non_elf = 0;
6627                   s->root.linker_def = 1;
6628                 }
6629             }
6630           continue;
6631         }
6632       if (h != NULL)
6633         {
6634           h->save_res = 1;
6635           if (!h->elf.def_regular)
6636             {
6637               h->elf.root.type = bfd_link_hash_defined;
6638               h->elf.root.u.def.section = htab->sfpr;
6639               h->elf.root.u.def.value = htab->sfpr->size;
6640               h->elf.type = STT_FUNC;
6641               h->elf.def_regular = 1;
6642               _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6643               writing = TRUE;
6644               if (htab->sfpr->contents == NULL)
6645                 {
6646                   htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6647                   if (htab->sfpr->contents == NULL)
6648                     return FALSE;
6649                 }
6650             }
6651         }
6652       if (writing)
6653         {
6654           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6655           if (i != parm->hi)
6656             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6657           else
6658             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6659           htab->sfpr->size = p - htab->sfpr->contents;
6660         }
6661     }
6662
6663   return TRUE;
6664 }
6665
6666 static bfd_byte *
6667 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6668 {
6669   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6670   return p + 4;
6671 }
6672
6673 static bfd_byte *
6674 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6675 {
6676   p = savegpr0 (abfd, p, r);
6677   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6678   p = p + 4;
6679   bfd_put_32 (abfd, BLR, p);
6680   return p + 4;
6681 }
6682
6683 static bfd_byte *
6684 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6685 {
6686   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6687   return p + 4;
6688 }
6689
6690 static bfd_byte *
6691 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6692 {
6693   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6694   p = p + 4;
6695   p = restgpr0 (abfd, p, r);
6696   bfd_put_32 (abfd, MTLR_R0, p);
6697   p = p + 4;
6698   if (r == 29)
6699     {
6700       p = restgpr0 (abfd, p, 30);
6701       p = restgpr0 (abfd, p, 31);
6702     }
6703   bfd_put_32 (abfd, BLR, p);
6704   return p + 4;
6705 }
6706
6707 static bfd_byte *
6708 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6709 {
6710   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6711   return p + 4;
6712 }
6713
6714 static bfd_byte *
6715 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6716 {
6717   p = savegpr1 (abfd, p, r);
6718   bfd_put_32 (abfd, BLR, p);
6719   return p + 4;
6720 }
6721
6722 static bfd_byte *
6723 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6724 {
6725   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6726   return p + 4;
6727 }
6728
6729 static bfd_byte *
6730 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6731 {
6732   p = restgpr1 (abfd, p, r);
6733   bfd_put_32 (abfd, BLR, p);
6734   return p + 4;
6735 }
6736
6737 static bfd_byte *
6738 savefpr (bfd *abfd, bfd_byte *p, int r)
6739 {
6740   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6741   return p + 4;
6742 }
6743
6744 static bfd_byte *
6745 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6746 {
6747   p = savefpr (abfd, p, r);
6748   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6749   p = p + 4;
6750   bfd_put_32 (abfd, BLR, p);
6751   return p + 4;
6752 }
6753
6754 static bfd_byte *
6755 restfpr (bfd *abfd, bfd_byte *p, int r)
6756 {
6757   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6758   return p + 4;
6759 }
6760
6761 static bfd_byte *
6762 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6763 {
6764   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6765   p = p + 4;
6766   p = restfpr (abfd, p, r);
6767   bfd_put_32 (abfd, MTLR_R0, p);
6768   p = p + 4;
6769   if (r == 29)
6770     {
6771       p = restfpr (abfd, p, 30);
6772       p = restfpr (abfd, p, 31);
6773     }
6774   bfd_put_32 (abfd, BLR, p);
6775   return p + 4;
6776 }
6777
6778 static bfd_byte *
6779 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6780 {
6781   p = savefpr (abfd, p, r);
6782   bfd_put_32 (abfd, BLR, p);
6783   return p + 4;
6784 }
6785
6786 static bfd_byte *
6787 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6788 {
6789   p = restfpr (abfd, p, r);
6790   bfd_put_32 (abfd, BLR, p);
6791   return p + 4;
6792 }
6793
6794 static bfd_byte *
6795 savevr (bfd *abfd, bfd_byte *p, int r)
6796 {
6797   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6798   p = p + 4;
6799   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6800   return p + 4;
6801 }
6802
6803 static bfd_byte *
6804 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6805 {
6806   p = savevr (abfd, p, r);
6807   bfd_put_32 (abfd, BLR, p);
6808   return p + 4;
6809 }
6810
6811 static bfd_byte *
6812 restvr (bfd *abfd, bfd_byte *p, int r)
6813 {
6814   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6815   p = p + 4;
6816   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6817   return p + 4;
6818 }
6819
6820 static bfd_byte *
6821 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6822 {
6823   p = restvr (abfd, p, r);
6824   bfd_put_32 (abfd, BLR, p);
6825   return p + 4;
6826 }
6827
6828 /* Called via elf_link_hash_traverse to transfer dynamic linking
6829    information on function code symbol entries to their corresponding
6830    function descriptor symbol entries.  */
6831
6832 static bfd_boolean
6833 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6834 {
6835   struct bfd_link_info *info;
6836   struct ppc_link_hash_table *htab;
6837   struct plt_entry *ent;
6838   struct ppc_link_hash_entry *fh;
6839   struct ppc_link_hash_entry *fdh;
6840   bfd_boolean force_local;
6841
6842   fh = (struct ppc_link_hash_entry *) h;
6843   if (fh->elf.root.type == bfd_link_hash_indirect)
6844     return TRUE;
6845
6846   info = inf;
6847   htab = ppc_hash_table (info);
6848   if (htab == NULL)
6849     return FALSE;
6850
6851   /* Resolve undefined references to dot-symbols as the value
6852      in the function descriptor, if we have one in a regular object.
6853      This is to satisfy cases like ".quad .foo".  Calls to functions
6854      in dynamic objects are handled elsewhere.  */
6855   if (fh->elf.root.type == bfd_link_hash_undefweak
6856       && fh->was_undefined
6857       && (fdh = defined_func_desc (fh)) != NULL
6858       && get_opd_info (fdh->elf.root.u.def.section) != NULL
6859       && opd_entry_value (fdh->elf.root.u.def.section,
6860                           fdh->elf.root.u.def.value,
6861                           &fh->elf.root.u.def.section,
6862                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6863     {
6864       fh->elf.root.type = fdh->elf.root.type;
6865       fh->elf.forced_local = 1;
6866       fh->elf.def_regular = fdh->elf.def_regular;
6867       fh->elf.def_dynamic = fdh->elf.def_dynamic;
6868     }
6869
6870   /* If this is a function code symbol, transfer dynamic linking
6871      information to the function descriptor symbol.  */
6872   if (!fh->is_func)
6873     return TRUE;
6874
6875   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6876     if (ent->plt.refcount > 0)
6877       break;
6878   if (ent == NULL
6879       || fh->elf.root.root.string[0] != '.'
6880       || fh->elf.root.root.string[1] == '\0')
6881     return TRUE;
6882
6883   /* Find the corresponding function descriptor symbol.  Create it
6884      as undefined if necessary.  */
6885
6886   fdh = lookup_fdh (fh, htab);
6887   if (fdh == NULL
6888       && !bfd_link_executable (info)
6889       && (fh->elf.root.type == bfd_link_hash_undefined
6890           || fh->elf.root.type == bfd_link_hash_undefweak))
6891     {
6892       fdh = make_fdh (info, fh);
6893       if (fdh == NULL)
6894         return FALSE;
6895     }
6896
6897   /* Fake function descriptors are made undefweak.  If the function
6898      code symbol is strong undefined, make the fake sym the same.
6899      If the function code symbol is defined, then force the fake
6900      descriptor local;  We can't support overriding of symbols in a
6901      shared library on a fake descriptor.  */
6902
6903   if (fdh != NULL
6904       && fdh->fake
6905       && fdh->elf.root.type == bfd_link_hash_undefweak)
6906     {
6907       if (fh->elf.root.type == bfd_link_hash_undefined)
6908         {
6909           fdh->elf.root.type = bfd_link_hash_undefined;
6910           bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6911         }
6912       else if (fh->elf.root.type == bfd_link_hash_defined
6913                || fh->elf.root.type == bfd_link_hash_defweak)
6914         {
6915           _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6916         }
6917     }
6918
6919   if (fdh != NULL
6920       && !fdh->elf.forced_local
6921       && (!bfd_link_executable (info)
6922           || fdh->elf.def_dynamic
6923           || fdh->elf.ref_dynamic
6924           || (fdh->elf.root.type == bfd_link_hash_undefweak
6925               && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6926     {
6927       if (fdh->elf.dynindx == -1)
6928         if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6929           return FALSE;
6930       fdh->elf.ref_regular |= fh->elf.ref_regular;
6931       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6932       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6933       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6934       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6935         {
6936           move_plt_plist (fh, fdh);
6937           fdh->elf.needs_plt = 1;
6938         }
6939       fdh->is_func_descriptor = 1;
6940       fdh->oh = fh;
6941       fh->oh = fdh;
6942     }
6943
6944   /* Now that the info is on the function descriptor, clear the
6945      function code sym info.  Any function code syms for which we
6946      don't have a definition in a regular file, we force local.
6947      This prevents a shared library from exporting syms that have
6948      been imported from another library.  Function code syms that
6949      are really in the library we must leave global to prevent the
6950      linker dragging in a definition from a static library.  */
6951   force_local = (!fh->elf.def_regular
6952                  || fdh == NULL
6953                  || !fdh->elf.def_regular
6954                  || fdh->elf.forced_local);
6955   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6956
6957   return TRUE;
6958 }
6959
6960 static const struct sfpr_def_parms save_res_funcs[] =
6961   {
6962     { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6963     { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6964     { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6965     { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6966     { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6967     { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6968     { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6969     { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6970     { "._savef", 14, 31, savefpr, savefpr1_tail },
6971     { "._restf", 14, 31, restfpr, restfpr1_tail },
6972     { "_savevr_", 20, 31, savevr, savevr_tail },
6973     { "_restvr_", 20, 31, restvr, restvr_tail }
6974   };
6975
6976 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
6977    this hook to a) provide some gcc support functions, and b) transfer
6978    dynamic linking information gathered so far on function code symbol
6979    entries, to their corresponding function descriptor symbol entries.  */
6980
6981 static bfd_boolean
6982 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6983                             struct bfd_link_info *info)
6984 {
6985   struct ppc_link_hash_table *htab;
6986   unsigned int i;
6987
6988   htab = ppc_hash_table (info);
6989   if (htab == NULL)
6990     return FALSE;
6991
6992   if (!bfd_link_relocatable (info)
6993       && htab->elf.hgot != NULL)
6994     {
6995       _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6996       /* Make .TOC. defined so as to prevent it being made dynamic.
6997          The wrong value here is fixed later in ppc64_elf_set_toc.  */
6998       if (!htab->elf.hgot->def_regular
6999           || htab->elf.hgot->root.type != bfd_link_hash_defined)
7000         {
7001           htab->elf.hgot->root.type = bfd_link_hash_defined;
7002           htab->elf.hgot->root.u.def.value = 0;
7003           htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7004           htab->elf.hgot->def_regular = 1;
7005           htab->elf.hgot->root.linker_def = 1;
7006         }
7007       htab->elf.hgot->type = STT_OBJECT;
7008       htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7009                                | STV_HIDDEN);
7010     }
7011
7012   if (htab->sfpr == NULL)
7013     /* We don't have any relocs.  */
7014     return TRUE;
7015
7016   /* Provide any missing _save* and _rest* functions.  */
7017   htab->sfpr->size = 0;
7018   if (htab->params->save_restore_funcs)
7019     for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7020       if (!sfpr_define (info, &save_res_funcs[i], NULL))
7021         return FALSE;
7022
7023   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7024
7025   if (htab->sfpr->size == 0)
7026     htab->sfpr->flags |= SEC_EXCLUDE;
7027
7028   return TRUE;
7029 }
7030
7031 /* Return true if we have dynamic relocs that apply to read-only sections.  */
7032
7033 static bfd_boolean
7034 readonly_dynrelocs (struct elf_link_hash_entry *h)
7035 {
7036   struct ppc_link_hash_entry *eh;
7037   struct elf_dyn_relocs *p;
7038
7039   eh = (struct ppc_link_hash_entry *) h;
7040   for (p = eh->dyn_relocs; p != NULL; p = p->next)
7041     {
7042       asection *s = p->sec->output_section;
7043
7044       if (s != NULL && (s->flags & SEC_READONLY) != 0)
7045         return TRUE;
7046     }
7047   return FALSE;
7048 }
7049
7050 /* Adjust a symbol defined by a dynamic object and referenced by a
7051    regular object.  The current definition is in some section of the
7052    dynamic object, but we're not including those sections.  We have to
7053    change the definition to something the rest of the link can
7054    understand.  */
7055
7056 static bfd_boolean
7057 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7058                                  struct elf_link_hash_entry *h)
7059 {
7060   struct ppc_link_hash_table *htab;
7061   asection *s;
7062
7063   htab = ppc_hash_table (info);
7064   if (htab == NULL)
7065     return FALSE;
7066
7067   /* Deal with function syms.  */
7068   if (h->type == STT_FUNC
7069       || h->type == STT_GNU_IFUNC
7070       || h->needs_plt)
7071     {
7072       /* Clear procedure linkage table information for any symbol that
7073          won't need a .plt entry.  */
7074       struct plt_entry *ent;
7075       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7076         if (ent->plt.refcount > 0)
7077           break;
7078       if (ent == NULL
7079           || (h->type != STT_GNU_IFUNC
7080               && (SYMBOL_CALLS_LOCAL (info, h)
7081                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7082                       && h->root.type == bfd_link_hash_undefweak)))
7083           || ((struct ppc_link_hash_entry *) h)->save_res)
7084         {
7085           h->plt.plist = NULL;
7086           h->needs_plt = 0;
7087           h->pointer_equality_needed = 0;
7088         }
7089       else if (abiversion (info->output_bfd) == 2)
7090         {
7091           /* Taking a function's address in a read/write section
7092              doesn't require us to define the function symbol in the
7093              executable on a global entry stub.  A dynamic reloc can
7094              be used instead.  */
7095           if (h->pointer_equality_needed
7096               && h->type != STT_GNU_IFUNC
7097               && !readonly_dynrelocs (h))
7098             {
7099               h->pointer_equality_needed = 0;
7100               h->non_got_ref = 0;
7101             }
7102
7103           /* After adjust_dynamic_symbol, non_got_ref set in the
7104              non-shared case means that we have allocated space in
7105              .dynbss for the symbol and thus dyn_relocs for this
7106              symbol should be discarded.
7107              If we get here we know we are making a PLT entry for this
7108              symbol, and in an executable we'd normally resolve
7109              relocations against this symbol to the PLT entry.  Allow
7110              dynamic relocs if the reference is weak, and the dynamic
7111              relocs will not cause text relocation.  */
7112           else if (!h->ref_regular_nonweak
7113                    && h->non_got_ref
7114                    && h->type != STT_GNU_IFUNC
7115                    && !readonly_dynrelocs (h))
7116             h->non_got_ref = 0;
7117
7118           /* If making a plt entry, then we don't need copy relocs.  */
7119           return TRUE;
7120         }
7121     }
7122   else
7123     h->plt.plist = NULL;
7124
7125   /* If this is a weak symbol, and there is a real definition, the
7126      processor independent code will have arranged for us to see the
7127      real definition first, and we can just use the same value.  */
7128   if (h->u.weakdef != NULL)
7129     {
7130       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7131                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
7132       h->root.u.def.section = h->u.weakdef->root.u.def.section;
7133       h->root.u.def.value = h->u.weakdef->root.u.def.value;
7134       if (ELIMINATE_COPY_RELOCS)
7135         h->non_got_ref = h->u.weakdef->non_got_ref;
7136       return TRUE;
7137     }
7138
7139   /* If we are creating a shared library, we must presume that the
7140      only references to the symbol are via the global offset table.
7141      For such cases we need not do anything here; the relocations will
7142      be handled correctly by relocate_section.  */
7143   if (bfd_link_pic (info))
7144     return TRUE;
7145
7146   /* If there are no references to this symbol that do not use the
7147      GOT, we don't need to generate a copy reloc.  */
7148   if (!h->non_got_ref)
7149     return TRUE;
7150
7151   /* Don't generate a copy reloc for symbols defined in the executable.  */
7152   if (!h->def_dynamic || !h->ref_regular || h->def_regular)
7153     return TRUE;
7154
7155   /* If -z nocopyreloc was given, don't generate them either.  */
7156   if (info->nocopyreloc)
7157     {
7158       h->non_got_ref = 0;
7159       return TRUE;
7160     }
7161
7162   /* If we didn't find any dynamic relocs in read-only sections, then
7163      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
7164   if (ELIMINATE_COPY_RELOCS && !readonly_dynrelocs (h))
7165     {
7166       h->non_got_ref = 0;
7167       return TRUE;
7168     }
7169
7170   /* Protected variables do not work with .dynbss.  The copy in
7171      .dynbss won't be used by the shared library with the protected
7172      definition for the variable.  Text relocations are preferable
7173      to an incorrect program.  */
7174   if (h->protected_def)
7175     {
7176       h->non_got_ref = 0;
7177       return TRUE;
7178     }
7179
7180   if (h->plt.plist != NULL)
7181     {
7182       /* We should never get here, but unfortunately there are versions
7183          of gcc out there that improperly (for this ABI) put initialized
7184          function pointers, vtable refs and suchlike in read-only
7185          sections.  Allow them to proceed, but warn that this might
7186          break at runtime.  */
7187       info->callbacks->einfo
7188         (_("%P: copy reloc against `%T' requires lazy plt linking; "
7189            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7190          h->root.root.string);
7191     }
7192
7193   /* This is a reference to a symbol defined by a dynamic object which
7194      is not a function.  */
7195
7196   /* We must allocate the symbol in our .dynbss section, which will
7197      become part of the .bss section of the executable.  There will be
7198      an entry for this symbol in the .dynsym section.  The dynamic
7199      object will contain position independent code, so all references
7200      from the dynamic object to this symbol will go through the global
7201      offset table.  The dynamic linker will use the .dynsym entry to
7202      determine the address it must put in the global offset table, so
7203      both the dynamic object and the regular object will refer to the
7204      same memory location for the variable.  */
7205
7206   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7207      to copy the initial value out of the dynamic object and into the
7208      runtime process image.  We need to remember the offset into the
7209      .rela.bss section we are going to use.  */
7210   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7211     {
7212       htab->relbss->size += sizeof (Elf64_External_Rela);
7213       h->needs_copy = 1;
7214     }
7215
7216   s = htab->dynbss;
7217
7218   return _bfd_elf_adjust_dynamic_copy (info, h, s);
7219 }
7220
7221 /* If given a function descriptor symbol, hide both the function code
7222    sym and the descriptor.  */
7223 static void
7224 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7225                        struct elf_link_hash_entry *h,
7226                        bfd_boolean force_local)
7227 {
7228   struct ppc_link_hash_entry *eh;
7229   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7230
7231   eh = (struct ppc_link_hash_entry *) h;
7232   if (eh->is_func_descriptor)
7233     {
7234       struct ppc_link_hash_entry *fh = eh->oh;
7235
7236       if (fh == NULL)
7237         {
7238           const char *p, *q;
7239           struct ppc_link_hash_table *htab;
7240           char save;
7241
7242           /* We aren't supposed to use alloca in BFD because on
7243              systems which do not have alloca the version in libiberty
7244              calls xmalloc, which might cause the program to crash
7245              when it runs out of memory.  This function doesn't have a
7246              return status, so there's no way to gracefully return an
7247              error.  So cheat.  We know that string[-1] can be safely
7248              accessed;  It's either a string in an ELF string table,
7249              or allocated in an objalloc structure.  */
7250
7251           p = eh->elf.root.root.string - 1;
7252           save = *p;
7253           *(char *) p = '.';
7254           htab = ppc_hash_table (info);
7255           if (htab == NULL)
7256             return;
7257
7258           fh = (struct ppc_link_hash_entry *)
7259             elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7260           *(char *) p = save;
7261
7262           /* Unfortunately, if it so happens that the string we were
7263              looking for was allocated immediately before this string,
7264              then we overwrote the string terminator.  That's the only
7265              reason the lookup should fail.  */
7266           if (fh == NULL)
7267             {
7268               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7269               while (q >= eh->elf.root.root.string && *q == *p)
7270                 --q, --p;
7271               if (q < eh->elf.root.root.string && *p == '.')
7272                 fh = (struct ppc_link_hash_entry *)
7273                   elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7274             }
7275           if (fh != NULL)
7276             {
7277               eh->oh = fh;
7278               fh->oh = eh;
7279             }
7280         }
7281       if (fh != NULL)
7282         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7283     }
7284 }
7285
7286 static bfd_boolean
7287 get_sym_h (struct elf_link_hash_entry **hp,
7288            Elf_Internal_Sym **symp,
7289            asection **symsecp,
7290            unsigned char **tls_maskp,
7291            Elf_Internal_Sym **locsymsp,
7292            unsigned long r_symndx,
7293            bfd *ibfd)
7294 {
7295   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7296
7297   if (r_symndx >= symtab_hdr->sh_info)
7298     {
7299       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7300       struct elf_link_hash_entry *h;
7301
7302       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7303       h = elf_follow_link (h);
7304
7305       if (hp != NULL)
7306         *hp = h;
7307
7308       if (symp != NULL)
7309         *symp = NULL;
7310
7311       if (symsecp != NULL)
7312         {
7313           asection *symsec = NULL;
7314           if (h->root.type == bfd_link_hash_defined
7315               || h->root.type == bfd_link_hash_defweak)
7316             symsec = h->root.u.def.section;
7317           *symsecp = symsec;
7318         }
7319
7320       if (tls_maskp != NULL)
7321         {
7322           struct ppc_link_hash_entry *eh;
7323
7324           eh = (struct ppc_link_hash_entry *) h;
7325           *tls_maskp = &eh->tls_mask;
7326         }
7327     }
7328   else
7329     {
7330       Elf_Internal_Sym *sym;
7331       Elf_Internal_Sym *locsyms = *locsymsp;
7332
7333       if (locsyms == NULL)
7334         {
7335           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7336           if (locsyms == NULL)
7337             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7338                                             symtab_hdr->sh_info,
7339                                             0, NULL, NULL, NULL);
7340           if (locsyms == NULL)
7341             return FALSE;
7342           *locsymsp = locsyms;
7343         }
7344       sym = locsyms + r_symndx;
7345
7346       if (hp != NULL)
7347         *hp = NULL;
7348
7349       if (symp != NULL)
7350         *symp = sym;
7351
7352       if (symsecp != NULL)
7353         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7354
7355       if (tls_maskp != NULL)
7356         {
7357           struct got_entry **lgot_ents;
7358           unsigned char *tls_mask;
7359
7360           tls_mask = NULL;
7361           lgot_ents = elf_local_got_ents (ibfd);
7362           if (lgot_ents != NULL)
7363             {
7364               struct plt_entry **local_plt = (struct plt_entry **)
7365                 (lgot_ents + symtab_hdr->sh_info);
7366               unsigned char *lgot_masks = (unsigned char *)
7367                 (local_plt + symtab_hdr->sh_info);
7368               tls_mask = &lgot_masks[r_symndx];
7369             }
7370           *tls_maskp = tls_mask;
7371         }
7372     }
7373   return TRUE;
7374 }
7375
7376 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
7377    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7378    type suitable for optimization, and 1 otherwise.  */
7379
7380 static int
7381 get_tls_mask (unsigned char **tls_maskp,
7382               unsigned long *toc_symndx,
7383               bfd_vma *toc_addend,
7384               Elf_Internal_Sym **locsymsp,
7385               const Elf_Internal_Rela *rel,
7386               bfd *ibfd)
7387 {
7388   unsigned long r_symndx;
7389   int next_r;
7390   struct elf_link_hash_entry *h;
7391   Elf_Internal_Sym *sym;
7392   asection *sec;
7393   bfd_vma off;
7394
7395   r_symndx = ELF64_R_SYM (rel->r_info);
7396   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7397     return 0;
7398
7399   if ((*tls_maskp != NULL && **tls_maskp != 0)
7400       || sec == NULL
7401       || ppc64_elf_section_data (sec) == NULL
7402       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7403     return 1;
7404
7405   /* Look inside a TOC section too.  */
7406   if (h != NULL)
7407     {
7408       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7409       off = h->root.u.def.value;
7410     }
7411   else
7412     off = sym->st_value;
7413   off += rel->r_addend;
7414   BFD_ASSERT (off % 8 == 0);
7415   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7416   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7417   if (toc_symndx != NULL)
7418     *toc_symndx = r_symndx;
7419   if (toc_addend != NULL)
7420     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7421   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7422     return 0;
7423   if ((h == NULL || is_static_defined (h))
7424       && (next_r == -1 || next_r == -2))
7425     return 1 - next_r;
7426   return 1;
7427 }
7428
7429 /* Find (or create) an entry in the tocsave hash table.  */
7430
7431 static struct tocsave_entry *
7432 tocsave_find (struct ppc_link_hash_table *htab,
7433               enum insert_option insert,
7434               Elf_Internal_Sym **local_syms,
7435               const Elf_Internal_Rela *irela,
7436               bfd *ibfd)
7437 {
7438   unsigned long r_indx;
7439   struct elf_link_hash_entry *h;
7440   Elf_Internal_Sym *sym;
7441   struct tocsave_entry ent, *p;
7442   hashval_t hash;
7443   struct tocsave_entry **slot;
7444
7445   r_indx = ELF64_R_SYM (irela->r_info);
7446   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7447     return NULL;
7448   if (ent.sec == NULL || ent.sec->output_section == NULL)
7449     {
7450       (*_bfd_error_handler)
7451         (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7452       return NULL;
7453     }
7454
7455   if (h != NULL)
7456     ent.offset = h->root.u.def.value;
7457   else
7458     ent.offset = sym->st_value;
7459   ent.offset += irela->r_addend;
7460
7461   hash = tocsave_htab_hash (&ent);
7462   slot = ((struct tocsave_entry **)
7463           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7464   if (slot == NULL)
7465     return NULL;
7466
7467   if (*slot == NULL)
7468     {
7469       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7470       if (p == NULL)
7471         return NULL;
7472       *p = ent;
7473       *slot = p;
7474     }
7475   return *slot;
7476 }
7477
7478 /* Adjust all global syms defined in opd sections.  In gcc generated
7479    code for the old ABI, these will already have been done.  */
7480
7481 static bfd_boolean
7482 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7483 {
7484   struct ppc_link_hash_entry *eh;
7485   asection *sym_sec;
7486   struct _opd_sec_data *opd;
7487
7488   if (h->root.type == bfd_link_hash_indirect)
7489     return TRUE;
7490
7491   if (h->root.type != bfd_link_hash_defined
7492       && h->root.type != bfd_link_hash_defweak)
7493     return TRUE;
7494
7495   eh = (struct ppc_link_hash_entry *) h;
7496   if (eh->adjust_done)
7497     return TRUE;
7498
7499   sym_sec = eh->elf.root.u.def.section;
7500   opd = get_opd_info (sym_sec);
7501   if (opd != NULL && opd->adjust != NULL)
7502     {
7503       long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7504       if (adjust == -1)
7505         {
7506           /* This entry has been deleted.  */
7507           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7508           if (dsec == NULL)
7509             {
7510               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7511                 if (discarded_section (dsec))
7512                   {
7513                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7514                     break;
7515                   }
7516             }
7517           eh->elf.root.u.def.value = 0;
7518           eh->elf.root.u.def.section = dsec;
7519         }
7520       else
7521         eh->elf.root.u.def.value += adjust;
7522       eh->adjust_done = 1;
7523     }
7524   return TRUE;
7525 }
7526
7527 /* Handles decrementing dynamic reloc counts for the reloc specified by
7528    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
7529    have already been determined.  */
7530
7531 static bfd_boolean
7532 dec_dynrel_count (bfd_vma r_info,
7533                   asection *sec,
7534                   struct bfd_link_info *info,
7535                   Elf_Internal_Sym **local_syms,
7536                   struct elf_link_hash_entry *h,
7537                   Elf_Internal_Sym *sym)
7538 {
7539   enum elf_ppc64_reloc_type r_type;
7540   asection *sym_sec = NULL;
7541
7542   /* Can this reloc be dynamic?  This switch, and later tests here
7543      should be kept in sync with the code in check_relocs.  */
7544   r_type = ELF64_R_TYPE (r_info);
7545   switch (r_type)
7546     {
7547     default:
7548       return TRUE;
7549
7550     case R_PPC64_TPREL16:
7551     case R_PPC64_TPREL16_LO:
7552     case R_PPC64_TPREL16_HI:
7553     case R_PPC64_TPREL16_HA:
7554     case R_PPC64_TPREL16_DS:
7555     case R_PPC64_TPREL16_LO_DS:
7556     case R_PPC64_TPREL16_HIGH:
7557     case R_PPC64_TPREL16_HIGHA:
7558     case R_PPC64_TPREL16_HIGHER:
7559     case R_PPC64_TPREL16_HIGHERA:
7560     case R_PPC64_TPREL16_HIGHEST:
7561     case R_PPC64_TPREL16_HIGHESTA:
7562       if (!bfd_link_pic (info))
7563         return TRUE;
7564
7565     case R_PPC64_TPREL64:
7566     case R_PPC64_DTPMOD64:
7567     case R_PPC64_DTPREL64:
7568     case R_PPC64_ADDR64:
7569     case R_PPC64_REL30:
7570     case R_PPC64_REL32:
7571     case R_PPC64_REL64:
7572     case R_PPC64_ADDR14:
7573     case R_PPC64_ADDR14_BRNTAKEN:
7574     case R_PPC64_ADDR14_BRTAKEN:
7575     case R_PPC64_ADDR16:
7576     case R_PPC64_ADDR16_DS:
7577     case R_PPC64_ADDR16_HA:
7578     case R_PPC64_ADDR16_HI:
7579     case R_PPC64_ADDR16_HIGH:
7580     case R_PPC64_ADDR16_HIGHA:
7581     case R_PPC64_ADDR16_HIGHER:
7582     case R_PPC64_ADDR16_HIGHERA:
7583     case R_PPC64_ADDR16_HIGHEST:
7584     case R_PPC64_ADDR16_HIGHESTA:
7585     case R_PPC64_ADDR16_LO:
7586     case R_PPC64_ADDR16_LO_DS:
7587     case R_PPC64_ADDR24:
7588     case R_PPC64_ADDR32:
7589     case R_PPC64_UADDR16:
7590     case R_PPC64_UADDR32:
7591     case R_PPC64_UADDR64:
7592     case R_PPC64_TOC:
7593       break;
7594     }
7595
7596   if (local_syms != NULL)
7597     {
7598       unsigned long r_symndx;
7599       bfd *ibfd = sec->owner;
7600
7601       r_symndx = ELF64_R_SYM (r_info);
7602       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7603         return FALSE;
7604     }
7605
7606   if ((bfd_link_pic (info)
7607        && (must_be_dyn_reloc (info, r_type)
7608            || (h != NULL
7609                && (!SYMBOLIC_BIND (info, h)
7610                    || h->root.type == bfd_link_hash_defweak
7611                    || !h->def_regular))))
7612       || (ELIMINATE_COPY_RELOCS
7613           && !bfd_link_pic (info)
7614           && h != NULL
7615           && (h->root.type == bfd_link_hash_defweak
7616               || !h->def_regular)))
7617     ;
7618   else
7619     return TRUE;
7620
7621   if (h != NULL)
7622     {
7623       struct elf_dyn_relocs *p;
7624       struct elf_dyn_relocs **pp;
7625       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7626
7627       /* elf_gc_sweep may have already removed all dyn relocs associated
7628          with local syms for a given section.  Also, symbol flags are
7629          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
7630          report a dynreloc miscount.  */
7631       if (*pp == NULL && info->gc_sections)
7632         return TRUE;
7633
7634       while ((p = *pp) != NULL)
7635         {
7636           if (p->sec == sec)
7637             {
7638               if (!must_be_dyn_reloc (info, r_type))
7639                 p->pc_count -= 1;
7640               p->count -= 1;
7641               if (p->count == 0)
7642                 *pp = p->next;
7643               return TRUE;
7644             }
7645           pp = &p->next;
7646         }
7647     }
7648   else
7649     {
7650       struct ppc_dyn_relocs *p;
7651       struct ppc_dyn_relocs **pp;
7652       void *vpp;
7653       bfd_boolean is_ifunc;
7654
7655       if (local_syms == NULL)
7656         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7657       if (sym_sec == NULL)
7658         sym_sec = sec;
7659
7660       vpp = &elf_section_data (sym_sec)->local_dynrel;
7661       pp = (struct ppc_dyn_relocs **) vpp;
7662
7663       if (*pp == NULL && info->gc_sections)
7664         return TRUE;
7665
7666       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7667       while ((p = *pp) != NULL)
7668         {
7669           if (p->sec == sec && p->ifunc == is_ifunc)
7670             {
7671               p->count -= 1;
7672               if (p->count == 0)
7673                 *pp = p->next;
7674               return TRUE;
7675             }
7676           pp = &p->next;
7677         }
7678     }
7679
7680   info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7681                           sec->owner, sec);
7682   bfd_set_error (bfd_error_bad_value);
7683   return FALSE;
7684 }
7685
7686 /* Remove unused Official Procedure Descriptor entries.  Currently we
7687    only remove those associated with functions in discarded link-once
7688    sections, or weakly defined functions that have been overridden.  It
7689    would be possible to remove many more entries for statically linked
7690    applications.  */
7691
7692 bfd_boolean
7693 ppc64_elf_edit_opd (struct bfd_link_info *info)
7694 {
7695   bfd *ibfd;
7696   bfd_boolean some_edited = FALSE;
7697   asection *need_pad = NULL;
7698   struct ppc_link_hash_table *htab;
7699
7700   htab = ppc_hash_table (info);
7701   if (htab == NULL)
7702     return FALSE;
7703
7704   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7705     {
7706       asection *sec;
7707       Elf_Internal_Rela *relstart, *rel, *relend;
7708       Elf_Internal_Shdr *symtab_hdr;
7709       Elf_Internal_Sym *local_syms;
7710       struct _opd_sec_data *opd;
7711       bfd_boolean need_edit, add_aux_fields, broken;
7712       bfd_size_type cnt_16b = 0;
7713
7714       if (!is_ppc64_elf (ibfd))
7715         continue;
7716
7717       sec = bfd_get_section_by_name (ibfd, ".opd");
7718       if (sec == NULL || sec->size == 0)
7719         continue;
7720
7721       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7722         continue;
7723
7724       if (sec->output_section == bfd_abs_section_ptr)
7725         continue;
7726
7727       /* Look through the section relocs.  */
7728       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7729         continue;
7730
7731       local_syms = NULL;
7732       symtab_hdr = &elf_symtab_hdr (ibfd);
7733
7734       /* Read the relocations.  */
7735       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7736                                             info->keep_memory);
7737       if (relstart == NULL)
7738         return FALSE;
7739
7740       /* First run through the relocs to check they are sane, and to
7741          determine whether we need to edit this opd section.  */
7742       need_edit = FALSE;
7743       broken = FALSE;
7744       need_pad = sec;
7745       relend = relstart + sec->reloc_count;
7746       for (rel = relstart; rel < relend; )
7747         {
7748           enum elf_ppc64_reloc_type r_type;
7749           unsigned long r_symndx;
7750           asection *sym_sec;
7751           struct elf_link_hash_entry *h;
7752           Elf_Internal_Sym *sym;
7753           bfd_vma offset;
7754
7755           /* .opd contains an array of 16 or 24 byte entries.  We're
7756              only interested in the reloc pointing to a function entry
7757              point.  */
7758           offset = rel->r_offset;
7759           if (rel + 1 == relend
7760               || rel[1].r_offset != offset + 8)
7761             {
7762               /* If someone messes with .opd alignment then after a
7763                  "ld -r" we might have padding in the middle of .opd.
7764                  Also, there's nothing to prevent someone putting
7765                  something silly in .opd with the assembler.  No .opd
7766                  optimization for them!  */
7767             broken_opd:
7768               (*_bfd_error_handler)
7769                 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7770               broken = TRUE;
7771               break;
7772             }
7773
7774           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7775               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7776             {
7777               (*_bfd_error_handler)
7778                 (_("%B: unexpected reloc type %u in .opd section"),
7779                  ibfd, r_type);
7780               broken = TRUE;
7781               break;
7782             }
7783
7784           r_symndx = ELF64_R_SYM (rel->r_info);
7785           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7786                           r_symndx, ibfd))
7787             goto error_ret;
7788
7789           if (sym_sec == NULL || sym_sec->owner == NULL)
7790             {
7791               const char *sym_name;
7792               if (h != NULL)
7793                 sym_name = h->root.root.string;
7794               else
7795                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7796                                              sym_sec);
7797
7798               (*_bfd_error_handler)
7799                 (_("%B: undefined sym `%s' in .opd section"),
7800                  ibfd, sym_name);
7801               broken = TRUE;
7802               break;
7803             }
7804
7805           /* opd entries are always for functions defined in the
7806              current input bfd.  If the symbol isn't defined in the
7807              input bfd, then we won't be using the function in this
7808              bfd;  It must be defined in a linkonce section in another
7809              bfd, or is weak.  It's also possible that we are
7810              discarding the function due to a linker script /DISCARD/,
7811              which we test for via the output_section.  */
7812           if (sym_sec->owner != ibfd
7813               || sym_sec->output_section == bfd_abs_section_ptr)
7814             need_edit = TRUE;
7815
7816           rel += 2;
7817           if (rel + 1 == relend
7818               || (rel + 2 < relend
7819                   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7820             ++rel;
7821
7822           if (rel == relend)
7823             {
7824               if (sec->size == offset + 24)
7825                 {
7826                   need_pad = NULL;
7827                   break;
7828                 }
7829               if (sec->size == offset + 16)
7830                 {
7831                   cnt_16b++;
7832                   break;
7833                 }
7834               goto broken_opd;
7835             }
7836           else if (rel + 1 < relend
7837                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7838                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7839             {
7840               if (rel[0].r_offset == offset + 16)
7841                 cnt_16b++;
7842               else if (rel[0].r_offset != offset + 24)
7843                 goto broken_opd;
7844             }
7845           else
7846             goto broken_opd;
7847         }
7848
7849       add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7850
7851       if (!broken && (need_edit || add_aux_fields))
7852         {
7853           Elf_Internal_Rela *write_rel;
7854           Elf_Internal_Shdr *rel_hdr;
7855           bfd_byte *rptr, *wptr;
7856           bfd_byte *new_contents;
7857           bfd_size_type amt;
7858
7859           new_contents = NULL;
7860           amt = OPD_NDX (sec->size) * sizeof (long);
7861           opd = &ppc64_elf_section_data (sec)->u.opd;
7862           opd->adjust = bfd_zalloc (sec->owner, amt);
7863           if (opd->adjust == NULL)
7864             return FALSE;
7865           ppc64_elf_section_data (sec)->sec_type = sec_opd;
7866
7867           /* This seems a waste of time as input .opd sections are all
7868              zeros as generated by gcc, but I suppose there's no reason
7869              this will always be so.  We might start putting something in
7870              the third word of .opd entries.  */
7871           if ((sec->flags & SEC_IN_MEMORY) == 0)
7872             {
7873               bfd_byte *loc;
7874               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7875                 {
7876                   if (loc != NULL)
7877                     free (loc);
7878                 error_ret:
7879                   if (local_syms != NULL
7880                       && symtab_hdr->contents != (unsigned char *) local_syms)
7881                     free (local_syms);
7882                   if (elf_section_data (sec)->relocs != relstart)
7883                     free (relstart);
7884                   return FALSE;
7885                 }
7886               sec->contents = loc;
7887               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7888             }
7889
7890           elf_section_data (sec)->relocs = relstart;
7891
7892           new_contents = sec->contents;
7893           if (add_aux_fields)
7894             {
7895               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7896               if (new_contents == NULL)
7897                 return FALSE;
7898               need_pad = NULL;
7899             }
7900           wptr = new_contents;
7901           rptr = sec->contents;
7902           write_rel = relstart;
7903           for (rel = relstart; rel < relend; )
7904             {
7905               unsigned long r_symndx;
7906               asection *sym_sec;
7907               struct elf_link_hash_entry *h;
7908               struct ppc_link_hash_entry *fdh = NULL;
7909               Elf_Internal_Sym *sym;
7910               long opd_ent_size;
7911               Elf_Internal_Rela *next_rel;
7912               bfd_boolean skip;
7913
7914               r_symndx = ELF64_R_SYM (rel->r_info);
7915               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7916                               r_symndx, ibfd))
7917                 goto error_ret;
7918
7919               next_rel = rel + 2;
7920               if (next_rel + 1 == relend
7921                   || (next_rel + 2 < relend
7922                       && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7923                 ++next_rel;
7924
7925               /* See if the .opd entry is full 24 byte or
7926                  16 byte (with fd_aux entry overlapped with next
7927                  fd_func).  */
7928               opd_ent_size = 24;
7929               if (next_rel == relend)
7930                 {
7931                   if (sec->size == rel->r_offset + 16)
7932                     opd_ent_size = 16;
7933                 }
7934               else if (next_rel->r_offset == rel->r_offset + 16)
7935                 opd_ent_size = 16;
7936
7937               if (h != NULL
7938                   && h->root.root.string[0] == '.')
7939                 {
7940                   fdh = lookup_fdh ((struct ppc_link_hash_entry *) h, htab);
7941                   if (fdh != NULL
7942                       && fdh->elf.root.type != bfd_link_hash_defined
7943                       && fdh->elf.root.type != bfd_link_hash_defweak)
7944                     fdh = NULL;
7945                 }
7946
7947               skip = (sym_sec->owner != ibfd
7948                       || sym_sec->output_section == bfd_abs_section_ptr);
7949               if (skip)
7950                 {
7951                   if (fdh != NULL && sym_sec->owner == ibfd)
7952                     {
7953                       /* Arrange for the function descriptor sym
7954                          to be dropped.  */
7955                       fdh->elf.root.u.def.value = 0;
7956                       fdh->elf.root.u.def.section = sym_sec;
7957                     }
7958                   opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7959
7960                   if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7961                     rel = next_rel;
7962                   else
7963                     while (1)
7964                       {
7965                         if (!dec_dynrel_count (rel->r_info, sec, info,
7966                                                NULL, h, sym))
7967                           goto error_ret;
7968
7969                         if (++rel == next_rel)
7970                           break;
7971
7972                         r_symndx = ELF64_R_SYM (rel->r_info);
7973                         if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7974                                         r_symndx, ibfd))
7975                           goto error_ret;
7976                       }
7977                 }
7978               else
7979                 {
7980                   /* We'll be keeping this opd entry.  */
7981                   long adjust;
7982
7983                   if (fdh != NULL)
7984                     {
7985                       /* Redefine the function descriptor symbol to
7986                          this location in the opd section.  It is
7987                          necessary to update the value here rather
7988                          than using an array of adjustments as we do
7989                          for local symbols, because various places
7990                          in the generic ELF code use the value
7991                          stored in u.def.value.  */
7992                       fdh->elf.root.u.def.value = wptr - new_contents;
7993                       fdh->adjust_done = 1;
7994                     }
7995
7996                   /* Local syms are a bit tricky.  We could
7997                      tweak them as they can be cached, but
7998                      we'd need to look through the local syms
7999                      for the function descriptor sym which we
8000                      don't have at the moment.  So keep an
8001                      array of adjustments.  */
8002                   adjust = (wptr - new_contents) - (rptr - sec->contents);
8003                   opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8004
8005                   if (wptr != rptr)
8006                     memcpy (wptr, rptr, opd_ent_size);
8007                   wptr += opd_ent_size;
8008                   if (add_aux_fields && opd_ent_size == 16)
8009                     {
8010                       memset (wptr, '\0', 8);
8011                       wptr += 8;
8012                     }
8013
8014                   /* We need to adjust any reloc offsets to point to the
8015                      new opd entries.  */
8016                   for ( ; rel != next_rel; ++rel)
8017                     {
8018                       rel->r_offset += adjust;
8019                       if (write_rel != rel)
8020                         memcpy (write_rel, rel, sizeof (*rel));
8021                       ++write_rel;
8022                     }
8023                 }
8024
8025               rptr += opd_ent_size;
8026             }
8027
8028           sec->size = wptr - new_contents;
8029           sec->reloc_count = write_rel - relstart;
8030           if (add_aux_fields)
8031             {
8032               free (sec->contents);
8033               sec->contents = new_contents;
8034             }
8035
8036           /* Fudge the header size too, as this is used later in
8037              elf_bfd_final_link if we are emitting relocs.  */
8038           rel_hdr = _bfd_elf_single_rel_hdr (sec);
8039           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8040           some_edited = TRUE;
8041         }
8042       else if (elf_section_data (sec)->relocs != relstart)
8043         free (relstart);
8044
8045       if (local_syms != NULL
8046           && symtab_hdr->contents != (unsigned char *) local_syms)
8047         {
8048           if (!info->keep_memory)
8049             free (local_syms);
8050           else
8051             symtab_hdr->contents = (unsigned char *) local_syms;
8052         }
8053     }
8054
8055   if (some_edited)
8056     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8057
8058   /* If we are doing a final link and the last .opd entry is just 16 byte
8059      long, add a 8 byte padding after it.  */
8060   if (need_pad != NULL && !bfd_link_relocatable (info))
8061     {
8062       bfd_byte *p;
8063
8064       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8065         {
8066           BFD_ASSERT (need_pad->size > 0);
8067
8068           p = bfd_malloc (need_pad->size + 8);
8069           if (p == NULL)
8070             return FALSE;
8071
8072           if (! bfd_get_section_contents (need_pad->owner, need_pad,
8073                                           p, 0, need_pad->size))
8074             return FALSE;
8075
8076           need_pad->contents = p;
8077           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8078         }
8079       else
8080         {
8081           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8082           if (p == NULL)
8083             return FALSE;
8084
8085           need_pad->contents = p;
8086         }
8087
8088       memset (need_pad->contents + need_pad->size, 0, 8);
8089       need_pad->size += 8;
8090     }
8091
8092   return TRUE;
8093 }
8094
8095 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
8096
8097 asection *
8098 ppc64_elf_tls_setup (struct bfd_link_info *info)
8099 {
8100   struct ppc_link_hash_table *htab;
8101
8102   htab = ppc_hash_table (info);
8103   if (htab == NULL)
8104     return NULL;
8105
8106   if (abiversion (info->output_bfd) == 1)
8107     htab->opd_abi = 1;
8108
8109   if (htab->params->no_multi_toc)
8110     htab->do_multi_toc = 0;
8111   else if (!htab->do_multi_toc)
8112     htab->params->no_multi_toc = 1;
8113
8114   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8115                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8116                                               FALSE, FALSE, TRUE));
8117   /* Move dynamic linking info to the function descriptor sym.  */
8118   if (htab->tls_get_addr != NULL)
8119     func_desc_adjust (&htab->tls_get_addr->elf, info);
8120   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8121                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8122                                                  FALSE, FALSE, TRUE));
8123   if (!htab->params->no_tls_get_addr_opt)
8124     {
8125       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8126
8127       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8128                                   FALSE, FALSE, TRUE);
8129       if (opt != NULL)
8130         func_desc_adjust (opt, info);
8131       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8132                                      FALSE, FALSE, TRUE);
8133       if (opt_fd != NULL
8134           && (opt_fd->root.type == bfd_link_hash_defined
8135               || opt_fd->root.type == bfd_link_hash_defweak))
8136         {
8137           /* If glibc supports an optimized __tls_get_addr call stub,
8138              signalled by the presence of __tls_get_addr_opt, and we'll
8139              be calling __tls_get_addr via a plt call stub, then
8140              make __tls_get_addr point to __tls_get_addr_opt.  */
8141           tga_fd = &htab->tls_get_addr_fd->elf;
8142           if (htab->elf.dynamic_sections_created
8143               && tga_fd != NULL
8144               && (tga_fd->type == STT_FUNC
8145                   || tga_fd->needs_plt)
8146               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8147                    || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
8148                        && tga_fd->root.type == bfd_link_hash_undefweak)))
8149             {
8150               struct plt_entry *ent;
8151
8152               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8153                 if (ent->plt.refcount > 0)
8154                   break;
8155               if (ent != NULL)
8156                 {
8157                   tga_fd->root.type = bfd_link_hash_indirect;
8158                   tga_fd->root.u.i.link = &opt_fd->root;
8159                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8160                   if (opt_fd->dynindx != -1)
8161                     {
8162                       /* Use __tls_get_addr_opt in dynamic relocations.  */
8163                       opt_fd->dynindx = -1;
8164                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8165                                               opt_fd->dynstr_index);
8166                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8167                         return NULL;
8168                     }
8169                   htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8170                   tga = &htab->tls_get_addr->elf;
8171                   if (opt != NULL && tga != NULL)
8172                     {
8173                       tga->root.type = bfd_link_hash_indirect;
8174                       tga->root.u.i.link = &opt->root;
8175                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
8176                       _bfd_elf_link_hash_hide_symbol (info, opt,
8177                                                       tga->forced_local);
8178                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8179                     }
8180                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8181                   htab->tls_get_addr_fd->is_func_descriptor = 1;
8182                   if (htab->tls_get_addr != NULL)
8183                     {
8184                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8185                       htab->tls_get_addr->is_func = 1;
8186                     }
8187                 }
8188             }
8189         }
8190       else
8191         htab->params->no_tls_get_addr_opt = TRUE;
8192     }
8193   return _bfd_elf_tls_setup (info->output_bfd, info);
8194 }
8195
8196 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8197    HASH1 or HASH2.  */
8198
8199 static bfd_boolean
8200 branch_reloc_hash_match (const bfd *ibfd,
8201                          const Elf_Internal_Rela *rel,
8202                          const struct ppc_link_hash_entry *hash1,
8203                          const struct ppc_link_hash_entry *hash2)
8204 {
8205   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8206   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8207   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8208
8209   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8210     {
8211       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8212       struct elf_link_hash_entry *h;
8213
8214       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8215       h = elf_follow_link (h);
8216       if (h == &hash1->elf || h == &hash2->elf)
8217         return TRUE;
8218     }
8219   return FALSE;
8220 }
8221
8222 /* Run through all the TLS relocs looking for optimization
8223    opportunities.  The linker has been hacked (see ppc64elf.em) to do
8224    a preliminary section layout so that we know the TLS segment
8225    offsets.  We can't optimize earlier because some optimizations need
8226    to know the tp offset, and we need to optimize before allocating
8227    dynamic relocations.  */
8228
8229 bfd_boolean
8230 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8231 {
8232   bfd *ibfd;
8233   asection *sec;
8234   struct ppc_link_hash_table *htab;
8235   unsigned char *toc_ref;
8236   int pass;
8237
8238   if (!bfd_link_executable (info))
8239     return TRUE;
8240
8241   htab = ppc_hash_table (info);
8242   if (htab == NULL)
8243     return FALSE;
8244
8245   /* Make two passes over the relocs.  On the first pass, mark toc
8246      entries involved with tls relocs, and check that tls relocs
8247      involved in setting up a tls_get_addr call are indeed followed by
8248      such a call.  If they are not, we can't do any tls optimization.
8249      On the second pass twiddle tls_mask flags to notify
8250      relocate_section that optimization can be done, and adjust got
8251      and plt refcounts.  */
8252   toc_ref = NULL;
8253   for (pass = 0; pass < 2; ++pass)
8254     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8255       {
8256         Elf_Internal_Sym *locsyms = NULL;
8257         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8258
8259         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8260           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8261             {
8262               Elf_Internal_Rela *relstart, *rel, *relend;
8263               bfd_boolean found_tls_get_addr_arg = 0;
8264
8265               /* Read the relocations.  */
8266               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8267                                                     info->keep_memory);
8268               if (relstart == NULL)
8269                 {
8270                   free (toc_ref);
8271                   return FALSE;
8272                 }
8273
8274               relend = relstart + sec->reloc_count;
8275               for (rel = relstart; rel < relend; rel++)
8276                 {
8277                   enum elf_ppc64_reloc_type r_type;
8278                   unsigned long r_symndx;
8279                   struct elf_link_hash_entry *h;
8280                   Elf_Internal_Sym *sym;
8281                   asection *sym_sec;
8282                   unsigned char *tls_mask;
8283                   unsigned char tls_set, tls_clear, tls_type = 0;
8284                   bfd_vma value;
8285                   bfd_boolean ok_tprel, is_local;
8286                   long toc_ref_index = 0;
8287                   int expecting_tls_get_addr = 0;
8288                   bfd_boolean ret = FALSE;
8289
8290                   r_symndx = ELF64_R_SYM (rel->r_info);
8291                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8292                                   r_symndx, ibfd))
8293                     {
8294                     err_free_rel:
8295                       if (elf_section_data (sec)->relocs != relstart)
8296                         free (relstart);
8297                       if (toc_ref != NULL)
8298                         free (toc_ref);
8299                       if (locsyms != NULL
8300                           && (elf_symtab_hdr (ibfd).contents
8301                               != (unsigned char *) locsyms))
8302                         free (locsyms);
8303                       return ret;
8304                     }
8305
8306                   if (h != NULL)
8307                     {
8308                       if (h->root.type == bfd_link_hash_defined
8309                           || h->root.type == bfd_link_hash_defweak)
8310                         value = h->root.u.def.value;
8311                       else if (h->root.type == bfd_link_hash_undefweak)
8312                         value = 0;
8313                       else
8314                         {
8315                           found_tls_get_addr_arg = 0;
8316                           continue;
8317                         }
8318                     }
8319                   else
8320                     /* Symbols referenced by TLS relocs must be of type
8321                        STT_TLS.  So no need for .opd local sym adjust.  */
8322                     value = sym->st_value;
8323
8324                   ok_tprel = FALSE;
8325                   is_local = FALSE;
8326                   if (h == NULL
8327                       || !h->def_dynamic)
8328                     {
8329                       is_local = TRUE;
8330                       if (h != NULL
8331                           && h->root.type == bfd_link_hash_undefweak)
8332                         ok_tprel = TRUE;
8333                       else
8334                         {
8335                           value += sym_sec->output_offset;
8336                           value += sym_sec->output_section->vma;
8337                           value -= htab->elf.tls_sec->vma;
8338                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8339                                       < (bfd_vma) 1 << 32);
8340                         }
8341                     }
8342
8343                   r_type = ELF64_R_TYPE (rel->r_info);
8344                   /* If this section has old-style __tls_get_addr calls
8345                      without marker relocs, then check that each
8346                      __tls_get_addr call reloc is preceded by a reloc
8347                      that conceivably belongs to the __tls_get_addr arg
8348                      setup insn.  If we don't find matching arg setup
8349                      relocs, don't do any tls optimization.  */
8350                   if (pass == 0
8351                       && sec->has_tls_get_addr_call
8352                       && h != NULL
8353                       && (h == &htab->tls_get_addr->elf
8354                           || h == &htab->tls_get_addr_fd->elf)
8355                       && !found_tls_get_addr_arg
8356                       && is_branch_reloc (r_type))
8357                     {
8358                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8359                                                 "TLS optimization disabled\n"),
8360                                               ibfd, sec, rel->r_offset);
8361                       ret = TRUE;
8362                       goto err_free_rel;
8363                     }
8364
8365                   found_tls_get_addr_arg = 0;
8366                   switch (r_type)
8367                     {
8368                     case R_PPC64_GOT_TLSLD16:
8369                     case R_PPC64_GOT_TLSLD16_LO:
8370                       expecting_tls_get_addr = 1;
8371                       found_tls_get_addr_arg = 1;
8372                       /* Fall thru */
8373
8374                     case R_PPC64_GOT_TLSLD16_HI:
8375                     case R_PPC64_GOT_TLSLD16_HA:
8376                       /* These relocs should never be against a symbol
8377                          defined in a shared lib.  Leave them alone if
8378                          that turns out to be the case.  */
8379                       if (!is_local)
8380                         continue;
8381
8382                       /* LD -> LE */
8383                       tls_set = 0;
8384                       tls_clear = TLS_LD;
8385                       tls_type = TLS_TLS | TLS_LD;
8386                       break;
8387
8388                     case R_PPC64_GOT_TLSGD16:
8389                     case R_PPC64_GOT_TLSGD16_LO:
8390                       expecting_tls_get_addr = 1;
8391                       found_tls_get_addr_arg = 1;
8392                       /* Fall thru */
8393
8394                     case R_PPC64_GOT_TLSGD16_HI:
8395                     case R_PPC64_GOT_TLSGD16_HA:
8396                       if (ok_tprel)
8397                         /* GD -> LE */
8398                         tls_set = 0;
8399                       else
8400                         /* GD -> IE */
8401                         tls_set = TLS_TLS | TLS_TPRELGD;
8402                       tls_clear = TLS_GD;
8403                       tls_type = TLS_TLS | TLS_GD;
8404                       break;
8405
8406                     case R_PPC64_GOT_TPREL16_DS:
8407                     case R_PPC64_GOT_TPREL16_LO_DS:
8408                     case R_PPC64_GOT_TPREL16_HI:
8409                     case R_PPC64_GOT_TPREL16_HA:
8410                       if (ok_tprel)
8411                         {
8412                           /* IE -> LE */
8413                           tls_set = 0;
8414                           tls_clear = TLS_TPREL;
8415                           tls_type = TLS_TLS | TLS_TPREL;
8416                           break;
8417                         }
8418                       continue;
8419
8420                     case R_PPC64_TLSGD:
8421                     case R_PPC64_TLSLD:
8422                       found_tls_get_addr_arg = 1;
8423                       /* Fall thru */
8424
8425                     case R_PPC64_TLS:
8426                     case R_PPC64_TOC16:
8427                     case R_PPC64_TOC16_LO:
8428                       if (sym_sec == NULL || sym_sec != toc)
8429                         continue;
8430
8431                       /* Mark this toc entry as referenced by a TLS
8432                          code sequence.  We can do that now in the
8433                          case of R_PPC64_TLS, and after checking for
8434                          tls_get_addr for the TOC16 relocs.  */
8435                       if (toc_ref == NULL)
8436                         toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8437                       if (toc_ref == NULL)
8438                         goto err_free_rel;
8439
8440                       if (h != NULL)
8441                         value = h->root.u.def.value;
8442                       else
8443                         value = sym->st_value;
8444                       value += rel->r_addend;
8445                       if (value % 8 != 0)
8446                         continue;
8447                       BFD_ASSERT (value < toc->size
8448                                   && toc->output_offset % 8 == 0);
8449                       toc_ref_index = (value + toc->output_offset) / 8;
8450                       if (r_type == R_PPC64_TLS
8451                           || r_type == R_PPC64_TLSGD
8452                           || r_type == R_PPC64_TLSLD)
8453                         {
8454                           toc_ref[toc_ref_index] = 1;
8455                           continue;
8456                         }
8457
8458                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
8459                         continue;
8460
8461                       tls_set = 0;
8462                       tls_clear = 0;
8463                       expecting_tls_get_addr = 2;
8464                       break;
8465
8466                     case R_PPC64_TPREL64:
8467                       if (pass == 0
8468                           || sec != toc
8469                           || toc_ref == NULL
8470                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8471                         continue;
8472                       if (ok_tprel)
8473                         {
8474                           /* IE -> LE */
8475                           tls_set = TLS_EXPLICIT;
8476                           tls_clear = TLS_TPREL;
8477                           break;
8478                         }
8479                       continue;
8480
8481                     case R_PPC64_DTPMOD64:
8482                       if (pass == 0
8483                           || sec != toc
8484                           || toc_ref == NULL
8485                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8486                         continue;
8487                       if (rel + 1 < relend
8488                           && (rel[1].r_info
8489                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8490                           && rel[1].r_offset == rel->r_offset + 8)
8491                         {
8492                           if (ok_tprel)
8493                             /* GD -> LE */
8494                             tls_set = TLS_EXPLICIT | TLS_GD;
8495                           else
8496                             /* GD -> IE */
8497                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8498                           tls_clear = TLS_GD;
8499                         }
8500                       else
8501                         {
8502                           if (!is_local)
8503                             continue;
8504
8505                           /* LD -> LE */
8506                           tls_set = TLS_EXPLICIT;
8507                           tls_clear = TLS_LD;
8508                         }
8509                       break;
8510
8511                     default:
8512                       continue;
8513                     }
8514
8515                   if (pass == 0)
8516                     {
8517                       if (!expecting_tls_get_addr
8518                           || !sec->has_tls_get_addr_call)
8519                         continue;
8520
8521                       if (rel + 1 < relend
8522                           && branch_reloc_hash_match (ibfd, rel + 1,
8523                                                       htab->tls_get_addr,
8524                                                       htab->tls_get_addr_fd))
8525                         {
8526                           if (expecting_tls_get_addr == 2)
8527                             {
8528                               /* Check for toc tls entries.  */
8529                               unsigned char *toc_tls;
8530                               int retval;
8531
8532                               retval = get_tls_mask (&toc_tls, NULL, NULL,
8533                                                      &locsyms,
8534                                                      rel, ibfd);
8535                               if (retval == 0)
8536                                 goto err_free_rel;
8537                               if (toc_tls != NULL)
8538                                 {
8539                                   if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8540                                     found_tls_get_addr_arg = 1;
8541                                   if (retval > 1)
8542                                     toc_ref[toc_ref_index] = 1;
8543                                 }
8544                             }
8545                           continue;
8546                         }
8547
8548                       if (expecting_tls_get_addr != 1)
8549                         continue;
8550
8551                       /* Uh oh, we didn't find the expected call.  We
8552                          could just mark this symbol to exclude it
8553                          from tls optimization but it's safer to skip
8554                          the entire optimization.  */
8555                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8556                                                 "TLS optimization disabled\n"),
8557                                               ibfd, sec, rel->r_offset);
8558                       ret = TRUE;
8559                       goto err_free_rel;
8560                     }
8561
8562                   if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8563                     {
8564                       struct plt_entry *ent;
8565                       for (ent = htab->tls_get_addr->elf.plt.plist;
8566                            ent != NULL;
8567                            ent = ent->next)
8568                         if (ent->addend == 0)
8569                           {
8570                             if (ent->plt.refcount > 0)
8571                               {
8572                                 ent->plt.refcount -= 1;
8573                                 expecting_tls_get_addr = 0;
8574                               }
8575                             break;
8576                           }
8577                     }
8578
8579                   if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8580                     {
8581                       struct plt_entry *ent;
8582                       for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8583                            ent != NULL;
8584                            ent = ent->next)
8585                         if (ent->addend == 0)
8586                           {
8587                             if (ent->plt.refcount > 0)
8588                               ent->plt.refcount -= 1;
8589                             break;
8590                           }
8591                     }
8592
8593                   if (tls_clear == 0)
8594                     continue;
8595
8596                   if ((tls_set & TLS_EXPLICIT) == 0)
8597                     {
8598                       struct got_entry *ent;
8599
8600                       /* Adjust got entry for this reloc.  */
8601                       if (h != NULL)
8602                         ent = h->got.glist;
8603                       else
8604                         ent = elf_local_got_ents (ibfd)[r_symndx];
8605
8606                       for (; ent != NULL; ent = ent->next)
8607                         if (ent->addend == rel->r_addend
8608                             && ent->owner == ibfd
8609                             && ent->tls_type == tls_type)
8610                           break;
8611                       if (ent == NULL)
8612                         abort ();
8613
8614                       if (tls_set == 0)
8615                         {
8616                           /* We managed to get rid of a got entry.  */
8617                           if (ent->got.refcount > 0)
8618                             ent->got.refcount -= 1;
8619                         }
8620                     }
8621                   else
8622                     {
8623                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
8624                          we'll lose one or two dyn relocs.  */
8625                       if (!dec_dynrel_count (rel->r_info, sec, info,
8626                                              NULL, h, sym))
8627                         return FALSE;
8628
8629                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
8630                         {
8631                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8632                                                  NULL, h, sym))
8633                             return FALSE;
8634                         }
8635                     }
8636
8637                   *tls_mask |= tls_set;
8638                   *tls_mask &= ~tls_clear;
8639                 }
8640
8641               if (elf_section_data (sec)->relocs != relstart)
8642                 free (relstart);
8643             }
8644
8645         if (locsyms != NULL
8646             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8647           {
8648             if (!info->keep_memory)
8649               free (locsyms);
8650             else
8651               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8652           }
8653       }
8654
8655   if (toc_ref != NULL)
8656     free (toc_ref);
8657   return TRUE;
8658 }
8659
8660 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8661    the values of any global symbols in a toc section that has been
8662    edited.  Globals in toc sections should be a rarity, so this function
8663    sets a flag if any are found in toc sections other than the one just
8664    edited, so that futher hash table traversals can be avoided.  */
8665
8666 struct adjust_toc_info
8667 {
8668   asection *toc;
8669   unsigned long *skip;
8670   bfd_boolean global_toc_syms;
8671 };
8672
8673 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8674
8675 static bfd_boolean
8676 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8677 {
8678   struct ppc_link_hash_entry *eh;
8679   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8680   unsigned long i;
8681
8682   if (h->root.type != bfd_link_hash_defined
8683       && h->root.type != bfd_link_hash_defweak)
8684     return TRUE;
8685
8686   eh = (struct ppc_link_hash_entry *) h;
8687   if (eh->adjust_done)
8688     return TRUE;
8689
8690   if (eh->elf.root.u.def.section == toc_inf->toc)
8691     {
8692       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8693         i = toc_inf->toc->rawsize >> 3;
8694       else
8695         i = eh->elf.root.u.def.value >> 3;
8696
8697       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8698         {
8699           (*_bfd_error_handler)
8700             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8701           do
8702             ++i;
8703           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8704           eh->elf.root.u.def.value = (bfd_vma) i << 3;
8705         }
8706
8707       eh->elf.root.u.def.value -= toc_inf->skip[i];
8708       eh->adjust_done = 1;
8709     }
8710   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8711     toc_inf->global_toc_syms = TRUE;
8712
8713   return TRUE;
8714 }
8715
8716 /* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc.  */
8717
8718 static bfd_boolean
8719 ok_lo_toc_insn (unsigned int insn)
8720 {
8721   return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8722           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8723           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8724           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8725           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8726           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8727           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8728           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8729           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8730           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8731           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8732           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8733           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8734           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8735           || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8736               && (insn & 3) != 1)
8737           || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8738               && ((insn & 3) == 0 || (insn & 3) == 3))
8739           || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8740 }
8741
8742 /* Examine all relocs referencing .toc sections in order to remove
8743    unused .toc entries.  */
8744
8745 bfd_boolean
8746 ppc64_elf_edit_toc (struct bfd_link_info *info)
8747 {
8748   bfd *ibfd;
8749   struct adjust_toc_info toc_inf;
8750   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8751
8752   htab->do_toc_opt = 1;
8753   toc_inf.global_toc_syms = TRUE;
8754   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8755     {
8756       asection *toc, *sec;
8757       Elf_Internal_Shdr *symtab_hdr;
8758       Elf_Internal_Sym *local_syms;
8759       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8760       unsigned long *skip, *drop;
8761       unsigned char *used;
8762       unsigned char *keep, last, some_unused;
8763
8764       if (!is_ppc64_elf (ibfd))
8765         continue;
8766
8767       toc = bfd_get_section_by_name (ibfd, ".toc");
8768       if (toc == NULL
8769           || toc->size == 0
8770           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8771           || discarded_section (toc))
8772         continue;
8773
8774       toc_relocs = NULL;
8775       local_syms = NULL;
8776       symtab_hdr = &elf_symtab_hdr (ibfd);
8777
8778       /* Look at sections dropped from the final link.  */
8779       skip = NULL;
8780       relstart = NULL;
8781       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8782         {
8783           if (sec->reloc_count == 0
8784               || !discarded_section (sec)
8785               || get_opd_info (sec)
8786               || (sec->flags & SEC_ALLOC) == 0
8787               || (sec->flags & SEC_DEBUGGING) != 0)
8788             continue;
8789
8790           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8791           if (relstart == NULL)
8792             goto error_ret;
8793
8794           /* Run through the relocs to see which toc entries might be
8795              unused.  */
8796           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8797             {
8798               enum elf_ppc64_reloc_type r_type;
8799               unsigned long r_symndx;
8800               asection *sym_sec;
8801               struct elf_link_hash_entry *h;
8802               Elf_Internal_Sym *sym;
8803               bfd_vma val;
8804
8805               r_type = ELF64_R_TYPE (rel->r_info);
8806               switch (r_type)
8807                 {
8808                 default:
8809                   continue;
8810
8811                 case R_PPC64_TOC16:
8812                 case R_PPC64_TOC16_LO:
8813                 case R_PPC64_TOC16_HI:
8814                 case R_PPC64_TOC16_HA:
8815                 case R_PPC64_TOC16_DS:
8816                 case R_PPC64_TOC16_LO_DS:
8817                   break;
8818                 }
8819
8820               r_symndx = ELF64_R_SYM (rel->r_info);
8821               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8822                               r_symndx, ibfd))
8823                 goto error_ret;
8824
8825               if (sym_sec != toc)
8826                 continue;
8827
8828               if (h != NULL)
8829                 val = h->root.u.def.value;
8830               else
8831                 val = sym->st_value;
8832               val += rel->r_addend;
8833
8834               if (val >= toc->size)
8835                 continue;
8836
8837               /* Anything in the toc ought to be aligned to 8 bytes.
8838                  If not, don't mark as unused.  */
8839               if (val & 7)
8840                 continue;
8841
8842               if (skip == NULL)
8843                 {
8844                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8845                   if (skip == NULL)
8846                     goto error_ret;
8847                 }
8848
8849               skip[val >> 3] = ref_from_discarded;
8850             }
8851
8852           if (elf_section_data (sec)->relocs != relstart)
8853             free (relstart);
8854         }
8855
8856       /* For largetoc loads of address constants, we can convert
8857          .  addis rx,2,addr@got@ha
8858          .  ld ry,addr@got@l(rx)
8859          to
8860          .  addis rx,2,addr@toc@ha
8861          .  addi ry,rx,addr@toc@l
8862          when addr is within 2G of the toc pointer.  This then means
8863          that the word storing "addr" in the toc is no longer needed.  */
8864
8865       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8866           && toc->output_section->rawsize < (bfd_vma) 1 << 31
8867           && toc->reloc_count != 0)
8868         {
8869           /* Read toc relocs.  */
8870           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8871                                                   info->keep_memory);
8872           if (toc_relocs == NULL)
8873             goto error_ret;
8874
8875           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8876             {
8877               enum elf_ppc64_reloc_type r_type;
8878               unsigned long r_symndx;
8879               asection *sym_sec;
8880               struct elf_link_hash_entry *h;
8881               Elf_Internal_Sym *sym;
8882               bfd_vma val, addr;
8883
8884               r_type = ELF64_R_TYPE (rel->r_info);
8885               if (r_type != R_PPC64_ADDR64)
8886                 continue;
8887
8888               r_symndx = ELF64_R_SYM (rel->r_info);
8889               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8890                               r_symndx, ibfd))
8891                 goto error_ret;
8892
8893               if (sym_sec == NULL
8894                   || discarded_section (sym_sec))
8895                 continue;
8896
8897               if (!SYMBOL_REFERENCES_LOCAL (info, h))
8898                 continue;
8899
8900               if (h != NULL)
8901                 {
8902                   if (h->type == STT_GNU_IFUNC)
8903                     continue;
8904                   val = h->root.u.def.value;
8905                 }
8906               else
8907                 {
8908                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8909                     continue;
8910                   val = sym->st_value;
8911                 }
8912               val += rel->r_addend;
8913               val += sym_sec->output_section->vma + sym_sec->output_offset;
8914
8915               /* We don't yet know the exact toc pointer value, but we
8916                  know it will be somewhere in the toc section.  Don't
8917                  optimize if the difference from any possible toc
8918                  pointer is outside [ff..f80008000, 7fff7fff].  */
8919               addr = toc->output_section->vma + TOC_BASE_OFF;
8920               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8921                 continue;
8922
8923               addr = toc->output_section->vma + toc->output_section->rawsize;
8924               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8925                 continue;
8926
8927               if (skip == NULL)
8928                 {
8929                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8930                   if (skip == NULL)
8931                     goto error_ret;
8932                 }
8933
8934               skip[rel->r_offset >> 3]
8935                 |= can_optimize | ((rel - toc_relocs) << 2);
8936             }
8937         }
8938
8939       if (skip == NULL)
8940         continue;
8941
8942       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8943       if (used == NULL)
8944         {
8945         error_ret:
8946           if (local_syms != NULL
8947               && symtab_hdr->contents != (unsigned char *) local_syms)
8948             free (local_syms);
8949           if (sec != NULL
8950               && relstart != NULL
8951               && elf_section_data (sec)->relocs != relstart)
8952             free (relstart);
8953           if (toc_relocs != NULL
8954               && elf_section_data (toc)->relocs != toc_relocs)
8955             free (toc_relocs);
8956           if (skip != NULL)
8957             free (skip);
8958           return FALSE;
8959         }
8960
8961       /* Now check all kept sections that might reference the toc.
8962          Check the toc itself last.  */
8963       for (sec = (ibfd->sections == toc && toc->next ? toc->next
8964                   : ibfd->sections);
8965            sec != NULL;
8966            sec = (sec == toc ? NULL
8967                   : sec->next == NULL ? toc
8968                   : sec->next == toc && toc->next ? toc->next
8969                   : sec->next))
8970         {
8971           int repeat;
8972
8973           if (sec->reloc_count == 0
8974               || discarded_section (sec)
8975               || get_opd_info (sec)
8976               || (sec->flags & SEC_ALLOC) == 0
8977               || (sec->flags & SEC_DEBUGGING) != 0)
8978             continue;
8979
8980           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8981                                                 info->keep_memory);
8982           if (relstart == NULL)
8983             {
8984               free (used);
8985               goto error_ret;
8986             }
8987
8988           /* Mark toc entries referenced as used.  */
8989           do
8990             {
8991               repeat = 0;
8992               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8993                 {
8994                   enum elf_ppc64_reloc_type r_type;
8995                   unsigned long r_symndx;
8996                   asection *sym_sec;
8997                   struct elf_link_hash_entry *h;
8998                   Elf_Internal_Sym *sym;
8999                   bfd_vma val;
9000                   enum {no_check, check_lo, check_ha} insn_check;
9001
9002                   r_type = ELF64_R_TYPE (rel->r_info);
9003                   switch (r_type)
9004                     {
9005                     default:
9006                       insn_check = no_check;
9007                       break;
9008
9009                     case R_PPC64_GOT_TLSLD16_HA:
9010                     case R_PPC64_GOT_TLSGD16_HA:
9011                     case R_PPC64_GOT_TPREL16_HA:
9012                     case R_PPC64_GOT_DTPREL16_HA:
9013                     case R_PPC64_GOT16_HA:
9014                     case R_PPC64_TOC16_HA:
9015                       insn_check = check_ha;
9016                       break;
9017
9018                     case R_PPC64_GOT_TLSLD16_LO:
9019                     case R_PPC64_GOT_TLSGD16_LO:
9020                     case R_PPC64_GOT_TPREL16_LO_DS:
9021                     case R_PPC64_GOT_DTPREL16_LO_DS:
9022                     case R_PPC64_GOT16_LO:
9023                     case R_PPC64_GOT16_LO_DS:
9024                     case R_PPC64_TOC16_LO:
9025                     case R_PPC64_TOC16_LO_DS:
9026                       insn_check = check_lo;
9027                       break;
9028                     }
9029
9030                   if (insn_check != no_check)
9031                     {
9032                       bfd_vma off = rel->r_offset & ~3;
9033                       unsigned char buf[4];
9034                       unsigned int insn;
9035
9036                       if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9037                         {
9038                           free (used);
9039                           goto error_ret;
9040                         }
9041                       insn = bfd_get_32 (ibfd, buf);
9042                       if (insn_check == check_lo
9043                           ? !ok_lo_toc_insn (insn)
9044                           : ((insn & ((0x3f << 26) | 0x1f << 16))
9045                              != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9046                         {
9047                           char str[12];
9048
9049                           ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9050                           sprintf (str, "%#08x", insn);
9051                           info->callbacks->einfo
9052                             (_("%P: %H: toc optimization is not supported for"
9053                                " %s instruction.\n"),
9054                              ibfd, sec, rel->r_offset & ~3, str);
9055                         }
9056                     }
9057
9058                   switch (r_type)
9059                     {
9060                     case R_PPC64_TOC16:
9061                     case R_PPC64_TOC16_LO:
9062                     case R_PPC64_TOC16_HI:
9063                     case R_PPC64_TOC16_HA:
9064                     case R_PPC64_TOC16_DS:
9065                     case R_PPC64_TOC16_LO_DS:
9066                       /* In case we're taking addresses of toc entries.  */
9067                     case R_PPC64_ADDR64:
9068                       break;
9069
9070                     default:
9071                       continue;
9072                     }
9073
9074                   r_symndx = ELF64_R_SYM (rel->r_info);
9075                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9076                                   r_symndx, ibfd))
9077                     {
9078                       free (used);
9079                       goto error_ret;
9080                     }
9081
9082                   if (sym_sec != toc)
9083                     continue;
9084
9085                   if (h != NULL)
9086                     val = h->root.u.def.value;
9087                   else
9088                     val = sym->st_value;
9089                   val += rel->r_addend;
9090
9091                   if (val >= toc->size)
9092                     continue;
9093
9094                   if ((skip[val >> 3] & can_optimize) != 0)
9095                     {
9096                       bfd_vma off;
9097                       unsigned char opc;
9098
9099                       switch (r_type)
9100                         {
9101                         case R_PPC64_TOC16_HA:
9102                           break;
9103
9104                         case R_PPC64_TOC16_LO_DS:
9105                           off = rel->r_offset;
9106                           off += (bfd_big_endian (ibfd) ? -2 : 3);
9107                           if (!bfd_get_section_contents (ibfd, sec, &opc,
9108                                                          off, 1))
9109                             {
9110                               free (used);
9111                               goto error_ret;
9112                             }
9113                           if ((opc & (0x3f << 2)) == (58u << 2))
9114                             break;
9115                           /* Fall thru */
9116
9117                         default:
9118                           /* Wrong sort of reloc, or not a ld.  We may
9119                              as well clear ref_from_discarded too.  */
9120                           skip[val >> 3] = 0;
9121                         }
9122                     }
9123
9124                   if (sec != toc)
9125                     used[val >> 3] = 1;
9126                   /* For the toc section, we only mark as used if this
9127                      entry itself isn't unused.  */
9128                   else if ((used[rel->r_offset >> 3]
9129                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9130                            && !used[val >> 3])
9131                     {
9132                       /* Do all the relocs again, to catch reference
9133                          chains.  */
9134                       repeat = 1;
9135                       used[val >> 3] = 1;
9136                     }
9137                 }
9138             }
9139           while (repeat);
9140
9141           if (elf_section_data (sec)->relocs != relstart)
9142             free (relstart);
9143         }
9144
9145       /* Merge the used and skip arrays.  Assume that TOC
9146          doublewords not appearing as either used or unused belong
9147          to to an entry more than one doubleword in size.  */
9148       for (drop = skip, keep = used, last = 0, some_unused = 0;
9149            drop < skip + (toc->size + 7) / 8;
9150            ++drop, ++keep)
9151         {
9152           if (*keep)
9153             {
9154               *drop &= ~ref_from_discarded;
9155               if ((*drop & can_optimize) != 0)
9156                 some_unused = 1;
9157               last = 0;
9158             }
9159           else if ((*drop & ref_from_discarded) != 0)
9160             {
9161               some_unused = 1;
9162               last = ref_from_discarded;
9163             }
9164           else
9165             *drop = last;
9166         }
9167
9168       free (used);
9169
9170       if (some_unused)
9171         {
9172           bfd_byte *contents, *src;
9173           unsigned long off;
9174           Elf_Internal_Sym *sym;
9175           bfd_boolean local_toc_syms = FALSE;
9176
9177           /* Shuffle the toc contents, and at the same time convert the
9178              skip array from booleans into offsets.  */
9179           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9180             goto error_ret;
9181
9182           elf_section_data (toc)->this_hdr.contents = contents;
9183
9184           for (src = contents, off = 0, drop = skip;
9185                src < contents + toc->size;
9186                src += 8, ++drop)
9187             {
9188               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9189                 off += 8;
9190               else if (off != 0)
9191                 {
9192                   *drop = off;
9193                   memcpy (src - off, src, 8);
9194                 }
9195             }
9196           *drop = off;
9197           toc->rawsize = toc->size;
9198           toc->size = src - contents - off;
9199
9200           /* Adjust addends for relocs against the toc section sym,
9201              and optimize any accesses we can.  */
9202           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9203             {
9204               if (sec->reloc_count == 0
9205                   || discarded_section (sec))
9206                 continue;
9207
9208               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9209                                                     info->keep_memory);
9210               if (relstart == NULL)
9211                 goto error_ret;
9212
9213               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9214                 {
9215                   enum elf_ppc64_reloc_type r_type;
9216                   unsigned long r_symndx;
9217                   asection *sym_sec;
9218                   struct elf_link_hash_entry *h;
9219                   bfd_vma val;
9220
9221                   r_type = ELF64_R_TYPE (rel->r_info);
9222                   switch (r_type)
9223                     {
9224                     default:
9225                       continue;
9226
9227                     case R_PPC64_TOC16:
9228                     case R_PPC64_TOC16_LO:
9229                     case R_PPC64_TOC16_HI:
9230                     case R_PPC64_TOC16_HA:
9231                     case R_PPC64_TOC16_DS:
9232                     case R_PPC64_TOC16_LO_DS:
9233                     case R_PPC64_ADDR64:
9234                       break;
9235                     }
9236
9237                   r_symndx = ELF64_R_SYM (rel->r_info);
9238                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9239                                   r_symndx, ibfd))
9240                     goto error_ret;
9241
9242                   if (sym_sec != toc)
9243                     continue;
9244
9245                   if (h != NULL)
9246                     val = h->root.u.def.value;
9247                   else
9248                     {
9249                       val = sym->st_value;
9250                       if (val != 0)
9251                         local_toc_syms = TRUE;
9252                     }
9253
9254                   val += rel->r_addend;
9255
9256                   if (val > toc->rawsize)
9257                     val = toc->rawsize;
9258                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
9259                     continue;
9260                   else if ((skip[val >> 3] & can_optimize) != 0)
9261                     {
9262                       Elf_Internal_Rela *tocrel
9263                         = toc_relocs + (skip[val >> 3] >> 2);
9264                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9265
9266                       switch (r_type)
9267                         {
9268                         case R_PPC64_TOC16_HA:
9269                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9270                           break;
9271
9272                         case R_PPC64_TOC16_LO_DS:
9273                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9274                           break;
9275
9276                         default:
9277                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9278                             ppc_howto_init ();
9279                           info->callbacks->einfo
9280                             (_("%P: %H: %s references "
9281                                "optimized away TOC entry\n"),
9282                              ibfd, sec, rel->r_offset,
9283                              ppc64_elf_howto_table[r_type]->name);
9284                           bfd_set_error (bfd_error_bad_value);
9285                           goto error_ret;
9286                         }
9287                       rel->r_addend = tocrel->r_addend;
9288                       elf_section_data (sec)->relocs = relstart;
9289                       continue;
9290                     }
9291
9292                   if (h != NULL || sym->st_value != 0)
9293                     continue;
9294
9295                   rel->r_addend -= skip[val >> 3];
9296                   elf_section_data (sec)->relocs = relstart;
9297                 }
9298
9299               if (elf_section_data (sec)->relocs != relstart)
9300                 free (relstart);
9301             }
9302
9303           /* We shouldn't have local or global symbols defined in the TOC,
9304              but handle them anyway.  */
9305           if (local_syms != NULL)
9306             for (sym = local_syms;
9307                  sym < local_syms + symtab_hdr->sh_info;
9308                  ++sym)
9309               if (sym->st_value != 0
9310                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9311                 {
9312                   unsigned long i;
9313
9314                   if (sym->st_value > toc->rawsize)
9315                     i = toc->rawsize >> 3;
9316                   else
9317                     i = sym->st_value >> 3;
9318
9319                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9320                     {
9321                       if (local_toc_syms)
9322                         (*_bfd_error_handler)
9323                           (_("%s defined on removed toc entry"),
9324                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9325                       do
9326                         ++i;
9327                       while ((skip[i] & (ref_from_discarded | can_optimize)));
9328                       sym->st_value = (bfd_vma) i << 3;
9329                     }
9330
9331                   sym->st_value -= skip[i];
9332                   symtab_hdr->contents = (unsigned char *) local_syms;
9333                 }
9334
9335           /* Adjust any global syms defined in this toc input section.  */
9336           if (toc_inf.global_toc_syms)
9337             {
9338               toc_inf.toc = toc;
9339               toc_inf.skip = skip;
9340               toc_inf.global_toc_syms = FALSE;
9341               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9342                                       &toc_inf);
9343             }
9344
9345           if (toc->reloc_count != 0)
9346             {
9347               Elf_Internal_Shdr *rel_hdr;
9348               Elf_Internal_Rela *wrel;
9349               bfd_size_type sz;
9350
9351               /* Remove unused toc relocs, and adjust those we keep.  */
9352               if (toc_relocs == NULL)
9353                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9354                                                         info->keep_memory);
9355               if (toc_relocs == NULL)
9356                 goto error_ret;
9357
9358               wrel = toc_relocs;
9359               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9360                 if ((skip[rel->r_offset >> 3]
9361                      & (ref_from_discarded | can_optimize)) == 0)
9362                   {
9363                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9364                     wrel->r_info = rel->r_info;
9365                     wrel->r_addend = rel->r_addend;
9366                     ++wrel;
9367                   }
9368                 else if (!dec_dynrel_count (rel->r_info, toc, info,
9369                                             &local_syms, NULL, NULL))
9370                   goto error_ret;
9371
9372               elf_section_data (toc)->relocs = toc_relocs;
9373               toc->reloc_count = wrel - toc_relocs;
9374               rel_hdr = _bfd_elf_single_rel_hdr (toc);
9375               sz = rel_hdr->sh_entsize;
9376               rel_hdr->sh_size = toc->reloc_count * sz;
9377             }
9378         }
9379       else if (toc_relocs != NULL
9380                && elf_section_data (toc)->relocs != toc_relocs)
9381         free (toc_relocs);
9382
9383       if (local_syms != NULL
9384           && symtab_hdr->contents != (unsigned char *) local_syms)
9385         {
9386           if (!info->keep_memory)
9387             free (local_syms);
9388           else
9389             symtab_hdr->contents = (unsigned char *) local_syms;
9390         }
9391       free (skip);
9392     }
9393
9394   return TRUE;
9395 }
9396
9397 /* Return true iff input section I references the TOC using
9398    instructions limited to +/-32k offsets.  */
9399
9400 bfd_boolean
9401 ppc64_elf_has_small_toc_reloc (asection *i)
9402 {
9403   return (is_ppc64_elf (i->owner)
9404           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9405 }
9406
9407 /* Allocate space for one GOT entry.  */
9408
9409 static void
9410 allocate_got (struct elf_link_hash_entry *h,
9411               struct bfd_link_info *info,
9412               struct got_entry *gent)
9413 {
9414   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9415   bfd_boolean dyn;
9416   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9417   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9418                  ? 16 : 8);
9419   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9420                   ? 2 : 1) * sizeof (Elf64_External_Rela);
9421   asection *got = ppc64_elf_tdata (gent->owner)->got;
9422
9423   gent->got.offset = got->size;
9424   got->size += entsize;
9425
9426   dyn = htab->elf.dynamic_sections_created;
9427   if (h->type == STT_GNU_IFUNC)
9428     {
9429       htab->elf.irelplt->size += rentsize;
9430       htab->got_reli_size += rentsize;
9431     }
9432   else if ((bfd_link_pic (info)
9433             || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9434            && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9435                || h->root.type != bfd_link_hash_undefweak))
9436     {
9437       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9438       relgot->size += rentsize;
9439     }
9440 }
9441
9442 /* This function merges got entries in the same toc group.  */
9443
9444 static void
9445 merge_got_entries (struct got_entry **pent)
9446 {
9447   struct got_entry *ent, *ent2;
9448
9449   for (ent = *pent; ent != NULL; ent = ent->next)
9450     if (!ent->is_indirect)
9451       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9452         if (!ent2->is_indirect
9453             && ent2->addend == ent->addend
9454             && ent2->tls_type == ent->tls_type
9455             && elf_gp (ent2->owner) == elf_gp (ent->owner))
9456           {
9457             ent2->is_indirect = TRUE;
9458             ent2->got.ent = ent;
9459           }
9460 }
9461
9462 /* Allocate space in .plt, .got and associated reloc sections for
9463    dynamic relocs.  */
9464
9465 static bfd_boolean
9466 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9467 {
9468   struct bfd_link_info *info;
9469   struct ppc_link_hash_table *htab;
9470   asection *s;
9471   struct ppc_link_hash_entry *eh;
9472   struct elf_dyn_relocs *p;
9473   struct got_entry **pgent, *gent;
9474
9475   if (h->root.type == bfd_link_hash_indirect)
9476     return TRUE;
9477
9478   info = (struct bfd_link_info *) inf;
9479   htab = ppc_hash_table (info);
9480   if (htab == NULL)
9481     return FALSE;
9482
9483   if ((htab->elf.dynamic_sections_created
9484        && h->dynindx != -1
9485        && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
9486       || h->type == STT_GNU_IFUNC)
9487     {
9488       struct plt_entry *pent;
9489       bfd_boolean doneone = FALSE;
9490       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9491         if (pent->plt.refcount > 0)
9492           {
9493             if (!htab->elf.dynamic_sections_created
9494                 || h->dynindx == -1)
9495               {
9496                 s = htab->elf.iplt;
9497                 pent->plt.offset = s->size;
9498                 s->size += PLT_ENTRY_SIZE (htab);
9499                 s = htab->elf.irelplt;
9500               }
9501             else
9502               {
9503                 /* If this is the first .plt entry, make room for the special
9504                    first entry.  */
9505                 s = htab->elf.splt;
9506                 if (s->size == 0)
9507                   s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9508
9509                 pent->plt.offset = s->size;
9510
9511                 /* Make room for this entry.  */
9512                 s->size += PLT_ENTRY_SIZE (htab);
9513
9514                 /* Make room for the .glink code.  */
9515                 s = htab->glink;
9516                 if (s->size == 0)
9517                   s->size += GLINK_CALL_STUB_SIZE;
9518                 if (htab->opd_abi)
9519                   {
9520                     /* We need bigger stubs past index 32767.  */
9521                     if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9522                       s->size += 4;
9523                     s->size += 2*4;
9524                   }
9525                 else
9526                   s->size += 4;
9527
9528                 /* We also need to make an entry in the .rela.plt section.  */
9529                 s = htab->elf.srelplt;
9530               }
9531             s->size += sizeof (Elf64_External_Rela);
9532             doneone = TRUE;
9533           }
9534         else
9535           pent->plt.offset = (bfd_vma) -1;
9536       if (!doneone)
9537         {
9538           h->plt.plist = NULL;
9539           h->needs_plt = 0;
9540         }
9541     }
9542   else
9543     {
9544       h->plt.plist = NULL;
9545       h->needs_plt = 0;
9546     }
9547
9548   eh = (struct ppc_link_hash_entry *) h;
9549   /* Run through the TLS GD got entries first if we're changing them
9550      to TPREL.  */
9551   if ((eh->tls_mask & TLS_TPRELGD) != 0)
9552     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9553       if (gent->got.refcount > 0
9554           && (gent->tls_type & TLS_GD) != 0)
9555         {
9556           /* This was a GD entry that has been converted to TPREL.  If
9557              there happens to be a TPREL entry we can use that one.  */
9558           struct got_entry *ent;
9559           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9560             if (ent->got.refcount > 0
9561                 && (ent->tls_type & TLS_TPREL) != 0
9562                 && ent->addend == gent->addend
9563                 && ent->owner == gent->owner)
9564               {
9565                 gent->got.refcount = 0;
9566                 break;
9567               }
9568
9569           /* If not, then we'll be using our own TPREL entry.  */
9570           if (gent->got.refcount != 0)
9571             gent->tls_type = TLS_TLS | TLS_TPREL;
9572         }
9573
9574   /* Remove any list entry that won't generate a word in the GOT before
9575      we call merge_got_entries.  Otherwise we risk merging to empty
9576      entries.  */
9577   pgent = &h->got.glist;
9578   while ((gent = *pgent) != NULL)
9579     if (gent->got.refcount > 0)
9580       {
9581         if ((gent->tls_type & TLS_LD) != 0
9582             && !h->def_dynamic)
9583           {
9584             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9585             *pgent = gent->next;
9586           }
9587         else
9588           pgent = &gent->next;
9589       }
9590     else
9591       *pgent = gent->next;
9592
9593   if (!htab->do_multi_toc)
9594     merge_got_entries (&h->got.glist);
9595
9596   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9597     if (!gent->is_indirect)
9598       {
9599         /* Make sure this symbol is output as a dynamic symbol.
9600            Undefined weak syms won't yet be marked as dynamic,
9601            nor will all TLS symbols.  */
9602         if (h->dynindx == -1
9603             && !h->forced_local
9604             && h->type != STT_GNU_IFUNC
9605             && htab->elf.dynamic_sections_created)
9606           {
9607             if (! bfd_elf_link_record_dynamic_symbol (info, h))
9608               return FALSE;
9609           }
9610
9611         if (!is_ppc64_elf (gent->owner))
9612           abort ();
9613
9614         allocate_got (h, info, gent);
9615       }
9616
9617   if (eh->dyn_relocs == NULL
9618       || (!htab->elf.dynamic_sections_created
9619           && h->type != STT_GNU_IFUNC))
9620     return TRUE;
9621
9622   /* In the shared -Bsymbolic case, discard space allocated for
9623      dynamic pc-relative relocs against symbols which turn out to be
9624      defined in regular objects.  For the normal shared case, discard
9625      space for relocs that have become local due to symbol visibility
9626      changes.  */
9627
9628   if (bfd_link_pic (info))
9629     {
9630       /* Relocs that use pc_count are those that appear on a call insn,
9631          or certain REL relocs (see must_be_dyn_reloc) that can be
9632          generated via assembly.  We want calls to protected symbols to
9633          resolve directly to the function rather than going via the plt.
9634          If people want function pointer comparisons to work as expected
9635          then they should avoid writing weird assembly.  */
9636       if (SYMBOL_CALLS_LOCAL (info, h))
9637         {
9638           struct elf_dyn_relocs **pp;
9639
9640           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9641             {
9642               p->count -= p->pc_count;
9643               p->pc_count = 0;
9644               if (p->count == 0)
9645                 *pp = p->next;
9646               else
9647                 pp = &p->next;
9648             }
9649         }
9650
9651       /* Also discard relocs on undefined weak syms with non-default
9652          visibility.  */
9653       if (eh->dyn_relocs != NULL
9654           && h->root.type == bfd_link_hash_undefweak)
9655         {
9656           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9657             eh->dyn_relocs = NULL;
9658
9659           /* Make sure this symbol is output as a dynamic symbol.
9660              Undefined weak syms won't yet be marked as dynamic.  */
9661           else if (h->dynindx == -1
9662                    && !h->forced_local)
9663             {
9664               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9665                 return FALSE;
9666             }
9667         }
9668     }
9669   else if (h->type == STT_GNU_IFUNC)
9670     {
9671       if (!h->non_got_ref)
9672         eh->dyn_relocs = NULL;
9673     }
9674   else if (ELIMINATE_COPY_RELOCS)
9675     {
9676       /* For the non-shared case, discard space for relocs against
9677          symbols which turn out to need copy relocs or are not
9678          dynamic.  */
9679
9680       if (!h->non_got_ref
9681           && !h->def_regular)
9682         {
9683           /* Make sure this symbol is output as a dynamic symbol.
9684              Undefined weak syms won't yet be marked as dynamic.  */
9685           if (h->dynindx == -1
9686               && !h->forced_local)
9687             {
9688               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9689                 return FALSE;
9690             }
9691
9692           /* If that succeeded, we know we'll be keeping all the
9693              relocs.  */
9694           if (h->dynindx != -1)
9695             goto keep;
9696         }
9697
9698       eh->dyn_relocs = NULL;
9699
9700     keep: ;
9701     }
9702
9703   /* Finally, allocate space.  */
9704   for (p = eh->dyn_relocs; p != NULL; p = p->next)
9705     {
9706       asection *sreloc = elf_section_data (p->sec)->sreloc;
9707       if (eh->elf.type == STT_GNU_IFUNC)
9708         sreloc = htab->elf.irelplt;
9709       sreloc->size += p->count * sizeof (Elf64_External_Rela);
9710     }
9711
9712   return TRUE;
9713 }
9714
9715 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9716    to set up space for global entry stubs.  These are put in glink,
9717    after the branch table.  */
9718
9719 static bfd_boolean
9720 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9721 {
9722   struct bfd_link_info *info;
9723   struct ppc_link_hash_table *htab;
9724   struct plt_entry *pent;
9725   asection *s;
9726
9727   if (h->root.type == bfd_link_hash_indirect)
9728     return TRUE;
9729
9730   if (!h->pointer_equality_needed)
9731     return TRUE;
9732
9733   if (h->def_regular)
9734     return TRUE;
9735
9736   info = inf;
9737   htab = ppc_hash_table (info);
9738   if (htab == NULL)
9739     return FALSE;
9740
9741   s = htab->glink;
9742   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9743     if (pent->plt.offset != (bfd_vma) -1
9744         && pent->addend == 0)
9745       {
9746         /* For ELFv2, if this symbol is not defined in a regular file
9747            and we are not generating a shared library or pie, then we
9748            need to define the symbol in the executable on a call stub.
9749            This is to avoid text relocations.  */
9750         s->size = (s->size + 15) & -16;
9751         h->root.u.def.section = s;
9752         h->root.u.def.value = s->size;
9753         s->size += 16;
9754         break;
9755       }
9756   return TRUE;
9757 }
9758
9759 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9760    read-only sections.  */
9761
9762 static bfd_boolean
9763 maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
9764 {
9765   if (h->root.type == bfd_link_hash_indirect)
9766     return TRUE;
9767
9768   if (readonly_dynrelocs (h))
9769     {
9770       ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
9771
9772       /* Not an error, just cut short the traversal.  */
9773       return FALSE;
9774     }
9775   return TRUE;
9776 }
9777
9778 /* Set the sizes of the dynamic sections.  */
9779
9780 static bfd_boolean
9781 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9782                                  struct bfd_link_info *info)
9783 {
9784   struct ppc_link_hash_table *htab;
9785   bfd *dynobj;
9786   asection *s;
9787   bfd_boolean relocs;
9788   bfd *ibfd;
9789   struct got_entry *first_tlsld;
9790
9791   htab = ppc_hash_table (info);
9792   if (htab == NULL)
9793     return FALSE;
9794
9795   dynobj = htab->elf.dynobj;
9796   if (dynobj == NULL)
9797     abort ();
9798
9799   if (htab->elf.dynamic_sections_created)
9800     {
9801       /* Set the contents of the .interp section to the interpreter.  */
9802       if (bfd_link_executable (info))
9803         {
9804           s = bfd_get_linker_section (dynobj, ".interp");
9805           if (s == NULL)
9806             abort ();
9807           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9808           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9809         }
9810     }
9811
9812   /* Set up .got offsets for local syms, and space for local dynamic
9813      relocs.  */
9814   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9815     {
9816       struct got_entry **lgot_ents;
9817       struct got_entry **end_lgot_ents;
9818       struct plt_entry **local_plt;
9819       struct plt_entry **end_local_plt;
9820       unsigned char *lgot_masks;
9821       bfd_size_type locsymcount;
9822       Elf_Internal_Shdr *symtab_hdr;
9823
9824       if (!is_ppc64_elf (ibfd))
9825         continue;
9826
9827       for (s = ibfd->sections; s != NULL; s = s->next)
9828         {
9829           struct ppc_dyn_relocs *p;
9830
9831           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9832             {
9833               if (!bfd_is_abs_section (p->sec)
9834                   && bfd_is_abs_section (p->sec->output_section))
9835                 {
9836                   /* Input section has been discarded, either because
9837                      it is a copy of a linkonce section or due to
9838                      linker script /DISCARD/, so we'll be discarding
9839                      the relocs too.  */
9840                 }
9841               else if (p->count != 0)
9842                 {
9843                   asection *srel = elf_section_data (p->sec)->sreloc;
9844                   if (p->ifunc)
9845                     srel = htab->elf.irelplt;
9846                   srel->size += p->count * sizeof (Elf64_External_Rela);
9847                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9848                     info->flags |= DF_TEXTREL;
9849                 }
9850             }
9851         }
9852
9853       lgot_ents = elf_local_got_ents (ibfd);
9854       if (!lgot_ents)
9855         continue;
9856
9857       symtab_hdr = &elf_symtab_hdr (ibfd);
9858       locsymcount = symtab_hdr->sh_info;
9859       end_lgot_ents = lgot_ents + locsymcount;
9860       local_plt = (struct plt_entry **) end_lgot_ents;
9861       end_local_plt = local_plt + locsymcount;
9862       lgot_masks = (unsigned char *) end_local_plt;
9863       s = ppc64_elf_tdata (ibfd)->got;
9864       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9865         {
9866           struct got_entry **pent, *ent;
9867
9868           pent = lgot_ents;
9869           while ((ent = *pent) != NULL)
9870             if (ent->got.refcount > 0)
9871               {
9872                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9873                   {
9874                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
9875                     *pent = ent->next;
9876                   }
9877                 else
9878                   {
9879                     unsigned int ent_size = 8;
9880                     unsigned int rel_size = sizeof (Elf64_External_Rela);
9881
9882                     ent->got.offset = s->size;
9883                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9884                       {
9885                         ent_size *= 2;
9886                         rel_size *= 2;
9887                       }
9888                     s->size += ent_size;
9889                     if ((*lgot_masks & PLT_IFUNC) != 0)
9890                       {
9891                         htab->elf.irelplt->size += rel_size;
9892                         htab->got_reli_size += rel_size;
9893                       }
9894                     else if (bfd_link_pic (info))
9895                       {
9896                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9897                         srel->size += rel_size;
9898                       }
9899                     pent = &ent->next;
9900                   }
9901               }
9902             else
9903               *pent = ent->next;
9904         }
9905
9906       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
9907       for (; local_plt < end_local_plt; ++local_plt)
9908         {
9909           struct plt_entry *ent;
9910
9911           for (ent = *local_plt; ent != NULL; ent = ent->next)
9912             if (ent->plt.refcount > 0)
9913               {
9914                 s = htab->elf.iplt;
9915                 ent->plt.offset = s->size;
9916                 s->size += PLT_ENTRY_SIZE (htab);
9917
9918                 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9919               }
9920             else
9921               ent->plt.offset = (bfd_vma) -1;
9922         }
9923     }
9924
9925   /* Allocate global sym .plt and .got entries, and space for global
9926      sym dynamic relocs.  */
9927   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9928   /* Stash the end of glink branch table.  */
9929   if (htab->glink != NULL)
9930     htab->glink->rawsize = htab->glink->size;
9931
9932   if (!htab->opd_abi && !bfd_link_pic (info))
9933     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
9934
9935   first_tlsld = NULL;
9936   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9937     {
9938       struct got_entry *ent;
9939
9940       if (!is_ppc64_elf (ibfd))
9941         continue;
9942
9943       ent = ppc64_tlsld_got (ibfd);
9944       if (ent->got.refcount > 0)
9945         {
9946           if (!htab->do_multi_toc && first_tlsld != NULL)
9947             {
9948               ent->is_indirect = TRUE;
9949               ent->got.ent = first_tlsld;
9950             }
9951           else
9952             {
9953               if (first_tlsld == NULL)
9954                 first_tlsld = ent;
9955               s = ppc64_elf_tdata (ibfd)->got;
9956               ent->got.offset = s->size;
9957               ent->owner = ibfd;
9958               s->size += 16;
9959               if (bfd_link_pic (info))
9960                 {
9961                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9962                   srel->size += sizeof (Elf64_External_Rela);
9963                 }
9964             }
9965         }
9966       else
9967         ent->got.offset = (bfd_vma) -1;
9968     }
9969
9970   /* We now have determined the sizes of the various dynamic sections.
9971      Allocate memory for them.  */
9972   relocs = FALSE;
9973   for (s = dynobj->sections; s != NULL; s = s->next)
9974     {
9975       if ((s->flags & SEC_LINKER_CREATED) == 0)
9976         continue;
9977
9978       if (s == htab->brlt || s == htab->relbrlt)
9979         /* These haven't been allocated yet;  don't strip.  */
9980         continue;
9981       else if (s == htab->elf.sgot
9982                || s == htab->elf.splt
9983                || s == htab->elf.iplt
9984                || s == htab->glink
9985                || s == htab->dynbss)
9986         {
9987           /* Strip this section if we don't need it; see the
9988              comment below.  */
9989         }
9990       else if (s == htab->glink_eh_frame)
9991         {
9992           if (!bfd_is_abs_section (s->output_section))
9993             /* Not sized yet.  */
9994             continue;
9995         }
9996       else if (CONST_STRNEQ (s->name, ".rela"))
9997         {
9998           if (s->size != 0)
9999             {
10000               if (s != htab->elf.srelplt)
10001                 relocs = TRUE;
10002
10003               /* We use the reloc_count field as a counter if we need
10004                  to copy relocs into the output file.  */
10005               s->reloc_count = 0;
10006             }
10007         }
10008       else
10009         {
10010           /* It's not one of our sections, so don't allocate space.  */
10011           continue;
10012         }
10013
10014       if (s->size == 0)
10015         {
10016           /* If we don't need this section, strip it from the
10017              output file.  This is mostly to handle .rela.bss and
10018              .rela.plt.  We must create both sections in
10019              create_dynamic_sections, because they must be created
10020              before the linker maps input sections to output
10021              sections.  The linker does that before
10022              adjust_dynamic_symbol is called, and it is that
10023              function which decides whether anything needs to go
10024              into these sections.  */
10025           s->flags |= SEC_EXCLUDE;
10026           continue;
10027         }
10028
10029       if ((s->flags & SEC_HAS_CONTENTS) == 0)
10030         continue;
10031
10032       /* Allocate memory for the section contents.  We use bfd_zalloc
10033          here in case unused entries are not reclaimed before the
10034          section's contents are written out.  This should not happen,
10035          but this way if it does we get a R_PPC64_NONE reloc in .rela
10036          sections instead of garbage.
10037          We also rely on the section contents being zero when writing
10038          the GOT.  */
10039       s->contents = bfd_zalloc (dynobj, s->size);
10040       if (s->contents == NULL)
10041         return FALSE;
10042     }
10043
10044   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10045     {
10046       if (!is_ppc64_elf (ibfd))
10047         continue;
10048
10049       s = ppc64_elf_tdata (ibfd)->got;
10050       if (s != NULL && s != htab->elf.sgot)
10051         {
10052           if (s->size == 0)
10053             s->flags |= SEC_EXCLUDE;
10054           else
10055             {
10056               s->contents = bfd_zalloc (ibfd, s->size);
10057               if (s->contents == NULL)
10058                 return FALSE;
10059             }
10060         }
10061       s = ppc64_elf_tdata (ibfd)->relgot;
10062       if (s != NULL)
10063         {
10064           if (s->size == 0)
10065             s->flags |= SEC_EXCLUDE;
10066           else
10067             {
10068               s->contents = bfd_zalloc (ibfd, s->size);
10069               if (s->contents == NULL)
10070                 return FALSE;
10071               relocs = TRUE;
10072               s->reloc_count = 0;
10073             }
10074         }
10075     }
10076
10077   if (htab->elf.dynamic_sections_created)
10078     {
10079       bfd_boolean tls_opt;
10080
10081       /* Add some entries to the .dynamic section.  We fill in the
10082          values later, in ppc64_elf_finish_dynamic_sections, but we
10083          must add the entries now so that we get the correct size for
10084          the .dynamic section.  The DT_DEBUG entry is filled in by the
10085          dynamic linker and used by the debugger.  */
10086 #define add_dynamic_entry(TAG, VAL) \
10087   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10088
10089       if (bfd_link_executable (info))
10090         {
10091           if (!add_dynamic_entry (DT_DEBUG, 0))
10092             return FALSE;
10093         }
10094
10095       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10096         {
10097           if (!add_dynamic_entry (DT_PLTGOT, 0)
10098               || !add_dynamic_entry (DT_PLTRELSZ, 0)
10099               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10100               || !add_dynamic_entry (DT_JMPREL, 0)
10101               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10102             return FALSE;
10103         }
10104
10105       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10106         {
10107           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10108               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10109             return FALSE;
10110         }
10111
10112       tls_opt = (!htab->params->no_tls_get_addr_opt
10113                  && htab->tls_get_addr_fd != NULL
10114                  && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10115       if (tls_opt || !htab->opd_abi)
10116         {
10117           if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10118             return FALSE;
10119         }
10120
10121       if (relocs)
10122         {
10123           if (!add_dynamic_entry (DT_RELA, 0)
10124               || !add_dynamic_entry (DT_RELASZ, 0)
10125               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10126             return FALSE;
10127
10128           /* If any dynamic relocs apply to a read-only section,
10129              then we need a DT_TEXTREL entry.  */
10130           if ((info->flags & DF_TEXTREL) == 0)
10131             elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10132
10133           if ((info->flags & DF_TEXTREL) != 0)
10134             {
10135               if (!add_dynamic_entry (DT_TEXTREL, 0))
10136                 return FALSE;
10137             }
10138         }
10139     }
10140 #undef add_dynamic_entry
10141
10142   return TRUE;
10143 }
10144
10145 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
10146
10147 static bfd_boolean
10148 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10149 {
10150   if (h->plt.plist != NULL
10151       && !h->def_regular
10152       && !h->pointer_equality_needed)
10153     return FALSE;
10154
10155   return _bfd_elf_hash_symbol (h);
10156 }
10157
10158 /* Determine the type of stub needed, if any, for a call.  */
10159
10160 static inline enum ppc_stub_type
10161 ppc_type_of_stub (asection *input_sec,
10162                   const Elf_Internal_Rela *rel,
10163                   struct ppc_link_hash_entry **hash,
10164                   struct plt_entry **plt_ent,
10165                   bfd_vma destination,
10166                   unsigned long local_off)
10167 {
10168   struct ppc_link_hash_entry *h = *hash;
10169   bfd_vma location;
10170   bfd_vma branch_offset;
10171   bfd_vma max_branch_offset;
10172   enum elf_ppc64_reloc_type r_type;
10173
10174   if (h != NULL)
10175     {
10176       struct plt_entry *ent;
10177       struct ppc_link_hash_entry *fdh = h;
10178       if (h->oh != NULL
10179           && h->oh->is_func_descriptor)
10180         {
10181           fdh = ppc_follow_link (h->oh);
10182           *hash = fdh;
10183         }
10184
10185       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10186         if (ent->addend == rel->r_addend
10187             && ent->plt.offset != (bfd_vma) -1)
10188           {
10189             *plt_ent = ent;
10190             return ppc_stub_plt_call;
10191           }
10192
10193       /* Here, we know we don't have a plt entry.  If we don't have a
10194          either a defined function descriptor or a defined entry symbol
10195          in a regular object file, then it is pointless trying to make
10196          any other type of stub.  */
10197       if (!is_static_defined (&fdh->elf)
10198           && !is_static_defined (&h->elf))
10199         return ppc_stub_none;
10200     }
10201   else if (elf_local_got_ents (input_sec->owner) != NULL)
10202     {
10203       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10204       struct plt_entry **local_plt = (struct plt_entry **)
10205         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10206       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10207
10208       if (local_plt[r_symndx] != NULL)
10209         {
10210           struct plt_entry *ent;
10211
10212           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10213             if (ent->addend == rel->r_addend
10214                 && ent->plt.offset != (bfd_vma) -1)
10215               {
10216                 *plt_ent = ent;
10217                 return ppc_stub_plt_call;
10218               }
10219         }
10220     }
10221
10222   /* Determine where the call point is.  */
10223   location = (input_sec->output_offset
10224               + input_sec->output_section->vma
10225               + rel->r_offset);
10226
10227   branch_offset = destination - location;
10228   r_type = ELF64_R_TYPE (rel->r_info);
10229
10230   /* Determine if a long branch stub is needed.  */
10231   max_branch_offset = 1 << 25;
10232   if (r_type != R_PPC64_REL24)
10233     max_branch_offset = 1 << 15;
10234
10235   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10236     /* We need a stub.  Figure out whether a long_branch or plt_branch
10237        is needed later.  */
10238     return ppc_stub_long_branch;
10239
10240   return ppc_stub_none;
10241 }
10242
10243 /* With power7 weakly ordered memory model, it is possible for ld.so
10244    to update a plt entry in one thread and have another thread see a
10245    stale zero toc entry.  To avoid this we need some sort of acquire
10246    barrier in the call stub.  One solution is to make the load of the
10247    toc word seem to appear to depend on the load of the function entry
10248    word.  Another solution is to test for r2 being zero, and branch to
10249    the appropriate glink entry if so.
10250
10251    .    fake dep barrier        compare
10252    .    ld 12,xxx(2)            ld 12,xxx(2)
10253    .    mtctr 12                mtctr 12
10254    .    xor 11,12,12            ld 2,xxx+8(2)
10255    .    add 2,2,11              cmpldi 2,0
10256    .    ld 2,xxx+8(2)           bnectr+
10257    .    bctr                    b <glink_entry>
10258
10259    The solution involving the compare turns out to be faster, so
10260    that's what we use unless the branch won't reach.  */
10261
10262 #define ALWAYS_USE_FAKE_DEP 0
10263 #define ALWAYS_EMIT_R2SAVE 0
10264
10265 #define PPC_LO(v) ((v) & 0xffff)
10266 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10267 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10268
10269 static inline unsigned int
10270 plt_stub_size (struct ppc_link_hash_table *htab,
10271                struct ppc_stub_hash_entry *stub_entry,
10272                bfd_vma off)
10273 {
10274   unsigned size = 12;
10275
10276   if (ALWAYS_EMIT_R2SAVE
10277       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10278     size += 4;
10279   if (PPC_HA (off) != 0)
10280     size += 4;
10281   if (htab->opd_abi)
10282     {
10283       size += 4;
10284       if (htab->params->plt_static_chain)
10285         size += 4;
10286       if (htab->params->plt_thread_safe
10287           && htab->elf.dynamic_sections_created
10288           && stub_entry->h != NULL
10289           && stub_entry->h->elf.dynindx != -1)
10290         size += 8;
10291       if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10292         size += 4;
10293     }
10294   if (stub_entry->h != NULL
10295       && (stub_entry->h == htab->tls_get_addr_fd
10296           || stub_entry->h == htab->tls_get_addr)
10297       && !htab->params->no_tls_get_addr_opt)
10298     size += 13 * 4;
10299   return size;
10300 }
10301
10302 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10303    then return the padding needed to do so.  */
10304 static inline unsigned int
10305 plt_stub_pad (struct ppc_link_hash_table *htab,
10306               struct ppc_stub_hash_entry *stub_entry,
10307               bfd_vma plt_off)
10308 {
10309   int stub_align = 1 << htab->params->plt_stub_align;
10310   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10311   bfd_vma stub_off = stub_entry->group->stub_sec->size;
10312
10313   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10314       > ((stub_size - 1) & -stub_align))
10315     return stub_align - (stub_off & (stub_align - 1));
10316   return 0;
10317 }
10318
10319 /* Build a .plt call stub.  */
10320
10321 static inline bfd_byte *
10322 build_plt_stub (struct ppc_link_hash_table *htab,
10323                 struct ppc_stub_hash_entry *stub_entry,
10324                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10325 {
10326   bfd *obfd = htab->params->stub_bfd;
10327   bfd_boolean plt_load_toc = htab->opd_abi;
10328   bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10329   bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10330                                  && htab->elf.dynamic_sections_created
10331                                  && stub_entry->h != NULL
10332                                  && stub_entry->h->elf.dynindx != -1);
10333   bfd_boolean use_fake_dep = plt_thread_safe;
10334   bfd_vma cmp_branch_off = 0;
10335
10336   if (!ALWAYS_USE_FAKE_DEP
10337       && plt_load_toc
10338       && plt_thread_safe
10339       && !((stub_entry->h == htab->tls_get_addr_fd
10340             || stub_entry->h == htab->tls_get_addr)
10341            && !htab->params->no_tls_get_addr_opt))
10342     {
10343       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10344       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10345                           / PLT_ENTRY_SIZE (htab));
10346       bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10347       bfd_vma to, from;
10348
10349       if (pltindex > 32768)
10350         glinkoff += (pltindex - 32768) * 4;
10351       to = (glinkoff
10352             + htab->glink->output_offset
10353             + htab->glink->output_section->vma);
10354       from = (p - stub_entry->group->stub_sec->contents
10355               + 4 * (ALWAYS_EMIT_R2SAVE
10356                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10357               + 4 * (PPC_HA (offset) != 0)
10358               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10359                      != PPC_HA (offset))
10360               + 4 * (plt_static_chain != 0)
10361               + 20
10362               + stub_entry->group->stub_sec->output_offset
10363               + stub_entry->group->stub_sec->output_section->vma);
10364       cmp_branch_off = to - from;
10365       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10366     }
10367
10368   if (PPC_HA (offset) != 0)
10369     {
10370       if (r != NULL)
10371         {
10372           if (ALWAYS_EMIT_R2SAVE
10373               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10374             r[0].r_offset += 4;
10375           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10376           r[1].r_offset = r[0].r_offset + 4;
10377           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10378           r[1].r_addend = r[0].r_addend;
10379           if (plt_load_toc)
10380             {
10381               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10382                 {
10383                   r[2].r_offset = r[1].r_offset + 4;
10384                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10385                   r[2].r_addend = r[0].r_addend;
10386                 }
10387               else
10388                 {
10389                   r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10390                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10391                   r[2].r_addend = r[0].r_addend + 8;
10392                   if (plt_static_chain)
10393                     {
10394                       r[3].r_offset = r[2].r_offset + 4;
10395                       r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10396                       r[3].r_addend = r[0].r_addend + 16;
10397                     }
10398                 }
10399             }
10400         }
10401       if (ALWAYS_EMIT_R2SAVE
10402           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10403         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10404       if (plt_load_toc)
10405         {
10406           bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10407           bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),  p += 4;
10408         }
10409       else
10410         {
10411           bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10412           bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),  p += 4;
10413         }
10414       if (plt_load_toc
10415           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10416         {
10417           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10418           offset = 0;
10419         }
10420       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10421       if (plt_load_toc)
10422         {
10423           if (use_fake_dep)
10424             {
10425               bfd_put_32 (obfd, XOR_R2_R12_R12, p),             p += 4;
10426               bfd_put_32 (obfd, ADD_R11_R11_R2, p),             p += 4;
10427             }
10428           bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10429           if (plt_static_chain)
10430             bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10431         }
10432     }
10433   else
10434     {
10435       if (r != NULL)
10436         {
10437           if (ALWAYS_EMIT_R2SAVE
10438               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10439             r[0].r_offset += 4;
10440           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10441           if (plt_load_toc)
10442             {
10443               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10444                 {
10445                   r[1].r_offset = r[0].r_offset + 4;
10446                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10447                   r[1].r_addend = r[0].r_addend;
10448                 }
10449               else
10450                 {
10451                   r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10452                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10453                   r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10454                   if (plt_static_chain)
10455                     {
10456                       r[2].r_offset = r[1].r_offset + 4;
10457                       r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10458                       r[2].r_addend = r[0].r_addend + 8;
10459                     }
10460                 }
10461             }
10462         }
10463       if (ALWAYS_EMIT_R2SAVE
10464           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10465         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10466       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
10467       if (plt_load_toc
10468           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10469         {
10470           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
10471           offset = 0;
10472         }
10473       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10474       if (plt_load_toc)
10475         {
10476           if (use_fake_dep)
10477             {
10478               bfd_put_32 (obfd, XOR_R11_R12_R12, p),            p += 4;
10479               bfd_put_32 (obfd, ADD_R2_R2_R11, p),              p += 4;
10480             }
10481           if (plt_static_chain)
10482             bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10483           bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10484         }
10485     }
10486   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10487     {
10488       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
10489       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
10490       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10491     }
10492   else
10493     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
10494   return p;
10495 }
10496
10497 /* Build a special .plt call stub for __tls_get_addr.  */
10498
10499 #define LD_R11_0R3      0xe9630000
10500 #define LD_R12_0R3      0xe9830000
10501 #define MR_R0_R3        0x7c601b78
10502 #define CMPDI_R11_0     0x2c2b0000
10503 #define ADD_R3_R12_R13  0x7c6c6a14
10504 #define BEQLR           0x4d820020
10505 #define MR_R3_R0        0x7c030378
10506 #define STD_R11_0R1     0xf9610000
10507 #define BCTRL           0x4e800421
10508 #define LD_R11_0R1      0xe9610000
10509 #define MTLR_R11        0x7d6803a6
10510
10511 static inline bfd_byte *
10512 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10513                          struct ppc_stub_hash_entry *stub_entry,
10514                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10515 {
10516   bfd *obfd = htab->params->stub_bfd;
10517
10518   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
10519   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
10520   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
10521   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
10522   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
10523   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
10524   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
10525   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
10526   bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10527
10528   if (r != NULL)
10529     r[0].r_offset += 9 * 4;
10530   p = build_plt_stub (htab, stub_entry, p, offset, r);
10531   bfd_put_32 (obfd, BCTRL, p - 4);
10532
10533   bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p),     p += 4;
10534   bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10535   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
10536   bfd_put_32 (obfd, BLR, p),                    p += 4;
10537
10538   return p;
10539 }
10540
10541 static Elf_Internal_Rela *
10542 get_relocs (asection *sec, int count)
10543 {
10544   Elf_Internal_Rela *relocs;
10545   struct bfd_elf_section_data *elfsec_data;
10546
10547   elfsec_data = elf_section_data (sec);
10548   relocs = elfsec_data->relocs;
10549   if (relocs == NULL)
10550     {
10551       bfd_size_type relsize;
10552       relsize = sec->reloc_count * sizeof (*relocs);
10553       relocs = bfd_alloc (sec->owner, relsize);
10554       if (relocs == NULL)
10555         return NULL;
10556       elfsec_data->relocs = relocs;
10557       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10558                                           sizeof (Elf_Internal_Shdr));
10559       if (elfsec_data->rela.hdr == NULL)
10560         return NULL;
10561       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10562                                         * sizeof (Elf64_External_Rela));
10563       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10564       sec->reloc_count = 0;
10565     }
10566   relocs += sec->reloc_count;
10567   sec->reloc_count += count;
10568   return relocs;
10569 }
10570
10571 static bfd_vma
10572 get_r2off (struct bfd_link_info *info,
10573            struct ppc_stub_hash_entry *stub_entry)
10574 {
10575   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10576   bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10577
10578   if (r2off == 0)
10579     {
10580       /* Support linking -R objects.  Get the toc pointer from the
10581          opd entry.  */
10582       char buf[8];
10583       if (!htab->opd_abi)
10584         return r2off;
10585       asection *opd = stub_entry->h->elf.root.u.def.section;
10586       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10587
10588       if (strcmp (opd->name, ".opd") != 0
10589           || opd->reloc_count != 0)
10590         {
10591           info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10592                                   stub_entry->h->elf.root.root.string);
10593           bfd_set_error (bfd_error_bad_value);
10594           return (bfd_vma) -1;
10595         }
10596       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10597         return (bfd_vma) -1;
10598       r2off = bfd_get_64 (opd->owner, buf);
10599       r2off -= elf_gp (info->output_bfd);
10600     }
10601   r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10602   return r2off;
10603 }
10604
10605 static bfd_boolean
10606 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10607 {
10608   struct ppc_stub_hash_entry *stub_entry;
10609   struct ppc_branch_hash_entry *br_entry;
10610   struct bfd_link_info *info;
10611   struct ppc_link_hash_table *htab;
10612   bfd_byte *loc;
10613   bfd_byte *p;
10614   bfd_vma dest, off;
10615   int size;
10616   Elf_Internal_Rela *r;
10617   asection *plt;
10618
10619   /* Massage our args to the form they really have.  */
10620   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10621   info = in_arg;
10622
10623   htab = ppc_hash_table (info);
10624   if (htab == NULL)
10625     return FALSE;
10626
10627   /* Make a note of the offset within the stubs for this entry.  */
10628   stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10629   loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10630
10631   htab->stub_count[stub_entry->stub_type - 1] += 1;
10632   switch (stub_entry->stub_type)
10633     {
10634     case ppc_stub_long_branch:
10635     case ppc_stub_long_branch_r2off:
10636       /* Branches are relative.  This is where we are going to.  */
10637       dest = (stub_entry->target_value
10638               + stub_entry->target_section->output_offset
10639               + stub_entry->target_section->output_section->vma);
10640       dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10641       off = dest;
10642
10643       /* And this is where we are coming from.  */
10644       off -= (stub_entry->stub_offset
10645               + stub_entry->group->stub_sec->output_offset
10646               + stub_entry->group->stub_sec->output_section->vma);
10647
10648       size = 4;
10649       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10650         {
10651           bfd_vma r2off = get_r2off (info, stub_entry);
10652
10653           if (r2off == (bfd_vma) -1)
10654             {
10655               htab->stub_error = TRUE;
10656               return FALSE;
10657             }
10658           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10659           loc += 4;
10660           size = 8;
10661           if (PPC_HA (r2off) != 0)
10662             {
10663               bfd_put_32 (htab->params->stub_bfd,
10664                           ADDIS_R2_R2 | PPC_HA (r2off), loc);
10665               loc += 4;
10666               size += 4;
10667             }
10668           if (PPC_LO (r2off) != 0)
10669             {
10670               bfd_put_32 (htab->params->stub_bfd,
10671                           ADDI_R2_R2 | PPC_LO (r2off), loc);
10672               loc += 4;
10673               size += 4;
10674             }
10675           off -= size - 4;
10676         }
10677       bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
10678
10679       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10680         {
10681           info->callbacks->einfo
10682             (_("%P: long branch stub `%s' offset overflow\n"),
10683              stub_entry->root.string);
10684           htab->stub_error = TRUE;
10685           return FALSE;
10686         }
10687
10688       if (info->emitrelocations)
10689         {
10690           r = get_relocs (stub_entry->group->stub_sec, 1);
10691           if (r == NULL)
10692             return FALSE;
10693           r->r_offset = loc - stub_entry->group->stub_sec->contents;
10694           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10695           r->r_addend = dest;
10696           if (stub_entry->h != NULL)
10697             {
10698               struct elf_link_hash_entry **hashes;
10699               unsigned long symndx;
10700               struct ppc_link_hash_entry *h;
10701
10702               hashes = elf_sym_hashes (htab->params->stub_bfd);
10703               if (hashes == NULL)
10704                 {
10705                   bfd_size_type hsize;
10706
10707                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10708                   hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10709                   if (hashes == NULL)
10710                     return FALSE;
10711                   elf_sym_hashes (htab->params->stub_bfd) = hashes;
10712                   htab->stub_globals = 1;
10713                 }
10714               symndx = htab->stub_globals++;
10715               h = stub_entry->h;
10716               hashes[symndx] = &h->elf;
10717               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10718               if (h->oh != NULL && h->oh->is_func)
10719                 h = ppc_follow_link (h->oh);
10720               if (h->elf.root.u.def.section != stub_entry->target_section)
10721                 /* H is an opd symbol.  The addend must be zero.  */
10722                 r->r_addend = 0;
10723               else
10724                 {
10725                   off = (h->elf.root.u.def.value
10726                          + h->elf.root.u.def.section->output_offset
10727                          + h->elf.root.u.def.section->output_section->vma);
10728                   r->r_addend -= off;
10729                 }
10730             }
10731         }
10732       break;
10733
10734     case ppc_stub_plt_branch:
10735     case ppc_stub_plt_branch_r2off:
10736       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10737                                          stub_entry->root.string + 9,
10738                                          FALSE, FALSE);
10739       if (br_entry == NULL)
10740         {
10741           info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10742                                   stub_entry->root.string);
10743           htab->stub_error = TRUE;
10744           return FALSE;
10745         }
10746
10747       dest = (stub_entry->target_value
10748               + stub_entry->target_section->output_offset
10749               + stub_entry->target_section->output_section->vma);
10750       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10751         dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10752
10753       bfd_put_64 (htab->brlt->owner, dest,
10754                   htab->brlt->contents + br_entry->offset);
10755
10756       if (br_entry->iter == htab->stub_iteration)
10757         {
10758           br_entry->iter = 0;
10759
10760           if (htab->relbrlt != NULL)
10761             {
10762               /* Create a reloc for the branch lookup table entry.  */
10763               Elf_Internal_Rela rela;
10764               bfd_byte *rl;
10765
10766               rela.r_offset = (br_entry->offset
10767                                + htab->brlt->output_offset
10768                                + htab->brlt->output_section->vma);
10769               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10770               rela.r_addend = dest;
10771
10772               rl = htab->relbrlt->contents;
10773               rl += (htab->relbrlt->reloc_count++
10774                      * sizeof (Elf64_External_Rela));
10775               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10776             }
10777           else if (info->emitrelocations)
10778             {
10779               r = get_relocs (htab->brlt, 1);
10780               if (r == NULL)
10781                 return FALSE;
10782               /* brlt, being SEC_LINKER_CREATED does not go through the
10783                  normal reloc processing.  Symbols and offsets are not
10784                  translated from input file to output file form, so
10785                  set up the offset per the output file.  */
10786               r->r_offset = (br_entry->offset
10787                              + htab->brlt->output_offset
10788                              + htab->brlt->output_section->vma);
10789               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10790               r->r_addend = dest;
10791             }
10792         }
10793
10794       dest = (br_entry->offset
10795               + htab->brlt->output_offset
10796               + htab->brlt->output_section->vma);
10797
10798       off = (dest
10799              - elf_gp (htab->brlt->output_section->owner)
10800              - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10801
10802       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10803         {
10804           info->callbacks->einfo
10805             (_("%P: linkage table error against `%T'\n"),
10806              stub_entry->root.string);
10807           bfd_set_error (bfd_error_bad_value);
10808           htab->stub_error = TRUE;
10809           return FALSE;
10810         }
10811
10812       if (info->emitrelocations)
10813         {
10814           r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
10815           if (r == NULL)
10816             return FALSE;
10817           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10818           if (bfd_big_endian (info->output_bfd))
10819             r[0].r_offset += 2;
10820           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10821             r[0].r_offset += 4;
10822           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10823           r[0].r_addend = dest;
10824           if (PPC_HA (off) != 0)
10825             {
10826               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10827               r[1].r_offset = r[0].r_offset + 4;
10828               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10829               r[1].r_addend = r[0].r_addend;
10830             }
10831         }
10832
10833       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10834         {
10835           if (PPC_HA (off) != 0)
10836             {
10837               size = 16;
10838               bfd_put_32 (htab->params->stub_bfd,
10839                           ADDIS_R12_R2 | PPC_HA (off), loc);
10840               loc += 4;
10841               bfd_put_32 (htab->params->stub_bfd,
10842                           LD_R12_0R12 | PPC_LO (off), loc);
10843             }
10844           else
10845             {
10846               size = 12;
10847               bfd_put_32 (htab->params->stub_bfd,
10848                           LD_R12_0R2 | PPC_LO (off), loc);
10849             }
10850         }
10851       else
10852         {
10853           bfd_vma r2off = get_r2off (info, stub_entry);
10854
10855           if (r2off == (bfd_vma) -1)
10856             {
10857               htab->stub_error = TRUE;
10858               return FALSE;
10859             }
10860
10861           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10862           loc += 4;
10863           size = 16;
10864           if (PPC_HA (off) != 0)
10865             {
10866               size += 4;
10867               bfd_put_32 (htab->params->stub_bfd,
10868                           ADDIS_R12_R2 | PPC_HA (off), loc);
10869               loc += 4;
10870               bfd_put_32 (htab->params->stub_bfd,
10871                           LD_R12_0R12 | PPC_LO (off), loc);
10872             }
10873           else
10874             bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
10875
10876           if (PPC_HA (r2off) != 0)
10877             {
10878               size += 4;
10879               loc += 4;
10880               bfd_put_32 (htab->params->stub_bfd,
10881                           ADDIS_R2_R2 | PPC_HA (r2off), loc);
10882             }
10883           if (PPC_LO (r2off) != 0)
10884             {
10885               size += 4;
10886               loc += 4;
10887               bfd_put_32 (htab->params->stub_bfd,
10888                           ADDI_R2_R2 | PPC_LO (r2off), loc);
10889             }
10890         }
10891       loc += 4;
10892       bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
10893       loc += 4;
10894       bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
10895       break;
10896
10897     case ppc_stub_plt_call:
10898     case ppc_stub_plt_call_r2save:
10899       if (stub_entry->h != NULL
10900           && stub_entry->h->is_func_descriptor
10901           && stub_entry->h->oh != NULL)
10902         {
10903           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10904
10905           /* If the old-ABI "dot-symbol" is undefined make it weak so
10906              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
10907           if (fh->elf.root.type == bfd_link_hash_undefined)
10908             fh->elf.root.type = bfd_link_hash_undefweak;
10909           /* Stop undo_symbol_twiddle changing it back to undefined.  */
10910           fh->was_undefined = 0;
10911         }
10912
10913       /* Now build the stub.  */
10914       dest = stub_entry->plt_ent->plt.offset & ~1;
10915       if (dest >= (bfd_vma) -2)
10916         abort ();
10917
10918       plt = htab->elf.splt;
10919       if (!htab->elf.dynamic_sections_created
10920           || stub_entry->h == NULL
10921           || stub_entry->h->elf.dynindx == -1)
10922         plt = htab->elf.iplt;
10923
10924       dest += plt->output_offset + plt->output_section->vma;
10925
10926       if (stub_entry->h == NULL
10927           && (stub_entry->plt_ent->plt.offset & 1) == 0)
10928         {
10929           Elf_Internal_Rela rela;
10930           bfd_byte *rl;
10931
10932           rela.r_offset = dest;
10933           if (htab->opd_abi)
10934             rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
10935           else
10936             rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
10937           rela.r_addend = (stub_entry->target_value
10938                            + stub_entry->target_section->output_offset
10939                            + stub_entry->target_section->output_section->vma);
10940
10941           rl = (htab->elf.irelplt->contents
10942                 + (htab->elf.irelplt->reloc_count++
10943                    * sizeof (Elf64_External_Rela)));
10944           bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
10945           stub_entry->plt_ent->plt.offset |= 1;
10946         }
10947
10948       off = (dest
10949              - elf_gp (plt->output_section->owner)
10950              - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10951
10952       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10953         {
10954           info->callbacks->einfo
10955             (_("%P: linkage table error against `%T'\n"),
10956              stub_entry->h != NULL
10957              ? stub_entry->h->elf.root.root.string
10958              : "<local sym>");
10959           bfd_set_error (bfd_error_bad_value);
10960           htab->stub_error = TRUE;
10961           return FALSE;
10962         }
10963
10964       if (htab->params->plt_stub_align != 0)
10965         {
10966           unsigned pad = plt_stub_pad (htab, stub_entry, off);
10967
10968           stub_entry->group->stub_sec->size += pad;
10969           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10970           loc += pad;
10971         }
10972
10973       r = NULL;
10974       if (info->emitrelocations)
10975         {
10976           r = get_relocs (stub_entry->group->stub_sec,
10977                           ((PPC_HA (off) != 0)
10978                            + (htab->opd_abi
10979                               ? 2 + (htab->params->plt_static_chain
10980                                      && PPC_HA (off + 16) == PPC_HA (off))
10981                               : 1)));
10982           if (r == NULL)
10983             return FALSE;
10984           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10985           if (bfd_big_endian (info->output_bfd))
10986             r[0].r_offset += 2;
10987           r[0].r_addend = dest;
10988         }
10989       if (stub_entry->h != NULL
10990           && (stub_entry->h == htab->tls_get_addr_fd
10991               || stub_entry->h == htab->tls_get_addr)
10992           && !htab->params->no_tls_get_addr_opt)
10993         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
10994       else
10995         p = build_plt_stub (htab, stub_entry, loc, off, r);
10996       size = p - loc;
10997       break;
10998
10999     case ppc_stub_save_res:
11000       return TRUE;
11001
11002     default:
11003       BFD_FAIL ();
11004       return FALSE;
11005     }
11006
11007   stub_entry->group->stub_sec->size += size;
11008
11009   if (htab->params->emit_stub_syms)
11010     {
11011       struct elf_link_hash_entry *h;
11012       size_t len1, len2;
11013       char *name;
11014       const char *const stub_str[] = { "long_branch",
11015                                        "long_branch_r2off",
11016                                        "plt_branch",
11017                                        "plt_branch_r2off",
11018                                        "plt_call",
11019                                        "plt_call" };
11020
11021       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11022       len2 = strlen (stub_entry->root.string);
11023       name = bfd_malloc (len1 + len2 + 2);
11024       if (name == NULL)
11025         return FALSE;
11026       memcpy (name, stub_entry->root.string, 9);
11027       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11028       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11029       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11030       if (h == NULL)
11031         return FALSE;
11032       if (h->root.type == bfd_link_hash_new)
11033         {
11034           h->root.type = bfd_link_hash_defined;
11035           h->root.u.def.section = stub_entry->group->stub_sec;
11036           h->root.u.def.value = stub_entry->stub_offset;
11037           h->ref_regular = 1;
11038           h->def_regular = 1;
11039           h->ref_regular_nonweak = 1;
11040           h->forced_local = 1;
11041           h->non_elf = 0;
11042           h->root.linker_def = 1;
11043         }
11044     }
11045
11046   return TRUE;
11047 }
11048
11049 /* As above, but don't actually build the stub.  Just bump offset so
11050    we know stub section sizes, and select plt_branch stubs where
11051    long_branch stubs won't do.  */
11052
11053 static bfd_boolean
11054 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11055 {
11056   struct ppc_stub_hash_entry *stub_entry;
11057   struct bfd_link_info *info;
11058   struct ppc_link_hash_table *htab;
11059   bfd_vma off;
11060   int size;
11061
11062   /* Massage our args to the form they really have.  */
11063   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11064   info = in_arg;
11065
11066   htab = ppc_hash_table (info);
11067   if (htab == NULL)
11068     return FALSE;
11069
11070   if (stub_entry->h != NULL
11071       && stub_entry->h->save_res
11072       && stub_entry->h->elf.root.type == bfd_link_hash_defined
11073       && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11074     {
11075       /* Don't make stubs to out-of-line register save/restore
11076          functions.  Instead, emit copies of the functions.  */
11077       stub_entry->group->needs_save_res = 1;
11078       stub_entry->stub_type = ppc_stub_save_res;
11079       return TRUE;
11080     }
11081
11082   if (stub_entry->stub_type == ppc_stub_plt_call
11083       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11084     {
11085       asection *plt;
11086       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11087       if (off >= (bfd_vma) -2)
11088         abort ();
11089       plt = htab->elf.splt;
11090       if (!htab->elf.dynamic_sections_created
11091           || stub_entry->h == NULL
11092           || stub_entry->h->elf.dynindx == -1)
11093         plt = htab->elf.iplt;
11094       off += (plt->output_offset
11095               + plt->output_section->vma
11096               - elf_gp (plt->output_section->owner)
11097               - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11098
11099       size = plt_stub_size (htab, stub_entry, off);
11100       if (htab->params->plt_stub_align)
11101         size += plt_stub_pad (htab, stub_entry, off);
11102       if (info->emitrelocations)
11103         {
11104           stub_entry->group->stub_sec->reloc_count
11105             += ((PPC_HA (off) != 0)
11106                 + (htab->opd_abi
11107                    ? 2 + (htab->params->plt_static_chain
11108                           && PPC_HA (off + 16) == PPC_HA (off))
11109                    : 1));
11110           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11111         }
11112     }
11113   else
11114     {
11115       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11116          variants.  */
11117       bfd_vma r2off = 0;
11118       bfd_vma local_off = 0;
11119
11120       off = (stub_entry->target_value
11121              + stub_entry->target_section->output_offset
11122              + stub_entry->target_section->output_section->vma);
11123       off -= (stub_entry->group->stub_sec->size
11124               + stub_entry->group->stub_sec->output_offset
11125               + stub_entry->group->stub_sec->output_section->vma);
11126
11127       /* Reset the stub type from the plt variant in case we now
11128          can reach with a shorter stub.  */
11129       if (stub_entry->stub_type >= ppc_stub_plt_branch)
11130         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11131
11132       size = 4;
11133       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11134         {
11135           r2off = get_r2off (info, stub_entry);
11136           if (r2off == (bfd_vma) -1)
11137             {
11138               htab->stub_error = TRUE;
11139               return FALSE;
11140             }
11141           size = 8;
11142           if (PPC_HA (r2off) != 0)
11143             size += 4;
11144           if (PPC_LO (r2off) != 0)
11145             size += 4;
11146           off -= size - 4;
11147         }
11148
11149       local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11150
11151       /* If the branch offset if too big, use a ppc_stub_plt_branch.
11152          Do the same for -R objects without function descriptors.  */
11153       if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11154           || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11155               && r2off == 0
11156               && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11157         {
11158           struct ppc_branch_hash_entry *br_entry;
11159
11160           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11161                                              stub_entry->root.string + 9,
11162                                              TRUE, FALSE);
11163           if (br_entry == NULL)
11164             {
11165               info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
11166                                       stub_entry->root.string);
11167               htab->stub_error = TRUE;
11168               return FALSE;
11169             }
11170
11171           if (br_entry->iter != htab->stub_iteration)
11172             {
11173               br_entry->iter = htab->stub_iteration;
11174               br_entry->offset = htab->brlt->size;
11175               htab->brlt->size += 8;
11176
11177               if (htab->relbrlt != NULL)
11178                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11179               else if (info->emitrelocations)
11180                 {
11181                   htab->brlt->reloc_count += 1;
11182                   htab->brlt->flags |= SEC_RELOC;
11183                 }
11184             }
11185
11186           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11187           off = (br_entry->offset
11188                  + htab->brlt->output_offset
11189                  + htab->brlt->output_section->vma
11190                  - elf_gp (htab->brlt->output_section->owner)
11191                  - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11192
11193           if (info->emitrelocations)
11194             {
11195               stub_entry->group->stub_sec->reloc_count
11196                 += 1 + (PPC_HA (off) != 0);
11197               stub_entry->group->stub_sec->flags |= SEC_RELOC;
11198             }
11199
11200           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11201             {
11202               size = 12;
11203               if (PPC_HA (off) != 0)
11204                 size = 16;
11205             }
11206           else
11207             {
11208               size = 16;
11209               if (PPC_HA (off) != 0)
11210                 size += 4;
11211
11212               if (PPC_HA (r2off) != 0)
11213                 size += 4;
11214               if (PPC_LO (r2off) != 0)
11215                 size += 4;
11216             }
11217         }
11218       else if (info->emitrelocations)
11219         {
11220           stub_entry->group->stub_sec->reloc_count += 1;
11221           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11222         }
11223     }
11224
11225   stub_entry->group->stub_sec->size += size;
11226   return TRUE;
11227 }
11228
11229 /* Set up various things so that we can make a list of input sections
11230    for each output section included in the link.  Returns -1 on error,
11231    0 when no stubs will be needed, and 1 on success.  */
11232
11233 int
11234 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11235 {
11236   unsigned int id;
11237   bfd_size_type amt;
11238   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11239
11240   if (htab == NULL)
11241     return -1;
11242
11243   htab->sec_info_arr_size = bfd_get_next_section_id ();
11244   amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11245   htab->sec_info = bfd_zmalloc (amt);
11246   if (htab->sec_info == NULL)
11247     return -1;
11248
11249   /* Set toc_off for com, und, abs and ind sections.  */
11250   for (id = 0; id < 3; id++)
11251     htab->sec_info[id].toc_off = TOC_BASE_OFF;
11252
11253   return 1;
11254 }
11255
11256 /* Set up for first pass at multitoc partitioning.  */
11257
11258 void
11259 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11260 {
11261   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11262
11263   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11264   htab->toc_bfd = NULL;
11265   htab->toc_first_sec = NULL;
11266 }
11267
11268 /* The linker repeatedly calls this function for each TOC input section
11269    and linker generated GOT section.  Group input bfds such that the toc
11270    within a group is less than 64k in size.  */
11271
11272 bfd_boolean
11273 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11274 {
11275   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11276   bfd_vma addr, off, limit;
11277
11278   if (htab == NULL)
11279     return FALSE;
11280
11281   if (!htab->second_toc_pass)
11282     {
11283       /* Keep track of the first .toc or .got section for this input bfd.  */
11284       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11285
11286       if (new_bfd)
11287         {
11288           htab->toc_bfd = isec->owner;
11289           htab->toc_first_sec = isec;
11290         }
11291
11292       addr = isec->output_offset + isec->output_section->vma;
11293       off = addr - htab->toc_curr;
11294       limit = 0x80008000;
11295       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11296         limit = 0x10000;
11297       if (off + isec->size > limit)
11298         {
11299           addr = (htab->toc_first_sec->output_offset
11300                   + htab->toc_first_sec->output_section->vma);
11301           htab->toc_curr = addr;
11302           htab->toc_curr &= -TOC_BASE_ALIGN;
11303         }
11304
11305       /* toc_curr is the base address of this toc group.  Set elf_gp
11306          for the input section to be the offset relative to the
11307          output toc base plus 0x8000.  Making the input elf_gp an
11308          offset allows us to move the toc as a whole without
11309          recalculating input elf_gp.  */
11310       off = htab->toc_curr - elf_gp (isec->output_section->owner);
11311       off += TOC_BASE_OFF;
11312
11313       /* Die if someone uses a linker script that doesn't keep input
11314          file .toc and .got together.  */
11315       if (new_bfd
11316           && elf_gp (isec->owner) != 0
11317           && elf_gp (isec->owner) != off)
11318         return FALSE;
11319
11320       elf_gp (isec->owner) = off;
11321       return TRUE;
11322     }
11323
11324   /* During the second pass toc_first_sec points to the start of
11325      a toc group, and toc_curr is used to track the old elf_gp.
11326      We use toc_bfd to ensure we only look at each bfd once.  */
11327   if (htab->toc_bfd == isec->owner)
11328     return TRUE;
11329   htab->toc_bfd = isec->owner;
11330
11331   if (htab->toc_first_sec == NULL
11332       || htab->toc_curr != elf_gp (isec->owner))
11333     {
11334       htab->toc_curr = elf_gp (isec->owner);
11335       htab->toc_first_sec = isec;
11336     }
11337   addr = (htab->toc_first_sec->output_offset
11338           + htab->toc_first_sec->output_section->vma);
11339   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11340   elf_gp (isec->owner) = off;
11341
11342   return TRUE;
11343 }
11344
11345 /* Called via elf_link_hash_traverse to merge GOT entries for global
11346    symbol H.  */
11347
11348 static bfd_boolean
11349 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11350 {
11351   if (h->root.type == bfd_link_hash_indirect)
11352     return TRUE;
11353
11354   merge_got_entries (&h->got.glist);
11355
11356   return TRUE;
11357 }
11358
11359 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11360    symbol H.  */
11361
11362 static bfd_boolean
11363 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11364 {
11365   struct got_entry *gent;
11366
11367   if (h->root.type == bfd_link_hash_indirect)
11368     return TRUE;
11369
11370   for (gent = h->got.glist; gent != NULL; gent = gent->next)
11371     if (!gent->is_indirect)
11372       allocate_got (h, (struct bfd_link_info *) inf, gent);
11373   return TRUE;
11374 }
11375
11376 /* Called on the first multitoc pass after the last call to
11377    ppc64_elf_next_toc_section.  This function removes duplicate GOT
11378    entries.  */
11379
11380 bfd_boolean
11381 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11382 {
11383   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11384   struct bfd *ibfd, *ibfd2;
11385   bfd_boolean done_something;
11386
11387   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11388
11389   if (!htab->do_multi_toc)
11390     return FALSE;
11391
11392   /* Merge global sym got entries within a toc group.  */
11393   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11394
11395   /* And tlsld_got.  */
11396   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11397     {
11398       struct got_entry *ent, *ent2;
11399
11400       if (!is_ppc64_elf (ibfd))
11401         continue;
11402
11403       ent = ppc64_tlsld_got (ibfd);
11404       if (!ent->is_indirect
11405           && ent->got.offset != (bfd_vma) -1)
11406         {
11407           for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11408             {
11409               if (!is_ppc64_elf (ibfd2))
11410                 continue;
11411
11412               ent2 = ppc64_tlsld_got (ibfd2);
11413               if (!ent2->is_indirect
11414                   && ent2->got.offset != (bfd_vma) -1
11415                   && elf_gp (ibfd2) == elf_gp (ibfd))
11416                 {
11417                   ent2->is_indirect = TRUE;
11418                   ent2->got.ent = ent;
11419                 }
11420             }
11421         }
11422     }
11423
11424   /* Zap sizes of got sections.  */
11425   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11426   htab->elf.irelplt->size -= htab->got_reli_size;
11427   htab->got_reli_size = 0;
11428
11429   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11430     {
11431       asection *got, *relgot;
11432
11433       if (!is_ppc64_elf (ibfd))
11434         continue;
11435
11436       got = ppc64_elf_tdata (ibfd)->got;
11437       if (got != NULL)
11438         {
11439           got->rawsize = got->size;
11440           got->size = 0;
11441           relgot = ppc64_elf_tdata (ibfd)->relgot;
11442           relgot->rawsize = relgot->size;
11443           relgot->size = 0;
11444         }
11445     }
11446
11447   /* Now reallocate the got, local syms first.  We don't need to
11448      allocate section contents again since we never increase size.  */
11449   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11450     {
11451       struct got_entry **lgot_ents;
11452       struct got_entry **end_lgot_ents;
11453       struct plt_entry **local_plt;
11454       struct plt_entry **end_local_plt;
11455       unsigned char *lgot_masks;
11456       bfd_size_type locsymcount;
11457       Elf_Internal_Shdr *symtab_hdr;
11458       asection *s;
11459
11460       if (!is_ppc64_elf (ibfd))
11461         continue;
11462
11463       lgot_ents = elf_local_got_ents (ibfd);
11464       if (!lgot_ents)
11465         continue;
11466
11467       symtab_hdr = &elf_symtab_hdr (ibfd);
11468       locsymcount = symtab_hdr->sh_info;
11469       end_lgot_ents = lgot_ents + locsymcount;
11470       local_plt = (struct plt_entry **) end_lgot_ents;
11471       end_local_plt = local_plt + locsymcount;
11472       lgot_masks = (unsigned char *) end_local_plt;
11473       s = ppc64_elf_tdata (ibfd)->got;
11474       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11475         {
11476           struct got_entry *ent;
11477
11478           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11479             {
11480               unsigned int ent_size = 8;
11481               unsigned int rel_size = sizeof (Elf64_External_Rela);
11482
11483               ent->got.offset = s->size;
11484               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11485                 {
11486                   ent_size *= 2;
11487                   rel_size *= 2;
11488                 }
11489               s->size += ent_size;
11490               if ((*lgot_masks & PLT_IFUNC) != 0)
11491                 {
11492                   htab->elf.irelplt->size += rel_size;
11493                   htab->got_reli_size += rel_size;
11494                 }
11495               else if (bfd_link_pic (info))
11496                 {
11497                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11498                   srel->size += rel_size;
11499                 }
11500             }
11501         }
11502     }
11503
11504   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11505
11506   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11507     {
11508       struct got_entry *ent;
11509
11510       if (!is_ppc64_elf (ibfd))
11511         continue;
11512
11513       ent = ppc64_tlsld_got (ibfd);
11514       if (!ent->is_indirect
11515           && ent->got.offset != (bfd_vma) -1)
11516         {
11517           asection *s = ppc64_elf_tdata (ibfd)->got;
11518           ent->got.offset = s->size;
11519           s->size += 16;
11520           if (bfd_link_pic (info))
11521             {
11522               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11523               srel->size += sizeof (Elf64_External_Rela);
11524             }
11525         }
11526     }
11527
11528   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11529   if (!done_something)
11530     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11531       {
11532         asection *got;
11533
11534         if (!is_ppc64_elf (ibfd))
11535           continue;
11536
11537         got = ppc64_elf_tdata (ibfd)->got;
11538         if (got != NULL)
11539           {
11540             done_something = got->rawsize != got->size;
11541             if (done_something)
11542               break;
11543           }
11544       }
11545
11546   if (done_something)
11547     (*htab->params->layout_sections_again) ();
11548
11549   /* Set up for second pass over toc sections to recalculate elf_gp
11550      on input sections.  */
11551   htab->toc_bfd = NULL;
11552   htab->toc_first_sec = NULL;
11553   htab->second_toc_pass = TRUE;
11554   return done_something;
11555 }
11556
11557 /* Called after second pass of multitoc partitioning.  */
11558
11559 void
11560 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11561 {
11562   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11563
11564   /* After the second pass, toc_curr tracks the TOC offset used
11565      for code sections below in ppc64_elf_next_input_section.  */
11566   htab->toc_curr = TOC_BASE_OFF;
11567 }
11568
11569 /* No toc references were found in ISEC.  If the code in ISEC makes no
11570    calls, then there's no need to use toc adjusting stubs when branching
11571    into ISEC.  Actually, indirect calls from ISEC are OK as they will
11572    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
11573    needed, and 2 if a cyclical call-graph was found but no other reason
11574    for a stub was detected.  If called from the top level, a return of
11575    2 means the same as a return of 0.  */
11576
11577 static int
11578 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11579 {
11580   int ret;
11581
11582   /* Mark this section as checked.  */
11583   isec->call_check_done = 1;
11584
11585   /* We know none of our code bearing sections will need toc stubs.  */
11586   if ((isec->flags & SEC_LINKER_CREATED) != 0)
11587     return 0;
11588
11589   if (isec->size == 0)
11590     return 0;
11591
11592   if (isec->output_section == NULL)
11593     return 0;
11594
11595   ret = 0;
11596   if (isec->reloc_count != 0)
11597     {
11598       Elf_Internal_Rela *relstart, *rel;
11599       Elf_Internal_Sym *local_syms;
11600       struct ppc_link_hash_table *htab;
11601
11602       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11603                                             info->keep_memory);
11604       if (relstart == NULL)
11605         return -1;
11606
11607       /* Look for branches to outside of this section.  */
11608       local_syms = NULL;
11609       htab = ppc_hash_table (info);
11610       if (htab == NULL)
11611         return -1;
11612
11613       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11614         {
11615           enum elf_ppc64_reloc_type r_type;
11616           unsigned long r_symndx;
11617           struct elf_link_hash_entry *h;
11618           struct ppc_link_hash_entry *eh;
11619           Elf_Internal_Sym *sym;
11620           asection *sym_sec;
11621           struct _opd_sec_data *opd;
11622           bfd_vma sym_value;
11623           bfd_vma dest;
11624
11625           r_type = ELF64_R_TYPE (rel->r_info);
11626           if (r_type != R_PPC64_REL24
11627               && r_type != R_PPC64_REL14
11628               && r_type != R_PPC64_REL14_BRTAKEN
11629               && r_type != R_PPC64_REL14_BRNTAKEN)
11630             continue;
11631
11632           r_symndx = ELF64_R_SYM (rel->r_info);
11633           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11634                           isec->owner))
11635             {
11636               ret = -1;
11637               break;
11638             }
11639
11640           /* Calls to dynamic lib functions go through a plt call stub
11641              that uses r2.  */
11642           eh = (struct ppc_link_hash_entry *) h;
11643           if (eh != NULL
11644               && (eh->elf.plt.plist != NULL
11645                   || (eh->oh != NULL
11646                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11647             {
11648               ret = 1;
11649               break;
11650             }
11651
11652           if (sym_sec == NULL)
11653             /* Ignore other undefined symbols.  */
11654             continue;
11655
11656           /* Assume branches to other sections not included in the
11657              link need stubs too, to cover -R and absolute syms.  */
11658           if (sym_sec->output_section == NULL)
11659             {
11660               ret = 1;
11661               break;
11662             }
11663
11664           if (h == NULL)
11665             sym_value = sym->st_value;
11666           else
11667             {
11668               if (h->root.type != bfd_link_hash_defined
11669                   && h->root.type != bfd_link_hash_defweak)
11670                 abort ();
11671               sym_value = h->root.u.def.value;
11672             }
11673           sym_value += rel->r_addend;
11674
11675           /* If this branch reloc uses an opd sym, find the code section.  */
11676           opd = get_opd_info (sym_sec);
11677           if (opd != NULL)
11678             {
11679               if (h == NULL && opd->adjust != NULL)
11680                 {
11681                   long adjust;
11682
11683                   adjust = opd->adjust[OPD_NDX (sym_value)];
11684                   if (adjust == -1)
11685                     /* Assume deleted functions won't ever be called.  */
11686                     continue;
11687                   sym_value += adjust;
11688                 }
11689
11690               dest = opd_entry_value (sym_sec, sym_value,
11691                                       &sym_sec, NULL, FALSE);
11692               if (dest == (bfd_vma) -1)
11693                 continue;
11694             }
11695           else
11696             dest = (sym_value
11697                     + sym_sec->output_offset
11698                     + sym_sec->output_section->vma);
11699
11700           /* Ignore branch to self.  */
11701           if (sym_sec == isec)
11702             continue;
11703
11704           /* If the called function uses the toc, we need a stub.  */
11705           if (sym_sec->has_toc_reloc
11706               || sym_sec->makes_toc_func_call)
11707             {
11708               ret = 1;
11709               break;
11710             }
11711
11712           /* Assume any branch that needs a long branch stub might in fact
11713              need a plt_branch stub.  A plt_branch stub uses r2.  */
11714           else if (dest - (isec->output_offset
11715                            + isec->output_section->vma
11716                            + rel->r_offset) + (1 << 25)
11717                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11718                                                              ? h->other
11719                                                              : sym->st_other))
11720             {
11721               ret = 1;
11722               break;
11723             }
11724
11725           /* If calling back to a section in the process of being
11726              tested, we can't say for sure that no toc adjusting stubs
11727              are needed, so don't return zero.  */
11728           else if (sym_sec->call_check_in_progress)
11729             ret = 2;
11730
11731           /* Branches to another section that itself doesn't have any TOC
11732              references are OK.  Recursively call ourselves to check.  */
11733           else if (!sym_sec->call_check_done)
11734             {
11735               int recur;
11736
11737               /* Mark current section as indeterminate, so that other
11738                  sections that call back to current won't be marked as
11739                  known.  */
11740               isec->call_check_in_progress = 1;
11741               recur = toc_adjusting_stub_needed (info, sym_sec);
11742               isec->call_check_in_progress = 0;
11743
11744               if (recur != 0)
11745                 {
11746                   ret = recur;
11747                   if (recur != 2)
11748                     break;
11749                 }
11750             }
11751         }
11752
11753       if (local_syms != NULL
11754           && (elf_symtab_hdr (isec->owner).contents
11755               != (unsigned char *) local_syms))
11756         free (local_syms);
11757       if (elf_section_data (isec)->relocs != relstart)
11758         free (relstart);
11759     }
11760
11761   if ((ret & 1) == 0
11762       && isec->map_head.s != NULL
11763       && (strcmp (isec->output_section->name, ".init") == 0
11764           || strcmp (isec->output_section->name, ".fini") == 0))
11765     {
11766       if (isec->map_head.s->has_toc_reloc
11767           || isec->map_head.s->makes_toc_func_call)
11768         ret = 1;
11769       else if (!isec->map_head.s->call_check_done)
11770         {
11771           int recur;
11772           isec->call_check_in_progress = 1;
11773           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11774           isec->call_check_in_progress = 0;
11775           if (recur != 0)
11776             ret = recur;
11777         }
11778     }
11779
11780   if (ret == 1)
11781     isec->makes_toc_func_call = 1;
11782
11783   return ret;
11784 }
11785
11786 /* The linker repeatedly calls this function for each input section,
11787    in the order that input sections are linked into output sections.
11788    Build lists of input sections to determine groupings between which
11789    we may insert linker stubs.  */
11790
11791 bfd_boolean
11792 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11793 {
11794   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11795
11796   if (htab == NULL)
11797     return FALSE;
11798
11799   if ((isec->output_section->flags & SEC_CODE) != 0
11800       && isec->output_section->id < htab->sec_info_arr_size)
11801     {
11802       /* This happens to make the list in reverse order,
11803          which is what we want.  */
11804       htab->sec_info[isec->id].u.list
11805         = htab->sec_info[isec->output_section->id].u.list;
11806       htab->sec_info[isec->output_section->id].u.list = isec;
11807     }
11808
11809   if (htab->multi_toc_needed)
11810     {
11811       /* Analyse sections that aren't already flagged as needing a
11812          valid toc pointer.  Exclude .fixup for the linux kernel.
11813          .fixup contains branches, but only back to the function that
11814          hit an exception.  */
11815       if (!(isec->has_toc_reloc
11816             || (isec->flags & SEC_CODE) == 0
11817             || strcmp (isec->name, ".fixup") == 0
11818             || isec->call_check_done))
11819         {
11820           if (toc_adjusting_stub_needed (info, isec) < 0)
11821             return FALSE;
11822         }
11823       /* Make all sections use the TOC assigned for this object file.
11824          This will be wrong for pasted sections;  We fix that in
11825          check_pasted_section().  */
11826       if (elf_gp (isec->owner) != 0)
11827         htab->toc_curr = elf_gp (isec->owner);
11828     }
11829
11830   htab->sec_info[isec->id].toc_off = htab->toc_curr;
11831   return TRUE;
11832 }
11833
11834 /* Check that all .init and .fini sections use the same toc, if they
11835    have toc relocs.  */
11836
11837 static bfd_boolean
11838 check_pasted_section (struct bfd_link_info *info, const char *name)
11839 {
11840   asection *o = bfd_get_section_by_name (info->output_bfd, name);
11841
11842   if (o != NULL)
11843     {
11844       struct ppc_link_hash_table *htab = ppc_hash_table (info);
11845       bfd_vma toc_off = 0;
11846       asection *i;
11847
11848       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11849         if (i->has_toc_reloc)
11850           {
11851             if (toc_off == 0)
11852               toc_off = htab->sec_info[i->id].toc_off;
11853             else if (toc_off != htab->sec_info[i->id].toc_off)
11854               return FALSE;
11855           }
11856
11857       if (toc_off == 0)
11858         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11859           if (i->makes_toc_func_call)
11860             {
11861               toc_off = htab->sec_info[i->id].toc_off;
11862               break;
11863             }
11864
11865       /* Make sure the whole pasted function uses the same toc offset.  */
11866       if (toc_off != 0)
11867         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11868           htab->sec_info[i->id].toc_off = toc_off;
11869     }
11870   return TRUE;
11871 }
11872
11873 bfd_boolean
11874 ppc64_elf_check_init_fini (struct bfd_link_info *info)
11875 {
11876   return (check_pasted_section (info, ".init")
11877           & check_pasted_section (info, ".fini"));
11878 }
11879
11880 /* See whether we can group stub sections together.  Grouping stub
11881    sections may result in fewer stubs.  More importantly, we need to
11882    put all .init* and .fini* stubs at the beginning of the .init or
11883    .fini output sections respectively, because glibc splits the
11884    _init and _fini functions into multiple parts.  Putting a stub in
11885    the middle of a function is not a good idea.  */
11886
11887 static bfd_boolean
11888 group_sections (struct bfd_link_info *info,
11889                 bfd_size_type stub_group_size,
11890                 bfd_boolean stubs_always_before_branch)
11891 {
11892   struct ppc_link_hash_table *htab;
11893   asection *osec;
11894   bfd_size_type stub14_group_size;
11895   bfd_boolean suppress_size_errors;
11896
11897   htab = ppc_hash_table (info);
11898   if (htab == NULL)
11899     return FALSE;
11900
11901   suppress_size_errors = FALSE;
11902   stub14_group_size = stub_group_size >> 10;
11903   if (stub_group_size == 1)
11904     {
11905       /* Default values.  */
11906       if (stubs_always_before_branch)
11907         {
11908           stub_group_size = 0x1e00000;
11909           stub14_group_size = 0x7800;
11910         }
11911       else
11912         {
11913           stub_group_size = 0x1c00000;
11914           stub14_group_size = 0x7000;
11915         }
11916       suppress_size_errors = TRUE;
11917     }
11918
11919   for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
11920     {
11921       asection *tail;
11922
11923       if (osec->id >= htab->sec_info_arr_size)
11924         continue;
11925
11926       tail = htab->sec_info[osec->id].u.list;
11927       while (tail != NULL)
11928         {
11929           asection *curr;
11930           asection *prev;
11931           bfd_size_type total;
11932           bfd_boolean big_sec;
11933           bfd_vma curr_toc;
11934           struct map_stub *group;
11935
11936           curr = tail;
11937           total = tail->size;
11938           big_sec = total > (ppc64_elf_section_data (tail) != NULL
11939                              && ppc64_elf_section_data (tail)->has_14bit_branch
11940                              ? stub14_group_size : stub_group_size);
11941           if (big_sec && !suppress_size_errors)
11942             (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
11943                                      tail->owner, tail);
11944           curr_toc = htab->sec_info[tail->id].toc_off;
11945
11946           while ((prev = htab->sec_info[curr->id].u.list) != NULL
11947                  && ((total += curr->output_offset - prev->output_offset)
11948                      < (ppc64_elf_section_data (prev) != NULL
11949                         && ppc64_elf_section_data (prev)->has_14bit_branch
11950                         ? stub14_group_size : stub_group_size))
11951                  && htab->sec_info[prev->id].toc_off == curr_toc)
11952             curr = prev;
11953
11954           /* OK, the size from the start of CURR to the end is less
11955              than stub_group_size and thus can be handled by one stub
11956              section.  (or the tail section is itself larger than
11957              stub_group_size, in which case we may be toast.)  We
11958              should really be keeping track of the total size of stubs
11959              added here, as stubs contribute to the final output
11960              section size.  That's a little tricky, and this way will
11961              only break if stubs added make the total size more than
11962              2^25, ie. for the default stub_group_size, if stubs total
11963              more than 2097152 bytes, or nearly 75000 plt call stubs.  */
11964           group = bfd_alloc (curr->owner, sizeof (*group));
11965           if (group == NULL)
11966             return FALSE;
11967           group->link_sec = curr;
11968           group->stub_sec = NULL;
11969           group->needs_save_res = 0;
11970           group->next = htab->group;
11971           htab->group = group;
11972           do
11973             {
11974               prev = htab->sec_info[tail->id].u.list;
11975               /* Set up this stub group.  */
11976               htab->sec_info[tail->id].u.group = group;
11977             }
11978           while (tail != curr && (tail = prev) != NULL);
11979
11980           /* But wait, there's more!  Input sections up to stub_group_size
11981              bytes before the stub section can be handled by it too.
11982              Don't do this if we have a really large section after the
11983              stubs, as adding more stubs increases the chance that
11984              branches may not reach into the stub section.  */
11985           if (!stubs_always_before_branch && !big_sec)
11986             {
11987               total = 0;
11988               while (prev != NULL
11989                      && ((total += tail->output_offset - prev->output_offset)
11990                          < (ppc64_elf_section_data (prev) != NULL
11991                             && ppc64_elf_section_data (prev)->has_14bit_branch
11992                             ? stub14_group_size : stub_group_size))
11993                      && htab->sec_info[prev->id].toc_off == curr_toc)
11994                 {
11995                   tail = prev;
11996                   prev = htab->sec_info[tail->id].u.list;
11997                   htab->sec_info[tail->id].u.group = group;
11998                 }
11999             }
12000           tail = prev;
12001         }
12002     }
12003   return TRUE;
12004 }
12005
12006 static const unsigned char glink_eh_frame_cie[] =
12007 {
12008   0, 0, 0, 16,                          /* length.  */
12009   0, 0, 0, 0,                           /* id.  */
12010   1,                                    /* CIE version.  */
12011   'z', 'R', 0,                          /* Augmentation string.  */
12012   4,                                    /* Code alignment.  */
12013   0x78,                                 /* Data alignment.  */
12014   65,                                   /* RA reg.  */
12015   1,                                    /* Augmentation size.  */
12016   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
12017   DW_CFA_def_cfa, 1, 0,                 /* def_cfa: r1 offset 0.  */
12018   0, 0, 0, 0
12019 };
12020
12021 /* Stripping output sections is normally done before dynamic section
12022    symbols have been allocated.  This function is called later, and
12023    handles cases like htab->brlt which is mapped to its own output
12024    section.  */
12025
12026 static void
12027 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12028 {
12029   if (isec->size == 0
12030       && isec->output_section->size == 0
12031       && !(isec->output_section->flags & SEC_KEEP)
12032       && !bfd_section_removed_from_list (info->output_bfd,
12033                                          isec->output_section)
12034       && elf_section_data (isec->output_section)->dynindx == 0)
12035     {
12036       isec->output_section->flags |= SEC_EXCLUDE;
12037       bfd_section_list_remove (info->output_bfd, isec->output_section);
12038       info->output_bfd->section_count--;
12039     }
12040 }
12041
12042 /* Determine and set the size of the stub section for a final link.
12043
12044    The basic idea here is to examine all the relocations looking for
12045    PC-relative calls to a target that is unreachable with a "bl"
12046    instruction.  */
12047
12048 bfd_boolean
12049 ppc64_elf_size_stubs (struct bfd_link_info *info)
12050 {
12051   bfd_size_type stub_group_size;
12052   bfd_boolean stubs_always_before_branch;
12053   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12054
12055   if (htab == NULL)
12056     return FALSE;
12057
12058   if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12059     htab->params->plt_thread_safe = 1;
12060   if (!htab->opd_abi)
12061     htab->params->plt_thread_safe = 0;
12062   else if (htab->params->plt_thread_safe == -1)
12063     {
12064       static const char *const thread_starter[] =
12065         {
12066           "pthread_create",
12067           /* libstdc++ */
12068           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12069           /* librt */
12070           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12071           "mq_notify", "create_timer",
12072           /* libanl */
12073           "getaddrinfo_a",
12074           /* libgomp */
12075           "GOMP_parallel",
12076           "GOMP_parallel_start",
12077           "GOMP_parallel_loop_static",
12078           "GOMP_parallel_loop_static_start",
12079           "GOMP_parallel_loop_dynamic",
12080           "GOMP_parallel_loop_dynamic_start",
12081           "GOMP_parallel_loop_guided",
12082           "GOMP_parallel_loop_guided_start",
12083           "GOMP_parallel_loop_runtime",
12084           "GOMP_parallel_loop_runtime_start",
12085           "GOMP_parallel_sections",
12086           "GOMP_parallel_sections_start",
12087           /* libgo */
12088           "__go_go",
12089         };
12090       unsigned i;
12091
12092       for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12093         {
12094           struct elf_link_hash_entry *h;
12095           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12096                                     FALSE, FALSE, TRUE);
12097           htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12098           if (htab->params->plt_thread_safe)
12099             break;
12100         }
12101     }
12102   stubs_always_before_branch = htab->params->group_size < 0;
12103   if (htab->params->group_size < 0)
12104     stub_group_size = -htab->params->group_size;
12105   else
12106     stub_group_size = htab->params->group_size;
12107
12108   if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12109     return FALSE;
12110
12111   while (1)
12112     {
12113       bfd *input_bfd;
12114       unsigned int bfd_indx;
12115       struct map_stub *group;
12116       asection *stub_sec;
12117
12118       htab->stub_iteration += 1;
12119
12120       for (input_bfd = info->input_bfds, bfd_indx = 0;
12121            input_bfd != NULL;
12122            input_bfd = input_bfd->link.next, bfd_indx++)
12123         {
12124           Elf_Internal_Shdr *symtab_hdr;
12125           asection *section;
12126           Elf_Internal_Sym *local_syms = NULL;
12127
12128           if (!is_ppc64_elf (input_bfd))
12129             continue;
12130
12131           /* We'll need the symbol table in a second.  */
12132           symtab_hdr = &elf_symtab_hdr (input_bfd);
12133           if (symtab_hdr->sh_info == 0)
12134             continue;
12135
12136           /* Walk over each section attached to the input bfd.  */
12137           for (section = input_bfd->sections;
12138                section != NULL;
12139                section = section->next)
12140             {
12141               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12142
12143               /* If there aren't any relocs, then there's nothing more
12144                  to do.  */
12145               if ((section->flags & SEC_RELOC) == 0
12146                   || (section->flags & SEC_ALLOC) == 0
12147                   || (section->flags & SEC_LOAD) == 0
12148                   || (section->flags & SEC_CODE) == 0
12149                   || section->reloc_count == 0)
12150                 continue;
12151
12152               /* If this section is a link-once section that will be
12153                  discarded, then don't create any stubs.  */
12154               if (section->output_section == NULL
12155                   || section->output_section->owner != info->output_bfd)
12156                 continue;
12157
12158               /* Get the relocs.  */
12159               internal_relocs
12160                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12161                                              info->keep_memory);
12162               if (internal_relocs == NULL)
12163                 goto error_ret_free_local;
12164
12165               /* Now examine each relocation.  */
12166               irela = internal_relocs;
12167               irelaend = irela + section->reloc_count;
12168               for (; irela < irelaend; irela++)
12169                 {
12170                   enum elf_ppc64_reloc_type r_type;
12171                   unsigned int r_indx;
12172                   enum ppc_stub_type stub_type;
12173                   struct ppc_stub_hash_entry *stub_entry;
12174                   asection *sym_sec, *code_sec;
12175                   bfd_vma sym_value, code_value;
12176                   bfd_vma destination;
12177                   unsigned long local_off;
12178                   bfd_boolean ok_dest;
12179                   struct ppc_link_hash_entry *hash;
12180                   struct ppc_link_hash_entry *fdh;
12181                   struct elf_link_hash_entry *h;
12182                   Elf_Internal_Sym *sym;
12183                   char *stub_name;
12184                   const asection *id_sec;
12185                   struct _opd_sec_data *opd;
12186                   struct plt_entry *plt_ent;
12187
12188                   r_type = ELF64_R_TYPE (irela->r_info);
12189                   r_indx = ELF64_R_SYM (irela->r_info);
12190
12191                   if (r_type >= R_PPC64_max)
12192                     {
12193                       bfd_set_error (bfd_error_bad_value);
12194                       goto error_ret_free_internal;
12195                     }
12196
12197                   /* Only look for stubs on branch instructions.  */
12198                   if (r_type != R_PPC64_REL24
12199                       && r_type != R_PPC64_REL14
12200                       && r_type != R_PPC64_REL14_BRTAKEN
12201                       && r_type != R_PPC64_REL14_BRNTAKEN)
12202                     continue;
12203
12204                   /* Now determine the call target, its name, value,
12205                      section.  */
12206                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12207                                   r_indx, input_bfd))
12208                     goto error_ret_free_internal;
12209                   hash = (struct ppc_link_hash_entry *) h;
12210
12211                   ok_dest = FALSE;
12212                   fdh = NULL;
12213                   sym_value = 0;
12214                   if (hash == NULL)
12215                     {
12216                       sym_value = sym->st_value;
12217                       ok_dest = TRUE;
12218                     }
12219                   else if (hash->elf.root.type == bfd_link_hash_defined
12220                            || hash->elf.root.type == bfd_link_hash_defweak)
12221                     {
12222                       sym_value = hash->elf.root.u.def.value;
12223                       if (sym_sec->output_section != NULL)
12224                         ok_dest = TRUE;
12225                     }
12226                   else if (hash->elf.root.type == bfd_link_hash_undefweak
12227                            || hash->elf.root.type == bfd_link_hash_undefined)
12228                     {
12229                       /* Recognise an old ABI func code entry sym, and
12230                          use the func descriptor sym instead if it is
12231                          defined.  */
12232                       if (hash->elf.root.root.string[0] == '.'
12233                           && (fdh = lookup_fdh (hash, htab)) != NULL)
12234                         {
12235                           if (fdh->elf.root.type == bfd_link_hash_defined
12236                               || fdh->elf.root.type == bfd_link_hash_defweak)
12237                             {
12238                               sym_sec = fdh->elf.root.u.def.section;
12239                               sym_value = fdh->elf.root.u.def.value;
12240                               if (sym_sec->output_section != NULL)
12241                                 ok_dest = TRUE;
12242                             }
12243                           else
12244                             fdh = NULL;
12245                         }
12246                     }
12247                   else
12248                     {
12249                       bfd_set_error (bfd_error_bad_value);
12250                       goto error_ret_free_internal;
12251                     }
12252
12253                   destination = 0;
12254                   local_off = 0;
12255                   if (ok_dest)
12256                     {
12257                       sym_value += irela->r_addend;
12258                       destination = (sym_value
12259                                      + sym_sec->output_offset
12260                                      + sym_sec->output_section->vma);
12261                       local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12262                                                             ? hash->elf.other
12263                                                             : sym->st_other);
12264                     }
12265
12266                   code_sec = sym_sec;
12267                   code_value = sym_value;
12268                   opd = get_opd_info (sym_sec);
12269                   if (opd != NULL)
12270                     {
12271                       bfd_vma dest;
12272
12273                       if (hash == NULL && opd->adjust != NULL)
12274                         {
12275                           long adjust = opd->adjust[OPD_NDX (sym_value)];
12276                           if (adjust == -1)
12277                             continue;
12278                           code_value += adjust;
12279                           sym_value += adjust;
12280                         }
12281                       dest = opd_entry_value (sym_sec, sym_value,
12282                                               &code_sec, &code_value, FALSE);
12283                       if (dest != (bfd_vma) -1)
12284                         {
12285                           destination = dest;
12286                           if (fdh != NULL)
12287                             {
12288                               /* Fixup old ABI sym to point at code
12289                                  entry.  */
12290                               hash->elf.root.type = bfd_link_hash_defweak;
12291                               hash->elf.root.u.def.section = code_sec;
12292                               hash->elf.root.u.def.value = code_value;
12293                             }
12294                         }
12295                     }
12296
12297                   /* Determine what (if any) linker stub is needed.  */
12298                   plt_ent = NULL;
12299                   stub_type = ppc_type_of_stub (section, irela, &hash,
12300                                                 &plt_ent, destination,
12301                                                 local_off);
12302
12303                   if (stub_type != ppc_stub_plt_call)
12304                     {
12305                       /* Check whether we need a TOC adjusting stub.
12306                          Since the linker pastes together pieces from
12307                          different object files when creating the
12308                          _init and _fini functions, it may be that a
12309                          call to what looks like a local sym is in
12310                          fact a call needing a TOC adjustment.  */
12311                       if (code_sec != NULL
12312                           && code_sec->output_section != NULL
12313                           && (htab->sec_info[code_sec->id].toc_off
12314                               != htab->sec_info[section->id].toc_off)
12315                           && (code_sec->has_toc_reloc
12316                               || code_sec->makes_toc_func_call))
12317                         stub_type = ppc_stub_long_branch_r2off;
12318                     }
12319
12320                   if (stub_type == ppc_stub_none)
12321                     continue;
12322
12323                   /* __tls_get_addr calls might be eliminated.  */
12324                   if (stub_type != ppc_stub_plt_call
12325                       && hash != NULL
12326                       && (hash == htab->tls_get_addr
12327                           || hash == htab->tls_get_addr_fd)
12328                       && section->has_tls_reloc
12329                       && irela != internal_relocs)
12330                     {
12331                       /* Get tls info.  */
12332                       unsigned char *tls_mask;
12333
12334                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12335                                          irela - 1, input_bfd))
12336                         goto error_ret_free_internal;
12337                       if (*tls_mask != 0)
12338                         continue;
12339                     }
12340
12341                   if (stub_type == ppc_stub_plt_call
12342                       && irela + 1 < irelaend
12343                       && irela[1].r_offset == irela->r_offset + 4
12344                       && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12345                     {
12346                       if (!tocsave_find (htab, INSERT,
12347                                          &local_syms, irela + 1, input_bfd))
12348                         goto error_ret_free_internal;
12349                     }
12350                   else if (stub_type == ppc_stub_plt_call)
12351                     stub_type = ppc_stub_plt_call_r2save;
12352
12353                   /* Support for grouping stub sections.  */
12354                   id_sec = htab->sec_info[section->id].u.group->link_sec;
12355
12356                   /* Get the name of this stub.  */
12357                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12358                   if (!stub_name)
12359                     goto error_ret_free_internal;
12360
12361                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12362                                                      stub_name, FALSE, FALSE);
12363                   if (stub_entry != NULL)
12364                     {
12365                       /* The proper stub has already been created.  */
12366                       free (stub_name);
12367                       if (stub_type == ppc_stub_plt_call_r2save)
12368                         stub_entry->stub_type = stub_type;
12369                       continue;
12370                     }
12371
12372                   stub_entry = ppc_add_stub (stub_name, section, info);
12373                   if (stub_entry == NULL)
12374                     {
12375                       free (stub_name);
12376                     error_ret_free_internal:
12377                       if (elf_section_data (section)->relocs == NULL)
12378                         free (internal_relocs);
12379                     error_ret_free_local:
12380                       if (local_syms != NULL
12381                           && (symtab_hdr->contents
12382                               != (unsigned char *) local_syms))
12383                         free (local_syms);
12384                       return FALSE;
12385                     }
12386
12387                   stub_entry->stub_type = stub_type;
12388                   if (stub_type != ppc_stub_plt_call
12389                       && stub_type != ppc_stub_plt_call_r2save)
12390                     {
12391                       stub_entry->target_value = code_value;
12392                       stub_entry->target_section = code_sec;
12393                     }
12394                   else
12395                     {
12396                       stub_entry->target_value = sym_value;
12397                       stub_entry->target_section = sym_sec;
12398                     }
12399                   stub_entry->h = hash;
12400                   stub_entry->plt_ent = plt_ent;
12401                   stub_entry->other = hash ? hash->elf.other : sym->st_other;
12402
12403                   if (stub_entry->h != NULL)
12404                     htab->stub_globals += 1;
12405                 }
12406
12407               /* We're done with the internal relocs, free them.  */
12408               if (elf_section_data (section)->relocs != internal_relocs)
12409                 free (internal_relocs);
12410             }
12411
12412           if (local_syms != NULL
12413               && symtab_hdr->contents != (unsigned char *) local_syms)
12414             {
12415               if (!info->keep_memory)
12416                 free (local_syms);
12417               else
12418                 symtab_hdr->contents = (unsigned char *) local_syms;
12419             }
12420         }
12421
12422       /* We may have added some stubs.  Find out the new size of the
12423          stub sections.  */
12424       for (stub_sec = htab->params->stub_bfd->sections;
12425            stub_sec != NULL;
12426            stub_sec = stub_sec->next)
12427         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12428           {
12429             stub_sec->rawsize = stub_sec->size;
12430             stub_sec->size = 0;
12431             stub_sec->reloc_count = 0;
12432             stub_sec->flags &= ~SEC_RELOC;
12433           }
12434
12435       htab->brlt->size = 0;
12436       htab->brlt->reloc_count = 0;
12437       htab->brlt->flags &= ~SEC_RELOC;
12438       if (htab->relbrlt != NULL)
12439         htab->relbrlt->size = 0;
12440
12441       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12442
12443       for (group = htab->group; group != NULL; group = group->next)
12444         if (group->needs_save_res)
12445           group->stub_sec->size += htab->sfpr->size;
12446
12447       if (info->emitrelocations
12448           && htab->glink != NULL && htab->glink->size != 0)
12449         {
12450           htab->glink->reloc_count = 1;
12451           htab->glink->flags |= SEC_RELOC;
12452         }
12453
12454       if (htab->glink_eh_frame != NULL
12455           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12456           && htab->glink_eh_frame->output_section->size != 0)
12457         {
12458           size_t size = 0, align;
12459
12460           for (stub_sec = htab->params->stub_bfd->sections;
12461                stub_sec != NULL;
12462                stub_sec = stub_sec->next)
12463             if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12464               size += 24;
12465           if (htab->glink != NULL && htab->glink->size != 0)
12466             size += 24;
12467           if (size != 0)
12468             size += sizeof (glink_eh_frame_cie);
12469           align = 1;
12470           align <<= htab->glink_eh_frame->output_section->alignment_power;
12471           align -= 1;
12472           size = (size + align) & ~align;
12473           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12474           htab->glink_eh_frame->size = size;
12475         }
12476
12477       if (htab->params->plt_stub_align != 0)
12478         for (stub_sec = htab->params->stub_bfd->sections;
12479              stub_sec != NULL;
12480              stub_sec = stub_sec->next)
12481           if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12482             stub_sec->size = ((stub_sec->size
12483                                + (1 << htab->params->plt_stub_align) - 1)
12484                               & (-1 << htab->params->plt_stub_align));
12485
12486       for (stub_sec = htab->params->stub_bfd->sections;
12487            stub_sec != NULL;
12488            stub_sec = stub_sec->next)
12489         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12490             && stub_sec->rawsize != stub_sec->size)
12491           break;
12492
12493       /* Exit from this loop when no stubs have been added, and no stubs
12494          have changed size.  */
12495       if (stub_sec == NULL
12496           && (htab->glink_eh_frame == NULL
12497               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12498         break;
12499
12500       /* Ask the linker to do its stuff.  */
12501       (*htab->params->layout_sections_again) ();
12502     }
12503
12504   if (htab->glink_eh_frame != NULL
12505       && htab->glink_eh_frame->size != 0)
12506     {
12507       bfd_vma val;
12508       bfd_byte *p, *last_fde;
12509       size_t last_fde_len, size, align, pad;
12510       asection *stub_sec;
12511
12512       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12513       if (p == NULL)
12514         return FALSE;
12515       htab->glink_eh_frame->contents = p;
12516       last_fde = p;
12517
12518       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12519       /* CIE length (rewrite in case little-endian).  */
12520       last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12521       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12522       p += sizeof (glink_eh_frame_cie);
12523
12524       for (stub_sec = htab->params->stub_bfd->sections;
12525            stub_sec != NULL;
12526            stub_sec = stub_sec->next)
12527         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12528           {
12529             last_fde = p;
12530             last_fde_len = 20;
12531             /* FDE length.  */
12532             bfd_put_32 (htab->elf.dynobj, 20, p);
12533             p += 4;
12534             /* CIE pointer.  */
12535             val = p - htab->glink_eh_frame->contents;
12536             bfd_put_32 (htab->elf.dynobj, val, p);
12537             p += 4;
12538             /* Offset to stub section, written later.  */
12539             p += 4;
12540             /* stub section size.  */
12541             bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
12542             p += 4;
12543             /* Augmentation.  */
12544             p += 1;
12545             /* Pad.  */
12546             p += 7;
12547           }
12548       if (htab->glink != NULL && htab->glink->size != 0)
12549         {
12550           last_fde = p;
12551           last_fde_len = 20;
12552           /* FDE length.  */
12553           bfd_put_32 (htab->elf.dynobj, 20, p);
12554           p += 4;
12555           /* CIE pointer.  */
12556           val = p - htab->glink_eh_frame->contents;
12557           bfd_put_32 (htab->elf.dynobj, val, p);
12558           p += 4;
12559           /* Offset to .glink, written later.  */
12560           p += 4;
12561           /* .glink size.  */
12562           bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12563           p += 4;
12564           /* Augmentation.  */
12565           p += 1;
12566
12567           *p++ = DW_CFA_advance_loc + 1;
12568           *p++ = DW_CFA_register;
12569           *p++ = 65;
12570           *p++ = 12;
12571           *p++ = DW_CFA_advance_loc + 4;
12572           *p++ = DW_CFA_restore_extended;
12573           *p++ = 65;
12574         }
12575       /* Subsume any padding into the last FDE if user .eh_frame
12576          sections are aligned more than glink_eh_frame.  Otherwise any
12577          zero padding will be seen as a terminator.  */
12578       size = p - htab->glink_eh_frame->contents;
12579       align = 1;
12580       align <<= htab->glink_eh_frame->output_section->alignment_power;
12581       align -= 1;
12582       pad = ((size + align) & ~align) - size;
12583       htab->glink_eh_frame->size = size + pad;
12584       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12585     }
12586
12587   maybe_strip_output (info, htab->brlt);
12588   if (htab->glink_eh_frame != NULL)
12589     maybe_strip_output (info, htab->glink_eh_frame);
12590
12591   return TRUE;
12592 }
12593
12594 /* Called after we have determined section placement.  If sections
12595    move, we'll be called again.  Provide a value for TOCstart.  */
12596
12597 bfd_vma
12598 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12599 {
12600   asection *s;
12601   bfd_vma TOCstart, adjust;
12602
12603   if (info != NULL)
12604     {
12605       struct elf_link_hash_entry *h;
12606       struct elf_link_hash_table *htab = elf_hash_table (info);
12607
12608       if (is_elf_hash_table (htab)
12609           && htab->hgot != NULL)
12610         h = htab->hgot;
12611       else
12612         {
12613           h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12614           if (is_elf_hash_table (htab))
12615             htab->hgot = h;
12616         }
12617       if (h != NULL
12618           && h->root.type == bfd_link_hash_defined
12619           && !h->root.linker_def
12620           && (!is_elf_hash_table (htab)
12621               || h->def_regular))
12622         {
12623           TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12624                       + h->root.u.def.section->output_offset
12625                       + h->root.u.def.section->output_section->vma);
12626           _bfd_set_gp_value (obfd, TOCstart);
12627           return TOCstart;
12628         }
12629     }
12630
12631   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12632      order.  The TOC starts where the first of these sections starts.  */
12633   s = bfd_get_section_by_name (obfd, ".got");
12634   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12635     s = bfd_get_section_by_name (obfd, ".toc");
12636   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12637     s = bfd_get_section_by_name (obfd, ".tocbss");
12638   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12639     s = bfd_get_section_by_name (obfd, ".plt");
12640   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12641     {
12642       /* This may happen for
12643          o  references to TOC base (SYM@toc / TOC[tc0]) without a
12644          .toc directive
12645          o  bad linker script
12646          o --gc-sections and empty TOC sections
12647
12648          FIXME: Warn user?  */
12649
12650       /* Look for a likely section.  We probably won't even be
12651          using TOCstart.  */
12652       for (s = obfd->sections; s != NULL; s = s->next)
12653         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12654                          | SEC_EXCLUDE))
12655             == (SEC_ALLOC | SEC_SMALL_DATA))
12656           break;
12657       if (s == NULL)
12658         for (s = obfd->sections; s != NULL; s = s->next)
12659           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12660               == (SEC_ALLOC | SEC_SMALL_DATA))
12661             break;
12662       if (s == NULL)
12663         for (s = obfd->sections; s != NULL; s = s->next)
12664           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12665               == SEC_ALLOC)
12666             break;
12667       if (s == NULL)
12668         for (s = obfd->sections; s != NULL; s = s->next)
12669           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12670             break;
12671     }
12672
12673   TOCstart = 0;
12674   if (s != NULL)
12675     TOCstart = s->output_section->vma + s->output_offset;
12676
12677   /* Force alignment.  */
12678   adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12679   TOCstart -= adjust;
12680   _bfd_set_gp_value (obfd, TOCstart);
12681
12682   if (info != NULL && s != NULL)
12683     {
12684       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12685
12686       if (htab != NULL)
12687         {
12688           if (htab->elf.hgot != NULL)
12689             {
12690               htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
12691               htab->elf.hgot->root.u.def.section = s;
12692             }
12693         }
12694       else
12695         {
12696           struct bfd_link_hash_entry *bh = NULL;
12697           _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
12698                                             s, TOC_BASE_OFF - adjust,
12699                                             NULL, FALSE, FALSE, &bh);
12700         }
12701     }
12702   return TOCstart;
12703 }
12704
12705 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12706    write out any global entry stubs.  */
12707
12708 static bfd_boolean
12709 build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12710 {
12711   struct bfd_link_info *info;
12712   struct ppc_link_hash_table *htab;
12713   struct plt_entry *pent;
12714   asection *s;
12715
12716   if (h->root.type == bfd_link_hash_indirect)
12717     return TRUE;
12718
12719   if (!h->pointer_equality_needed)
12720     return TRUE;
12721
12722   if (h->def_regular)
12723     return TRUE;
12724
12725   info = inf;
12726   htab = ppc_hash_table (info);
12727   if (htab == NULL)
12728     return FALSE;
12729
12730   s = htab->glink;
12731   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12732     if (pent->plt.offset != (bfd_vma) -1
12733         && pent->addend == 0)
12734       {
12735         bfd_byte *p;
12736         asection *plt;
12737         bfd_vma off;
12738
12739         p = s->contents + h->root.u.def.value;
12740         plt = htab->elf.splt;
12741         if (!htab->elf.dynamic_sections_created
12742             || h->dynindx == -1)
12743           plt = htab->elf.iplt;
12744         off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12745         off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12746
12747         if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12748           {
12749             info->callbacks->einfo
12750               (_("%P: linkage table error against `%T'\n"),
12751                h->root.root.string);
12752             bfd_set_error (bfd_error_bad_value);
12753             htab->stub_error = TRUE;
12754           }
12755
12756         htab->stub_count[ppc_stub_global_entry - 1] += 1;
12757         if (htab->params->emit_stub_syms)
12758           {
12759             size_t len = strlen (h->root.root.string);
12760             char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12761
12762             if (name == NULL)
12763               return FALSE;
12764
12765             sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12766             h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12767             if (h == NULL)
12768               return FALSE;
12769             if (h->root.type == bfd_link_hash_new)
12770               {
12771                 h->root.type = bfd_link_hash_defined;
12772                 h->root.u.def.section = s;
12773                 h->root.u.def.value = p - s->contents;
12774                 h->ref_regular = 1;
12775                 h->def_regular = 1;
12776                 h->ref_regular_nonweak = 1;
12777                 h->forced_local = 1;
12778                 h->non_elf = 0;
12779                 h->root.linker_def = 1;
12780               }
12781           }
12782
12783         if (PPC_HA (off) != 0)
12784           {
12785             bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12786             p += 4;
12787           }
12788         bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12789         p += 4;
12790         bfd_put_32 (s->owner, MTCTR_R12, p);
12791         p += 4;
12792         bfd_put_32 (s->owner, BCTR, p);
12793         break;
12794       }
12795   return TRUE;
12796 }
12797
12798 /* Build all the stubs associated with the current output file.
12799    The stubs are kept in a hash table attached to the main linker
12800    hash table.  This function is called via gldelf64ppc_finish.  */
12801
12802 bfd_boolean
12803 ppc64_elf_build_stubs (struct bfd_link_info *info,
12804                        char **stats)
12805 {
12806   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12807   struct map_stub *group;
12808   asection *stub_sec;
12809   bfd_byte *p;
12810   int stub_sec_count = 0;
12811
12812   if (htab == NULL)
12813     return FALSE;
12814
12815   /* Allocate memory to hold the linker stubs.  */
12816   for (stub_sec = htab->params->stub_bfd->sections;
12817        stub_sec != NULL;
12818        stub_sec = stub_sec->next)
12819     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12820         && stub_sec->size != 0)
12821       {
12822         stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
12823         if (stub_sec->contents == NULL)
12824           return FALSE;
12825         /* We want to check that built size is the same as calculated
12826            size.  rawsize is a convenient location to use.  */
12827         stub_sec->rawsize = stub_sec->size;
12828         stub_sec->size = 0;
12829       }
12830
12831   if (htab->glink != NULL && htab->glink->size != 0)
12832     {
12833       unsigned int indx;
12834       bfd_vma plt0;
12835
12836       /* Build the .glink plt call stub.  */
12837       if (htab->params->emit_stub_syms)
12838         {
12839           struct elf_link_hash_entry *h;
12840           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12841                                     TRUE, FALSE, FALSE);
12842           if (h == NULL)
12843             return FALSE;
12844           if (h->root.type == bfd_link_hash_new)
12845             {
12846               h->root.type = bfd_link_hash_defined;
12847               h->root.u.def.section = htab->glink;
12848               h->root.u.def.value = 8;
12849               h->ref_regular = 1;
12850               h->def_regular = 1;
12851               h->ref_regular_nonweak = 1;
12852               h->forced_local = 1;
12853               h->non_elf = 0;
12854               h->root.linker_def = 1;
12855             }
12856         }
12857       plt0 = (htab->elf.splt->output_section->vma
12858               + htab->elf.splt->output_offset
12859               - 16);
12860       if (info->emitrelocations)
12861         {
12862           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12863           if (r == NULL)
12864             return FALSE;
12865           r->r_offset = (htab->glink->output_offset
12866                          + htab->glink->output_section->vma);
12867           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12868           r->r_addend = plt0;
12869         }
12870       p = htab->glink->contents;
12871       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
12872       bfd_put_64 (htab->glink->owner, plt0, p);
12873       p += 8;
12874       if (htab->opd_abi)
12875         {
12876           bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12877           p += 4;
12878           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12879           p += 4;
12880           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12881           p += 4;
12882           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12883           p += 4;
12884           bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12885           p += 4;
12886           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12887           p += 4;
12888           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12889           p += 4;
12890           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12891           p += 4;
12892           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12893           p += 4;
12894           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12895           p += 4;
12896         }
12897       else
12898         {
12899           bfd_put_32 (htab->glink->owner, MFLR_R0, p);
12900           p += 4;
12901           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12902           p += 4;
12903           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12904           p += 4;
12905           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12906           p += 4;
12907           bfd_put_32 (htab->glink->owner, MTLR_R0, p);
12908           p += 4;
12909           bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
12910           p += 4;
12911           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12912           p += 4;
12913           bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
12914           p += 4;
12915           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12916           p += 4;
12917           bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
12918           p += 4;
12919           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12920           p += 4;
12921           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
12922           p += 4;
12923         }
12924       bfd_put_32 (htab->glink->owner, BCTR, p);
12925       p += 4;
12926       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
12927         {
12928           bfd_put_32 (htab->glink->owner, NOP, p);
12929           p += 4;
12930         }
12931
12932       /* Build the .glink lazy link call stubs.  */
12933       indx = 0;
12934       while (p < htab->glink->contents + htab->glink->rawsize)
12935         {
12936           if (htab->opd_abi)
12937             {
12938               if (indx < 0x8000)
12939                 {
12940                   bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
12941                   p += 4;
12942                 }
12943               else
12944                 {
12945                   bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
12946                   p += 4;
12947                   bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
12948                               p);
12949                   p += 4;
12950                 }
12951             }
12952           bfd_put_32 (htab->glink->owner,
12953                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
12954           indx++;
12955           p += 4;
12956         }
12957
12958       /* Build .glink global entry stubs.  */
12959       if (htab->glink->size > htab->glink->rawsize)
12960         elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
12961     }
12962
12963   if (htab->brlt != NULL && htab->brlt->size != 0)
12964     {
12965       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
12966                                          htab->brlt->size);
12967       if (htab->brlt->contents == NULL)
12968         return FALSE;
12969     }
12970   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
12971     {
12972       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
12973                                             htab->relbrlt->size);
12974       if (htab->relbrlt->contents == NULL)
12975         return FALSE;
12976     }
12977
12978   /* Build the stubs as directed by the stub hash table.  */
12979   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
12980
12981   for (group = htab->group; group != NULL; group = group->next)
12982     if (group->needs_save_res)
12983       {
12984         stub_sec = group->stub_sec;
12985         memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
12986                 htab->sfpr->size);
12987         if (htab->params->emit_stub_syms)
12988           {
12989             unsigned int i;
12990
12991             for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
12992               if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
12993                 return FALSE;
12994           }
12995         stub_sec->size += htab->sfpr->size;
12996       }
12997
12998   if (htab->relbrlt != NULL)
12999     htab->relbrlt->reloc_count = 0;
13000
13001   if (htab->params->plt_stub_align != 0)
13002     for (stub_sec = htab->params->stub_bfd->sections;
13003          stub_sec != NULL;
13004          stub_sec = stub_sec->next)
13005       if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13006         stub_sec->size = ((stub_sec->size
13007                            + (1 << htab->params->plt_stub_align) - 1)
13008                           & (-1 << htab->params->plt_stub_align));
13009
13010   for (stub_sec = htab->params->stub_bfd->sections;
13011        stub_sec != NULL;
13012        stub_sec = stub_sec->next)
13013     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13014       {
13015         stub_sec_count += 1;
13016         if (stub_sec->rawsize != stub_sec->size)
13017           break;
13018       }
13019
13020   /* Note that the glink_eh_frame check here is not only testing that
13021      the generated size matched the calculated size but also that
13022      bfd_elf_discard_info didn't make any changes to the section.  */
13023   if (stub_sec != NULL
13024       || (htab->glink_eh_frame != NULL
13025           && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
13026     {
13027       htab->stub_error = TRUE;
13028       info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
13029     }
13030
13031   if (htab->stub_error)
13032     return FALSE;
13033
13034   if (stats != NULL)
13035     {
13036       *stats = bfd_malloc (500);
13037       if (*stats == NULL)
13038         return FALSE;
13039
13040       sprintf (*stats, _("linker stubs in %u group%s\n"
13041                          "  branch       %lu\n"
13042                          "  toc adjust   %lu\n"
13043                          "  long branch  %lu\n"
13044                          "  long toc adj %lu\n"
13045                          "  plt call     %lu\n"
13046                          "  plt call toc %lu\n"
13047                          "  global entry %lu"),
13048                stub_sec_count,
13049                stub_sec_count == 1 ? "" : "s",
13050                htab->stub_count[ppc_stub_long_branch - 1],
13051                htab->stub_count[ppc_stub_long_branch_r2off - 1],
13052                htab->stub_count[ppc_stub_plt_branch - 1],
13053                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13054                htab->stub_count[ppc_stub_plt_call - 1],
13055                htab->stub_count[ppc_stub_plt_call_r2save - 1],
13056                htab->stub_count[ppc_stub_global_entry - 1]);
13057     }
13058   return TRUE;
13059 }
13060
13061 /* This function undoes the changes made by add_symbol_adjust.  */
13062
13063 static bfd_boolean
13064 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
13065 {
13066   struct ppc_link_hash_entry *eh;
13067
13068   if (h->root.type == bfd_link_hash_indirect)
13069     return TRUE;
13070
13071   eh = (struct ppc_link_hash_entry *) h;
13072   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
13073     return TRUE;
13074
13075   eh->elf.root.type = bfd_link_hash_undefined;
13076   return TRUE;
13077 }
13078
13079 void
13080 ppc64_elf_restore_symbols (struct bfd_link_info *info)
13081 {
13082   struct ppc_link_hash_table *htab = ppc_hash_table (info);
13083
13084   if (htab != NULL)
13085     elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
13086 }
13087
13088 /* What to do when ld finds relocations against symbols defined in
13089    discarded sections.  */
13090
13091 static unsigned int
13092 ppc64_elf_action_discarded (asection *sec)
13093 {
13094   if (strcmp (".opd", sec->name) == 0)
13095     return 0;
13096
13097   if (strcmp (".toc", sec->name) == 0)
13098     return 0;
13099
13100   if (strcmp (".toc1", sec->name) == 0)
13101     return 0;
13102
13103   return _bfd_elf_default_action_discarded (sec);
13104 }
13105
13106 /* The RELOCATE_SECTION function is called by the ELF backend linker
13107    to handle the relocations for a section.
13108
13109    The relocs are always passed as Rela structures; if the section
13110    actually uses Rel structures, the r_addend field will always be
13111    zero.
13112
13113    This function is responsible for adjust the section contents as
13114    necessary, and (if using Rela relocs and generating a
13115    relocatable output file) adjusting the reloc addend as
13116    necessary.
13117
13118    This function does not have to worry about setting the reloc
13119    address or the reloc symbol index.
13120
13121    LOCAL_SYMS is a pointer to the swapped in local symbols.
13122
13123    LOCAL_SECTIONS is an array giving the section in the input file
13124    corresponding to the st_shndx field of each local symbol.
13125
13126    The global hash table entry for the global symbols can be found
13127    via elf_sym_hashes (input_bfd).
13128
13129    When generating relocatable output, this function must handle
13130    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
13131    going to be the section symbol corresponding to the output
13132    section, which means that the addend must be adjusted
13133    accordingly.  */
13134
13135 static bfd_boolean
13136 ppc64_elf_relocate_section (bfd *output_bfd,
13137                             struct bfd_link_info *info,
13138                             bfd *input_bfd,
13139                             asection *input_section,
13140                             bfd_byte *contents,
13141                             Elf_Internal_Rela *relocs,
13142                             Elf_Internal_Sym *local_syms,
13143                             asection **local_sections)
13144 {
13145   struct ppc_link_hash_table *htab;
13146   Elf_Internal_Shdr *symtab_hdr;
13147   struct elf_link_hash_entry **sym_hashes;
13148   Elf_Internal_Rela *rel;
13149   Elf_Internal_Rela *relend;
13150   Elf_Internal_Rela outrel;
13151   bfd_byte *loc;
13152   struct got_entry **local_got_ents;
13153   bfd_vma TOCstart;
13154   bfd_boolean ret = TRUE;
13155   bfd_boolean is_opd;
13156   /* Assume 'at' branch hints.  */
13157   bfd_boolean is_isa_v2 = TRUE;
13158   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
13159
13160   /* Initialize howto table if needed.  */
13161   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13162     ppc_howto_init ();
13163
13164   htab = ppc_hash_table (info);
13165   if (htab == NULL)
13166     return FALSE;
13167
13168   /* Don't relocate stub sections.  */
13169   if (input_section->owner == htab->params->stub_bfd)
13170     return TRUE;
13171
13172   BFD_ASSERT (is_ppc64_elf (input_bfd));
13173
13174   local_got_ents = elf_local_got_ents (input_bfd);
13175   TOCstart = elf_gp (output_bfd);
13176   symtab_hdr = &elf_symtab_hdr (input_bfd);
13177   sym_hashes = elf_sym_hashes (input_bfd);
13178   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13179
13180   rel = relocs;
13181   relend = relocs + input_section->reloc_count;
13182   for (; rel < relend; rel++)
13183     {
13184       enum elf_ppc64_reloc_type r_type;
13185       bfd_vma addend;
13186       bfd_reloc_status_type r;
13187       Elf_Internal_Sym *sym;
13188       asection *sec;
13189       struct elf_link_hash_entry *h_elf;
13190       struct ppc_link_hash_entry *h;
13191       struct ppc_link_hash_entry *fdh;
13192       const char *sym_name;
13193       unsigned long r_symndx, toc_symndx;
13194       bfd_vma toc_addend;
13195       unsigned char tls_mask, tls_gd, tls_type;
13196       unsigned char sym_type;
13197       bfd_vma relocation;
13198       bfd_boolean unresolved_reloc;
13199       bfd_boolean warned;
13200       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13201       unsigned int insn;
13202       unsigned int mask;
13203       struct ppc_stub_hash_entry *stub_entry;
13204       bfd_vma max_br_offset;
13205       bfd_vma from;
13206       const Elf_Internal_Rela orig_rel = *rel;
13207       reloc_howto_type *howto;
13208       struct reloc_howto_struct alt_howto;
13209
13210       r_type = ELF64_R_TYPE (rel->r_info);
13211       r_symndx = ELF64_R_SYM (rel->r_info);
13212
13213       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13214          symbol of the previous ADDR64 reloc.  The symbol gives us the
13215          proper TOC base to use.  */
13216       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13217           && rel != relocs
13218           && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
13219           && is_opd)
13220         r_symndx = ELF64_R_SYM (rel[-1].r_info);
13221
13222       sym = NULL;
13223       sec = NULL;
13224       h_elf = NULL;
13225       sym_name = NULL;
13226       unresolved_reloc = FALSE;
13227       warned = FALSE;
13228
13229       if (r_symndx < symtab_hdr->sh_info)
13230         {
13231           /* It's a local symbol.  */
13232           struct _opd_sec_data *opd;
13233
13234           sym = local_syms + r_symndx;
13235           sec = local_sections[r_symndx];
13236           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13237           sym_type = ELF64_ST_TYPE (sym->st_info);
13238           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13239           opd = get_opd_info (sec);
13240           if (opd != NULL && opd->adjust != NULL)
13241             {
13242               long adjust = opd->adjust[OPD_NDX (sym->st_value
13243                                                  + rel->r_addend)];
13244               if (adjust == -1)
13245                 relocation = 0;
13246               else
13247                 {
13248                   /* If this is a relocation against the opd section sym
13249                      and we have edited .opd, adjust the reloc addend so
13250                      that ld -r and ld --emit-relocs output is correct.
13251                      If it is a reloc against some other .opd symbol,
13252                      then the symbol value will be adjusted later.  */
13253                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13254                     rel->r_addend += adjust;
13255                   else
13256                     relocation += adjust;
13257                 }
13258             }
13259         }
13260       else
13261         {
13262           bfd_boolean ignored;
13263
13264           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13265                                    r_symndx, symtab_hdr, sym_hashes,
13266                                    h_elf, sec, relocation,
13267                                    unresolved_reloc, warned, ignored);
13268           sym_name = h_elf->root.root.string;
13269           sym_type = h_elf->type;
13270           if (sec != NULL
13271               && sec->owner == output_bfd
13272               && strcmp (sec->name, ".opd") == 0)
13273             {
13274               /* This is a symbol defined in a linker script.  All
13275                  such are defined in output sections, even those
13276                  defined by simple assignment from a symbol defined in
13277                  an input section.  Transfer the symbol to an
13278                  appropriate input .opd section, so that a branch to
13279                  this symbol will be mapped to the location specified
13280                  by the opd entry.  */
13281               struct bfd_link_order *lo;
13282               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13283                 if (lo->type == bfd_indirect_link_order)
13284                   {
13285                     asection *isec = lo->u.indirect.section;
13286                     if (h_elf->root.u.def.value >= isec->output_offset
13287                         && h_elf->root.u.def.value < (isec->output_offset
13288                                                       + isec->size))
13289                       {
13290                         h_elf->root.u.def.value -= isec->output_offset;
13291                         h_elf->root.u.def.section = isec;
13292                         sec = isec;
13293                         break;
13294                       }
13295                   }
13296             }
13297         }
13298       h = (struct ppc_link_hash_entry *) h_elf;
13299
13300       if (sec != NULL && discarded_section (sec))
13301         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
13302                                          rel, 1, relend,
13303                                          ppc64_elf_howto_table[r_type], 0,
13304                                          contents);
13305
13306       if (bfd_link_relocatable (info))
13307         continue;
13308
13309       if (h != NULL && &h->elf == htab->elf.hgot)
13310         {
13311           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13312           sec = bfd_abs_section_ptr;
13313           unresolved_reloc = FALSE;
13314         }
13315
13316       /* TLS optimizations.  Replace instruction sequences and relocs
13317          based on information we collected in tls_optimize.  We edit
13318          RELOCS so that --emit-relocs will output something sensible
13319          for the final instruction stream.  */
13320       tls_mask = 0;
13321       tls_gd = 0;
13322       toc_symndx = 0;
13323       if (h != NULL)
13324         tls_mask = h->tls_mask;
13325       else if (local_got_ents != NULL)
13326         {
13327           struct plt_entry **local_plt = (struct plt_entry **)
13328             (local_got_ents + symtab_hdr->sh_info);
13329           unsigned char *lgot_masks = (unsigned char *)
13330             (local_plt + symtab_hdr->sh_info);
13331           tls_mask = lgot_masks[r_symndx];
13332         }
13333       if (tls_mask == 0
13334           && (r_type == R_PPC64_TLS
13335               || r_type == R_PPC64_TLSGD
13336               || r_type == R_PPC64_TLSLD))
13337         {
13338           /* Check for toc tls entries.  */
13339           unsigned char *toc_tls;
13340
13341           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13342                              &local_syms, rel, input_bfd))
13343             return FALSE;
13344
13345           if (toc_tls)
13346             tls_mask = *toc_tls;
13347         }
13348
13349       /* Check that tls relocs are used with tls syms, and non-tls
13350          relocs are used with non-tls syms.  */
13351       if (r_symndx != STN_UNDEF
13352           && r_type != R_PPC64_NONE
13353           && (h == NULL
13354               || h->elf.root.type == bfd_link_hash_defined
13355               || h->elf.root.type == bfd_link_hash_defweak)
13356           && (IS_PPC64_TLS_RELOC (r_type)
13357               != (sym_type == STT_TLS
13358                   || (sym_type == STT_SECTION
13359                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13360         {
13361           if (tls_mask != 0
13362               && (r_type == R_PPC64_TLS
13363                   || r_type == R_PPC64_TLSGD
13364                   || r_type == R_PPC64_TLSLD))
13365             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
13366             ;
13367           else
13368             info->callbacks->einfo
13369               (!IS_PPC64_TLS_RELOC (r_type)
13370                ? _("%P: %H: %s used with TLS symbol `%T'\n")
13371                : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
13372                input_bfd, input_section, rel->r_offset,
13373                ppc64_elf_howto_table[r_type]->name,
13374                sym_name);
13375         }
13376
13377       /* Ensure reloc mapping code below stays sane.  */
13378       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13379           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13380           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
13381           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13382           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13383           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13384           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
13385           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13386           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13387           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13388         abort ();
13389
13390       switch (r_type)
13391         {
13392         default:
13393           break;
13394
13395         case R_PPC64_LO_DS_OPT:
13396           insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
13397           if ((insn & (0x3f << 26)) != 58u << 26)
13398             abort ();
13399           insn += (14u << 26) - (58u << 26);
13400           bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
13401           r_type = R_PPC64_TOC16_LO;
13402           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13403           break;
13404
13405         case R_PPC64_TOC16:
13406         case R_PPC64_TOC16_LO:
13407         case R_PPC64_TOC16_DS:
13408         case R_PPC64_TOC16_LO_DS:
13409           {
13410             /* Check for toc tls entries.  */
13411             unsigned char *toc_tls;
13412             int retval;
13413
13414             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13415                                    &local_syms, rel, input_bfd);
13416             if (retval == 0)
13417               return FALSE;
13418
13419             if (toc_tls)
13420               {
13421                 tls_mask = *toc_tls;
13422                 if (r_type == R_PPC64_TOC16_DS
13423                     || r_type == R_PPC64_TOC16_LO_DS)
13424                   {
13425                     if (tls_mask != 0
13426                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13427                       goto toctprel;
13428                   }
13429                 else
13430                   {
13431                     /* If we found a GD reloc pair, then we might be
13432                        doing a GD->IE transition.  */
13433                     if (retval == 2)
13434                       {
13435                         tls_gd = TLS_TPRELGD;
13436                         if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13437                           goto tls_ldgd_opt;
13438                       }
13439                     else if (retval == 3)
13440                       {
13441                         if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13442                           goto tls_ldgd_opt;
13443                       }
13444                   }
13445               }
13446           }
13447           break;
13448
13449         case R_PPC64_GOT_TPREL16_HI:
13450         case R_PPC64_GOT_TPREL16_HA:
13451           if (tls_mask != 0
13452               && (tls_mask & TLS_TPREL) == 0)
13453             {
13454               rel->r_offset -= d_offset;
13455               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13456               r_type = R_PPC64_NONE;
13457               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13458             }
13459           break;
13460
13461         case R_PPC64_GOT_TPREL16_DS:
13462         case R_PPC64_GOT_TPREL16_LO_DS:
13463           if (tls_mask != 0
13464               && (tls_mask & TLS_TPREL) == 0)
13465             {
13466             toctprel:
13467               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
13468               insn &= 31 << 21;
13469               insn |= 0x3c0d0000;       /* addis 0,13,0 */
13470               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
13471               r_type = R_PPC64_TPREL16_HA;
13472               if (toc_symndx != 0)
13473                 {
13474                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13475                   rel->r_addend = toc_addend;
13476                   /* We changed the symbol.  Start over in order to
13477                      get h, sym, sec etc. right.  */
13478                   rel--;
13479                   continue;
13480                 }
13481               else
13482                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13483             }
13484           break;
13485
13486         case R_PPC64_TLS:
13487           if (tls_mask != 0
13488               && (tls_mask & TLS_TPREL) == 0)
13489             {
13490               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
13491               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13492               if (insn == 0)
13493                 abort ();
13494               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
13495               /* Was PPC64_TLS which sits on insn boundary, now
13496                  PPC64_TPREL16_LO which is at low-order half-word.  */
13497               rel->r_offset += d_offset;
13498               r_type = R_PPC64_TPREL16_LO;
13499               if (toc_symndx != 0)
13500                 {
13501                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13502                   rel->r_addend = toc_addend;
13503                   /* We changed the symbol.  Start over in order to
13504                      get h, sym, sec etc. right.  */
13505                   rel--;
13506                   continue;
13507                 }
13508               else
13509                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13510             }
13511           break;
13512
13513         case R_PPC64_GOT_TLSGD16_HI:
13514         case R_PPC64_GOT_TLSGD16_HA:
13515           tls_gd = TLS_TPRELGD;
13516           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13517             goto tls_gdld_hi;
13518           break;
13519
13520         case R_PPC64_GOT_TLSLD16_HI:
13521         case R_PPC64_GOT_TLSLD16_HA:
13522           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13523             {
13524             tls_gdld_hi:
13525               if ((tls_mask & tls_gd) != 0)
13526                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13527                           + R_PPC64_GOT_TPREL16_DS);
13528               else
13529                 {
13530                   rel->r_offset -= d_offset;
13531                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13532                   r_type = R_PPC64_NONE;
13533                 }
13534               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13535             }
13536           break;
13537
13538         case R_PPC64_GOT_TLSGD16:
13539         case R_PPC64_GOT_TLSGD16_LO:
13540           tls_gd = TLS_TPRELGD;
13541           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13542             goto tls_ldgd_opt;
13543           break;
13544
13545         case R_PPC64_GOT_TLSLD16:
13546         case R_PPC64_GOT_TLSLD16_LO:
13547           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13548             {
13549               unsigned int insn1, insn2, insn3;
13550               bfd_vma offset;
13551
13552             tls_ldgd_opt:
13553               offset = (bfd_vma) -1;
13554               /* If not using the newer R_PPC64_TLSGD/LD to mark
13555                  __tls_get_addr calls, we must trust that the call
13556                  stays with its arg setup insns, ie. that the next
13557                  reloc is the __tls_get_addr call associated with
13558                  the current reloc.  Edit both insns.  */
13559               if (input_section->has_tls_get_addr_call
13560                   && rel + 1 < relend
13561                   && branch_reloc_hash_match (input_bfd, rel + 1,
13562                                               htab->tls_get_addr,
13563                                               htab->tls_get_addr_fd))
13564                 offset = rel[1].r_offset;
13565               /* We read the low GOT_TLS (or TOC16) insn because we
13566                  need to keep the destination reg.  It may be
13567                  something other than the usual r3, and moved to r3
13568                  before the call by intervening code.  */
13569               insn1 = bfd_get_32 (output_bfd,
13570                                   contents + rel->r_offset - d_offset);
13571               if ((tls_mask & tls_gd) != 0)
13572                 {
13573                   /* IE */
13574                   insn1 &= (0x1f << 21) | (0x1f << 16);
13575                   insn1 |= 58 << 26;    /* ld */
13576                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13577                   if (offset != (bfd_vma) -1)
13578                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13579                   if ((tls_mask & TLS_EXPLICIT) == 0)
13580                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13581                               + R_PPC64_GOT_TPREL16_DS);
13582                   else
13583                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13584                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13585                 }
13586               else
13587                 {
13588                   /* LE */
13589                   insn1 &= 0x1f << 21;
13590                   insn1 |= 0x3c0d0000;  /* addis r,13,0 */
13591                   insn2 = 0x38630000;   /* addi 3,3,0 */
13592                   if (tls_gd == 0)
13593                     {
13594                       /* Was an LD reloc.  */
13595                       if (toc_symndx)
13596                         sec = local_sections[toc_symndx];
13597                       for (r_symndx = 0;
13598                            r_symndx < symtab_hdr->sh_info;
13599                            r_symndx++)
13600                         if (local_sections[r_symndx] == sec)
13601                           break;
13602                       if (r_symndx >= symtab_hdr->sh_info)
13603                         r_symndx = STN_UNDEF;
13604                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13605                       if (r_symndx != STN_UNDEF)
13606                         rel->r_addend -= (local_syms[r_symndx].st_value
13607                                           + sec->output_offset
13608                                           + sec->output_section->vma);
13609                     }
13610                   else if (toc_symndx != 0)
13611                     {
13612                       r_symndx = toc_symndx;
13613                       rel->r_addend = toc_addend;
13614                     }
13615                   r_type = R_PPC64_TPREL16_HA;
13616                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13617                   if (offset != (bfd_vma) -1)
13618                     {
13619                       rel[1].r_info = ELF64_R_INFO (r_symndx,
13620                                                     R_PPC64_TPREL16_LO);
13621                       rel[1].r_offset = offset + d_offset;
13622                       rel[1].r_addend = rel->r_addend;
13623                     }
13624                 }
13625               bfd_put_32 (output_bfd, insn1,
13626                           contents + rel->r_offset - d_offset);
13627               if (offset != (bfd_vma) -1)
13628                 {
13629                   insn3 = bfd_get_32 (output_bfd,
13630                                       contents + offset + 4);
13631                   if (insn3 == NOP
13632                       || insn3 == CROR_151515 || insn3 == CROR_313131)
13633                     {
13634                       rel[1].r_offset += 4;
13635                       bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13636                       insn2 = NOP;
13637                     }
13638                   bfd_put_32 (output_bfd, insn2, contents + offset);
13639                 }
13640               if ((tls_mask & tls_gd) == 0
13641                   && (tls_gd == 0 || toc_symndx != 0))
13642                 {
13643                   /* We changed the symbol.  Start over in order
13644                      to get h, sym, sec etc. right.  */
13645                   rel--;
13646                   continue;
13647                 }
13648             }
13649           break;
13650
13651         case R_PPC64_TLSGD:
13652           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13653             {
13654               unsigned int insn2, insn3;
13655               bfd_vma offset = rel->r_offset;
13656
13657               if ((tls_mask & TLS_TPRELGD) != 0)
13658                 {
13659                   /* IE */
13660                   r_type = R_PPC64_NONE;
13661                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13662                 }
13663               else
13664                 {
13665                   /* LE */
13666                   if (toc_symndx != 0)
13667                     {
13668                       r_symndx = toc_symndx;
13669                       rel->r_addend = toc_addend;
13670                     }
13671                   r_type = R_PPC64_TPREL16_LO;
13672                   rel->r_offset = offset + d_offset;
13673                   insn2 = 0x38630000;   /* addi 3,3,0 */
13674                 }
13675               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13676               /* Zap the reloc on the _tls_get_addr call too.  */
13677               BFD_ASSERT (offset == rel[1].r_offset);
13678               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13679               insn3 = bfd_get_32 (output_bfd,
13680                                   contents + offset + 4);
13681               if (insn3 == NOP
13682                   || insn3 == CROR_151515 || insn3 == CROR_313131)
13683                 {
13684                   rel->r_offset += 4;
13685                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13686                   insn2 = NOP;
13687                 }
13688               bfd_put_32 (output_bfd, insn2, contents + offset);
13689               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13690                 {
13691                   rel--;
13692                   continue;
13693                 }
13694             }
13695           break;
13696
13697         case R_PPC64_TLSLD:
13698           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13699             {
13700               unsigned int insn2, insn3;
13701               bfd_vma offset = rel->r_offset;
13702
13703               if (toc_symndx)
13704                 sec = local_sections[toc_symndx];
13705               for (r_symndx = 0;
13706                    r_symndx < symtab_hdr->sh_info;
13707                    r_symndx++)
13708                 if (local_sections[r_symndx] == sec)
13709                   break;
13710               if (r_symndx >= symtab_hdr->sh_info)
13711                 r_symndx = STN_UNDEF;
13712               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13713               if (r_symndx != STN_UNDEF)
13714                 rel->r_addend -= (local_syms[r_symndx].st_value
13715                                   + sec->output_offset
13716                                   + sec->output_section->vma);
13717
13718               r_type = R_PPC64_TPREL16_LO;
13719               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13720               rel->r_offset = offset + d_offset;
13721               /* Zap the reloc on the _tls_get_addr call too.  */
13722               BFD_ASSERT (offset == rel[1].r_offset);
13723               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13724               insn2 = 0x38630000;       /* addi 3,3,0 */
13725               insn3 = bfd_get_32 (output_bfd,
13726                                   contents + offset + 4);
13727               if (insn3 == NOP
13728                   || insn3 == CROR_151515 || insn3 == CROR_313131)
13729                 {
13730                   rel->r_offset += 4;
13731                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13732                   insn2 = NOP;
13733                 }
13734               bfd_put_32 (output_bfd, insn2, contents + offset);
13735               rel--;
13736               continue;
13737             }
13738           break;
13739
13740         case R_PPC64_DTPMOD64:
13741           if (rel + 1 < relend
13742               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13743               && rel[1].r_offset == rel->r_offset + 8)
13744             {
13745               if ((tls_mask & TLS_GD) == 0)
13746                 {
13747                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13748                   if ((tls_mask & TLS_TPRELGD) != 0)
13749                     r_type = R_PPC64_TPREL64;
13750                   else
13751                     {
13752                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13753                       r_type = R_PPC64_NONE;
13754                     }
13755                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13756                 }
13757             }
13758           else
13759             {
13760               if ((tls_mask & TLS_LD) == 0)
13761                 {
13762                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13763                   r_type = R_PPC64_NONE;
13764                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13765                 }
13766             }
13767           break;
13768
13769         case R_PPC64_TPREL64:
13770           if ((tls_mask & TLS_TPREL) == 0)
13771             {
13772               r_type = R_PPC64_NONE;
13773               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13774             }
13775           break;
13776
13777         case R_PPC64_REL16_HA:
13778           /* If we are generating a non-PIC executable, edit
13779              .  0:      addis 2,12,.TOC.-0b@ha
13780              .          addi 2,2,.TOC.-0b@l
13781              used by ELFv2 global entry points to set up r2, to
13782              .          lis 2,.TOC.@ha
13783              .          addi 2,2,.TOC.@l
13784              if .TOC. is in range.  */
13785           if (!bfd_link_pic (info)
13786               && !info->traditional_format
13787               && h != NULL && &h->elf == htab->elf.hgot
13788               && rel + 1 < relend
13789               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
13790               && rel[1].r_offset == rel->r_offset + 4
13791               && rel[1].r_addend == rel->r_addend + 4
13792               && relocation + 0x80008000 <= 0xffffffff)
13793             {
13794               unsigned int insn1, insn2;
13795               bfd_vma offset = rel->r_offset - d_offset;
13796               insn1 = bfd_get_32 (output_bfd, contents + offset);
13797               insn2 = bfd_get_32 (output_bfd, contents + offset + 4);
13798               if ((insn1 & 0xffff0000) == 0x3c4c0000 /* addis 2,12 */
13799                   && (insn2 & 0xffff0000) == 0x38420000 /* addi 2,2 */)
13800                 {
13801                   r_type = R_PPC64_ADDR16_HA;
13802                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13803                   rel->r_addend -= d_offset;
13804                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
13805                   rel[1].r_addend -= d_offset + 4;
13806                   bfd_put_32 (output_bfd, 0x3c400000, contents + offset);
13807                 }
13808             }
13809           break;
13810         }
13811
13812       /* Handle other relocations that tweak non-addend part of insn.  */
13813       insn = 0;
13814       max_br_offset = 1 << 25;
13815       addend = rel->r_addend;
13816       reloc_dest = DEST_NORMAL;
13817       switch (r_type)
13818         {
13819         default:
13820           break;
13821
13822         case R_PPC64_TOCSAVE:
13823           if (relocation + addend == (rel->r_offset
13824                                       + input_section->output_offset
13825                                       + input_section->output_section->vma)
13826               && tocsave_find (htab, NO_INSERT,
13827                                &local_syms, rel, input_bfd))
13828             {
13829               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13830               if (insn == NOP
13831                   || insn == CROR_151515 || insn == CROR_313131)
13832                 bfd_put_32 (input_bfd,
13833                             STD_R2_0R1 + STK_TOC (htab),
13834                             contents + rel->r_offset);
13835             }
13836           break;
13837
13838           /* Branch taken prediction relocations.  */
13839         case R_PPC64_ADDR14_BRTAKEN:
13840         case R_PPC64_REL14_BRTAKEN:
13841           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
13842           /* Fall thru.  */
13843
13844           /* Branch not taken prediction relocations.  */
13845         case R_PPC64_ADDR14_BRNTAKEN:
13846         case R_PPC64_REL14_BRNTAKEN:
13847           insn |= bfd_get_32 (output_bfd,
13848                               contents + rel->r_offset) & ~(0x01 << 21);
13849           /* Fall thru.  */
13850
13851         case R_PPC64_REL14:
13852           max_br_offset = 1 << 15;
13853           /* Fall thru.  */
13854
13855         case R_PPC64_REL24:
13856           /* Calls to functions with a different TOC, such as calls to
13857              shared objects, need to alter the TOC pointer.  This is
13858              done using a linkage stub.  A REL24 branching to these
13859              linkage stubs needs to be followed by a nop, as the nop
13860              will be replaced with an instruction to restore the TOC
13861              base pointer.  */
13862           fdh = h;
13863           if (h != NULL
13864               && h->oh != NULL
13865               && h->oh->is_func_descriptor)
13866             fdh = ppc_follow_link (h->oh);
13867           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13868                                            htab);
13869           if (stub_entry != NULL
13870               && (stub_entry->stub_type == ppc_stub_plt_call
13871                   || stub_entry->stub_type == ppc_stub_plt_call_r2save
13872                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13873                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
13874             {
13875               bfd_boolean can_plt_call = FALSE;
13876
13877               /* All of these stubs will modify r2, so there must be a
13878                  branch and link followed by a nop.  The nop is
13879                  replaced by an insn to restore r2.  */
13880               if (rel->r_offset + 8 <= input_section->size)
13881                 {
13882                   unsigned long br;
13883
13884                   br = bfd_get_32 (input_bfd,
13885                                    contents + rel->r_offset);
13886                   if ((br & 1) != 0)
13887                     {
13888                       unsigned long nop;
13889
13890                       nop = bfd_get_32 (input_bfd,
13891                                         contents + rel->r_offset + 4);
13892                       if (nop == NOP
13893                           || nop == CROR_151515 || nop == CROR_313131)
13894                         {
13895                           if (h != NULL
13896                               && (h == htab->tls_get_addr_fd
13897                                   || h == htab->tls_get_addr)
13898                               && !htab->params->no_tls_get_addr_opt)
13899                             {
13900                               /* Special stub used, leave nop alone.  */
13901                             }
13902                           else
13903                             bfd_put_32 (input_bfd,
13904                                         LD_R2_0R1 + STK_TOC (htab),
13905                                         contents + rel->r_offset + 4);
13906                           can_plt_call = TRUE;
13907                         }
13908                     }
13909                 }
13910
13911               if (!can_plt_call && h != NULL)
13912                 {
13913                   const char *name = h->elf.root.root.string;
13914
13915                   if (*name == '.')
13916                     ++name;
13917
13918                   if (strncmp (name, "__libc_start_main", 17) == 0
13919                       && (name[17] == 0 || name[17] == '@'))
13920                     {
13921                       /* Allow crt1 branch to go via a toc adjusting
13922                          stub.  Other calls that never return could do
13923                          the same, if we could detect such.  */
13924                       can_plt_call = TRUE;
13925                     }
13926                 }
13927
13928               if (!can_plt_call)
13929                 {
13930                   /* g++ as of 20130507 emits self-calls without a
13931                      following nop.  This is arguably wrong since we
13932                      have conflicting information.  On the one hand a
13933                      global symbol and on the other a local call
13934                      sequence, but don't error for this special case.
13935                      It isn't possible to cheaply verify we have
13936                      exactly such a call.  Allow all calls to the same
13937                      section.  */
13938                   asection *code_sec = sec;
13939
13940                   if (get_opd_info (sec) != NULL)
13941                     {
13942                       bfd_vma off = (relocation + addend
13943                                      - sec->output_section->vma
13944                                      - sec->output_offset);
13945
13946                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
13947                     }
13948                   if (code_sec == input_section)
13949                     can_plt_call = TRUE;
13950                 }
13951
13952               if (!can_plt_call)
13953                 {
13954                   if (stub_entry->stub_type == ppc_stub_plt_call
13955                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13956                     info->callbacks->einfo
13957                       (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13958                          "recompile with -fPIC\n"),
13959                        input_bfd, input_section, rel->r_offset, sym_name);
13960                   else
13961                     info->callbacks->einfo
13962                       (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13963                          "(-mcmodel=small toc adjust stub)\n"),
13964                        input_bfd, input_section, rel->r_offset, sym_name);
13965
13966                   bfd_set_error (bfd_error_bad_value);
13967                   ret = FALSE;
13968                 }
13969
13970               if (can_plt_call
13971                   && (stub_entry->stub_type == ppc_stub_plt_call
13972                       || stub_entry->stub_type == ppc_stub_plt_call_r2save))
13973                 unresolved_reloc = FALSE;
13974             }
13975
13976           if ((stub_entry == NULL
13977                || stub_entry->stub_type == ppc_stub_long_branch
13978                || stub_entry->stub_type == ppc_stub_plt_branch)
13979               && get_opd_info (sec) != NULL)
13980             {
13981               /* The branch destination is the value of the opd entry. */
13982               bfd_vma off = (relocation + addend
13983                              - sec->output_section->vma
13984                              - sec->output_offset);
13985               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
13986               if (dest != (bfd_vma) -1)
13987                 {
13988                   relocation = dest;
13989                   addend = 0;
13990                   reloc_dest = DEST_OPD;
13991                 }
13992             }
13993
13994           /* If the branch is out of reach we ought to have a long
13995              branch stub.  */
13996           from = (rel->r_offset
13997                   + input_section->output_offset
13998                   + input_section->output_section->vma);
13999
14000           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14001                                                   ? fdh->elf.other
14002                                                   : sym->st_other);
14003
14004           if (stub_entry != NULL
14005               && (stub_entry->stub_type == ppc_stub_long_branch
14006                   || stub_entry->stub_type == ppc_stub_plt_branch)
14007               && (r_type == R_PPC64_ADDR14_BRTAKEN
14008                   || r_type == R_PPC64_ADDR14_BRNTAKEN
14009                   || (relocation + addend - from + max_br_offset
14010                       < 2 * max_br_offset)))
14011             /* Don't use the stub if this branch is in range.  */
14012             stub_entry = NULL;
14013
14014           if (stub_entry != NULL)
14015             {
14016               /* Munge up the value and addend so that we call the stub
14017                  rather than the procedure directly.  */
14018               asection *stub_sec = stub_entry->group->stub_sec;
14019
14020               if (stub_entry->stub_type == ppc_stub_save_res)
14021                 relocation += (stub_sec->output_offset
14022                                + stub_sec->output_section->vma
14023                                + stub_sec->size - htab->sfpr->size
14024                                - htab->sfpr->output_offset
14025                                - htab->sfpr->output_section->vma);
14026               else
14027                 relocation = (stub_entry->stub_offset
14028                               + stub_sec->output_offset
14029                               + stub_sec->output_section->vma);
14030               addend = 0;
14031               reloc_dest = DEST_STUB;
14032
14033               if ((stub_entry->stub_type == ppc_stub_plt_call
14034                    || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14035                   && (ALWAYS_EMIT_R2SAVE
14036                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14037                   && rel + 1 < relend
14038                   && rel[1].r_offset == rel->r_offset + 4
14039                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14040                 relocation += 4;
14041             }
14042
14043           if (insn != 0)
14044             {
14045               if (is_isa_v2)
14046                 {
14047                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
14048                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
14049                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
14050                   if ((insn & (0x14 << 21)) == (0x04 << 21))
14051                     insn |= 0x02 << 21;
14052                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
14053                     insn |= 0x08 << 21;
14054                   else
14055                     break;
14056                 }
14057               else
14058                 {
14059                   /* Invert 'y' bit if not the default.  */
14060                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
14061                     insn ^= 0x01 << 21;
14062                 }
14063
14064               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
14065             }
14066
14067           /* NOP out calls to undefined weak functions.
14068              We can thus call a weak function without first
14069              checking whether the function is defined.  */
14070           else if (h != NULL
14071                    && h->elf.root.type == bfd_link_hash_undefweak
14072                    && h->elf.dynindx == -1
14073                    && r_type == R_PPC64_REL24
14074                    && relocation == 0
14075                    && addend == 0)
14076             {
14077               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
14078               continue;
14079             }
14080           break;
14081         }
14082
14083       /* Set `addend'.  */
14084       tls_type = 0;
14085       switch (r_type)
14086         {
14087         default:
14088           info->callbacks->einfo
14089             (_("%P: %B: unknown relocation type %d for `%T'\n"),
14090              input_bfd, (int) r_type, sym_name);
14091
14092           bfd_set_error (bfd_error_bad_value);
14093           ret = FALSE;
14094           continue;
14095
14096         case R_PPC64_NONE:
14097         case R_PPC64_TLS:
14098         case R_PPC64_TLSGD:
14099         case R_PPC64_TLSLD:
14100         case R_PPC64_TOCSAVE:
14101         case R_PPC64_GNU_VTINHERIT:
14102         case R_PPC64_GNU_VTENTRY:
14103           continue;
14104
14105           /* GOT16 relocations.  Like an ADDR16 using the symbol's
14106              address in the GOT as relocation value instead of the
14107              symbol's value itself.  Also, create a GOT entry for the
14108              symbol and put the symbol value there.  */
14109         case R_PPC64_GOT_TLSGD16:
14110         case R_PPC64_GOT_TLSGD16_LO:
14111         case R_PPC64_GOT_TLSGD16_HI:
14112         case R_PPC64_GOT_TLSGD16_HA:
14113           tls_type = TLS_TLS | TLS_GD;
14114           goto dogot;
14115
14116         case R_PPC64_GOT_TLSLD16:
14117         case R_PPC64_GOT_TLSLD16_LO:
14118         case R_PPC64_GOT_TLSLD16_HI:
14119         case R_PPC64_GOT_TLSLD16_HA:
14120           tls_type = TLS_TLS | TLS_LD;
14121           goto dogot;
14122
14123         case R_PPC64_GOT_TPREL16_DS:
14124         case R_PPC64_GOT_TPREL16_LO_DS:
14125         case R_PPC64_GOT_TPREL16_HI:
14126         case R_PPC64_GOT_TPREL16_HA:
14127           tls_type = TLS_TLS | TLS_TPREL;
14128           goto dogot;
14129
14130         case R_PPC64_GOT_DTPREL16_DS:
14131         case R_PPC64_GOT_DTPREL16_LO_DS:
14132         case R_PPC64_GOT_DTPREL16_HI:
14133         case R_PPC64_GOT_DTPREL16_HA:
14134           tls_type = TLS_TLS | TLS_DTPREL;
14135           goto dogot;
14136
14137         case R_PPC64_GOT16:
14138         case R_PPC64_GOT16_LO:
14139         case R_PPC64_GOT16_HI:
14140         case R_PPC64_GOT16_HA:
14141         case R_PPC64_GOT16_DS:
14142         case R_PPC64_GOT16_LO_DS:
14143         dogot:
14144           {
14145             /* Relocation is to the entry for this symbol in the global
14146                offset table.  */
14147             asection *got;
14148             bfd_vma *offp;
14149             bfd_vma off;
14150             unsigned long indx = 0;
14151             struct got_entry *ent;
14152
14153             if (tls_type == (TLS_TLS | TLS_LD)
14154                 && (h == NULL
14155                     || !h->elf.def_dynamic))
14156               ent = ppc64_tlsld_got (input_bfd);
14157             else
14158               {
14159
14160                 if (h != NULL)
14161                   {
14162                     bfd_boolean dyn = htab->elf.dynamic_sections_created;
14163                     if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info),
14164                                                           &h->elf)
14165                         || (bfd_link_pic (info)
14166                             && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
14167                       /* This is actually a static link, or it is a
14168                          -Bsymbolic link and the symbol is defined
14169                          locally, or the symbol was forced to be local
14170                          because of a version file.  */
14171                       ;
14172                     else
14173                       {
14174                         BFD_ASSERT (h->elf.dynindx != -1);
14175                         indx = h->elf.dynindx;
14176                         unresolved_reloc = FALSE;
14177                       }
14178                     ent = h->elf.got.glist;
14179                   }
14180                 else
14181                   {
14182                     if (local_got_ents == NULL)
14183                       abort ();
14184                     ent = local_got_ents[r_symndx];
14185                   }
14186
14187                 for (; ent != NULL; ent = ent->next)
14188                   if (ent->addend == orig_rel.r_addend
14189                       && ent->owner == input_bfd
14190                       && ent->tls_type == tls_type)
14191                     break;
14192               }
14193
14194             if (ent == NULL)
14195               abort ();
14196             if (ent->is_indirect)
14197               ent = ent->got.ent;
14198             offp = &ent->got.offset;
14199             got = ppc64_elf_tdata (ent->owner)->got;
14200             if (got == NULL)
14201               abort ();
14202
14203             /* The offset must always be a multiple of 8.  We use the
14204                least significant bit to record whether we have already
14205                processed this entry.  */
14206             off = *offp;
14207             if ((off & 1) != 0)
14208               off &= ~1;
14209             else
14210               {
14211                 /* Generate relocs for the dynamic linker, except in
14212                    the case of TLSLD where we'll use one entry per
14213                    module.  */
14214                 asection *relgot;
14215                 bfd_boolean ifunc;
14216
14217                 *offp = off | 1;
14218                 relgot = NULL;
14219                 ifunc = (h != NULL
14220                          ? h->elf.type == STT_GNU_IFUNC
14221                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14222                 if (ifunc)
14223                   relgot = htab->elf.irelplt;
14224                 else if ((bfd_link_pic (info) || indx != 0)
14225                          && (h == NULL
14226                              || (tls_type == (TLS_TLS | TLS_LD)
14227                                  && !h->elf.def_dynamic)
14228                              || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14229                              || h->elf.root.type != bfd_link_hash_undefweak))
14230                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
14231                 if (relgot != NULL)
14232                   {
14233                     outrel.r_offset = (got->output_section->vma
14234                                        + got->output_offset
14235                                        + off);
14236                     outrel.r_addend = addend;
14237                     if (tls_type & (TLS_LD | TLS_GD))
14238                       {
14239                         outrel.r_addend = 0;
14240                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14241                         if (tls_type == (TLS_TLS | TLS_GD))
14242                           {
14243                             loc = relgot->contents;
14244                             loc += (relgot->reloc_count++
14245                                     * sizeof (Elf64_External_Rela));
14246                             bfd_elf64_swap_reloca_out (output_bfd,
14247                                                        &outrel, loc);
14248                             outrel.r_offset += 8;
14249                             outrel.r_addend = addend;
14250                             outrel.r_info
14251                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14252                           }
14253                       }
14254                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
14255                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14256                     else if (tls_type == (TLS_TLS | TLS_TPREL))
14257                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14258                     else if (indx != 0)
14259                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14260                     else
14261                       {
14262                         if (ifunc)
14263                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14264                         else
14265                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14266
14267                         /* Write the .got section contents for the sake
14268                            of prelink.  */
14269                         loc = got->contents + off;
14270                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14271                                     loc);
14272                       }
14273
14274                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14275                       {
14276                         outrel.r_addend += relocation;
14277                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14278                           {
14279                             if (htab->elf.tls_sec == NULL)
14280                               outrel.r_addend = 0;
14281                             else
14282                               outrel.r_addend -= htab->elf.tls_sec->vma;
14283                           }
14284                       }
14285                     loc = relgot->contents;
14286                     loc += (relgot->reloc_count++
14287                             * sizeof (Elf64_External_Rela));
14288                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14289                   }
14290
14291                 /* Init the .got section contents here if we're not
14292                    emitting a reloc.  */
14293                 else
14294                   {
14295                     relocation += addend;
14296                     if (tls_type == (TLS_TLS | TLS_LD))
14297                       relocation = 1;
14298                     else if (tls_type != 0)
14299                       {
14300                         if (htab->elf.tls_sec == NULL)
14301                           relocation = 0;
14302                         else
14303                           {
14304                             relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14305                             if (tls_type == (TLS_TLS | TLS_TPREL))
14306                               relocation += DTP_OFFSET - TP_OFFSET;
14307                           }
14308
14309                         if (tls_type == (TLS_TLS | TLS_GD))
14310                           {
14311                             bfd_put_64 (output_bfd, relocation,
14312                                         got->contents + off + 8);
14313                             relocation = 1;
14314                           }
14315                       }
14316
14317                     bfd_put_64 (output_bfd, relocation,
14318                                 got->contents + off);
14319                   }
14320               }
14321
14322             if (off >= (bfd_vma) -2)
14323               abort ();
14324
14325             relocation = got->output_section->vma + got->output_offset + off;
14326             addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14327           }
14328           break;
14329
14330         case R_PPC64_PLT16_HA:
14331         case R_PPC64_PLT16_HI:
14332         case R_PPC64_PLT16_LO:
14333         case R_PPC64_PLT32:
14334         case R_PPC64_PLT64:
14335           /* Relocation is to the entry for this symbol in the
14336              procedure linkage table.  */
14337
14338           /* Resolve a PLT reloc against a local symbol directly,
14339              without using the procedure linkage table.  */
14340           if (h == NULL)
14341             break;
14342
14343           /* It's possible that we didn't make a PLT entry for this
14344              symbol.  This happens when statically linking PIC code,
14345              or when using -Bsymbolic.  Go find a match if there is a
14346              PLT entry.  */
14347           if (htab->elf.splt != NULL)
14348             {
14349               struct plt_entry *ent;
14350               for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
14351                 if (ent->plt.offset != (bfd_vma) -1
14352                     && ent->addend == orig_rel.r_addend)
14353                   {
14354                     relocation = (htab->elf.splt->output_section->vma
14355                                   + htab->elf.splt->output_offset
14356                                   + ent->plt.offset);
14357                     unresolved_reloc = FALSE;
14358                     break;
14359                   }
14360             }
14361           break;
14362
14363         case R_PPC64_TOC:
14364           /* Relocation value is TOC base.  */
14365           relocation = TOCstart;
14366           if (r_symndx == STN_UNDEF)
14367             relocation += htab->sec_info[input_section->id].toc_off;
14368           else if (unresolved_reloc)
14369             ;
14370           else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14371             relocation += htab->sec_info[sec->id].toc_off;
14372           else
14373             unresolved_reloc = TRUE;
14374           goto dodyn;
14375
14376           /* TOC16 relocs.  We want the offset relative to the TOC base,
14377              which is the address of the start of the TOC plus 0x8000.
14378              The TOC consists of sections .got, .toc, .tocbss, and .plt,
14379              in this order.  */
14380         case R_PPC64_TOC16:
14381         case R_PPC64_TOC16_LO:
14382         case R_PPC64_TOC16_HI:
14383         case R_PPC64_TOC16_DS:
14384         case R_PPC64_TOC16_LO_DS:
14385         case R_PPC64_TOC16_HA:
14386           addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14387           break;
14388
14389           /* Relocate against the beginning of the section.  */
14390         case R_PPC64_SECTOFF:
14391         case R_PPC64_SECTOFF_LO:
14392         case R_PPC64_SECTOFF_HI:
14393         case R_PPC64_SECTOFF_DS:
14394         case R_PPC64_SECTOFF_LO_DS:
14395         case R_PPC64_SECTOFF_HA:
14396           if (sec != NULL)
14397             addend -= sec->output_section->vma;
14398           break;
14399
14400         case R_PPC64_REL16:
14401         case R_PPC64_REL16_LO:
14402         case R_PPC64_REL16_HI:
14403         case R_PPC64_REL16_HA:
14404           break;
14405
14406         case R_PPC64_REL14:
14407         case R_PPC64_REL14_BRNTAKEN:
14408         case R_PPC64_REL14_BRTAKEN:
14409         case R_PPC64_REL24:
14410           break;
14411
14412         case R_PPC64_TPREL16:
14413         case R_PPC64_TPREL16_LO:
14414         case R_PPC64_TPREL16_HI:
14415         case R_PPC64_TPREL16_HA:
14416         case R_PPC64_TPREL16_DS:
14417         case R_PPC64_TPREL16_LO_DS:
14418         case R_PPC64_TPREL16_HIGH:
14419         case R_PPC64_TPREL16_HIGHA:
14420         case R_PPC64_TPREL16_HIGHER:
14421         case R_PPC64_TPREL16_HIGHERA:
14422         case R_PPC64_TPREL16_HIGHEST:
14423         case R_PPC64_TPREL16_HIGHESTA:
14424           if (h != NULL
14425               && h->elf.root.type == bfd_link_hash_undefweak
14426               && h->elf.dynindx == -1)
14427             {
14428               /* Make this relocation against an undefined weak symbol
14429                  resolve to zero.  This is really just a tweak, since
14430                  code using weak externs ought to check that they are
14431                  defined before using them.  */
14432               bfd_byte *p = contents + rel->r_offset - d_offset;
14433
14434               insn = bfd_get_32 (output_bfd, p);
14435               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14436               if (insn != 0)
14437                 bfd_put_32 (output_bfd, insn, p);
14438               break;
14439             }
14440           if (htab->elf.tls_sec != NULL)
14441             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14442           if (bfd_link_pic (info))
14443             /* The TPREL16 relocs shouldn't really be used in shared
14444                libs as they will result in DT_TEXTREL being set, but
14445                support them anyway.  */
14446             goto dodyn;
14447           break;
14448
14449         case R_PPC64_DTPREL16:
14450         case R_PPC64_DTPREL16_LO:
14451         case R_PPC64_DTPREL16_HI:
14452         case R_PPC64_DTPREL16_HA:
14453         case R_PPC64_DTPREL16_DS:
14454         case R_PPC64_DTPREL16_LO_DS:
14455         case R_PPC64_DTPREL16_HIGH:
14456         case R_PPC64_DTPREL16_HIGHA:
14457         case R_PPC64_DTPREL16_HIGHER:
14458         case R_PPC64_DTPREL16_HIGHERA:
14459         case R_PPC64_DTPREL16_HIGHEST:
14460         case R_PPC64_DTPREL16_HIGHESTA:
14461           if (htab->elf.tls_sec != NULL)
14462             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14463           break;
14464
14465         case R_PPC64_ADDR64_LOCAL:
14466           addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14467                                               ? h->elf.other
14468                                               : sym->st_other);
14469           break;
14470
14471         case R_PPC64_DTPMOD64:
14472           relocation = 1;
14473           addend = 0;
14474           goto dodyn;
14475
14476         case R_PPC64_TPREL64:
14477           if (htab->elf.tls_sec != NULL)
14478             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14479           goto dodyn;
14480
14481         case R_PPC64_DTPREL64:
14482           if (htab->elf.tls_sec != NULL)
14483             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14484           /* Fall thru */
14485
14486           /* Relocations that may need to be propagated if this is a
14487              dynamic object.  */
14488         case R_PPC64_REL30:
14489         case R_PPC64_REL32:
14490         case R_PPC64_REL64:
14491         case R_PPC64_ADDR14:
14492         case R_PPC64_ADDR14_BRNTAKEN:
14493         case R_PPC64_ADDR14_BRTAKEN:
14494         case R_PPC64_ADDR16:
14495         case R_PPC64_ADDR16_DS:
14496         case R_PPC64_ADDR16_HA:
14497         case R_PPC64_ADDR16_HI:
14498         case R_PPC64_ADDR16_HIGH:
14499         case R_PPC64_ADDR16_HIGHA:
14500         case R_PPC64_ADDR16_HIGHER:
14501         case R_PPC64_ADDR16_HIGHERA:
14502         case R_PPC64_ADDR16_HIGHEST:
14503         case R_PPC64_ADDR16_HIGHESTA:
14504         case R_PPC64_ADDR16_LO:
14505         case R_PPC64_ADDR16_LO_DS:
14506         case R_PPC64_ADDR24:
14507         case R_PPC64_ADDR32:
14508         case R_PPC64_ADDR64:
14509         case R_PPC64_UADDR16:
14510         case R_PPC64_UADDR32:
14511         case R_PPC64_UADDR64:
14512         dodyn:
14513           if ((input_section->flags & SEC_ALLOC) == 0)
14514             break;
14515
14516           if (NO_OPD_RELOCS && is_opd)
14517             break;
14518
14519           if ((bfd_link_pic (info)
14520                && (h == NULL
14521                    || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14522                    || h->elf.root.type != bfd_link_hash_undefweak)
14523                && (must_be_dyn_reloc (info, r_type)
14524                    || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
14525               || (ELIMINATE_COPY_RELOCS
14526                   && !bfd_link_pic (info)
14527                   && h != NULL
14528                   && h->elf.dynindx != -1
14529                   && !h->elf.non_got_ref
14530                   && !h->elf.def_regular)
14531               || (!bfd_link_pic (info)
14532                   && (h != NULL
14533                       ? h->elf.type == STT_GNU_IFUNC
14534                       : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
14535             {
14536               bfd_boolean skip, relocate;
14537               asection *sreloc;
14538               bfd_vma out_off;
14539
14540               /* When generating a dynamic object, these relocations
14541                  are copied into the output file to be resolved at run
14542                  time.  */
14543
14544               skip = FALSE;
14545               relocate = FALSE;
14546
14547               out_off = _bfd_elf_section_offset (output_bfd, info,
14548                                                  input_section, rel->r_offset);
14549               if (out_off == (bfd_vma) -1)
14550                 skip = TRUE;
14551               else if (out_off == (bfd_vma) -2)
14552                 skip = TRUE, relocate = TRUE;
14553               out_off += (input_section->output_section->vma
14554                           + input_section->output_offset);
14555               outrel.r_offset = out_off;
14556               outrel.r_addend = rel->r_addend;
14557
14558               /* Optimize unaligned reloc use.  */
14559               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14560                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14561                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14562               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14563                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14564                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14565               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14566                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14567                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14568
14569               if (skip)
14570                 memset (&outrel, 0, sizeof outrel);
14571               else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14572                        && !is_opd
14573                        && r_type != R_PPC64_TOC)
14574                 {
14575                   BFD_ASSERT (h->elf.dynindx != -1);
14576                   outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14577                 }
14578               else
14579                 {
14580                   /* This symbol is local, or marked to become local,
14581                      or this is an opd section reloc which must point
14582                      at a local function.  */
14583                   outrel.r_addend += relocation;
14584                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14585                     {
14586                       if (is_opd && h != NULL)
14587                         {
14588                           /* Lie about opd entries.  This case occurs
14589                              when building shared libraries and we
14590                              reference a function in another shared
14591                              lib.  The same thing happens for a weak
14592                              definition in an application that's
14593                              overridden by a strong definition in a
14594                              shared lib.  (I believe this is a generic
14595                              bug in binutils handling of weak syms.)
14596                              In these cases we won't use the opd
14597                              entry in this lib.  */
14598                           unresolved_reloc = FALSE;
14599                         }
14600                       if (!is_opd
14601                           && r_type == R_PPC64_ADDR64
14602                           && (h != NULL
14603                               ? h->elf.type == STT_GNU_IFUNC
14604                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14605                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14606                       else
14607                         {
14608                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14609
14610                           /* We need to relocate .opd contents for ld.so.
14611                              Prelink also wants simple and consistent rules
14612                              for relocs.  This make all RELATIVE relocs have
14613                              *r_offset equal to r_addend.  */
14614                           relocate = TRUE;
14615                         }
14616                     }
14617                   else
14618                     {
14619                       long indx = 0;
14620
14621                       if (h != NULL
14622                           ? h->elf.type == STT_GNU_IFUNC
14623                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14624                         {
14625                           info->callbacks->einfo
14626                             (_("%P: %H: %s for indirect "
14627                                "function `%T' unsupported\n"),
14628                              input_bfd, input_section, rel->r_offset,
14629                              ppc64_elf_howto_table[r_type]->name,
14630                              sym_name);
14631                           ret = FALSE;
14632                         }
14633                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14634                         ;
14635                       else if (sec == NULL || sec->owner == NULL)
14636                         {
14637                           bfd_set_error (bfd_error_bad_value);
14638                           return FALSE;
14639                         }
14640                       else
14641                         {
14642                           asection *osec;
14643
14644                           osec = sec->output_section;
14645                           indx = elf_section_data (osec)->dynindx;
14646
14647                           if (indx == 0)
14648                             {
14649                               if ((osec->flags & SEC_READONLY) == 0
14650                                   && htab->elf.data_index_section != NULL)
14651                                 osec = htab->elf.data_index_section;
14652                               else
14653                                 osec = htab->elf.text_index_section;
14654                               indx = elf_section_data (osec)->dynindx;
14655                             }
14656                           BFD_ASSERT (indx != 0);
14657
14658                           /* We are turning this relocation into one
14659                              against a section symbol, so subtract out
14660                              the output section's address but not the
14661                              offset of the input section in the output
14662                              section.  */
14663                           outrel.r_addend -= osec->vma;
14664                         }
14665
14666                       outrel.r_info = ELF64_R_INFO (indx, r_type);
14667                     }
14668                 }
14669
14670               sreloc = elf_section_data (input_section)->sreloc;
14671               if (h != NULL
14672                   ? h->elf.type == STT_GNU_IFUNC
14673                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14674                 sreloc = htab->elf.irelplt;
14675               if (sreloc == NULL)
14676                 abort ();
14677
14678               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14679                   >= sreloc->size)
14680                 abort ();
14681               loc = sreloc->contents;
14682               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14683               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14684
14685               /* If this reloc is against an external symbol, it will
14686                  be computed at runtime, so there's no need to do
14687                  anything now.  However, for the sake of prelink ensure
14688                  that the section contents are a known value.  */
14689               if (! relocate)
14690                 {
14691                   unresolved_reloc = FALSE;
14692                   /* The value chosen here is quite arbitrary as ld.so
14693                      ignores section contents except for the special
14694                      case of .opd where the contents might be accessed
14695                      before relocation.  Choose zero, as that won't
14696                      cause reloc overflow.  */
14697                   relocation = 0;
14698                   addend = 0;
14699                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14700                      to improve backward compatibility with older
14701                      versions of ld.  */
14702                   if (r_type == R_PPC64_ADDR64)
14703                     addend = outrel.r_addend;
14704                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
14705                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
14706                     addend = (input_section->output_section->vma
14707                               + input_section->output_offset
14708                               + rel->r_offset);
14709                 }
14710             }
14711           break;
14712
14713         case R_PPC64_COPY:
14714         case R_PPC64_GLOB_DAT:
14715         case R_PPC64_JMP_SLOT:
14716         case R_PPC64_JMP_IREL:
14717         case R_PPC64_RELATIVE:
14718           /* We shouldn't ever see these dynamic relocs in relocatable
14719              files.  */
14720           /* Fall through.  */
14721
14722         case R_PPC64_PLTGOT16:
14723         case R_PPC64_PLTGOT16_DS:
14724         case R_PPC64_PLTGOT16_HA:
14725         case R_PPC64_PLTGOT16_HI:
14726         case R_PPC64_PLTGOT16_LO:
14727         case R_PPC64_PLTGOT16_LO_DS:
14728         case R_PPC64_PLTREL32:
14729         case R_PPC64_PLTREL64:
14730           /* These ones haven't been implemented yet.  */
14731
14732           info->callbacks->einfo
14733             (_("%P: %B: %s is not supported for `%T'\n"),
14734              input_bfd,
14735              ppc64_elf_howto_table[r_type]->name, sym_name);
14736
14737           bfd_set_error (bfd_error_invalid_operation);
14738           ret = FALSE;
14739           continue;
14740         }
14741
14742       /* Multi-instruction sequences that access the TOC can be
14743          optimized, eg. addis ra,r2,0; addi rb,ra,x;
14744          to             nop;           addi rb,r2,x;  */
14745       switch (r_type)
14746         {
14747         default:
14748           break;
14749
14750         case R_PPC64_GOT_TLSLD16_HI:
14751         case R_PPC64_GOT_TLSGD16_HI:
14752         case R_PPC64_GOT_TPREL16_HI:
14753         case R_PPC64_GOT_DTPREL16_HI:
14754         case R_PPC64_GOT16_HI:
14755         case R_PPC64_TOC16_HI:
14756           /* These relocs would only be useful if building up an
14757              offset to later add to r2, perhaps in an indexed
14758              addressing mode instruction.  Don't try to optimize.
14759              Unfortunately, the possibility of someone building up an
14760              offset like this or even with the HA relocs, means that
14761              we need to check the high insn when optimizing the low
14762              insn.  */
14763           break;
14764
14765         case R_PPC64_GOT_TLSLD16_HA:
14766         case R_PPC64_GOT_TLSGD16_HA:
14767         case R_PPC64_GOT_TPREL16_HA:
14768         case R_PPC64_GOT_DTPREL16_HA:
14769         case R_PPC64_GOT16_HA:
14770         case R_PPC64_TOC16_HA:
14771           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14772               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14773             {
14774               bfd_byte *p = contents + (rel->r_offset & ~3);
14775               bfd_put_32 (input_bfd, NOP, p);
14776             }
14777           break;
14778
14779         case R_PPC64_GOT_TLSLD16_LO:
14780         case R_PPC64_GOT_TLSGD16_LO:
14781         case R_PPC64_GOT_TPREL16_LO_DS:
14782         case R_PPC64_GOT_DTPREL16_LO_DS:
14783         case R_PPC64_GOT16_LO:
14784         case R_PPC64_GOT16_LO_DS:
14785         case R_PPC64_TOC16_LO:
14786         case R_PPC64_TOC16_LO_DS:
14787           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14788               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14789             {
14790               bfd_byte *p = contents + (rel->r_offset & ~3);
14791               insn = bfd_get_32 (input_bfd, p);
14792               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14793                 {
14794                   /* Transform addic to addi when we change reg.  */
14795                   insn &= ~((0x3f << 26) | (0x1f << 16));
14796                   insn |= (14u << 26) | (2 << 16);
14797                 }
14798               else
14799                 {
14800                   insn &= ~(0x1f << 16);
14801                   insn |= 2 << 16;
14802                 }
14803               bfd_put_32 (input_bfd, insn, p);
14804             }
14805           break;
14806         }
14807
14808       /* Do any further special processing.  */
14809       howto = ppc64_elf_howto_table[(int) r_type];
14810       switch (r_type)
14811         {
14812         default:
14813           break;
14814
14815         case R_PPC64_REL16_HA:
14816         case R_PPC64_ADDR16_HA:
14817         case R_PPC64_ADDR16_HIGHA:
14818         case R_PPC64_ADDR16_HIGHERA:
14819         case R_PPC64_ADDR16_HIGHESTA:
14820         case R_PPC64_TOC16_HA:
14821         case R_PPC64_SECTOFF_HA:
14822         case R_PPC64_TPREL16_HA:
14823         case R_PPC64_TPREL16_HIGHA:
14824         case R_PPC64_TPREL16_HIGHERA:
14825         case R_PPC64_TPREL16_HIGHESTA:
14826         case R_PPC64_DTPREL16_HA:
14827         case R_PPC64_DTPREL16_HIGHA:
14828         case R_PPC64_DTPREL16_HIGHERA:
14829         case R_PPC64_DTPREL16_HIGHESTA:
14830           /* It's just possible that this symbol is a weak symbol
14831              that's not actually defined anywhere. In that case,
14832              'sec' would be NULL, and we should leave the symbol
14833              alone (it will be set to zero elsewhere in the link).  */
14834           if (sec == NULL)
14835             break;
14836           /* Fall thru */
14837
14838         case R_PPC64_GOT16_HA:
14839         case R_PPC64_PLTGOT16_HA:
14840         case R_PPC64_PLT16_HA:
14841         case R_PPC64_GOT_TLSGD16_HA:
14842         case R_PPC64_GOT_TLSLD16_HA:
14843         case R_PPC64_GOT_TPREL16_HA:
14844         case R_PPC64_GOT_DTPREL16_HA:
14845           /* Add 0x10000 if sign bit in 0:15 is set.
14846              Bits 0:15 are not used.  */
14847           addend += 0x8000;
14848           break;
14849
14850         case R_PPC64_ADDR16_DS:
14851         case R_PPC64_ADDR16_LO_DS:
14852         case R_PPC64_GOT16_DS:
14853         case R_PPC64_GOT16_LO_DS:
14854         case R_PPC64_PLT16_LO_DS:
14855         case R_PPC64_SECTOFF_DS:
14856         case R_PPC64_SECTOFF_LO_DS:
14857         case R_PPC64_TOC16_DS:
14858         case R_PPC64_TOC16_LO_DS:
14859         case R_PPC64_PLTGOT16_DS:
14860         case R_PPC64_PLTGOT16_LO_DS:
14861         case R_PPC64_GOT_TPREL16_DS:
14862         case R_PPC64_GOT_TPREL16_LO_DS:
14863         case R_PPC64_GOT_DTPREL16_DS:
14864         case R_PPC64_GOT_DTPREL16_LO_DS:
14865         case R_PPC64_TPREL16_DS:
14866         case R_PPC64_TPREL16_LO_DS:
14867         case R_PPC64_DTPREL16_DS:
14868         case R_PPC64_DTPREL16_LO_DS:
14869           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14870           mask = 3;
14871           /* If this reloc is against an lq insn, then the value must be
14872              a multiple of 16.  This is somewhat of a hack, but the
14873              "correct" way to do this by defining _DQ forms of all the
14874              _DS relocs bloats all reloc switches in this file.  It
14875              doesn't seem to make much sense to use any of these relocs
14876              in data, so testing the insn should be safe.  */
14877           if ((insn & (0x3f << 26)) == (56u << 26))
14878             mask = 15;
14879           if (((relocation + addend) & mask) != 0)
14880             {
14881               info->callbacks->einfo
14882                 (_("%P: %H: error: %s not a multiple of %u\n"),
14883                  input_bfd, input_section, rel->r_offset,
14884                  howto->name,
14885                  mask + 1);
14886               bfd_set_error (bfd_error_bad_value);
14887               ret = FALSE;
14888               continue;
14889             }
14890           break;
14891         }
14892
14893       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
14894          because such sections are not SEC_ALLOC and thus ld.so will
14895          not process them.  */
14896       if (unresolved_reloc
14897           && !((input_section->flags & SEC_DEBUGGING) != 0
14898                && h->elf.def_dynamic)
14899           && _bfd_elf_section_offset (output_bfd, info, input_section,
14900                                       rel->r_offset) != (bfd_vma) -1)
14901         {
14902           info->callbacks->einfo
14903             (_("%P: %H: unresolvable %s against `%T'\n"),
14904              input_bfd, input_section, rel->r_offset,
14905              howto->name,
14906              h->elf.root.root.string);
14907           ret = FALSE;
14908         }
14909
14910       /* 16-bit fields in insns mostly have signed values, but a
14911          few insns have 16-bit unsigned values.  Really, we should
14912          have different reloc types.  */
14913       if (howto->complain_on_overflow != complain_overflow_dont
14914           && howto->dst_mask == 0xffff
14915           && (input_section->flags & SEC_CODE) != 0)
14916         {
14917           enum complain_overflow complain = complain_overflow_signed;
14918
14919           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14920           if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
14921             complain = complain_overflow_bitfield;
14922           else if (howto->rightshift == 0
14923                    ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
14924                       || (insn & (0x3f << 26)) == 24u << 26 /* ori */
14925                       || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
14926                    : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
14927                       || (insn & (0x3f << 26)) == 25u << 26 /* oris */
14928                       || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
14929             complain = complain_overflow_unsigned;
14930           if (howto->complain_on_overflow != complain)
14931             {
14932               alt_howto = *howto;
14933               alt_howto.complain_on_overflow = complain;
14934               howto = &alt_howto;
14935             }
14936         }
14937
14938       r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
14939                                     rel->r_offset, relocation, addend);
14940
14941       if (r != bfd_reloc_ok)
14942         {
14943           char *more_info = NULL;
14944           const char *reloc_name = howto->name;
14945
14946           if (reloc_dest != DEST_NORMAL)
14947             {
14948               more_info = bfd_malloc (strlen (reloc_name) + 8);
14949               if (more_info != NULL)
14950                 {
14951                   strcpy (more_info, reloc_name);
14952                   strcat (more_info, (reloc_dest == DEST_OPD
14953                                       ? " (OPD)" : " (stub)"));
14954                   reloc_name = more_info;
14955                 }
14956             }
14957
14958           if (r == bfd_reloc_overflow)
14959             {
14960               /* On code like "if (foo) foo();" don't report overflow
14961                  on a branch to zero when foo is undefined.  */
14962               if (!warned
14963                   && (reloc_dest == DEST_STUB
14964                       || !(h != NULL
14965                            && (h->elf.root.type == bfd_link_hash_undefweak
14966                                || h->elf.root.type == bfd_link_hash_undefined)
14967                            && is_branch_reloc (r_type))))
14968                 {
14969                   if (!((*info->callbacks->reloc_overflow)
14970                         (info, &h->elf.root, sym_name,
14971                          reloc_name, orig_rel.r_addend,
14972                          input_bfd, input_section, rel->r_offset)))
14973                     return FALSE;
14974                 }
14975             }
14976           else
14977             {
14978               info->callbacks->einfo
14979                 (_("%P: %H: %s against `%T': error %d\n"),
14980                  input_bfd, input_section, rel->r_offset,
14981                  reloc_name, sym_name, (int) r);
14982               ret = FALSE;
14983             }
14984           if (more_info != NULL)
14985             free (more_info);
14986         }
14987     }
14988
14989   /* If we're emitting relocations, then shortly after this function
14990      returns, reloc offsets and addends for this section will be
14991      adjusted.  Worse, reloc symbol indices will be for the output
14992      file rather than the input.  Save a copy of the relocs for
14993      opd_entry_value.  */
14994   if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
14995     {
14996       bfd_size_type amt;
14997       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
14998       rel = bfd_alloc (input_bfd, amt);
14999       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15000       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15001       if (rel == NULL)
15002         return FALSE;
15003       memcpy (rel, relocs, amt);
15004     }
15005   return ret;
15006 }
15007
15008 /* Adjust the value of any local symbols in opd sections.  */
15009
15010 static int
15011 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15012                               const char *name ATTRIBUTE_UNUSED,
15013                               Elf_Internal_Sym *elfsym,
15014                               asection *input_sec,
15015                               struct elf_link_hash_entry *h)
15016 {
15017   struct _opd_sec_data *opd;
15018   long adjust;
15019   bfd_vma value;
15020
15021   if (h != NULL)
15022     return 1;
15023
15024   opd = get_opd_info (input_sec);
15025   if (opd == NULL || opd->adjust == NULL)
15026     return 1;
15027
15028   value = elfsym->st_value - input_sec->output_offset;
15029   if (!bfd_link_relocatable (info))
15030     value -= input_sec->output_section->vma;
15031
15032   adjust = opd->adjust[OPD_NDX (value)];
15033   if (adjust == -1)
15034     return 2;
15035
15036   elfsym->st_value += adjust;
15037   return 1;
15038 }
15039
15040 /* Finish up dynamic symbol handling.  We set the contents of various
15041    dynamic sections here.  */
15042
15043 static bfd_boolean
15044 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15045                                  struct bfd_link_info *info,
15046                                  struct elf_link_hash_entry *h,
15047                                  Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
15048 {
15049   struct ppc_link_hash_table *htab;
15050   struct plt_entry *ent;
15051   Elf_Internal_Rela rela;
15052   bfd_byte *loc;
15053
15054   htab = ppc_hash_table (info);
15055   if (htab == NULL)
15056     return FALSE;
15057
15058   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15059     if (ent->plt.offset != (bfd_vma) -1)
15060       {
15061         /* This symbol has an entry in the procedure linkage
15062            table.  Set it up.  */
15063         if (!htab->elf.dynamic_sections_created
15064             || h->dynindx == -1)
15065           {
15066             BFD_ASSERT (h->type == STT_GNU_IFUNC
15067                         && h->def_regular
15068                         && (h->root.type == bfd_link_hash_defined
15069                             || h->root.type == bfd_link_hash_defweak));
15070             rela.r_offset = (htab->elf.iplt->output_section->vma
15071                              + htab->elf.iplt->output_offset
15072                              + ent->plt.offset);
15073             if (htab->opd_abi)
15074               rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15075             else
15076               rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15077             rela.r_addend = (h->root.u.def.value
15078                              + h->root.u.def.section->output_offset
15079                              + h->root.u.def.section->output_section->vma
15080                              + ent->addend);
15081             loc = (htab->elf.irelplt->contents
15082                    + (htab->elf.irelplt->reloc_count++
15083                       * sizeof (Elf64_External_Rela)));
15084           }
15085         else
15086           {
15087             rela.r_offset = (htab->elf.splt->output_section->vma
15088                              + htab->elf.splt->output_offset
15089                              + ent->plt.offset);
15090             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15091             rela.r_addend = ent->addend;
15092             loc = (htab->elf.srelplt->contents
15093                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15094                       / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
15095           }
15096         bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15097
15098         if (!htab->opd_abi)
15099           {
15100             if (!h->def_regular)
15101               {
15102                 /* Mark the symbol as undefined, rather than as
15103                    defined in glink.  Leave the value if there were
15104                    any relocations where pointer equality matters
15105                    (this is a clue for the dynamic linker, to make
15106                    function pointer comparisons work between an
15107                    application and shared library), otherwise set it
15108                    to zero.  */
15109                 sym->st_shndx = SHN_UNDEF;
15110                 if (!h->pointer_equality_needed)
15111                   sym->st_value = 0;
15112                 else if (!h->ref_regular_nonweak)
15113                   {
15114                     /* This breaks function pointer comparisons, but
15115                        that is better than breaking tests for a NULL
15116                        function pointer.  */
15117                     sym->st_value = 0;
15118                   }
15119               }
15120           }
15121       }
15122
15123   if (h->needs_copy)
15124     {
15125       /* This symbol needs a copy reloc.  Set it up.  */
15126
15127       if (h->dynindx == -1
15128           || (h->root.type != bfd_link_hash_defined
15129               && h->root.type != bfd_link_hash_defweak)
15130           || htab->relbss == NULL)
15131         abort ();
15132
15133       rela.r_offset = (h->root.u.def.value
15134                        + h->root.u.def.section->output_section->vma
15135                        + h->root.u.def.section->output_offset);
15136       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15137       rela.r_addend = 0;
15138       loc = htab->relbss->contents;
15139       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
15140       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15141     }
15142
15143   return TRUE;
15144 }
15145
15146 /* Used to decide how to sort relocs in an optimal manner for the
15147    dynamic linker, before writing them out.  */
15148
15149 static enum elf_reloc_type_class
15150 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15151                             const asection *rel_sec,
15152                             const Elf_Internal_Rela *rela)
15153 {
15154   enum elf_ppc64_reloc_type r_type;
15155   struct ppc_link_hash_table *htab = ppc_hash_table (info);
15156
15157   if (rel_sec == htab->elf.irelplt)
15158     return reloc_class_ifunc;
15159
15160   r_type = ELF64_R_TYPE (rela->r_info);
15161   switch (r_type)
15162     {
15163     case R_PPC64_RELATIVE:
15164       return reloc_class_relative;
15165     case R_PPC64_JMP_SLOT:
15166       return reloc_class_plt;
15167     case R_PPC64_COPY:
15168       return reloc_class_copy;
15169     default:
15170       return reloc_class_normal;
15171     }
15172 }
15173
15174 /* Finish up the dynamic sections.  */
15175
15176 static bfd_boolean
15177 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15178                                    struct bfd_link_info *info)
15179 {
15180   struct ppc_link_hash_table *htab;
15181   bfd *dynobj;
15182   asection *sdyn;
15183
15184   htab = ppc_hash_table (info);
15185   if (htab == NULL)
15186     return FALSE;
15187
15188   dynobj = htab->elf.dynobj;
15189   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15190
15191   if (htab->elf.dynamic_sections_created)
15192     {
15193       Elf64_External_Dyn *dyncon, *dynconend;
15194
15195       if (sdyn == NULL || htab->elf.sgot == NULL)
15196         abort ();
15197
15198       dyncon = (Elf64_External_Dyn *) sdyn->contents;
15199       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15200       for (; dyncon < dynconend; dyncon++)
15201         {
15202           Elf_Internal_Dyn dyn;
15203           asection *s;
15204
15205           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15206
15207           switch (dyn.d_tag)
15208             {
15209             default:
15210               continue;
15211
15212             case DT_PPC64_GLINK:
15213               s = htab->glink;
15214               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15215               /* We stupidly defined DT_PPC64_GLINK to be the start
15216                  of glink rather than the first entry point, which is
15217                  what ld.so needs, and now have a bigger stub to
15218                  support automatic multiple TOCs.  */
15219               dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
15220               break;
15221
15222             case DT_PPC64_OPD:
15223               s = bfd_get_section_by_name (output_bfd, ".opd");
15224               if (s == NULL)
15225                 continue;
15226               dyn.d_un.d_ptr = s->vma;
15227               break;
15228
15229             case DT_PPC64_OPT:
15230               if (htab->do_multi_toc && htab->multi_toc_needed)
15231                 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15232               break;
15233
15234             case DT_PPC64_OPDSZ:
15235               s = bfd_get_section_by_name (output_bfd, ".opd");
15236               if (s == NULL)
15237                 continue;
15238               dyn.d_un.d_val = s->size;
15239               break;
15240
15241             case DT_PLTGOT:
15242               s = htab->elf.splt;
15243               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15244               break;
15245
15246             case DT_JMPREL:
15247               s = htab->elf.srelplt;
15248               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15249               break;
15250
15251             case DT_PLTRELSZ:
15252               dyn.d_un.d_val = htab->elf.srelplt->size;
15253               break;
15254
15255             case DT_RELASZ:
15256               /* Don't count procedure linkage table relocs in the
15257                  overall reloc count.  */
15258               s = htab->elf.srelplt;
15259               if (s == NULL)
15260                 continue;
15261               dyn.d_un.d_val -= s->size;
15262               break;
15263
15264             case DT_RELA:
15265               /* We may not be using the standard ELF linker script.
15266                  If .rela.plt is the first .rela section, we adjust
15267                  DT_RELA to not include it.  */
15268               s = htab->elf.srelplt;
15269               if (s == NULL)
15270                 continue;
15271               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
15272                 continue;
15273               dyn.d_un.d_ptr += s->size;
15274               break;
15275             }
15276
15277           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15278         }
15279     }
15280
15281   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0)
15282     {
15283       /* Fill in the first entry in the global offset table.
15284          We use it to hold the link-time TOCbase.  */
15285       bfd_put_64 (output_bfd,
15286                   elf_gp (output_bfd) + TOC_BASE_OFF,
15287                   htab->elf.sgot->contents);
15288
15289       /* Set .got entry size.  */
15290       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
15291     }
15292
15293   if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
15294     {
15295       /* Set .plt entry size.  */
15296       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15297         = PLT_ENTRY_SIZE (htab);
15298     }
15299
15300   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15301      brlt ourselves if emitrelocations.  */
15302   if (htab->brlt != NULL
15303       && htab->brlt->reloc_count != 0
15304       && !_bfd_elf_link_output_relocs (output_bfd,
15305                                        htab->brlt,
15306                                        elf_section_data (htab->brlt)->rela.hdr,
15307                                        elf_section_data (htab->brlt)->relocs,
15308                                        NULL))
15309     return FALSE;
15310
15311   if (htab->glink != NULL
15312       && htab->glink->reloc_count != 0
15313       && !_bfd_elf_link_output_relocs (output_bfd,
15314                                        htab->glink,
15315                                        elf_section_data (htab->glink)->rela.hdr,
15316                                        elf_section_data (htab->glink)->relocs,
15317                                        NULL))
15318     return FALSE;
15319
15320   if (htab->glink_eh_frame != NULL
15321       && htab->glink_eh_frame->size != 0)
15322     {
15323       bfd_vma val;
15324       bfd_byte *p;
15325       asection *stub_sec;
15326
15327       p = htab->glink_eh_frame->contents + sizeof (glink_eh_frame_cie);
15328       for (stub_sec = htab->params->stub_bfd->sections;
15329            stub_sec != NULL;
15330            stub_sec = stub_sec->next)
15331         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
15332           {
15333             /* FDE length.  */
15334             p += 4;
15335             /* CIE pointer.  */
15336             p += 4;
15337             /* Offset to stub section.  */
15338             val = (stub_sec->output_section->vma
15339                    + stub_sec->output_offset);
15340             val -= (htab->glink_eh_frame->output_section->vma
15341                     + htab->glink_eh_frame->output_offset
15342                     + (p - htab->glink_eh_frame->contents));
15343             if (val + 0x80000000 > 0xffffffff)
15344               {
15345                 info->callbacks->einfo
15346                   (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15347                    stub_sec->name);
15348                 return FALSE;
15349               }
15350             bfd_put_32 (dynobj, val, p);
15351             p += 4;
15352             /* stub section size.  */
15353             p += 4;
15354             /* Augmentation.  */
15355             p += 1;
15356             /* Pad.  */
15357             p += 7;
15358           }
15359       if (htab->glink != NULL && htab->glink->size != 0)
15360         {
15361           /* FDE length.  */
15362           p += 4;
15363           /* CIE pointer.  */
15364           p += 4;
15365           /* Offset to .glink.  */
15366           val = (htab->glink->output_section->vma
15367                  + htab->glink->output_offset
15368                  + 8);
15369           val -= (htab->glink_eh_frame->output_section->vma
15370                   + htab->glink_eh_frame->output_offset
15371                   + (p - htab->glink_eh_frame->contents));
15372           if (val + 0x80000000 > 0xffffffff)
15373             {
15374               info->callbacks->einfo
15375                 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15376                  htab->glink->name);
15377               return FALSE;
15378             }
15379           bfd_put_32 (dynobj, val, p);
15380           p += 4;
15381           /* .glink size.  */
15382           p += 4;
15383           /* Augmentation.  */
15384           p += 1;
15385           /* Ops.  */
15386           p += 7;
15387         }
15388
15389       if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15390           && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15391                                                htab->glink_eh_frame,
15392                                                htab->glink_eh_frame->contents))
15393         return FALSE;
15394     }
15395
15396   /* We need to handle writing out multiple GOT sections ourselves,
15397      since we didn't add them to DYNOBJ.  We know dynobj is the first
15398      bfd.  */
15399   while ((dynobj = dynobj->link.next) != NULL)
15400     {
15401       asection *s;
15402
15403       if (!is_ppc64_elf (dynobj))
15404         continue;
15405
15406       s = ppc64_elf_tdata (dynobj)->got;
15407       if (s != NULL
15408           && s->size != 0
15409           && s->output_section != bfd_abs_section_ptr
15410           && !bfd_set_section_contents (output_bfd, s->output_section,
15411                                         s->contents, s->output_offset,
15412                                         s->size))
15413         return FALSE;
15414       s = ppc64_elf_tdata (dynobj)->relgot;
15415       if (s != NULL
15416           && s->size != 0
15417           && s->output_section != bfd_abs_section_ptr
15418           && !bfd_set_section_contents (output_bfd, s->output_section,
15419                                         s->contents, s->output_offset,
15420                                         s->size))
15421         return FALSE;
15422     }
15423
15424   return TRUE;
15425 }
15426
15427 #include "elf64-target.h"
15428
15429 /* FreeBSD support */
15430
15431 #undef  TARGET_LITTLE_SYM
15432 #undef  TARGET_LITTLE_NAME
15433
15434 #undef  TARGET_BIG_SYM
15435 #define TARGET_BIG_SYM  powerpc_elf64_fbsd_vec
15436 #undef  TARGET_BIG_NAME
15437 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15438
15439 #undef  ELF_OSABI
15440 #define ELF_OSABI       ELFOSABI_FREEBSD
15441
15442 #undef  elf64_bed
15443 #define elf64_bed       elf64_powerpc_fbsd_bed
15444
15445 #include "elf64-target.h"
15446