Automatic date update in version.in
[external/binutils.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2    Copyright (C) 1999-2018 Free Software Foundation, Inc.
3    Written by Linus Nordberg, Swox AB <info@swox.com>,
4    based on elf32-ppc.c by Ian Lance Taylor.
5    Largely rewritten by Alan Modra.
6
7    This file is part of BFD, the Binary File Descriptor library.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License along
20    with this program; if not, write to the Free Software Foundation, Inc.,
21    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
22
23
24 /* The 64-bit PowerPC ELF ABI may be found at
25    http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26    http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
27
28 #include "sysdep.h"
29 #include <stdarg.h>
30 #include "bfd.h"
31 #include "bfdlink.h"
32 #include "libbfd.h"
33 #include "elf-bfd.h"
34 #include "elf/ppc64.h"
35 #include "elf64-ppc.h"
36 #include "dwarf2.h"
37
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
55   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_vma opd_entry_value
57   (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
58
59 #define TARGET_LITTLE_SYM       powerpc_elf64_le_vec
60 #define TARGET_LITTLE_NAME      "elf64-powerpcle"
61 #define TARGET_BIG_SYM          powerpc_elf64_vec
62 #define TARGET_BIG_NAME         "elf64-powerpc"
63 #define ELF_ARCH                bfd_arch_powerpc
64 #define ELF_TARGET_ID           PPC64_ELF_DATA
65 #define ELF_MACHINE_CODE        EM_PPC64
66 #define ELF_MAXPAGESIZE         0x10000
67 #define ELF_COMMONPAGESIZE      0x1000
68 #define ELF_RELROPAGESIZE       ELF_MAXPAGESIZE
69 #define elf_info_to_howto       ppc64_elf_info_to_howto
70
71 #define elf_backend_want_got_sym 0
72 #define elf_backend_want_plt_sym 0
73 #define elf_backend_plt_alignment 3
74 #define elf_backend_plt_not_loaded 1
75 #define elf_backend_got_header_size 8
76 #define elf_backend_want_dynrelro 1
77 #define elf_backend_can_gc_sections 1
78 #define elf_backend_can_refcount 1
79 #define elf_backend_rela_normal 1
80 #define elf_backend_dtrel_excludes_plt 1
81 #define elf_backend_default_execstack 0
82
83 #define bfd_elf64_mkobject                    ppc64_elf_mkobject
84 #define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
85 #define bfd_elf64_bfd_reloc_name_lookup       ppc64_elf_reloc_name_lookup
86 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
87 #define bfd_elf64_bfd_print_private_bfd_data  ppc64_elf_print_private_bfd_data
88 #define bfd_elf64_new_section_hook            ppc64_elf_new_section_hook
89 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
90 #define bfd_elf64_get_synthetic_symtab        ppc64_elf_get_synthetic_symtab
91 #define bfd_elf64_bfd_link_just_syms          ppc64_elf_link_just_syms
92 #define bfd_elf64_bfd_gc_sections             ppc64_elf_gc_sections
93
94 #define elf_backend_object_p                  ppc64_elf_object_p
95 #define elf_backend_grok_prstatus             ppc64_elf_grok_prstatus
96 #define elf_backend_grok_psinfo               ppc64_elf_grok_psinfo
97 #define elf_backend_write_core_note           ppc64_elf_write_core_note
98 #define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
99 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
100 #define elf_backend_add_symbol_hook           ppc64_elf_add_symbol_hook
101 #define elf_backend_check_directives          ppc64_elf_before_check_relocs
102 #define elf_backend_notice_as_needed          ppc64_elf_notice_as_needed
103 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
104 #define elf_backend_check_relocs              ppc64_elf_check_relocs
105 #define elf_backend_relocs_compatible         _bfd_elf_relocs_compatible
106 #define elf_backend_gc_keep                   ppc64_elf_gc_keep
107 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
108 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
109 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
110 #define elf_backend_hide_symbol               ppc64_elf_hide_symbol
111 #define elf_backend_maybe_function_sym        ppc64_elf_maybe_function_sym
112 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
113 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
114 #define elf_backend_hash_symbol               ppc64_elf_hash_symbol
115 #define elf_backend_init_index_section        _bfd_elf_init_2_index_sections
116 #define elf_backend_action_discarded          ppc64_elf_action_discarded
117 #define elf_backend_relocate_section          ppc64_elf_relocate_section
118 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
119 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
120 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
121 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
122 #define elf_backend_special_sections          ppc64_elf_special_sections
123 #define elf_backend_merge_symbol_attribute    ppc64_elf_merge_symbol_attribute
124 #define elf_backend_merge_symbol              ppc64_elf_merge_symbol
125 #define elf_backend_get_reloc_section         bfd_get_section_by_name
126
127 /* The name of the dynamic interpreter.  This is put in the .interp
128    section.  */
129 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
130
131 /* The size in bytes of an entry in the procedure linkage table.  */
132 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
133 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
134
135 /* The initial size of the plt reserved for the dynamic linker.  */
136 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
137
138 /* Offsets to some stack save slots.  */
139 #define STK_LR 16
140 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
141 /* This one is dodgy.  ELFv2 does not have a linker word, so use the
142    CR save slot.  Used only by optimised __tls_get_addr call stub,
143    relying on __tls_get_addr_opt not saving CR..  */
144 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
145
146 /* TOC base pointers offset from start of TOC.  */
147 #define TOC_BASE_OFF    0x8000
148 /* TOC base alignment.  */
149 #define TOC_BASE_ALIGN  256
150
151 /* Offset of tp and dtp pointers from start of TLS block.  */
152 #define TP_OFFSET       0x7000
153 #define DTP_OFFSET      0x8000
154
155 /* .plt call stub instructions.  The normal stub is like this, but
156    sometimes the .plt entry crosses a 64k boundary and we need to
157    insert an addi to adjust r11.  */
158 #define STD_R2_0R1      0xf8410000      /* std   %r2,0+40(%r1)       */
159 #define ADDIS_R11_R2    0x3d620000      /* addis %r11,%r2,xxx@ha     */
160 #define LD_R12_0R11     0xe98b0000      /* ld    %r12,xxx+0@l(%r11)  */
161 #define MTCTR_R12       0x7d8903a6      /* mtctr %r12                */
162 #define LD_R2_0R11      0xe84b0000      /* ld    %r2,xxx+8@l(%r11)   */
163 #define LD_R11_0R11     0xe96b0000      /* ld    %r11,xxx+16@l(%r11) */
164 #define BCTR            0x4e800420      /* bctr                      */
165
166 #define ADDI_R11_R11    0x396b0000      /* addi %r11,%r11,off@l  */
167 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
168 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
169
170 #define XOR_R2_R12_R12  0x7d826278      /* xor   %r2,%r12,%r12   */
171 #define ADD_R11_R11_R2  0x7d6b1214      /* add   %r11,%r11,%r2   */
172 #define XOR_R11_R12_R12 0x7d8b6278      /* xor   %r11,%r12,%r12  */
173 #define ADD_R2_R2_R11   0x7c425a14      /* add   %r2,%r2,%r11    */
174 #define CMPLDI_R2_0     0x28220000      /* cmpldi %r2,0          */
175 #define BNECTR          0x4ca20420      /* bnectr+               */
176 #define BNECTR_P4       0x4ce20420      /* bnectr+               */
177
178 #define LD_R12_0R2      0xe9820000      /* ld    %r12,xxx+0(%r2) */
179 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
180 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
181
182 #define LD_R2_0R1       0xe8410000      /* ld    %r2,0(%r1)      */
183 #define LD_R2_0R12      0xe84c0000      /* ld    %r2,0(%r12)     */
184 #define ADD_R2_R2_R12   0x7c426214      /* add   %r2,%r2,%r12    */
185
186 #define LIS_R2          0x3c400000      /* lis %r2,xxx@ha         */
187 #define ADDIS_R2_R12    0x3c4c0000      /* addis %r2,%r12,xxx@ha  */
188 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha  */
189 #define ADDIS_R12_R12   0x3d8c0000      /* addis %r12,%r12,xxx@ha */
190 #define LD_R12_0R12     0xe98c0000      /* ld    %r12,xxx@l(%r12) */
191
192 /* __glink_PLTresolve stub instructions.  We enter with the index in R0.  */
193 #define GLINK_PLTRESOLVE_SIZE(htab)                     \
194   (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
195                                         /* 0:                           */
196                                         /*  .quad plt0-1f               */
197                                         /* __glink:                     */
198 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
199 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
200                                         /* 1:                           */
201 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
202                                         /*  ld %2,(0b-1b)(%11)          */
203 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
204 #define ADD_R11_R2_R11  0x7d625a14      /*  add %11,%2,%11              */
205                                         /*  ld %12,0(%11)               */
206                                         /*  ld %2,8(%11)                */
207                                         /*  mtctr %12                   */
208                                         /*  ld %11,16(%11)              */
209                                         /*  bctr                        */
210 #define MFLR_R0         0x7c0802a6      /*  mflr %r0                    */
211 #define MTLR_R0         0x7c0803a6      /*  mtlr %r0                    */
212 #define SUB_R12_R12_R11 0x7d8b6050      /*  subf %r12,%r11,%r12         */
213 #define ADDI_R0_R12     0x380c0000      /*  addi %r0,%r12,0             */
214 #define SRDI_R0_R0_2    0x7800f082      /*  rldicl %r0,%r0,62,2         */
215
216 /* Pad with this.  */
217 #define NOP             0x60000000
218
219 /* Some other nops.  */
220 #define CROR_151515     0x4def7b82
221 #define CROR_313131     0x4ffffb82
222
223 /* .glink entries for the first 32k functions are two instructions.  */
224 #define LI_R0_0         0x38000000      /* li    %r0,0          */
225 #define B_DOT           0x48000000      /* b     .              */
226
227 /* After that, we need two instructions to load the index, followed by
228    a branch.  */
229 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
230 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
231
232 /* Instructions used by the save and restore reg functions.  */
233 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
234 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
235 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
236 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
237 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
238 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
239 #define LI_R12_0        0x39800000      /* li    %r12,0         */
240 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
241 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
242 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
243 #define BLR             0x4e800020      /* blr                  */
244
245 /* Since .opd is an array of descriptors and each entry will end up
246    with identical R_PPC64_RELATIVE relocs, there is really no need to
247    propagate .opd relocs;  The dynamic linker should be taught to
248    relocate .opd without reloc entries.  */
249 #ifndef NO_OPD_RELOCS
250 #define NO_OPD_RELOCS 0
251 #endif
252
253 #ifndef ARRAY_SIZE
254 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
255 #endif
256
257 static inline int
258 abiversion (bfd *abfd)
259 {
260   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
261 }
262
263 static inline void
264 set_abiversion (bfd *abfd, int ver)
265 {
266   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
267   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
268 }
269 \f
270 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
271
272 /* Relocation HOWTO's.  */
273 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
274
275 static reloc_howto_type ppc64_elf_howto_raw[] =
276 {
277   /* This reloc does nothing.  */
278   HOWTO (R_PPC64_NONE,          /* type */
279          0,                     /* rightshift */
280          3,                     /* size (0 = byte, 1 = short, 2 = long) */
281          0,                     /* bitsize */
282          FALSE,                 /* pc_relative */
283          0,                     /* bitpos */
284          complain_overflow_dont, /* complain_on_overflow */
285          bfd_elf_generic_reloc, /* special_function */
286          "R_PPC64_NONE",        /* name */
287          FALSE,                 /* partial_inplace */
288          0,                     /* src_mask */
289          0,                     /* dst_mask */
290          FALSE),                /* pcrel_offset */
291
292   /* A standard 32 bit relocation.  */
293   HOWTO (R_PPC64_ADDR32,        /* type */
294          0,                     /* rightshift */
295          2,                     /* size (0 = byte, 1 = short, 2 = long) */
296          32,                    /* bitsize */
297          FALSE,                 /* pc_relative */
298          0,                     /* bitpos */
299          complain_overflow_bitfield, /* complain_on_overflow */
300          bfd_elf_generic_reloc, /* special_function */
301          "R_PPC64_ADDR32",      /* name */
302          FALSE,                 /* partial_inplace */
303          0,                     /* src_mask */
304          0xffffffff,            /* dst_mask */
305          FALSE),                /* pcrel_offset */
306
307   /* An absolute 26 bit branch; the lower two bits must be zero.
308      FIXME: we don't check that, we just clear them.  */
309   HOWTO (R_PPC64_ADDR24,        /* type */
310          0,                     /* rightshift */
311          2,                     /* size (0 = byte, 1 = short, 2 = long) */
312          26,                    /* bitsize */
313          FALSE,                 /* pc_relative */
314          0,                     /* bitpos */
315          complain_overflow_bitfield, /* complain_on_overflow */
316          bfd_elf_generic_reloc, /* special_function */
317          "R_PPC64_ADDR24",      /* name */
318          FALSE,                 /* partial_inplace */
319          0,                     /* src_mask */
320          0x03fffffc,            /* dst_mask */
321          FALSE),                /* pcrel_offset */
322
323   /* A standard 16 bit relocation.  */
324   HOWTO (R_PPC64_ADDR16,        /* type */
325          0,                     /* rightshift */
326          1,                     /* size (0 = byte, 1 = short, 2 = long) */
327          16,                    /* bitsize */
328          FALSE,                 /* pc_relative */
329          0,                     /* bitpos */
330          complain_overflow_bitfield, /* complain_on_overflow */
331          bfd_elf_generic_reloc, /* special_function */
332          "R_PPC64_ADDR16",      /* name */
333          FALSE,                 /* partial_inplace */
334          0,                     /* src_mask */
335          0xffff,                /* dst_mask */
336          FALSE),                /* pcrel_offset */
337
338   /* A 16 bit relocation without overflow.  */
339   HOWTO (R_PPC64_ADDR16_LO,     /* type */
340          0,                     /* rightshift */
341          1,                     /* size (0 = byte, 1 = short, 2 = long) */
342          16,                    /* bitsize */
343          FALSE,                 /* pc_relative */
344          0,                     /* bitpos */
345          complain_overflow_dont,/* complain_on_overflow */
346          bfd_elf_generic_reloc, /* special_function */
347          "R_PPC64_ADDR16_LO",   /* name */
348          FALSE,                 /* partial_inplace */
349          0,                     /* src_mask */
350          0xffff,                /* dst_mask */
351          FALSE),                /* pcrel_offset */
352
353   /* Bits 16-31 of an address.  */
354   HOWTO (R_PPC64_ADDR16_HI,     /* type */
355          16,                    /* rightshift */
356          1,                     /* size (0 = byte, 1 = short, 2 = long) */
357          16,                    /* bitsize */
358          FALSE,                 /* pc_relative */
359          0,                     /* bitpos */
360          complain_overflow_signed, /* complain_on_overflow */
361          bfd_elf_generic_reloc, /* special_function */
362          "R_PPC64_ADDR16_HI",   /* name */
363          FALSE,                 /* partial_inplace */
364          0,                     /* src_mask */
365          0xffff,                /* dst_mask */
366          FALSE),                /* pcrel_offset */
367
368   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
369      bits, treated as a signed number, is negative.  */
370   HOWTO (R_PPC64_ADDR16_HA,     /* type */
371          16,                    /* rightshift */
372          1,                     /* size (0 = byte, 1 = short, 2 = long) */
373          16,                    /* bitsize */
374          FALSE,                 /* pc_relative */
375          0,                     /* bitpos */
376          complain_overflow_signed, /* complain_on_overflow */
377          ppc64_elf_ha_reloc,    /* special_function */
378          "R_PPC64_ADDR16_HA",   /* name */
379          FALSE,                 /* partial_inplace */
380          0,                     /* src_mask */
381          0xffff,                /* dst_mask */
382          FALSE),                /* pcrel_offset */
383
384   /* An absolute 16 bit branch; the lower two bits must be zero.
385      FIXME: we don't check that, we just clear them.  */
386   HOWTO (R_PPC64_ADDR14,        /* type */
387          0,                     /* rightshift */
388          2,                     /* size (0 = byte, 1 = short, 2 = long) */
389          16,                    /* bitsize */
390          FALSE,                 /* pc_relative */
391          0,                     /* bitpos */
392          complain_overflow_signed, /* complain_on_overflow */
393          ppc64_elf_branch_reloc, /* special_function */
394          "R_PPC64_ADDR14",      /* name */
395          FALSE,                 /* partial_inplace */
396          0,                     /* src_mask */
397          0x0000fffc,            /* dst_mask */
398          FALSE),                /* pcrel_offset */
399
400   /* An absolute 16 bit branch, for which bit 10 should be set to
401      indicate that the branch is expected to be taken.  The lower two
402      bits must be zero.  */
403   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
404          0,                     /* rightshift */
405          2,                     /* size (0 = byte, 1 = short, 2 = long) */
406          16,                    /* bitsize */
407          FALSE,                 /* pc_relative */
408          0,                     /* bitpos */
409          complain_overflow_signed, /* complain_on_overflow */
410          ppc64_elf_brtaken_reloc, /* special_function */
411          "R_PPC64_ADDR14_BRTAKEN",/* name */
412          FALSE,                 /* partial_inplace */
413          0,                     /* src_mask */
414          0x0000fffc,            /* dst_mask */
415          FALSE),                /* pcrel_offset */
416
417   /* An absolute 16 bit branch, for which bit 10 should be set to
418      indicate that the branch is not expected to be taken.  The lower
419      two bits must be zero.  */
420   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
421          0,                     /* rightshift */
422          2,                     /* size (0 = byte, 1 = short, 2 = long) */
423          16,                    /* bitsize */
424          FALSE,                 /* pc_relative */
425          0,                     /* bitpos */
426          complain_overflow_signed, /* complain_on_overflow */
427          ppc64_elf_brtaken_reloc, /* special_function */
428          "R_PPC64_ADDR14_BRNTAKEN",/* name */
429          FALSE,                 /* partial_inplace */
430          0,                     /* src_mask */
431          0x0000fffc,            /* dst_mask */
432          FALSE),                /* pcrel_offset */
433
434   /* A relative 26 bit branch; the lower two bits must be zero.  */
435   HOWTO (R_PPC64_REL24,         /* type */
436          0,                     /* rightshift */
437          2,                     /* size (0 = byte, 1 = short, 2 = long) */
438          26,                    /* bitsize */
439          TRUE,                  /* pc_relative */
440          0,                     /* bitpos */
441          complain_overflow_signed, /* complain_on_overflow */
442          ppc64_elf_branch_reloc, /* special_function */
443          "R_PPC64_REL24",       /* name */
444          FALSE,                 /* partial_inplace */
445          0,                     /* src_mask */
446          0x03fffffc,            /* dst_mask */
447          TRUE),                 /* pcrel_offset */
448
449   /* A relative 16 bit branch; the lower two bits must be zero.  */
450   HOWTO (R_PPC64_REL14,         /* type */
451          0,                     /* rightshift */
452          2,                     /* size (0 = byte, 1 = short, 2 = long) */
453          16,                    /* bitsize */
454          TRUE,                  /* pc_relative */
455          0,                     /* bitpos */
456          complain_overflow_signed, /* complain_on_overflow */
457          ppc64_elf_branch_reloc, /* special_function */
458          "R_PPC64_REL14",       /* name */
459          FALSE,                 /* partial_inplace */
460          0,                     /* src_mask */
461          0x0000fffc,            /* dst_mask */
462          TRUE),                 /* pcrel_offset */
463
464   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
465      the branch is expected to be taken.  The lower two bits must be
466      zero.  */
467   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
468          0,                     /* rightshift */
469          2,                     /* size (0 = byte, 1 = short, 2 = long) */
470          16,                    /* bitsize */
471          TRUE,                  /* pc_relative */
472          0,                     /* bitpos */
473          complain_overflow_signed, /* complain_on_overflow */
474          ppc64_elf_brtaken_reloc, /* special_function */
475          "R_PPC64_REL14_BRTAKEN", /* name */
476          FALSE,                 /* partial_inplace */
477          0,                     /* src_mask */
478          0x0000fffc,            /* dst_mask */
479          TRUE),                 /* pcrel_offset */
480
481   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
482      the branch is not expected to be taken.  The lower two bits must
483      be zero.  */
484   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
485          0,                     /* rightshift */
486          2,                     /* size (0 = byte, 1 = short, 2 = long) */
487          16,                    /* bitsize */
488          TRUE,                  /* pc_relative */
489          0,                     /* bitpos */
490          complain_overflow_signed, /* complain_on_overflow */
491          ppc64_elf_brtaken_reloc, /* special_function */
492          "R_PPC64_REL14_BRNTAKEN",/* name */
493          FALSE,                 /* partial_inplace */
494          0,                     /* src_mask */
495          0x0000fffc,            /* dst_mask */
496          TRUE),                 /* pcrel_offset */
497
498   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
499      symbol.  */
500   HOWTO (R_PPC64_GOT16,         /* type */
501          0,                     /* rightshift */
502          1,                     /* size (0 = byte, 1 = short, 2 = long) */
503          16,                    /* bitsize */
504          FALSE,                 /* pc_relative */
505          0,                     /* bitpos */
506          complain_overflow_signed, /* complain_on_overflow */
507          ppc64_elf_unhandled_reloc, /* special_function */
508          "R_PPC64_GOT16",       /* name */
509          FALSE,                 /* partial_inplace */
510          0,                     /* src_mask */
511          0xffff,                /* dst_mask */
512          FALSE),                /* pcrel_offset */
513
514   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
515      the symbol.  */
516   HOWTO (R_PPC64_GOT16_LO,      /* type */
517          0,                     /* rightshift */
518          1,                     /* size (0 = byte, 1 = short, 2 = long) */
519          16,                    /* bitsize */
520          FALSE,                 /* pc_relative */
521          0,                     /* bitpos */
522          complain_overflow_dont, /* complain_on_overflow */
523          ppc64_elf_unhandled_reloc, /* special_function */
524          "R_PPC64_GOT16_LO",    /* name */
525          FALSE,                 /* partial_inplace */
526          0,                     /* src_mask */
527          0xffff,                /* dst_mask */
528          FALSE),                /* pcrel_offset */
529
530   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
531      the symbol.  */
532   HOWTO (R_PPC64_GOT16_HI,      /* type */
533          16,                    /* rightshift */
534          1,                     /* size (0 = byte, 1 = short, 2 = long) */
535          16,                    /* bitsize */
536          FALSE,                 /* pc_relative */
537          0,                     /* bitpos */
538          complain_overflow_signed,/* complain_on_overflow */
539          ppc64_elf_unhandled_reloc, /* special_function */
540          "R_PPC64_GOT16_HI",    /* name */
541          FALSE,                 /* partial_inplace */
542          0,                     /* src_mask */
543          0xffff,                /* dst_mask */
544          FALSE),                /* pcrel_offset */
545
546   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
547      the symbol.  */
548   HOWTO (R_PPC64_GOT16_HA,      /* type */
549          16,                    /* rightshift */
550          1,                     /* size (0 = byte, 1 = short, 2 = long) */
551          16,                    /* bitsize */
552          FALSE,                 /* pc_relative */
553          0,                     /* bitpos */
554          complain_overflow_signed,/* complain_on_overflow */
555          ppc64_elf_unhandled_reloc, /* special_function */
556          "R_PPC64_GOT16_HA",    /* name */
557          FALSE,                 /* partial_inplace */
558          0,                     /* src_mask */
559          0xffff,                /* dst_mask */
560          FALSE),                /* pcrel_offset */
561
562   /* This is used only by the dynamic linker.  The symbol should exist
563      both in the object being run and in some shared library.  The
564      dynamic linker copies the data addressed by the symbol from the
565      shared library into the object, because the object being
566      run has to have the data at some particular address.  */
567   HOWTO (R_PPC64_COPY,          /* type */
568          0,                     /* rightshift */
569          0,                     /* this one is variable size */
570          0,                     /* bitsize */
571          FALSE,                 /* pc_relative */
572          0,                     /* bitpos */
573          complain_overflow_dont, /* complain_on_overflow */
574          ppc64_elf_unhandled_reloc, /* special_function */
575          "R_PPC64_COPY",        /* name */
576          FALSE,                 /* partial_inplace */
577          0,                     /* src_mask */
578          0,                     /* dst_mask */
579          FALSE),                /* pcrel_offset */
580
581   /* Like R_PPC64_ADDR64, but used when setting global offset table
582      entries.  */
583   HOWTO (R_PPC64_GLOB_DAT,      /* type */
584          0,                     /* rightshift */
585          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
586          64,                    /* bitsize */
587          FALSE,                 /* pc_relative */
588          0,                     /* bitpos */
589          complain_overflow_dont, /* complain_on_overflow */
590          ppc64_elf_unhandled_reloc,  /* special_function */
591          "R_PPC64_GLOB_DAT",    /* name */
592          FALSE,                 /* partial_inplace */
593          0,                     /* src_mask */
594          ONES (64),             /* dst_mask */
595          FALSE),                /* pcrel_offset */
596
597   /* Created by the link editor.  Marks a procedure linkage table
598      entry for a symbol.  */
599   HOWTO (R_PPC64_JMP_SLOT,      /* type */
600          0,                     /* rightshift */
601          0,                     /* size (0 = byte, 1 = short, 2 = long) */
602          0,                     /* bitsize */
603          FALSE,                 /* pc_relative */
604          0,                     /* bitpos */
605          complain_overflow_dont, /* complain_on_overflow */
606          ppc64_elf_unhandled_reloc, /* special_function */
607          "R_PPC64_JMP_SLOT",    /* name */
608          FALSE,                 /* partial_inplace */
609          0,                     /* src_mask */
610          0,                     /* dst_mask */
611          FALSE),                /* pcrel_offset */
612
613   /* Used only by the dynamic linker.  When the object is run, this
614      doubleword64 is set to the load address of the object, plus the
615      addend.  */
616   HOWTO (R_PPC64_RELATIVE,      /* type */
617          0,                     /* rightshift */
618          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
619          64,                    /* bitsize */
620          FALSE,                 /* pc_relative */
621          0,                     /* bitpos */
622          complain_overflow_dont, /* complain_on_overflow */
623          bfd_elf_generic_reloc, /* special_function */
624          "R_PPC64_RELATIVE",    /* name */
625          FALSE,                 /* partial_inplace */
626          0,                     /* src_mask */
627          ONES (64),             /* dst_mask */
628          FALSE),                /* pcrel_offset */
629
630   /* Like R_PPC64_ADDR32, but may be unaligned.  */
631   HOWTO (R_PPC64_UADDR32,       /* type */
632          0,                     /* rightshift */
633          2,                     /* size (0 = byte, 1 = short, 2 = long) */
634          32,                    /* bitsize */
635          FALSE,                 /* pc_relative */
636          0,                     /* bitpos */
637          complain_overflow_bitfield, /* complain_on_overflow */
638          bfd_elf_generic_reloc, /* special_function */
639          "R_PPC64_UADDR32",     /* name */
640          FALSE,                 /* partial_inplace */
641          0,                     /* src_mask */
642          0xffffffff,            /* dst_mask */
643          FALSE),                /* pcrel_offset */
644
645   /* Like R_PPC64_ADDR16, but may be unaligned.  */
646   HOWTO (R_PPC64_UADDR16,       /* type */
647          0,                     /* rightshift */
648          1,                     /* size (0 = byte, 1 = short, 2 = long) */
649          16,                    /* bitsize */
650          FALSE,                 /* pc_relative */
651          0,                     /* bitpos */
652          complain_overflow_bitfield, /* complain_on_overflow */
653          bfd_elf_generic_reloc, /* special_function */
654          "R_PPC64_UADDR16",     /* name */
655          FALSE,                 /* partial_inplace */
656          0,                     /* src_mask */
657          0xffff,                /* dst_mask */
658          FALSE),                /* pcrel_offset */
659
660   /* 32-bit PC relative.  */
661   HOWTO (R_PPC64_REL32,         /* type */
662          0,                     /* rightshift */
663          2,                     /* size (0 = byte, 1 = short, 2 = long) */
664          32,                    /* bitsize */
665          TRUE,                  /* pc_relative */
666          0,                     /* bitpos */
667          complain_overflow_signed, /* complain_on_overflow */
668          bfd_elf_generic_reloc, /* special_function */
669          "R_PPC64_REL32",       /* name */
670          FALSE,                 /* partial_inplace */
671          0,                     /* src_mask */
672          0xffffffff,            /* dst_mask */
673          TRUE),                 /* pcrel_offset */
674
675   /* 32-bit relocation to the symbol's procedure linkage table.  */
676   HOWTO (R_PPC64_PLT32,         /* type */
677          0,                     /* rightshift */
678          2,                     /* size (0 = byte, 1 = short, 2 = long) */
679          32,                    /* bitsize */
680          FALSE,                 /* pc_relative */
681          0,                     /* bitpos */
682          complain_overflow_bitfield, /* complain_on_overflow */
683          ppc64_elf_unhandled_reloc, /* special_function */
684          "R_PPC64_PLT32",       /* name */
685          FALSE,                 /* partial_inplace */
686          0,                     /* src_mask */
687          0xffffffff,            /* dst_mask */
688          FALSE),                /* pcrel_offset */
689
690   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
691      FIXME: R_PPC64_PLTREL32 not supported.  */
692   HOWTO (R_PPC64_PLTREL32,      /* type */
693          0,                     /* rightshift */
694          2,                     /* size (0 = byte, 1 = short, 2 = long) */
695          32,                    /* bitsize */
696          TRUE,                  /* pc_relative */
697          0,                     /* bitpos */
698          complain_overflow_signed, /* complain_on_overflow */
699          ppc64_elf_unhandled_reloc, /* special_function */
700          "R_PPC64_PLTREL32",    /* name */
701          FALSE,                 /* partial_inplace */
702          0,                     /* src_mask */
703          0xffffffff,            /* dst_mask */
704          TRUE),                 /* pcrel_offset */
705
706   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
707      the symbol.  */
708   HOWTO (R_PPC64_PLT16_LO,      /* type */
709          0,                     /* rightshift */
710          1,                     /* size (0 = byte, 1 = short, 2 = long) */
711          16,                    /* bitsize */
712          FALSE,                 /* pc_relative */
713          0,                     /* bitpos */
714          complain_overflow_dont, /* complain_on_overflow */
715          ppc64_elf_unhandled_reloc, /* special_function */
716          "R_PPC64_PLT16_LO",    /* name */
717          FALSE,                 /* partial_inplace */
718          0,                     /* src_mask */
719          0xffff,                /* dst_mask */
720          FALSE),                /* pcrel_offset */
721
722   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
723      the symbol.  */
724   HOWTO (R_PPC64_PLT16_HI,      /* type */
725          16,                    /* rightshift */
726          1,                     /* size (0 = byte, 1 = short, 2 = long) */
727          16,                    /* bitsize */
728          FALSE,                 /* pc_relative */
729          0,                     /* bitpos */
730          complain_overflow_signed, /* complain_on_overflow */
731          ppc64_elf_unhandled_reloc, /* special_function */
732          "R_PPC64_PLT16_HI",    /* name */
733          FALSE,                 /* partial_inplace */
734          0,                     /* src_mask */
735          0xffff,                /* dst_mask */
736          FALSE),                /* pcrel_offset */
737
738   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
739      the symbol.  */
740   HOWTO (R_PPC64_PLT16_HA,      /* type */
741          16,                    /* rightshift */
742          1,                     /* size (0 = byte, 1 = short, 2 = long) */
743          16,                    /* bitsize */
744          FALSE,                 /* pc_relative */
745          0,                     /* bitpos */
746          complain_overflow_signed, /* complain_on_overflow */
747          ppc64_elf_unhandled_reloc, /* special_function */
748          "R_PPC64_PLT16_HA",    /* name */
749          FALSE,                 /* partial_inplace */
750          0,                     /* src_mask */
751          0xffff,                /* dst_mask */
752          FALSE),                /* pcrel_offset */
753
754   /* 16-bit section relative relocation.  */
755   HOWTO (R_PPC64_SECTOFF,       /* type */
756          0,                     /* rightshift */
757          1,                     /* size (0 = byte, 1 = short, 2 = long) */
758          16,                    /* bitsize */
759          FALSE,                 /* pc_relative */
760          0,                     /* bitpos */
761          complain_overflow_signed, /* complain_on_overflow */
762          ppc64_elf_sectoff_reloc, /* special_function */
763          "R_PPC64_SECTOFF",     /* name */
764          FALSE,                 /* partial_inplace */
765          0,                     /* src_mask */
766          0xffff,                /* dst_mask */
767          FALSE),                /* pcrel_offset */
768
769   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
770   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
771          0,                     /* rightshift */
772          1,                     /* size (0 = byte, 1 = short, 2 = long) */
773          16,                    /* bitsize */
774          FALSE,                 /* pc_relative */
775          0,                     /* bitpos */
776          complain_overflow_dont, /* complain_on_overflow */
777          ppc64_elf_sectoff_reloc, /* special_function */
778          "R_PPC64_SECTOFF_LO",  /* name */
779          FALSE,                 /* partial_inplace */
780          0,                     /* src_mask */
781          0xffff,                /* dst_mask */
782          FALSE),                /* pcrel_offset */
783
784   /* 16-bit upper half section relative relocation.  */
785   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
786          16,                    /* rightshift */
787          1,                     /* size (0 = byte, 1 = short, 2 = long) */
788          16,                    /* bitsize */
789          FALSE,                 /* pc_relative */
790          0,                     /* bitpos */
791          complain_overflow_signed, /* complain_on_overflow */
792          ppc64_elf_sectoff_reloc, /* special_function */
793          "R_PPC64_SECTOFF_HI",  /* name */
794          FALSE,                 /* partial_inplace */
795          0,                     /* src_mask */
796          0xffff,                /* dst_mask */
797          FALSE),                /* pcrel_offset */
798
799   /* 16-bit upper half adjusted section relative relocation.  */
800   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
801          16,                    /* rightshift */
802          1,                     /* size (0 = byte, 1 = short, 2 = long) */
803          16,                    /* bitsize */
804          FALSE,                 /* pc_relative */
805          0,                     /* bitpos */
806          complain_overflow_signed, /* complain_on_overflow */
807          ppc64_elf_sectoff_ha_reloc, /* special_function */
808          "R_PPC64_SECTOFF_HA",  /* name */
809          FALSE,                 /* partial_inplace */
810          0,                     /* src_mask */
811          0xffff,                /* dst_mask */
812          FALSE),                /* pcrel_offset */
813
814   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
815   HOWTO (R_PPC64_REL30,         /* type */
816          2,                     /* rightshift */
817          2,                     /* size (0 = byte, 1 = short, 2 = long) */
818          30,                    /* bitsize */
819          TRUE,                  /* pc_relative */
820          0,                     /* bitpos */
821          complain_overflow_dont, /* complain_on_overflow */
822          bfd_elf_generic_reloc, /* special_function */
823          "R_PPC64_REL30",       /* name */
824          FALSE,                 /* partial_inplace */
825          0,                     /* src_mask */
826          0xfffffffc,            /* dst_mask */
827          TRUE),                 /* pcrel_offset */
828
829   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
830
831   /* A standard 64-bit relocation.  */
832   HOWTO (R_PPC64_ADDR64,        /* type */
833          0,                     /* rightshift */
834          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
835          64,                    /* bitsize */
836          FALSE,                 /* pc_relative */
837          0,                     /* bitpos */
838          complain_overflow_dont, /* complain_on_overflow */
839          bfd_elf_generic_reloc, /* special_function */
840          "R_PPC64_ADDR64",      /* name */
841          FALSE,                 /* partial_inplace */
842          0,                     /* src_mask */
843          ONES (64),             /* dst_mask */
844          FALSE),                /* pcrel_offset */
845
846   /* The bits 32-47 of an address.  */
847   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
848          32,                    /* rightshift */
849          1,                     /* size (0 = byte, 1 = short, 2 = long) */
850          16,                    /* bitsize */
851          FALSE,                 /* pc_relative */
852          0,                     /* bitpos */
853          complain_overflow_dont, /* complain_on_overflow */
854          bfd_elf_generic_reloc, /* special_function */
855          "R_PPC64_ADDR16_HIGHER", /* name */
856          FALSE,                 /* partial_inplace */
857          0,                     /* src_mask */
858          0xffff,                /* dst_mask */
859          FALSE),                /* pcrel_offset */
860
861   /* The bits 32-47 of an address, plus 1 if the contents of the low
862      16 bits, treated as a signed number, is negative.  */
863   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
864          32,                    /* rightshift */
865          1,                     /* size (0 = byte, 1 = short, 2 = long) */
866          16,                    /* bitsize */
867          FALSE,                 /* pc_relative */
868          0,                     /* bitpos */
869          complain_overflow_dont, /* complain_on_overflow */
870          ppc64_elf_ha_reloc,    /* special_function */
871          "R_PPC64_ADDR16_HIGHERA", /* name */
872          FALSE,                 /* partial_inplace */
873          0,                     /* src_mask */
874          0xffff,                /* dst_mask */
875          FALSE),                /* pcrel_offset */
876
877   /* The bits 48-63 of an address.  */
878   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
879          48,                    /* rightshift */
880          1,                     /* size (0 = byte, 1 = short, 2 = long) */
881          16,                    /* bitsize */
882          FALSE,                 /* pc_relative */
883          0,                     /* bitpos */
884          complain_overflow_dont, /* complain_on_overflow */
885          bfd_elf_generic_reloc, /* special_function */
886          "R_PPC64_ADDR16_HIGHEST", /* name */
887          FALSE,                 /* partial_inplace */
888          0,                     /* src_mask */
889          0xffff,                /* dst_mask */
890          FALSE),                /* pcrel_offset */
891
892   /* The bits 48-63 of an address, plus 1 if the contents of the low
893      16 bits, treated as a signed number, is negative.  */
894   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
895          48,                    /* rightshift */
896          1,                     /* size (0 = byte, 1 = short, 2 = long) */
897          16,                    /* bitsize */
898          FALSE,                 /* pc_relative */
899          0,                     /* bitpos */
900          complain_overflow_dont, /* complain_on_overflow */
901          ppc64_elf_ha_reloc,    /* special_function */
902          "R_PPC64_ADDR16_HIGHESTA", /* name */
903          FALSE,                 /* partial_inplace */
904          0,                     /* src_mask */
905          0xffff,                /* dst_mask */
906          FALSE),                /* pcrel_offset */
907
908   /* Like ADDR64, but may be unaligned.  */
909   HOWTO (R_PPC64_UADDR64,       /* type */
910          0,                     /* rightshift */
911          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
912          64,                    /* bitsize */
913          FALSE,                 /* pc_relative */
914          0,                     /* bitpos */
915          complain_overflow_dont, /* complain_on_overflow */
916          bfd_elf_generic_reloc, /* special_function */
917          "R_PPC64_UADDR64",     /* name */
918          FALSE,                 /* partial_inplace */
919          0,                     /* src_mask */
920          ONES (64),             /* dst_mask */
921          FALSE),                /* pcrel_offset */
922
923   /* 64-bit relative relocation.  */
924   HOWTO (R_PPC64_REL64,         /* type */
925          0,                     /* rightshift */
926          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
927          64,                    /* bitsize */
928          TRUE,                  /* pc_relative */
929          0,                     /* bitpos */
930          complain_overflow_dont, /* complain_on_overflow */
931          bfd_elf_generic_reloc, /* special_function */
932          "R_PPC64_REL64",       /* name */
933          FALSE,                 /* partial_inplace */
934          0,                     /* src_mask */
935          ONES (64),             /* dst_mask */
936          TRUE),                 /* pcrel_offset */
937
938   /* 64-bit relocation to the symbol's procedure linkage table.  */
939   HOWTO (R_PPC64_PLT64,         /* type */
940          0,                     /* rightshift */
941          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
942          64,                    /* bitsize */
943          FALSE,                 /* pc_relative */
944          0,                     /* bitpos */
945          complain_overflow_dont, /* complain_on_overflow */
946          ppc64_elf_unhandled_reloc, /* special_function */
947          "R_PPC64_PLT64",       /* name */
948          FALSE,                 /* partial_inplace */
949          0,                     /* src_mask */
950          ONES (64),             /* dst_mask */
951          FALSE),                /* pcrel_offset */
952
953   /* 64-bit PC relative relocation to the symbol's procedure linkage
954      table.  */
955   /* FIXME: R_PPC64_PLTREL64 not supported.  */
956   HOWTO (R_PPC64_PLTREL64,      /* type */
957          0,                     /* rightshift */
958          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
959          64,                    /* bitsize */
960          TRUE,                  /* pc_relative */
961          0,                     /* bitpos */
962          complain_overflow_dont, /* complain_on_overflow */
963          ppc64_elf_unhandled_reloc, /* special_function */
964          "R_PPC64_PLTREL64",    /* name */
965          FALSE,                 /* partial_inplace */
966          0,                     /* src_mask */
967          ONES (64),             /* dst_mask */
968          TRUE),                 /* pcrel_offset */
969
970   /* 16 bit TOC-relative relocation.  */
971
972   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
973   HOWTO (R_PPC64_TOC16,         /* type */
974          0,                     /* rightshift */
975          1,                     /* size (0 = byte, 1 = short, 2 = long) */
976          16,                    /* bitsize */
977          FALSE,                 /* pc_relative */
978          0,                     /* bitpos */
979          complain_overflow_signed, /* complain_on_overflow */
980          ppc64_elf_toc_reloc,   /* special_function */
981          "R_PPC64_TOC16",       /* name */
982          FALSE,                 /* partial_inplace */
983          0,                     /* src_mask */
984          0xffff,                /* dst_mask */
985          FALSE),                /* pcrel_offset */
986
987   /* 16 bit TOC-relative relocation without overflow.  */
988
989   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
990   HOWTO (R_PPC64_TOC16_LO,      /* type */
991          0,                     /* rightshift */
992          1,                     /* size (0 = byte, 1 = short, 2 = long) */
993          16,                    /* bitsize */
994          FALSE,                 /* pc_relative */
995          0,                     /* bitpos */
996          complain_overflow_dont, /* complain_on_overflow */
997          ppc64_elf_toc_reloc,   /* special_function */
998          "R_PPC64_TOC16_LO",    /* name */
999          FALSE,                 /* partial_inplace */
1000          0,                     /* src_mask */
1001          0xffff,                /* dst_mask */
1002          FALSE),                /* pcrel_offset */
1003
1004   /* 16 bit TOC-relative relocation, high 16 bits.  */
1005
1006   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
1007   HOWTO (R_PPC64_TOC16_HI,      /* type */
1008          16,                    /* rightshift */
1009          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1010          16,                    /* bitsize */
1011          FALSE,                 /* pc_relative */
1012          0,                     /* bitpos */
1013          complain_overflow_signed, /* complain_on_overflow */
1014          ppc64_elf_toc_reloc,   /* special_function */
1015          "R_PPC64_TOC16_HI",    /* name */
1016          FALSE,                 /* partial_inplace */
1017          0,                     /* src_mask */
1018          0xffff,                /* dst_mask */
1019          FALSE),                /* pcrel_offset */
1020
1021   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1022      contents of the low 16 bits, treated as a signed number, is
1023      negative.  */
1024
1025   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
1026   HOWTO (R_PPC64_TOC16_HA,      /* type */
1027          16,                    /* rightshift */
1028          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1029          16,                    /* bitsize */
1030          FALSE,                 /* pc_relative */
1031          0,                     /* bitpos */
1032          complain_overflow_signed, /* complain_on_overflow */
1033          ppc64_elf_toc_ha_reloc, /* special_function */
1034          "R_PPC64_TOC16_HA",    /* name */
1035          FALSE,                 /* partial_inplace */
1036          0,                     /* src_mask */
1037          0xffff,                /* dst_mask */
1038          FALSE),                /* pcrel_offset */
1039
1040   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
1041
1042   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
1043   HOWTO (R_PPC64_TOC,           /* type */
1044          0,                     /* rightshift */
1045          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1046          64,                    /* bitsize */
1047          FALSE,                 /* pc_relative */
1048          0,                     /* bitpos */
1049          complain_overflow_dont, /* complain_on_overflow */
1050          ppc64_elf_toc64_reloc, /* special_function */
1051          "R_PPC64_TOC",         /* name */
1052          FALSE,                 /* partial_inplace */
1053          0,                     /* src_mask */
1054          ONES (64),             /* dst_mask */
1055          FALSE),                /* pcrel_offset */
1056
1057   /* Like R_PPC64_GOT16, but also informs the link editor that the
1058      value to relocate may (!) refer to a PLT entry which the link
1059      editor (a) may replace with the symbol value.  If the link editor
1060      is unable to fully resolve the symbol, it may (b) create a PLT
1061      entry and store the address to the new PLT entry in the GOT.
1062      This permits lazy resolution of function symbols at run time.
1063      The link editor may also skip all of this and just (c) emit a
1064      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
1065   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
1066     HOWTO (R_PPC64_PLTGOT16,    /* type */
1067          0,                     /* rightshift */
1068          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1069          16,                    /* bitsize */
1070          FALSE,                 /* pc_relative */
1071          0,                     /* bitpos */
1072          complain_overflow_signed, /* complain_on_overflow */
1073          ppc64_elf_unhandled_reloc, /* special_function */
1074          "R_PPC64_PLTGOT16",    /* name */
1075          FALSE,                 /* partial_inplace */
1076          0,                     /* src_mask */
1077          0xffff,                /* dst_mask */
1078          FALSE),                /* pcrel_offset */
1079
1080   /* Like R_PPC64_PLTGOT16, but without overflow.  */
1081   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1082   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1083          0,                     /* rightshift */
1084          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1085          16,                    /* bitsize */
1086          FALSE,                 /* pc_relative */
1087          0,                     /* bitpos */
1088          complain_overflow_dont, /* complain_on_overflow */
1089          ppc64_elf_unhandled_reloc, /* special_function */
1090          "R_PPC64_PLTGOT16_LO", /* name */
1091          FALSE,                 /* partial_inplace */
1092          0,                     /* src_mask */
1093          0xffff,                /* dst_mask */
1094          FALSE),                /* pcrel_offset */
1095
1096   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1097   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1098   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1099          16,                    /* rightshift */
1100          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1101          16,                    /* bitsize */
1102          FALSE,                 /* pc_relative */
1103          0,                     /* bitpos */
1104          complain_overflow_signed, /* complain_on_overflow */
1105          ppc64_elf_unhandled_reloc, /* special_function */
1106          "R_PPC64_PLTGOT16_HI", /* name */
1107          FALSE,                 /* partial_inplace */
1108          0,                     /* src_mask */
1109          0xffff,                /* dst_mask */
1110          FALSE),                /* pcrel_offset */
1111
1112   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1113      1 if the contents of the low 16 bits, treated as a signed number,
1114      is negative.  */
1115   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1116   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1117          16,                    /* rightshift */
1118          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1119          16,                    /* bitsize */
1120          FALSE,                 /* pc_relative */
1121          0,                     /* bitpos */
1122          complain_overflow_signed, /* complain_on_overflow */
1123          ppc64_elf_unhandled_reloc, /* special_function */
1124          "R_PPC64_PLTGOT16_HA", /* name */
1125          FALSE,                 /* partial_inplace */
1126          0,                     /* src_mask */
1127          0xffff,                /* dst_mask */
1128          FALSE),                /* pcrel_offset */
1129
1130   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1131   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1132          0,                     /* rightshift */
1133          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1134          16,                    /* bitsize */
1135          FALSE,                 /* pc_relative */
1136          0,                     /* bitpos */
1137          complain_overflow_signed, /* complain_on_overflow */
1138          bfd_elf_generic_reloc, /* special_function */
1139          "R_PPC64_ADDR16_DS",   /* name */
1140          FALSE,                 /* partial_inplace */
1141          0,                     /* src_mask */
1142          0xfffc,                /* dst_mask */
1143          FALSE),                /* pcrel_offset */
1144
1145   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1146   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1147          0,                     /* rightshift */
1148          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1149          16,                    /* bitsize */
1150          FALSE,                 /* pc_relative */
1151          0,                     /* bitpos */
1152          complain_overflow_dont,/* complain_on_overflow */
1153          bfd_elf_generic_reloc, /* special_function */
1154          "R_PPC64_ADDR16_LO_DS",/* name */
1155          FALSE,                 /* partial_inplace */
1156          0,                     /* src_mask */
1157          0xfffc,                /* dst_mask */
1158          FALSE),                /* pcrel_offset */
1159
1160   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1161   HOWTO (R_PPC64_GOT16_DS,      /* type */
1162          0,                     /* rightshift */
1163          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1164          16,                    /* bitsize */
1165          FALSE,                 /* pc_relative */
1166          0,                     /* bitpos */
1167          complain_overflow_signed, /* complain_on_overflow */
1168          ppc64_elf_unhandled_reloc, /* special_function */
1169          "R_PPC64_GOT16_DS",    /* name */
1170          FALSE,                 /* partial_inplace */
1171          0,                     /* src_mask */
1172          0xfffc,                /* dst_mask */
1173          FALSE),                /* pcrel_offset */
1174
1175   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1176   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1177          0,                     /* rightshift */
1178          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1179          16,                    /* bitsize */
1180          FALSE,                 /* pc_relative */
1181          0,                     /* bitpos */
1182          complain_overflow_dont, /* complain_on_overflow */
1183          ppc64_elf_unhandled_reloc, /* special_function */
1184          "R_PPC64_GOT16_LO_DS", /* name */
1185          FALSE,                 /* partial_inplace */
1186          0,                     /* src_mask */
1187          0xfffc,                /* dst_mask */
1188          FALSE),                /* pcrel_offset */
1189
1190   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1191   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1192          0,                     /* rightshift */
1193          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1194          16,                    /* bitsize */
1195          FALSE,                 /* pc_relative */
1196          0,                     /* bitpos */
1197          complain_overflow_dont, /* complain_on_overflow */
1198          ppc64_elf_unhandled_reloc, /* special_function */
1199          "R_PPC64_PLT16_LO_DS", /* name */
1200          FALSE,                 /* partial_inplace */
1201          0,                     /* src_mask */
1202          0xfffc,                /* dst_mask */
1203          FALSE),                /* pcrel_offset */
1204
1205   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1206   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1207          0,                     /* rightshift */
1208          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1209          16,                    /* bitsize */
1210          FALSE,                 /* pc_relative */
1211          0,                     /* bitpos */
1212          complain_overflow_signed, /* complain_on_overflow */
1213          ppc64_elf_sectoff_reloc, /* special_function */
1214          "R_PPC64_SECTOFF_DS",  /* name */
1215          FALSE,                 /* partial_inplace */
1216          0,                     /* src_mask */
1217          0xfffc,                /* dst_mask */
1218          FALSE),                /* pcrel_offset */
1219
1220   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1221   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1222          0,                     /* rightshift */
1223          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1224          16,                    /* bitsize */
1225          FALSE,                 /* pc_relative */
1226          0,                     /* bitpos */
1227          complain_overflow_dont, /* complain_on_overflow */
1228          ppc64_elf_sectoff_reloc, /* special_function */
1229          "R_PPC64_SECTOFF_LO_DS",/* name */
1230          FALSE,                 /* partial_inplace */
1231          0,                     /* src_mask */
1232          0xfffc,                /* dst_mask */
1233          FALSE),                /* pcrel_offset */
1234
1235   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1236   HOWTO (R_PPC64_TOC16_DS,      /* type */
1237          0,                     /* rightshift */
1238          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1239          16,                    /* bitsize */
1240          FALSE,                 /* pc_relative */
1241          0,                     /* bitpos */
1242          complain_overflow_signed, /* complain_on_overflow */
1243          ppc64_elf_toc_reloc,   /* special_function */
1244          "R_PPC64_TOC16_DS",    /* name */
1245          FALSE,                 /* partial_inplace */
1246          0,                     /* src_mask */
1247          0xfffc,                /* dst_mask */
1248          FALSE),                /* pcrel_offset */
1249
1250   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1251   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1252          0,                     /* rightshift */
1253          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1254          16,                    /* bitsize */
1255          FALSE,                 /* pc_relative */
1256          0,                     /* bitpos */
1257          complain_overflow_dont, /* complain_on_overflow */
1258          ppc64_elf_toc_reloc,   /* special_function */
1259          "R_PPC64_TOC16_LO_DS", /* name */
1260          FALSE,                 /* partial_inplace */
1261          0,                     /* src_mask */
1262          0xfffc,                /* dst_mask */
1263          FALSE),                /* pcrel_offset */
1264
1265   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1266   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1267   HOWTO (R_PPC64_PLTGOT16_DS,   /* type */
1268          0,                     /* rightshift */
1269          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1270          16,                    /* bitsize */
1271          FALSE,                 /* pc_relative */
1272          0,                     /* bitpos */
1273          complain_overflow_signed, /* complain_on_overflow */
1274          ppc64_elf_unhandled_reloc, /* special_function */
1275          "R_PPC64_PLTGOT16_DS", /* name */
1276          FALSE,                 /* partial_inplace */
1277          0,                     /* src_mask */
1278          0xfffc,                /* dst_mask */
1279          FALSE),                /* pcrel_offset */
1280
1281   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1282   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1283   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1284          0,                     /* rightshift */
1285          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1286          16,                    /* bitsize */
1287          FALSE,                 /* pc_relative */
1288          0,                     /* bitpos */
1289          complain_overflow_dont, /* complain_on_overflow */
1290          ppc64_elf_unhandled_reloc, /* special_function */
1291          "R_PPC64_PLTGOT16_LO_DS",/* name */
1292          FALSE,                 /* partial_inplace */
1293          0,                     /* src_mask */
1294          0xfffc,                /* dst_mask */
1295          FALSE),                /* pcrel_offset */
1296
1297   /* Marker relocs for TLS.  */
1298   HOWTO (R_PPC64_TLS,
1299          0,                     /* rightshift */
1300          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1301          32,                    /* bitsize */
1302          FALSE,                 /* pc_relative */
1303          0,                     /* bitpos */
1304          complain_overflow_dont, /* complain_on_overflow */
1305          bfd_elf_generic_reloc, /* special_function */
1306          "R_PPC64_TLS",         /* name */
1307          FALSE,                 /* partial_inplace */
1308          0,                     /* src_mask */
1309          0,                     /* dst_mask */
1310          FALSE),                /* pcrel_offset */
1311
1312   HOWTO (R_PPC64_TLSGD,
1313          0,                     /* rightshift */
1314          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1315          32,                    /* bitsize */
1316          FALSE,                 /* pc_relative */
1317          0,                     /* bitpos */
1318          complain_overflow_dont, /* complain_on_overflow */
1319          bfd_elf_generic_reloc, /* special_function */
1320          "R_PPC64_TLSGD",       /* name */
1321          FALSE,                 /* partial_inplace */
1322          0,                     /* src_mask */
1323          0,                     /* dst_mask */
1324          FALSE),                /* pcrel_offset */
1325
1326   HOWTO (R_PPC64_TLSLD,
1327          0,                     /* rightshift */
1328          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1329          32,                    /* bitsize */
1330          FALSE,                 /* pc_relative */
1331          0,                     /* bitpos */
1332          complain_overflow_dont, /* complain_on_overflow */
1333          bfd_elf_generic_reloc, /* special_function */
1334          "R_PPC64_TLSLD",       /* name */
1335          FALSE,                 /* partial_inplace */
1336          0,                     /* src_mask */
1337          0,                     /* dst_mask */
1338          FALSE),                /* pcrel_offset */
1339
1340   /* Marker reloc for optimizing r2 save in prologue rather than on
1341      each plt call stub.  */
1342   HOWTO (R_PPC64_TOCSAVE,
1343          0,                     /* rightshift */
1344          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1345          32,                    /* bitsize */
1346          FALSE,                 /* pc_relative */
1347          0,                     /* bitpos */
1348          complain_overflow_dont, /* complain_on_overflow */
1349          bfd_elf_generic_reloc, /* special_function */
1350          "R_PPC64_TOCSAVE",     /* name */
1351          FALSE,                 /* partial_inplace */
1352          0,                     /* src_mask */
1353          0,                     /* dst_mask */
1354          FALSE),                /* pcrel_offset */
1355
1356   /* Marker relocs on inline plt call instructions.  */
1357   HOWTO (R_PPC64_PLTSEQ,
1358          0,                     /* rightshift */
1359          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1360          32,                    /* bitsize */
1361          FALSE,                 /* pc_relative */
1362          0,                     /* bitpos */
1363          complain_overflow_dont, /* complain_on_overflow */
1364          bfd_elf_generic_reloc, /* special_function */
1365          "R_PPC64_PLTSEQ",      /* name */
1366          FALSE,                 /* partial_inplace */
1367          0,                     /* src_mask */
1368          0,                     /* dst_mask */
1369          FALSE),                /* pcrel_offset */
1370
1371   HOWTO (R_PPC64_PLTCALL,
1372          0,                     /* rightshift */
1373          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1374          32,                    /* bitsize */
1375          FALSE,                 /* pc_relative */
1376          0,                     /* bitpos */
1377          complain_overflow_dont, /* complain_on_overflow */
1378          bfd_elf_generic_reloc, /* special_function */
1379          "R_PPC64_PLTCALL",     /* name */
1380          FALSE,                 /* partial_inplace */
1381          0,                     /* src_mask */
1382          0,                     /* dst_mask */
1383          FALSE),                /* pcrel_offset */
1384
1385   /* Computes the load module index of the load module that contains the
1386      definition of its TLS sym.  */
1387   HOWTO (R_PPC64_DTPMOD64,
1388          0,                     /* rightshift */
1389          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1390          64,                    /* bitsize */
1391          FALSE,                 /* pc_relative */
1392          0,                     /* bitpos */
1393          complain_overflow_dont, /* complain_on_overflow */
1394          ppc64_elf_unhandled_reloc, /* special_function */
1395          "R_PPC64_DTPMOD64",    /* name */
1396          FALSE,                 /* partial_inplace */
1397          0,                     /* src_mask */
1398          ONES (64),             /* dst_mask */
1399          FALSE),                /* pcrel_offset */
1400
1401   /* Computes a dtv-relative displacement, the difference between the value
1402      of sym+add and the base address of the thread-local storage block that
1403      contains the definition of sym, minus 0x8000.  */
1404   HOWTO (R_PPC64_DTPREL64,
1405          0,                     /* rightshift */
1406          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1407          64,                    /* bitsize */
1408          FALSE,                 /* pc_relative */
1409          0,                     /* bitpos */
1410          complain_overflow_dont, /* complain_on_overflow */
1411          ppc64_elf_unhandled_reloc, /* special_function */
1412          "R_PPC64_DTPREL64",    /* name */
1413          FALSE,                 /* partial_inplace */
1414          0,                     /* src_mask */
1415          ONES (64),             /* dst_mask */
1416          FALSE),                /* pcrel_offset */
1417
1418   /* A 16 bit dtprel reloc.  */
1419   HOWTO (R_PPC64_DTPREL16,
1420          0,                     /* rightshift */
1421          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1422          16,                    /* bitsize */
1423          FALSE,                 /* pc_relative */
1424          0,                     /* bitpos */
1425          complain_overflow_signed, /* complain_on_overflow */
1426          ppc64_elf_unhandled_reloc, /* special_function */
1427          "R_PPC64_DTPREL16",    /* name */
1428          FALSE,                 /* partial_inplace */
1429          0,                     /* src_mask */
1430          0xffff,                /* dst_mask */
1431          FALSE),                /* pcrel_offset */
1432
1433   /* Like DTPREL16, but no overflow.  */
1434   HOWTO (R_PPC64_DTPREL16_LO,
1435          0,                     /* rightshift */
1436          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1437          16,                    /* bitsize */
1438          FALSE,                 /* pc_relative */
1439          0,                     /* bitpos */
1440          complain_overflow_dont, /* complain_on_overflow */
1441          ppc64_elf_unhandled_reloc, /* special_function */
1442          "R_PPC64_DTPREL16_LO", /* name */
1443          FALSE,                 /* partial_inplace */
1444          0,                     /* src_mask */
1445          0xffff,                /* dst_mask */
1446          FALSE),                /* pcrel_offset */
1447
1448   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1449   HOWTO (R_PPC64_DTPREL16_HI,
1450          16,                    /* rightshift */
1451          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1452          16,                    /* bitsize */
1453          FALSE,                 /* pc_relative */
1454          0,                     /* bitpos */
1455          complain_overflow_signed, /* complain_on_overflow */
1456          ppc64_elf_unhandled_reloc, /* special_function */
1457          "R_PPC64_DTPREL16_HI", /* name */
1458          FALSE,                 /* partial_inplace */
1459          0,                     /* src_mask */
1460          0xffff,                /* dst_mask */
1461          FALSE),                /* pcrel_offset */
1462
1463   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1464   HOWTO (R_PPC64_DTPREL16_HA,
1465          16,                    /* rightshift */
1466          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1467          16,                    /* bitsize */
1468          FALSE,                 /* pc_relative */
1469          0,                     /* bitpos */
1470          complain_overflow_signed, /* complain_on_overflow */
1471          ppc64_elf_unhandled_reloc, /* special_function */
1472          "R_PPC64_DTPREL16_HA", /* name */
1473          FALSE,                 /* partial_inplace */
1474          0,                     /* src_mask */
1475          0xffff,                /* dst_mask */
1476          FALSE),                /* pcrel_offset */
1477
1478   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1479   HOWTO (R_PPC64_DTPREL16_HIGHER,
1480          32,                    /* rightshift */
1481          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1482          16,                    /* bitsize */
1483          FALSE,                 /* pc_relative */
1484          0,                     /* bitpos */
1485          complain_overflow_dont, /* complain_on_overflow */
1486          ppc64_elf_unhandled_reloc, /* special_function */
1487          "R_PPC64_DTPREL16_HIGHER", /* name */
1488          FALSE,                 /* partial_inplace */
1489          0,                     /* src_mask */
1490          0xffff,                /* dst_mask */
1491          FALSE),                /* pcrel_offset */
1492
1493   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1494   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1495          32,                    /* rightshift */
1496          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1497          16,                    /* bitsize */
1498          FALSE,                 /* pc_relative */
1499          0,                     /* bitpos */
1500          complain_overflow_dont, /* complain_on_overflow */
1501          ppc64_elf_unhandled_reloc, /* special_function */
1502          "R_PPC64_DTPREL16_HIGHERA", /* name */
1503          FALSE,                 /* partial_inplace */
1504          0,                     /* src_mask */
1505          0xffff,                /* dst_mask */
1506          FALSE),                /* pcrel_offset */
1507
1508   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1509   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1510          48,                    /* rightshift */
1511          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1512          16,                    /* bitsize */
1513          FALSE,                 /* pc_relative */
1514          0,                     /* bitpos */
1515          complain_overflow_dont, /* complain_on_overflow */
1516          ppc64_elf_unhandled_reloc, /* special_function */
1517          "R_PPC64_DTPREL16_HIGHEST", /* name */
1518          FALSE,                 /* partial_inplace */
1519          0,                     /* src_mask */
1520          0xffff,                /* dst_mask */
1521          FALSE),                /* pcrel_offset */
1522
1523   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1524   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1525          48,                    /* rightshift */
1526          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1527          16,                    /* bitsize */
1528          FALSE,                 /* pc_relative */
1529          0,                     /* bitpos */
1530          complain_overflow_dont, /* complain_on_overflow */
1531          ppc64_elf_unhandled_reloc, /* special_function */
1532          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1533          FALSE,                 /* partial_inplace */
1534          0,                     /* src_mask */
1535          0xffff,                /* dst_mask */
1536          FALSE),                /* pcrel_offset */
1537
1538   /* Like DTPREL16, but for insns with a DS field.  */
1539   HOWTO (R_PPC64_DTPREL16_DS,
1540          0,                     /* rightshift */
1541          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1542          16,                    /* bitsize */
1543          FALSE,                 /* pc_relative */
1544          0,                     /* bitpos */
1545          complain_overflow_signed, /* complain_on_overflow */
1546          ppc64_elf_unhandled_reloc, /* special_function */
1547          "R_PPC64_DTPREL16_DS", /* name */
1548          FALSE,                 /* partial_inplace */
1549          0,                     /* src_mask */
1550          0xfffc,                /* dst_mask */
1551          FALSE),                /* pcrel_offset */
1552
1553   /* Like DTPREL16_DS, but no overflow.  */
1554   HOWTO (R_PPC64_DTPREL16_LO_DS,
1555          0,                     /* rightshift */
1556          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1557          16,                    /* bitsize */
1558          FALSE,                 /* pc_relative */
1559          0,                     /* bitpos */
1560          complain_overflow_dont, /* complain_on_overflow */
1561          ppc64_elf_unhandled_reloc, /* special_function */
1562          "R_PPC64_DTPREL16_LO_DS", /* name */
1563          FALSE,                 /* partial_inplace */
1564          0,                     /* src_mask */
1565          0xfffc,                /* dst_mask */
1566          FALSE),                /* pcrel_offset */
1567
1568   /* Computes a tp-relative displacement, the difference between the value of
1569      sym+add and the value of the thread pointer (r13).  */
1570   HOWTO (R_PPC64_TPREL64,
1571          0,                     /* rightshift */
1572          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1573          64,                    /* bitsize */
1574          FALSE,                 /* pc_relative */
1575          0,                     /* bitpos */
1576          complain_overflow_dont, /* complain_on_overflow */
1577          ppc64_elf_unhandled_reloc, /* special_function */
1578          "R_PPC64_TPREL64",     /* name */
1579          FALSE,                 /* partial_inplace */
1580          0,                     /* src_mask */
1581          ONES (64),             /* dst_mask */
1582          FALSE),                /* pcrel_offset */
1583
1584   /* A 16 bit tprel reloc.  */
1585   HOWTO (R_PPC64_TPREL16,
1586          0,                     /* rightshift */
1587          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1588          16,                    /* bitsize */
1589          FALSE,                 /* pc_relative */
1590          0,                     /* bitpos */
1591          complain_overflow_signed, /* complain_on_overflow */
1592          ppc64_elf_unhandled_reloc, /* special_function */
1593          "R_PPC64_TPREL16",     /* name */
1594          FALSE,                 /* partial_inplace */
1595          0,                     /* src_mask */
1596          0xffff,                /* dst_mask */
1597          FALSE),                /* pcrel_offset */
1598
1599   /* Like TPREL16, but no overflow.  */
1600   HOWTO (R_PPC64_TPREL16_LO,
1601          0,                     /* rightshift */
1602          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1603          16,                    /* bitsize */
1604          FALSE,                 /* pc_relative */
1605          0,                     /* bitpos */
1606          complain_overflow_dont, /* complain_on_overflow */
1607          ppc64_elf_unhandled_reloc, /* special_function */
1608          "R_PPC64_TPREL16_LO",  /* name */
1609          FALSE,                 /* partial_inplace */
1610          0,                     /* src_mask */
1611          0xffff,                /* dst_mask */
1612          FALSE),                /* pcrel_offset */
1613
1614   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1615   HOWTO (R_PPC64_TPREL16_HI,
1616          16,                    /* rightshift */
1617          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1618          16,                    /* bitsize */
1619          FALSE,                 /* pc_relative */
1620          0,                     /* bitpos */
1621          complain_overflow_signed, /* complain_on_overflow */
1622          ppc64_elf_unhandled_reloc, /* special_function */
1623          "R_PPC64_TPREL16_HI",  /* name */
1624          FALSE,                 /* partial_inplace */
1625          0,                     /* src_mask */
1626          0xffff,                /* dst_mask */
1627          FALSE),                /* pcrel_offset */
1628
1629   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1630   HOWTO (R_PPC64_TPREL16_HA,
1631          16,                    /* rightshift */
1632          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1633          16,                    /* bitsize */
1634          FALSE,                 /* pc_relative */
1635          0,                     /* bitpos */
1636          complain_overflow_signed, /* complain_on_overflow */
1637          ppc64_elf_unhandled_reloc, /* special_function */
1638          "R_PPC64_TPREL16_HA",  /* name */
1639          FALSE,                 /* partial_inplace */
1640          0,                     /* src_mask */
1641          0xffff,                /* dst_mask */
1642          FALSE),                /* pcrel_offset */
1643
1644   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1645   HOWTO (R_PPC64_TPREL16_HIGHER,
1646          32,                    /* rightshift */
1647          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1648          16,                    /* bitsize */
1649          FALSE,                 /* pc_relative */
1650          0,                     /* bitpos */
1651          complain_overflow_dont, /* complain_on_overflow */
1652          ppc64_elf_unhandled_reloc, /* special_function */
1653          "R_PPC64_TPREL16_HIGHER",      /* name */
1654          FALSE,                 /* partial_inplace */
1655          0,                     /* src_mask */
1656          0xffff,                /* dst_mask */
1657          FALSE),                /* pcrel_offset */
1658
1659   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1660   HOWTO (R_PPC64_TPREL16_HIGHERA,
1661          32,                    /* rightshift */
1662          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1663          16,                    /* bitsize */
1664          FALSE,                 /* pc_relative */
1665          0,                     /* bitpos */
1666          complain_overflow_dont, /* complain_on_overflow */
1667          ppc64_elf_unhandled_reloc, /* special_function */
1668          "R_PPC64_TPREL16_HIGHERA", /* name */
1669          FALSE,                 /* partial_inplace */
1670          0,                     /* src_mask */
1671          0xffff,                /* dst_mask */
1672          FALSE),                /* pcrel_offset */
1673
1674   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1675   HOWTO (R_PPC64_TPREL16_HIGHEST,
1676          48,                    /* rightshift */
1677          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1678          16,                    /* bitsize */
1679          FALSE,                 /* pc_relative */
1680          0,                     /* bitpos */
1681          complain_overflow_dont, /* complain_on_overflow */
1682          ppc64_elf_unhandled_reloc, /* special_function */
1683          "R_PPC64_TPREL16_HIGHEST", /* name */
1684          FALSE,                 /* partial_inplace */
1685          0,                     /* src_mask */
1686          0xffff,                /* dst_mask */
1687          FALSE),                /* pcrel_offset */
1688
1689   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1690   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1691          48,                    /* rightshift */
1692          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1693          16,                    /* bitsize */
1694          FALSE,                 /* pc_relative */
1695          0,                     /* bitpos */
1696          complain_overflow_dont, /* complain_on_overflow */
1697          ppc64_elf_unhandled_reloc, /* special_function */
1698          "R_PPC64_TPREL16_HIGHESTA", /* name */
1699          FALSE,                 /* partial_inplace */
1700          0,                     /* src_mask */
1701          0xffff,                /* dst_mask */
1702          FALSE),                /* pcrel_offset */
1703
1704   /* Like TPREL16, but for insns with a DS field.  */
1705   HOWTO (R_PPC64_TPREL16_DS,
1706          0,                     /* rightshift */
1707          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1708          16,                    /* bitsize */
1709          FALSE,                 /* pc_relative */
1710          0,                     /* bitpos */
1711          complain_overflow_signed, /* complain_on_overflow */
1712          ppc64_elf_unhandled_reloc, /* special_function */
1713          "R_PPC64_TPREL16_DS",  /* name */
1714          FALSE,                 /* partial_inplace */
1715          0,                     /* src_mask */
1716          0xfffc,                /* dst_mask */
1717          FALSE),                /* pcrel_offset */
1718
1719   /* Like TPREL16_DS, but no overflow.  */
1720   HOWTO (R_PPC64_TPREL16_LO_DS,
1721          0,                     /* rightshift */
1722          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1723          16,                    /* bitsize */
1724          FALSE,                 /* pc_relative */
1725          0,                     /* bitpos */
1726          complain_overflow_dont, /* complain_on_overflow */
1727          ppc64_elf_unhandled_reloc, /* special_function */
1728          "R_PPC64_TPREL16_LO_DS", /* name */
1729          FALSE,                 /* partial_inplace */
1730          0,                     /* src_mask */
1731          0xfffc,                /* dst_mask */
1732          FALSE),                /* pcrel_offset */
1733
1734   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1735      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1736      to the first entry relative to the TOC base (r2).  */
1737   HOWTO (R_PPC64_GOT_TLSGD16,
1738          0,                     /* rightshift */
1739          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1740          16,                    /* bitsize */
1741          FALSE,                 /* pc_relative */
1742          0,                     /* bitpos */
1743          complain_overflow_signed, /* complain_on_overflow */
1744          ppc64_elf_unhandled_reloc, /* special_function */
1745          "R_PPC64_GOT_TLSGD16", /* name */
1746          FALSE,                 /* partial_inplace */
1747          0,                     /* src_mask */
1748          0xffff,                /* dst_mask */
1749          FALSE),                /* pcrel_offset */
1750
1751   /* Like GOT_TLSGD16, but no overflow.  */
1752   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1753          0,                     /* rightshift */
1754          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1755          16,                    /* bitsize */
1756          FALSE,                 /* pc_relative */
1757          0,                     /* bitpos */
1758          complain_overflow_dont, /* complain_on_overflow */
1759          ppc64_elf_unhandled_reloc, /* special_function */
1760          "R_PPC64_GOT_TLSGD16_LO", /* name */
1761          FALSE,                 /* partial_inplace */
1762          0,                     /* src_mask */
1763          0xffff,                /* dst_mask */
1764          FALSE),                /* pcrel_offset */
1765
1766   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1767   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1768          16,                    /* rightshift */
1769          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1770          16,                    /* bitsize */
1771          FALSE,                 /* pc_relative */
1772          0,                     /* bitpos */
1773          complain_overflow_signed, /* complain_on_overflow */
1774          ppc64_elf_unhandled_reloc, /* special_function */
1775          "R_PPC64_GOT_TLSGD16_HI", /* name */
1776          FALSE,                 /* partial_inplace */
1777          0,                     /* src_mask */
1778          0xffff,                /* dst_mask */
1779          FALSE),                /* pcrel_offset */
1780
1781   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1782   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1783          16,                    /* rightshift */
1784          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1785          16,                    /* bitsize */
1786          FALSE,                 /* pc_relative */
1787          0,                     /* bitpos */
1788          complain_overflow_signed, /* complain_on_overflow */
1789          ppc64_elf_unhandled_reloc, /* special_function */
1790          "R_PPC64_GOT_TLSGD16_HA", /* name */
1791          FALSE,                 /* partial_inplace */
1792          0,                     /* src_mask */
1793          0xffff,                /* dst_mask */
1794          FALSE),                /* pcrel_offset */
1795
1796   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1797      with values (sym+add)@dtpmod and zero, and computes the offset to the
1798      first entry relative to the TOC base (r2).  */
1799   HOWTO (R_PPC64_GOT_TLSLD16,
1800          0,                     /* rightshift */
1801          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1802          16,                    /* bitsize */
1803          FALSE,                 /* pc_relative */
1804          0,                     /* bitpos */
1805          complain_overflow_signed, /* complain_on_overflow */
1806          ppc64_elf_unhandled_reloc, /* special_function */
1807          "R_PPC64_GOT_TLSLD16", /* name */
1808          FALSE,                 /* partial_inplace */
1809          0,                     /* src_mask */
1810          0xffff,                /* dst_mask */
1811          FALSE),                /* pcrel_offset */
1812
1813   /* Like GOT_TLSLD16, but no overflow.  */
1814   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1815          0,                     /* rightshift */
1816          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1817          16,                    /* bitsize */
1818          FALSE,                 /* pc_relative */
1819          0,                     /* bitpos */
1820          complain_overflow_dont, /* complain_on_overflow */
1821          ppc64_elf_unhandled_reloc, /* special_function */
1822          "R_PPC64_GOT_TLSLD16_LO", /* name */
1823          FALSE,                 /* partial_inplace */
1824          0,                     /* src_mask */
1825          0xffff,                /* dst_mask */
1826          FALSE),                /* pcrel_offset */
1827
1828   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1829   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1830          16,                    /* rightshift */
1831          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1832          16,                    /* bitsize */
1833          FALSE,                 /* pc_relative */
1834          0,                     /* bitpos */
1835          complain_overflow_signed, /* complain_on_overflow */
1836          ppc64_elf_unhandled_reloc, /* special_function */
1837          "R_PPC64_GOT_TLSLD16_HI", /* name */
1838          FALSE,                 /* partial_inplace */
1839          0,                     /* src_mask */
1840          0xffff,                /* dst_mask */
1841          FALSE),                /* pcrel_offset */
1842
1843   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1844   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1845          16,                    /* rightshift */
1846          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1847          16,                    /* bitsize */
1848          FALSE,                 /* pc_relative */
1849          0,                     /* bitpos */
1850          complain_overflow_signed, /* complain_on_overflow */
1851          ppc64_elf_unhandled_reloc, /* special_function */
1852          "R_PPC64_GOT_TLSLD16_HA", /* name */
1853          FALSE,                 /* partial_inplace */
1854          0,                     /* src_mask */
1855          0xffff,                /* dst_mask */
1856          FALSE),                /* pcrel_offset */
1857
1858   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1859      the offset to the entry relative to the TOC base (r2).  */
1860   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1861          0,                     /* rightshift */
1862          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1863          16,                    /* bitsize */
1864          FALSE,                 /* pc_relative */
1865          0,                     /* bitpos */
1866          complain_overflow_signed, /* complain_on_overflow */
1867          ppc64_elf_unhandled_reloc, /* special_function */
1868          "R_PPC64_GOT_DTPREL16_DS", /* name */
1869          FALSE,                 /* partial_inplace */
1870          0,                     /* src_mask */
1871          0xfffc,                /* dst_mask */
1872          FALSE),                /* pcrel_offset */
1873
1874   /* Like GOT_DTPREL16_DS, but no overflow.  */
1875   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1876          0,                     /* rightshift */
1877          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1878          16,                    /* bitsize */
1879          FALSE,                 /* pc_relative */
1880          0,                     /* bitpos */
1881          complain_overflow_dont, /* complain_on_overflow */
1882          ppc64_elf_unhandled_reloc, /* special_function */
1883          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1884          FALSE,                 /* partial_inplace */
1885          0,                     /* src_mask */
1886          0xfffc,                /* dst_mask */
1887          FALSE),                /* pcrel_offset */
1888
1889   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1890   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1891          16,                    /* rightshift */
1892          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1893          16,                    /* bitsize */
1894          FALSE,                 /* pc_relative */
1895          0,                     /* bitpos */
1896          complain_overflow_signed, /* complain_on_overflow */
1897          ppc64_elf_unhandled_reloc, /* special_function */
1898          "R_PPC64_GOT_DTPREL16_HI", /* name */
1899          FALSE,                 /* partial_inplace */
1900          0,                     /* src_mask */
1901          0xffff,                /* dst_mask */
1902          FALSE),                /* pcrel_offset */
1903
1904   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1905   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1906          16,                    /* rightshift */
1907          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1908          16,                    /* bitsize */
1909          FALSE,                 /* pc_relative */
1910          0,                     /* bitpos */
1911          complain_overflow_signed, /* complain_on_overflow */
1912          ppc64_elf_unhandled_reloc, /* special_function */
1913          "R_PPC64_GOT_DTPREL16_HA", /* name */
1914          FALSE,                 /* partial_inplace */
1915          0,                     /* src_mask */
1916          0xffff,                /* dst_mask */
1917          FALSE),                /* pcrel_offset */
1918
1919   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1920      offset to the entry relative to the TOC base (r2).  */
1921   HOWTO (R_PPC64_GOT_TPREL16_DS,
1922          0,                     /* rightshift */
1923          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1924          16,                    /* bitsize */
1925          FALSE,                 /* pc_relative */
1926          0,                     /* bitpos */
1927          complain_overflow_signed, /* complain_on_overflow */
1928          ppc64_elf_unhandled_reloc, /* special_function */
1929          "R_PPC64_GOT_TPREL16_DS", /* name */
1930          FALSE,                 /* partial_inplace */
1931          0,                     /* src_mask */
1932          0xfffc,                /* dst_mask */
1933          FALSE),                /* pcrel_offset */
1934
1935   /* Like GOT_TPREL16_DS, but no overflow.  */
1936   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1937          0,                     /* rightshift */
1938          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1939          16,                    /* 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_GOT_TPREL16_LO_DS", /* name */
1945          FALSE,                 /* partial_inplace */
1946          0,                     /* src_mask */
1947          0xfffc,                /* dst_mask */
1948          FALSE),                /* pcrel_offset */
1949
1950   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1951   HOWTO (R_PPC64_GOT_TPREL16_HI,
1952          16,                    /* rightshift */
1953          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1954          16,                    /* bitsize */
1955          FALSE,                 /* pc_relative */
1956          0,                     /* bitpos */
1957          complain_overflow_signed, /* complain_on_overflow */
1958          ppc64_elf_unhandled_reloc, /* special_function */
1959          "R_PPC64_GOT_TPREL16_HI", /* name */
1960          FALSE,                 /* partial_inplace */
1961          0,                     /* src_mask */
1962          0xffff,                /* dst_mask */
1963          FALSE),                /* pcrel_offset */
1964
1965   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1966   HOWTO (R_PPC64_GOT_TPREL16_HA,
1967          16,                    /* rightshift */
1968          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1969          16,                    /* bitsize */
1970          FALSE,                 /* pc_relative */
1971          0,                     /* bitpos */
1972          complain_overflow_signed, /* complain_on_overflow */
1973          ppc64_elf_unhandled_reloc, /* special_function */
1974          "R_PPC64_GOT_TPREL16_HA", /* name */
1975          FALSE,                 /* partial_inplace */
1976          0,                     /* src_mask */
1977          0xffff,                /* dst_mask */
1978          FALSE),                /* pcrel_offset */
1979
1980   HOWTO (R_PPC64_JMP_IREL,      /* type */
1981          0,                     /* rightshift */
1982          0,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1983          0,                     /* bitsize */
1984          FALSE,                 /* pc_relative */
1985          0,                     /* bitpos */
1986          complain_overflow_dont, /* complain_on_overflow */
1987          ppc64_elf_unhandled_reloc, /* special_function */
1988          "R_PPC64_JMP_IREL",    /* name */
1989          FALSE,                 /* partial_inplace */
1990          0,                     /* src_mask */
1991          0,                     /* dst_mask */
1992          FALSE),                /* pcrel_offset */
1993
1994   HOWTO (R_PPC64_IRELATIVE,     /* type */
1995          0,                     /* rightshift */
1996          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1997          64,                    /* bitsize */
1998          FALSE,                 /* pc_relative */
1999          0,                     /* bitpos */
2000          complain_overflow_dont, /* complain_on_overflow */
2001          bfd_elf_generic_reloc, /* special_function */
2002          "R_PPC64_IRELATIVE",   /* name */
2003          FALSE,                 /* partial_inplace */
2004          0,                     /* src_mask */
2005          ONES (64),             /* dst_mask */
2006          FALSE),                /* pcrel_offset */
2007
2008   /* A 16 bit relative relocation.  */
2009   HOWTO (R_PPC64_REL16,         /* type */
2010          0,                     /* rightshift */
2011          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2012          16,                    /* bitsize */
2013          TRUE,                  /* pc_relative */
2014          0,                     /* bitpos */
2015          complain_overflow_signed, /* complain_on_overflow */
2016          bfd_elf_generic_reloc, /* special_function */
2017          "R_PPC64_REL16",       /* name */
2018          FALSE,                 /* partial_inplace */
2019          0,                     /* src_mask */
2020          0xffff,                /* dst_mask */
2021          TRUE),                 /* pcrel_offset */
2022
2023   /* A 16 bit relative relocation without overflow.  */
2024   HOWTO (R_PPC64_REL16_LO,      /* type */
2025          0,                     /* rightshift */
2026          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2027          16,                    /* bitsize */
2028          TRUE,                  /* pc_relative */
2029          0,                     /* bitpos */
2030          complain_overflow_dont,/* complain_on_overflow */
2031          bfd_elf_generic_reloc, /* special_function */
2032          "R_PPC64_REL16_LO",    /* name */
2033          FALSE,                 /* partial_inplace */
2034          0,                     /* src_mask */
2035          0xffff,                /* dst_mask */
2036          TRUE),                 /* pcrel_offset */
2037
2038   /* The high order 16 bits of a relative address.  */
2039   HOWTO (R_PPC64_REL16_HI,      /* type */
2040          16,                    /* rightshift */
2041          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2042          16,                    /* bitsize */
2043          TRUE,                  /* pc_relative */
2044          0,                     /* bitpos */
2045          complain_overflow_signed, /* complain_on_overflow */
2046          bfd_elf_generic_reloc, /* special_function */
2047          "R_PPC64_REL16_HI",    /* name */
2048          FALSE,                 /* partial_inplace */
2049          0,                     /* src_mask */
2050          0xffff,                /* dst_mask */
2051          TRUE),                 /* pcrel_offset */
2052
2053   /* The high order 16 bits of a relative address, plus 1 if the contents of
2054      the low 16 bits, treated as a signed number, is negative.  */
2055   HOWTO (R_PPC64_REL16_HA,      /* type */
2056          16,                    /* rightshift */
2057          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2058          16,                    /* bitsize */
2059          TRUE,                  /* pc_relative */
2060          0,                     /* bitpos */
2061          complain_overflow_signed, /* complain_on_overflow */
2062          ppc64_elf_ha_reloc,    /* special_function */
2063          "R_PPC64_REL16_HA",    /* name */
2064          FALSE,                 /* partial_inplace */
2065          0,                     /* src_mask */
2066          0xffff,                /* dst_mask */
2067          TRUE),                 /* pcrel_offset */
2068
2069   /* Like R_PPC64_REL16_HA but for split field in addpcis.  */
2070   HOWTO (R_PPC64_REL16DX_HA,    /* type */
2071          16,                    /* rightshift */
2072          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2073          16,                    /* bitsize */
2074          TRUE,                  /* pc_relative */
2075          0,                     /* bitpos */
2076          complain_overflow_signed, /* complain_on_overflow */
2077          ppc64_elf_ha_reloc,    /* special_function */
2078          "R_PPC64_REL16DX_HA",  /* name */
2079          FALSE,                 /* partial_inplace */
2080          0,                     /* src_mask */
2081          0x1fffc1,              /* dst_mask */
2082          TRUE),                 /* pcrel_offset */
2083
2084   /* A split-field reloc for addpcis, non-relative (gas internal use only).  */
2085   HOWTO (R_PPC64_16DX_HA,       /* type */
2086          16,                    /* rightshift */
2087          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2088          16,                    /* bitsize */
2089          FALSE,                 /* pc_relative */
2090          0,                     /* bitpos */
2091          complain_overflow_signed, /* complain_on_overflow */
2092          ppc64_elf_ha_reloc,    /* special_function */
2093          "R_PPC64_16DX_HA",     /* name */
2094          FALSE,                 /* partial_inplace */
2095          0,                     /* src_mask */
2096          0x1fffc1,              /* dst_mask */
2097          FALSE),                /* pcrel_offset */
2098
2099   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
2100   HOWTO (R_PPC64_ADDR16_HIGH,   /* type */
2101          16,                    /* rightshift */
2102          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2103          16,                    /* bitsize */
2104          FALSE,                 /* pc_relative */
2105          0,                     /* bitpos */
2106          complain_overflow_dont, /* complain_on_overflow */
2107          bfd_elf_generic_reloc, /* special_function */
2108          "R_PPC64_ADDR16_HIGH", /* name */
2109          FALSE,                 /* partial_inplace */
2110          0,                     /* src_mask */
2111          0xffff,                /* dst_mask */
2112          FALSE),                /* pcrel_offset */
2113
2114   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
2115   HOWTO (R_PPC64_ADDR16_HIGHA,  /* type */
2116          16,                    /* rightshift */
2117          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2118          16,                    /* bitsize */
2119          FALSE,                 /* pc_relative */
2120          0,                     /* bitpos */
2121          complain_overflow_dont, /* complain_on_overflow */
2122          ppc64_elf_ha_reloc,    /* special_function */
2123          "R_PPC64_ADDR16_HIGHA",        /* name */
2124          FALSE,                 /* partial_inplace */
2125          0,                     /* src_mask */
2126          0xffff,                /* dst_mask */
2127          FALSE),                /* pcrel_offset */
2128
2129   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
2130   HOWTO (R_PPC64_DTPREL16_HIGH,
2131          16,                    /* rightshift */
2132          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2133          16,                    /* bitsize */
2134          FALSE,                 /* pc_relative */
2135          0,                     /* bitpos */
2136          complain_overflow_dont, /* complain_on_overflow */
2137          ppc64_elf_unhandled_reloc, /* special_function */
2138          "R_PPC64_DTPREL16_HIGH", /* name */
2139          FALSE,                 /* partial_inplace */
2140          0,                     /* src_mask */
2141          0xffff,                /* dst_mask */
2142          FALSE),                /* pcrel_offset */
2143
2144   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
2145   HOWTO (R_PPC64_DTPREL16_HIGHA,
2146          16,                    /* rightshift */
2147          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2148          16,                    /* bitsize */
2149          FALSE,                 /* pc_relative */
2150          0,                     /* bitpos */
2151          complain_overflow_dont, /* complain_on_overflow */
2152          ppc64_elf_unhandled_reloc, /* special_function */
2153          "R_PPC64_DTPREL16_HIGHA", /* name */
2154          FALSE,                 /* partial_inplace */
2155          0,                     /* src_mask */
2156          0xffff,                /* dst_mask */
2157          FALSE),                /* pcrel_offset */
2158
2159   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
2160   HOWTO (R_PPC64_TPREL16_HIGH,
2161          16,                    /* rightshift */
2162          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2163          16,                    /* bitsize */
2164          FALSE,                 /* pc_relative */
2165          0,                     /* bitpos */
2166          complain_overflow_dont, /* complain_on_overflow */
2167          ppc64_elf_unhandled_reloc, /* special_function */
2168          "R_PPC64_TPREL16_HIGH",        /* name */
2169          FALSE,                 /* partial_inplace */
2170          0,                     /* src_mask */
2171          0xffff,                /* dst_mask */
2172          FALSE),                /* pcrel_offset */
2173
2174   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
2175   HOWTO (R_PPC64_TPREL16_HIGHA,
2176          16,                    /* rightshift */
2177          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2178          16,                    /* bitsize */
2179          FALSE,                 /* pc_relative */
2180          0,                     /* bitpos */
2181          complain_overflow_dont, /* complain_on_overflow */
2182          ppc64_elf_unhandled_reloc, /* special_function */
2183          "R_PPC64_TPREL16_HIGHA",       /* name */
2184          FALSE,                 /* partial_inplace */
2185          0,                     /* src_mask */
2186          0xffff,                /* dst_mask */
2187          FALSE),                /* pcrel_offset */
2188
2189   /* Marker reloc on ELFv2 large-model function entry.  */
2190   HOWTO (R_PPC64_ENTRY,
2191          0,                     /* rightshift */
2192          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2193          32,                    /* bitsize */
2194          FALSE,                 /* pc_relative */
2195          0,                     /* bitpos */
2196          complain_overflow_dont, /* complain_on_overflow */
2197          bfd_elf_generic_reloc, /* special_function */
2198          "R_PPC64_ENTRY",       /* name */
2199          FALSE,                 /* partial_inplace */
2200          0,                     /* src_mask */
2201          0,                     /* dst_mask */
2202          FALSE),                /* pcrel_offset */
2203
2204   /* Like ADDR64, but use local entry point of function.  */
2205   HOWTO (R_PPC64_ADDR64_LOCAL,  /* type */
2206          0,                     /* rightshift */
2207          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2208          64,                    /* bitsize */
2209          FALSE,                 /* pc_relative */
2210          0,                     /* bitpos */
2211          complain_overflow_dont, /* complain_on_overflow */
2212          bfd_elf_generic_reloc, /* special_function */
2213          "R_PPC64_ADDR64_LOCAL", /* name */
2214          FALSE,                 /* partial_inplace */
2215          0,                     /* src_mask */
2216          ONES (64),             /* dst_mask */
2217          FALSE),                /* pcrel_offset */
2218
2219   /* GNU extension to record C++ vtable hierarchy.  */
2220   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2221          0,                     /* rightshift */
2222          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2223          0,                     /* bitsize */
2224          FALSE,                 /* pc_relative */
2225          0,                     /* bitpos */
2226          complain_overflow_dont, /* complain_on_overflow */
2227          NULL,                  /* special_function */
2228          "R_PPC64_GNU_VTINHERIT", /* name */
2229          FALSE,                 /* partial_inplace */
2230          0,                     /* src_mask */
2231          0,                     /* dst_mask */
2232          FALSE),                /* pcrel_offset */
2233
2234   /* GNU extension to record C++ vtable member usage.  */
2235   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
2236          0,                     /* rightshift */
2237          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2238          0,                     /* bitsize */
2239          FALSE,                 /* pc_relative */
2240          0,                     /* bitpos */
2241          complain_overflow_dont, /* complain_on_overflow */
2242          NULL,                  /* special_function */
2243          "R_PPC64_GNU_VTENTRY", /* name */
2244          FALSE,                 /* partial_inplace */
2245          0,                     /* src_mask */
2246          0,                     /* dst_mask */
2247          FALSE),                /* pcrel_offset */
2248 };
2249
2250 \f
2251 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2252    be done.  */
2253
2254 static void
2255 ppc_howto_init (void)
2256 {
2257   unsigned int i, type;
2258
2259   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2260     {
2261       type = ppc64_elf_howto_raw[i].type;
2262       BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
2263       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2264     }
2265 }
2266
2267 static reloc_howto_type *
2268 ppc64_elf_reloc_type_lookup (bfd *abfd,
2269                              bfd_reloc_code_real_type code)
2270 {
2271   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2272
2273   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2274     /* Initialize howto table if needed.  */
2275     ppc_howto_init ();
2276
2277   switch (code)
2278     {
2279     default:
2280       /* xgettext:c-format */
2281       _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, (int) code);
2282       bfd_set_error (bfd_error_bad_value);
2283       return NULL;
2284
2285     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
2286       break;
2287     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
2288       break;
2289     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
2290       break;
2291     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
2292       break;
2293     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
2294       break;
2295     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
2296       break;
2297     case BFD_RELOC_PPC64_ADDR16_HIGH:           r = R_PPC64_ADDR16_HIGH;
2298       break;
2299     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
2300       break;
2301     case BFD_RELOC_PPC64_ADDR16_HIGHA:          r = R_PPC64_ADDR16_HIGHA;
2302       break;
2303     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
2304       break;
2305     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
2306       break;
2307     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
2308       break;
2309     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
2310       break;
2311     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
2312       break;
2313     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
2314       break;
2315     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
2316       break;
2317     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
2318       break;
2319     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
2320       break;
2321     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
2322       break;
2323     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
2324       break;
2325     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
2326       break;
2327     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
2328       break;
2329     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
2330       break;
2331     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
2332       break;
2333     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
2334       break;
2335     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
2336       break;
2337     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
2338       break;
2339     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
2340       break;
2341     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
2342       break;
2343     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
2344       break;
2345     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
2346       break;
2347     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
2348       break;
2349     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
2350       break;
2351     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
2352       break;
2353     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
2354       break;
2355     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
2356       break;
2357     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
2358       break;
2359     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
2360       break;
2361     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
2362       break;
2363     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
2364       break;
2365     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
2366       break;
2367     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
2368       break;
2369     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
2370       break;
2371     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
2372       break;
2373     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
2374       break;
2375     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
2376       break;
2377     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
2378       break;
2379     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
2380       break;
2381     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
2382       break;
2383     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
2384       break;
2385     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
2386       break;
2387     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
2388       break;
2389     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
2390       break;
2391     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
2392       break;
2393     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
2394       break;
2395     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
2396       break;
2397     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2398       break;
2399     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2400       break;
2401     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2402       break;
2403     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2404       break;
2405     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2406       break;
2407     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2408       break;
2409     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
2410       break;
2411     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
2412       break;
2413     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2414       break;
2415     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2416       break;
2417     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2418       break;
2419     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2420       break;
2421     case BFD_RELOC_PPC64_TPREL16_HIGH:          r = R_PPC64_TPREL16_HIGH;
2422       break;
2423     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2424       break;
2425     case BFD_RELOC_PPC64_TPREL16_HIGHA:         r = R_PPC64_TPREL16_HIGHA;
2426       break;
2427     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2428       break;
2429     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2430       break;
2431     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2432       break;
2433     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2434       break;
2435     case BFD_RELOC_PPC64_DTPREL16_HIGH:         r = R_PPC64_DTPREL16_HIGH;
2436       break;
2437     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2438       break;
2439     case BFD_RELOC_PPC64_DTPREL16_HIGHA:        r = R_PPC64_DTPREL16_HIGHA;
2440       break;
2441     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2442       break;
2443     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2444       break;
2445     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2446       break;
2447     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2448       break;
2449     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2450       break;
2451     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2452       break;
2453     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2454       break;
2455     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2456       break;
2457     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2458       break;
2459     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2460       break;
2461     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2462       break;
2463     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2464       break;
2465     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2466       break;
2467     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2468       break;
2469     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2470       break;
2471     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2472       break;
2473     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2474       break;
2475     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2476       break;
2477     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2478       break;
2479     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2480       break;
2481     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2482       break;
2483     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2484       break;
2485     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2486       break;
2487     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2488       break;
2489     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2490       break;
2491     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2492       break;
2493     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2494       break;
2495     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2496       break;
2497     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2498       break;
2499     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
2500       break;
2501     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
2502       break;
2503     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
2504       break;
2505     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
2506       break;
2507     case BFD_RELOC_PPC_16DX_HA:                 r = R_PPC64_16DX_HA;
2508       break;
2509     case BFD_RELOC_PPC_REL16DX_HA:              r = R_PPC64_REL16DX_HA;
2510       break;
2511     case BFD_RELOC_PPC64_ENTRY:                 r = R_PPC64_ENTRY;
2512       break;
2513     case BFD_RELOC_PPC64_ADDR64_LOCAL:          r = R_PPC64_ADDR64_LOCAL;
2514       break;
2515     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2516       break;
2517     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2518       break;
2519     }
2520
2521   return ppc64_elf_howto_table[r];
2522 };
2523
2524 static reloc_howto_type *
2525 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2526                              const char *r_name)
2527 {
2528   unsigned int i;
2529
2530   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2531     if (ppc64_elf_howto_raw[i].name != NULL
2532         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2533       return &ppc64_elf_howto_raw[i];
2534
2535   
2536   return NULL;
2537 }
2538
2539 /* Set the howto pointer for a PowerPC ELF reloc.  */
2540
2541 static bfd_boolean
2542 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
2543                          Elf_Internal_Rela *dst)
2544 {
2545   unsigned int type;
2546
2547   /* Initialize howto table if needed.  */
2548   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2549     ppc_howto_init ();
2550
2551   type = ELF64_R_TYPE (dst->r_info);
2552   if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
2553     {
2554       /* xgettext:c-format */
2555       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2556                           abfd, type);
2557       bfd_set_error (bfd_error_bad_value);
2558       return FALSE;
2559     }
2560   cache_ptr->howto = ppc64_elf_howto_table[type];
2561   if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
2562     {
2563       /* xgettext:c-format */
2564       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2565                           abfd, type);
2566       bfd_set_error (bfd_error_bad_value);
2567       return FALSE;
2568     }
2569   
2570   return TRUE;
2571 }
2572
2573 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2574
2575 static bfd_reloc_status_type
2576 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2577                     void *data, asection *input_section,
2578                     bfd *output_bfd, char **error_message)
2579 {
2580   enum elf_ppc64_reloc_type r_type;
2581   long insn;
2582   bfd_size_type octets;
2583   bfd_vma value;
2584
2585   /* If this is a relocatable link (output_bfd test tells us), just
2586      call the generic function.  Any adjustment will be done at final
2587      link time.  */
2588   if (output_bfd != NULL)
2589     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2590                                   input_section, output_bfd, error_message);
2591
2592   /* Adjust the addend for sign extension of the low 16 bits.
2593      We won't actually be using the low 16 bits, so trashing them
2594      doesn't matter.  */
2595   reloc_entry->addend += 0x8000;
2596   r_type = reloc_entry->howto->type;
2597   if (r_type != R_PPC64_REL16DX_HA)
2598     return bfd_reloc_continue;
2599
2600   value = 0;
2601   if (!bfd_is_com_section (symbol->section))
2602     value = symbol->value;
2603   value += (reloc_entry->addend
2604             + symbol->section->output_offset
2605             + symbol->section->output_section->vma);
2606   value -= (reloc_entry->address
2607             + input_section->output_offset
2608             + input_section->output_section->vma);
2609   value = (bfd_signed_vma) value >> 16;
2610
2611   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2612   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2613   insn &= ~0x1fffc1;
2614   insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2615   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2616   if (value + 0x8000 > 0xffff)
2617     return bfd_reloc_overflow;
2618   return bfd_reloc_ok;
2619 }
2620
2621 static bfd_reloc_status_type
2622 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2623                         void *data, asection *input_section,
2624                         bfd *output_bfd, char **error_message)
2625 {
2626   if (output_bfd != NULL)
2627     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2628                                   input_section, output_bfd, error_message);
2629
2630   if (strcmp (symbol->section->name, ".opd") == 0
2631       && (symbol->section->owner->flags & DYNAMIC) == 0)
2632     {
2633       bfd_vma dest = opd_entry_value (symbol->section,
2634                                       symbol->value + reloc_entry->addend,
2635                                       NULL, NULL, FALSE);
2636       if (dest != (bfd_vma) -1)
2637         reloc_entry->addend = dest - (symbol->value
2638                                       + symbol->section->output_section->vma
2639                                       + symbol->section->output_offset);
2640     }
2641   else
2642     {
2643       elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2644
2645       if (symbol->section->owner != abfd
2646           && symbol->section->owner != NULL
2647           && abiversion (symbol->section->owner) >= 2)
2648         {
2649           unsigned int i;
2650
2651           for (i = 0; i < symbol->section->owner->symcount; ++i)
2652             {
2653               asymbol *symdef = symbol->section->owner->outsymbols[i];
2654
2655               if (strcmp (symdef->name, symbol->name) == 0)
2656                 {
2657                   elfsym = (elf_symbol_type *) symdef;
2658                   break;
2659                 }
2660             }
2661         }
2662       reloc_entry->addend
2663         += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2664     }
2665   return bfd_reloc_continue;
2666 }
2667
2668 static bfd_reloc_status_type
2669 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2670                          void *data, asection *input_section,
2671                          bfd *output_bfd, char **error_message)
2672 {
2673   long insn;
2674   enum elf_ppc64_reloc_type r_type;
2675   bfd_size_type octets;
2676   /* Assume 'at' branch hints.  */
2677   bfd_boolean is_isa_v2 = TRUE;
2678
2679   /* If this is a relocatable link (output_bfd test tells us), just
2680      call the generic function.  Any adjustment will be done at final
2681      link time.  */
2682   if (output_bfd != NULL)
2683     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2684                                   input_section, output_bfd, error_message);
2685
2686   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2687   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2688   insn &= ~(0x01 << 21);
2689   r_type = reloc_entry->howto->type;
2690   if (r_type == R_PPC64_ADDR14_BRTAKEN
2691       || r_type == R_PPC64_REL14_BRTAKEN)
2692     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2693
2694   if (is_isa_v2)
2695     {
2696       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2697          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2698          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2699       if ((insn & (0x14 << 21)) == (0x04 << 21))
2700         insn |= 0x02 << 21;
2701       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2702         insn |= 0x08 << 21;
2703       else
2704         goto out;
2705     }
2706   else
2707     {
2708       bfd_vma target = 0;
2709       bfd_vma from;
2710
2711       if (!bfd_is_com_section (symbol->section))
2712         target = symbol->value;
2713       target += symbol->section->output_section->vma;
2714       target += symbol->section->output_offset;
2715       target += reloc_entry->addend;
2716
2717       from = (reloc_entry->address
2718               + input_section->output_offset
2719               + input_section->output_section->vma);
2720
2721       /* Invert 'y' bit if not the default.  */
2722       if ((bfd_signed_vma) (target - from) < 0)
2723         insn ^= 0x01 << 21;
2724     }
2725   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2726  out:
2727   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2728                                  input_section, output_bfd, error_message);
2729 }
2730
2731 static bfd_reloc_status_type
2732 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2733                          void *data, asection *input_section,
2734                          bfd *output_bfd, char **error_message)
2735 {
2736   /* If this is a relocatable link (output_bfd test tells us), just
2737      call the generic function.  Any adjustment will be done at final
2738      link time.  */
2739   if (output_bfd != NULL)
2740     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2741                                   input_section, output_bfd, error_message);
2742
2743   /* Subtract the symbol section base address.  */
2744   reloc_entry->addend -= symbol->section->output_section->vma;
2745   return bfd_reloc_continue;
2746 }
2747
2748 static bfd_reloc_status_type
2749 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2750                             void *data, asection *input_section,
2751                             bfd *output_bfd, char **error_message)
2752 {
2753   /* If this is a relocatable link (output_bfd test tells us), just
2754      call the generic function.  Any adjustment will be done at final
2755      link time.  */
2756   if (output_bfd != NULL)
2757     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2758                                   input_section, output_bfd, error_message);
2759
2760   /* Subtract the symbol section base address.  */
2761   reloc_entry->addend -= symbol->section->output_section->vma;
2762
2763   /* Adjust the addend for sign extension of the low 16 bits.  */
2764   reloc_entry->addend += 0x8000;
2765   return bfd_reloc_continue;
2766 }
2767
2768 static bfd_reloc_status_type
2769 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2770                      void *data, asection *input_section,
2771                      bfd *output_bfd, char **error_message)
2772 {
2773   bfd_vma TOCstart;
2774
2775   /* If this is a relocatable link (output_bfd test tells us), just
2776      call the generic function.  Any adjustment will be done at final
2777      link time.  */
2778   if (output_bfd != NULL)
2779     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2780                                   input_section, output_bfd, error_message);
2781
2782   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2783   if (TOCstart == 0)
2784     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2785
2786   /* Subtract the TOC base address.  */
2787   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2788   return bfd_reloc_continue;
2789 }
2790
2791 static bfd_reloc_status_type
2792 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2793                         void *data, asection *input_section,
2794                         bfd *output_bfd, char **error_message)
2795 {
2796   bfd_vma TOCstart;
2797
2798   /* If this is a relocatable link (output_bfd test tells us), just
2799      call the generic function.  Any adjustment will be done at final
2800      link time.  */
2801   if (output_bfd != NULL)
2802     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2803                                   input_section, output_bfd, error_message);
2804
2805   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2806   if (TOCstart == 0)
2807     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2808
2809   /* Subtract the TOC base address.  */
2810   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2811
2812   /* Adjust the addend for sign extension of the low 16 bits.  */
2813   reloc_entry->addend += 0x8000;
2814   return bfd_reloc_continue;
2815 }
2816
2817 static bfd_reloc_status_type
2818 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2819                        void *data, asection *input_section,
2820                        bfd *output_bfd, char **error_message)
2821 {
2822   bfd_vma TOCstart;
2823   bfd_size_type octets;
2824
2825   /* If this is a relocatable link (output_bfd test tells us), just
2826      call the generic function.  Any adjustment will be done at final
2827      link time.  */
2828   if (output_bfd != NULL)
2829     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2830                                   input_section, output_bfd, error_message);
2831
2832   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2833   if (TOCstart == 0)
2834     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2835
2836   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2837   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2838   return bfd_reloc_ok;
2839 }
2840
2841 static bfd_reloc_status_type
2842 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2843                            void *data, asection *input_section,
2844                            bfd *output_bfd, char **error_message)
2845 {
2846   /* If this is a relocatable link (output_bfd test tells us), just
2847      call the generic function.  Any adjustment will be done at final
2848      link time.  */
2849   if (output_bfd != NULL)
2850     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2851                                   input_section, output_bfd, error_message);
2852
2853   if (error_message != NULL)
2854     {
2855       static char buf[60];
2856       sprintf (buf, "generic linker can't handle %s",
2857                reloc_entry->howto->name);
2858       *error_message = buf;
2859     }
2860   return bfd_reloc_dangerous;
2861 }
2862
2863 /* Track GOT entries needed for a given symbol.  We might need more
2864    than one got entry per symbol.  */
2865 struct got_entry
2866 {
2867   struct got_entry *next;
2868
2869   /* The symbol addend that we'll be placing in the GOT.  */
2870   bfd_vma addend;
2871
2872   /* Unlike other ELF targets, we use separate GOT entries for the same
2873      symbol referenced from different input files.  This is to support
2874      automatic multiple TOC/GOT sections, where the TOC base can vary
2875      from one input file to another.  After partitioning into TOC groups
2876      we merge entries within the group.
2877
2878      Point to the BFD owning this GOT entry.  */
2879   bfd *owner;
2880
2881   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2882      TLS_TPREL or TLS_DTPREL for tls entries.  */
2883   unsigned char tls_type;
2884
2885   /* Non-zero if got.ent points to real entry.  */
2886   unsigned char is_indirect;
2887
2888   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2889   union
2890     {
2891       bfd_signed_vma refcount;
2892       bfd_vma offset;
2893       struct got_entry *ent;
2894     } got;
2895 };
2896
2897 /* The same for PLT.  */
2898 struct plt_entry
2899 {
2900   struct plt_entry *next;
2901
2902   bfd_vma addend;
2903
2904   union
2905     {
2906       bfd_signed_vma refcount;
2907       bfd_vma offset;
2908     } plt;
2909 };
2910
2911 struct ppc64_elf_obj_tdata
2912 {
2913   struct elf_obj_tdata elf;
2914
2915   /* Shortcuts to dynamic linker sections.  */
2916   asection *got;
2917   asection *relgot;
2918
2919   /* Used during garbage collection.  We attach global symbols defined
2920      on removed .opd entries to this section so that the sym is removed.  */
2921   asection *deleted_section;
2922
2923   /* TLS local dynamic got entry handling.  Support for multiple GOT
2924      sections means we potentially need one of these for each input bfd.  */
2925   struct got_entry tlsld_got;
2926
2927   union {
2928     /* A copy of relocs before they are modified for --emit-relocs.  */
2929     Elf_Internal_Rela *relocs;
2930
2931     /* Section contents.  */
2932     bfd_byte *contents;
2933   } opd;
2934
2935   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2936      the reloc to be in the range -32768 to 32767.  */
2937   unsigned int has_small_toc_reloc : 1;
2938
2939   /* Set if toc/got ha relocs detected not using r2, or lo reloc
2940      instruction not one we handle.  */
2941   unsigned int unexpected_toc_insn : 1;
2942 };
2943
2944 #define ppc64_elf_tdata(bfd) \
2945   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2946
2947 #define ppc64_tlsld_got(bfd) \
2948   (&ppc64_elf_tdata (bfd)->tlsld_got)
2949
2950 #define is_ppc64_elf(bfd) \
2951   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2952    && elf_object_id (bfd) == PPC64_ELF_DATA)
2953
2954 /* Override the generic function because we store some extras.  */
2955
2956 static bfd_boolean
2957 ppc64_elf_mkobject (bfd *abfd)
2958 {
2959   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2960                                   PPC64_ELF_DATA);
2961 }
2962
2963 /* Fix bad default arch selected for a 64 bit input bfd when the
2964    default is 32 bit.  Also select arch based on apuinfo.  */
2965
2966 static bfd_boolean
2967 ppc64_elf_object_p (bfd *abfd)
2968 {
2969   if (!abfd->arch_info->the_default)
2970     return TRUE;
2971
2972   if (abfd->arch_info->bits_per_word == 32)
2973     {
2974       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2975
2976       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2977         {
2978           /* Relies on arch after 32 bit default being 64 bit default.  */
2979           abfd->arch_info = abfd->arch_info->next;
2980           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2981         }
2982     }
2983   return _bfd_elf_ppc_set_arch (abfd);
2984 }
2985
2986 /* Support for core dump NOTE sections.  */
2987
2988 static bfd_boolean
2989 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2990 {
2991   size_t offset, size;
2992
2993   if (note->descsz != 504)
2994     return FALSE;
2995
2996   /* pr_cursig */
2997   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2998
2999   /* pr_pid */
3000   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
3001
3002   /* pr_reg */
3003   offset = 112;
3004   size = 384;
3005
3006   /* Make a ".reg/999" section.  */
3007   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3008                                           size, note->descpos + offset);
3009 }
3010
3011 static bfd_boolean
3012 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3013 {
3014   if (note->descsz != 136)
3015     return FALSE;
3016
3017   elf_tdata (abfd)->core->pid
3018     = bfd_get_32 (abfd, note->descdata + 24);
3019   elf_tdata (abfd)->core->program
3020     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
3021   elf_tdata (abfd)->core->command
3022     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
3023
3024   return TRUE;
3025 }
3026
3027 static char *
3028 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
3029                            ...)
3030 {
3031   switch (note_type)
3032     {
3033     default:
3034       return NULL;
3035
3036     case NT_PRPSINFO:
3037       {
3038         char data[136] ATTRIBUTE_NONSTRING;
3039         va_list ap;
3040
3041         va_start (ap, note_type);
3042         memset (data, 0, sizeof (data));
3043         strncpy (data + 40, va_arg (ap, const char *), 16);
3044         strncpy (data + 56, va_arg (ap, const char *), 80);
3045         va_end (ap);
3046         return elfcore_write_note (abfd, buf, bufsiz,
3047                                    "CORE", note_type, data, sizeof (data));
3048       }
3049
3050     case NT_PRSTATUS:
3051       {
3052         char data[504];
3053         va_list ap;
3054         long pid;
3055         int cursig;
3056         const void *greg;
3057
3058         va_start (ap, note_type);
3059         memset (data, 0, 112);
3060         pid = va_arg (ap, long);
3061         bfd_put_32 (abfd, pid, data + 32);
3062         cursig = va_arg (ap, int);
3063         bfd_put_16 (abfd, cursig, data + 12);
3064         greg = va_arg (ap, const void *);
3065         memcpy (data + 112, greg, 384);
3066         memset (data + 496, 0, 8);
3067         va_end (ap);
3068         return elfcore_write_note (abfd, buf, bufsiz,
3069                                    "CORE", note_type, data, sizeof (data));
3070       }
3071     }
3072 }
3073
3074 /* Add extra PPC sections.  */
3075
3076 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3077 {
3078   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
3079   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3080   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3081   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3082   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3083   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3084   { NULL,                     0,  0, 0,            0 }
3085 };
3086
3087 enum _ppc64_sec_type {
3088   sec_normal = 0,
3089   sec_opd = 1,
3090   sec_toc = 2
3091 };
3092
3093 struct _ppc64_elf_section_data
3094 {
3095   struct bfd_elf_section_data elf;
3096
3097   union
3098   {
3099     /* An array with one entry for each opd function descriptor,
3100        and some spares since opd entries may be either 16 or 24 bytes.  */
3101 #define OPD_NDX(OFF) ((OFF) >> 4)
3102     struct _opd_sec_data
3103     {
3104       /* Points to the function code section for local opd entries.  */
3105       asection **func_sec;
3106
3107       /* After editing .opd, adjust references to opd local syms.  */
3108       long *adjust;
3109     } opd;
3110
3111     /* An array for toc sections, indexed by offset/8.  */
3112     struct _toc_sec_data
3113     {
3114       /* Specifies the relocation symbol index used at a given toc offset.  */
3115       unsigned *symndx;
3116
3117       /* And the relocation addend.  */
3118       bfd_vma *add;
3119     } toc;
3120   } u;
3121
3122   enum _ppc64_sec_type sec_type:2;
3123
3124   /* Flag set when small branches are detected.  Used to
3125      select suitable defaults for the stub group size.  */
3126   unsigned int has_14bit_branch:1;
3127
3128   /* Flag set when PLTCALL relocs are detected.  */
3129   unsigned int has_pltcall:1;
3130 };
3131
3132 #define ppc64_elf_section_data(sec) \
3133   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3134
3135 static bfd_boolean
3136 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3137 {
3138   if (!sec->used_by_bfd)
3139     {
3140       struct _ppc64_elf_section_data *sdata;
3141       bfd_size_type amt = sizeof (*sdata);
3142
3143       sdata = bfd_zalloc (abfd, amt);
3144       if (sdata == NULL)
3145         return FALSE;
3146       sec->used_by_bfd = sdata;
3147     }
3148
3149   return _bfd_elf_new_section_hook (abfd, sec);
3150 }
3151
3152 static struct _opd_sec_data *
3153 get_opd_info (asection * sec)
3154 {
3155   if (sec != NULL
3156       && ppc64_elf_section_data (sec) != NULL
3157       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3158     return &ppc64_elf_section_data (sec)->u.opd;
3159   return NULL;
3160 }
3161 \f
3162 /* Parameters for the qsort hook.  */
3163 static bfd_boolean synthetic_relocatable;
3164 static asection *synthetic_opd;
3165
3166 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
3167
3168 static int
3169 compare_symbols (const void *ap, const void *bp)
3170 {
3171   const asymbol *a = * (const asymbol **) ap;
3172   const asymbol *b = * (const asymbol **) bp;
3173
3174   /* Section symbols first.  */
3175   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3176     return -1;
3177   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3178     return 1;
3179
3180   /* then .opd symbols.  */
3181   if (synthetic_opd != NULL)
3182     {
3183       if (strcmp (a->section->name, ".opd") == 0
3184           && strcmp (b->section->name, ".opd") != 0)
3185         return -1;
3186       if (strcmp (a->section->name, ".opd") != 0
3187           && strcmp (b->section->name, ".opd") == 0)
3188         return 1;
3189     }
3190
3191   /* then other code symbols.  */
3192   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3193       == (SEC_CODE | SEC_ALLOC)
3194       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3195          != (SEC_CODE | SEC_ALLOC))
3196     return -1;
3197
3198   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3199       != (SEC_CODE | SEC_ALLOC)
3200       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3201          == (SEC_CODE | SEC_ALLOC))
3202     return 1;
3203
3204   if (synthetic_relocatable)
3205     {
3206       if (a->section->id < b->section->id)
3207         return -1;
3208
3209       if (a->section->id > b->section->id)
3210         return 1;
3211     }
3212
3213   if (a->value + a->section->vma < b->value + b->section->vma)
3214     return -1;
3215
3216   if (a->value + a->section->vma > b->value + b->section->vma)
3217     return 1;
3218
3219   /* For syms with the same value, prefer strong dynamic global function
3220      syms over other syms.  */
3221   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3222     return -1;
3223
3224   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3225     return 1;
3226
3227   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3228     return -1;
3229
3230   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3231     return 1;
3232
3233   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3234     return -1;
3235
3236   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3237     return 1;
3238
3239   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3240     return -1;
3241
3242   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3243     return 1;
3244
3245   return a > b;
3246 }
3247
3248 /* Search SYMS for a symbol of the given VALUE.  */
3249
3250 static asymbol *
3251 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3252 {
3253   long mid;
3254
3255   if (id == (unsigned) -1)
3256     {
3257       while (lo < hi)
3258         {
3259           mid = (lo + hi) >> 1;
3260           if (syms[mid]->value + syms[mid]->section->vma < value)
3261             lo = mid + 1;
3262           else if (syms[mid]->value + syms[mid]->section->vma > value)
3263             hi = mid;
3264           else
3265             return syms[mid];
3266         }
3267     }
3268   else
3269     {
3270       while (lo < hi)
3271         {
3272           mid = (lo + hi) >> 1;
3273           if (syms[mid]->section->id < id)
3274             lo = mid + 1;
3275           else if (syms[mid]->section->id > id)
3276             hi = mid;
3277           else if (syms[mid]->value < value)
3278             lo = mid + 1;
3279           else if (syms[mid]->value > value)
3280             hi = mid;
3281           else
3282             return syms[mid];
3283         }
3284     }
3285   return NULL;
3286 }
3287
3288 static bfd_boolean
3289 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3290 {
3291   bfd_vma vma = *(bfd_vma *) ptr;
3292   return ((section->flags & SEC_ALLOC) != 0
3293           && section->vma <= vma
3294           && vma < section->vma + section->size);
3295 }
3296
3297 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3298    entry syms.  Also generate @plt symbols for the glink branch table.
3299    Returns count of synthetic symbols in RET or -1 on error.  */
3300
3301 static long
3302 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3303                                 long static_count, asymbol **static_syms,
3304                                 long dyn_count, asymbol **dyn_syms,
3305                                 asymbol **ret)
3306 {
3307   asymbol *s;
3308   size_t i, j, count;
3309   char *names;
3310   size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3311   asection *opd = NULL;
3312   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3313   asymbol **syms;
3314   int abi = abiversion (abfd);
3315
3316   *ret = NULL;
3317
3318   if (abi < 2)
3319     {
3320       opd = bfd_get_section_by_name (abfd, ".opd");
3321       if (opd == NULL && abi == 1)
3322         return 0;
3323     }
3324
3325   syms = NULL;
3326   codesecsym = 0;
3327   codesecsymend = 0;
3328   secsymend = 0;
3329   opdsymend = 0;
3330   symcount = 0;
3331   if (opd != NULL)
3332     {
3333       symcount = static_count;
3334       if (!relocatable)
3335         symcount += dyn_count;
3336       if (symcount == 0)
3337         return 0;
3338
3339       syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3340       if (syms == NULL)
3341         return -1;
3342
3343       if (!relocatable && static_count != 0 && dyn_count != 0)
3344         {
3345           /* Use both symbol tables.  */
3346           memcpy (syms, static_syms, static_count * sizeof (*syms));
3347           memcpy (syms + static_count, dyn_syms,
3348                   (dyn_count + 1) * sizeof (*syms));
3349         }
3350       else if (!relocatable && static_count == 0)
3351         memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3352       else
3353         memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3354
3355       /* Trim uninteresting symbols.  Interesting symbols are section,
3356          function, and notype symbols.  */
3357       for (i = 0, j = 0; i < symcount; ++i)
3358         if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
3359                                | BSF_RELC | BSF_SRELC)) == 0)
3360           syms[j++] = syms[i];
3361       symcount = j;
3362
3363       synthetic_relocatable = relocatable;
3364       synthetic_opd = opd;
3365       qsort (syms, symcount, sizeof (*syms), compare_symbols);
3366
3367       if (!relocatable && symcount > 1)
3368         {
3369           /* Trim duplicate syms, since we may have merged the normal
3370              and dynamic symbols.  Actually, we only care about syms
3371              that have different values, so trim any with the same
3372              value.  Don't consider ifunc and ifunc resolver symbols
3373              duplicates however, because GDB wants to know whether a
3374              text symbol is an ifunc resolver.  */
3375           for (i = 1, j = 1; i < symcount; ++i)
3376             {
3377               const asymbol *s0 = syms[i - 1];
3378               const asymbol *s1 = syms[i];
3379
3380               if ((s0->value + s0->section->vma
3381                    != s1->value + s1->section->vma)
3382                   || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
3383                       != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
3384                 syms[j++] = syms[i];
3385             }
3386           symcount = j;
3387         }
3388
3389       i = 0;
3390       /* Note that here and in compare_symbols we can't compare opd and
3391          sym->section directly.  With separate debug info files, the
3392          symbols will be extracted from the debug file while abfd passed
3393          to this function is the real binary.  */
3394       if (strcmp (syms[i]->section->name, ".opd") == 0)
3395         ++i;
3396       codesecsym = i;
3397
3398       for (; i < symcount; ++i)
3399         if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3400                                          | SEC_THREAD_LOCAL))
3401              != (SEC_CODE | SEC_ALLOC))
3402             || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3403           break;
3404       codesecsymend = i;
3405
3406       for (; i < symcount; ++i)
3407         if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3408           break;
3409       secsymend = i;
3410
3411       for (; i < symcount; ++i)
3412         if (strcmp (syms[i]->section->name, ".opd") != 0)
3413           break;
3414       opdsymend = i;
3415
3416       for (; i < symcount; ++i)
3417         if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3418             != (SEC_CODE | SEC_ALLOC))
3419           break;
3420       symcount = i;
3421     }
3422   count = 0;
3423
3424   if (relocatable)
3425     {
3426       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3427       arelent *r;
3428       size_t size;
3429       size_t relcount;
3430
3431       if (opdsymend == secsymend)
3432         goto done;
3433
3434       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3435       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3436       if (relcount == 0)
3437         goto done;
3438
3439       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3440         {
3441           count = -1;
3442           goto done;
3443         }
3444
3445       size = 0;
3446       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3447         {
3448           asymbol *sym;
3449
3450           while (r < opd->relocation + relcount
3451                  && r->address < syms[i]->value + opd->vma)
3452             ++r;
3453
3454           if (r == opd->relocation + relcount)
3455             break;
3456
3457           if (r->address != syms[i]->value + opd->vma)
3458             continue;
3459
3460           if (r->howto->type != R_PPC64_ADDR64)
3461             continue;
3462
3463           sym = *r->sym_ptr_ptr;
3464           if (!sym_exists_at (syms, opdsymend, symcount,
3465                               sym->section->id, sym->value + r->addend))
3466             {
3467               ++count;
3468               size += sizeof (asymbol);
3469               size += strlen (syms[i]->name) + 2;
3470             }
3471         }
3472
3473       if (size == 0)
3474         goto done;
3475       s = *ret = bfd_malloc (size);
3476       if (s == NULL)
3477         {
3478           count = -1;
3479           goto done;
3480         }
3481
3482       names = (char *) (s + count);
3483
3484       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3485         {
3486           asymbol *sym;
3487
3488           while (r < opd->relocation + relcount
3489                  && r->address < syms[i]->value + opd->vma)
3490             ++r;
3491
3492           if (r == opd->relocation + relcount)
3493             break;
3494
3495           if (r->address != syms[i]->value + opd->vma)
3496             continue;
3497
3498           if (r->howto->type != R_PPC64_ADDR64)
3499             continue;
3500
3501           sym = *r->sym_ptr_ptr;
3502           if (!sym_exists_at (syms, opdsymend, symcount,
3503                               sym->section->id, sym->value + r->addend))
3504             {
3505               size_t len;
3506
3507               *s = *syms[i];
3508               s->flags |= BSF_SYNTHETIC;
3509               s->section = sym->section;
3510               s->value = sym->value + r->addend;
3511               s->name = names;
3512               *names++ = '.';
3513               len = strlen (syms[i]->name);
3514               memcpy (names, syms[i]->name, len + 1);
3515               names += len + 1;
3516               /* Have udata.p point back to the original symbol this
3517                  synthetic symbol was derived from.  */
3518               s->udata.p = syms[i];
3519               s++;
3520             }
3521         }
3522     }
3523   else
3524     {
3525       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3526       bfd_byte *contents = NULL;
3527       size_t size;
3528       size_t plt_count = 0;
3529       bfd_vma glink_vma = 0, resolv_vma = 0;
3530       asection *dynamic, *glink = NULL, *relplt = NULL;
3531       arelent *p;
3532
3533       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3534         {
3535         free_contents_and_exit_err:
3536           count = -1;
3537         free_contents_and_exit:
3538           if (contents)
3539             free (contents);
3540           goto done;
3541         }
3542
3543       size = 0;
3544       for (i = secsymend; i < opdsymend; ++i)
3545         {
3546           bfd_vma ent;
3547
3548           /* Ignore bogus symbols.  */
3549           if (syms[i]->value > opd->size - 8)
3550             continue;
3551
3552           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3553           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3554             {
3555               ++count;
3556               size += sizeof (asymbol);
3557               size += strlen (syms[i]->name) + 2;
3558             }
3559         }
3560
3561       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
3562       if (dyn_count != 0
3563           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3564         {
3565           bfd_byte *dynbuf, *extdyn, *extdynend;
3566           size_t extdynsize;
3567           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3568
3569           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3570             goto free_contents_and_exit_err;
3571
3572           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3573           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3574
3575           extdyn = dynbuf;
3576           extdynend = extdyn + dynamic->size;
3577           for (; extdyn < extdynend; extdyn += extdynsize)
3578             {
3579               Elf_Internal_Dyn dyn;
3580               (*swap_dyn_in) (abfd, extdyn, &dyn);
3581
3582               if (dyn.d_tag == DT_NULL)
3583                 break;
3584
3585               if (dyn.d_tag == DT_PPC64_GLINK)
3586                 {
3587                   /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3588                      See comment in ppc64_elf_finish_dynamic_sections. */
3589                   glink_vma = dyn.d_un.d_val + 8 * 4;
3590                   /* The .glink section usually does not survive the final
3591                      link; search for the section (usually .text) where the
3592                      glink stubs now reside.  */
3593                   glink = bfd_sections_find_if (abfd, section_covers_vma,
3594                                                 &glink_vma);
3595                   break;
3596                 }
3597             }
3598
3599           free (dynbuf);
3600         }
3601
3602       if (glink != NULL)
3603         {
3604           /* Determine __glink trampoline by reading the relative branch
3605              from the first glink stub.  */
3606           bfd_byte buf[4];
3607           unsigned int off = 0;
3608
3609           while (bfd_get_section_contents (abfd, glink, buf,
3610                                            glink_vma + off - glink->vma, 4))
3611             {
3612               unsigned int insn = bfd_get_32 (abfd, buf);
3613               insn ^= B_DOT;
3614               if ((insn & ~0x3fffffc) == 0)
3615                 {
3616                   resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3617                   break;
3618                 }
3619               off += 4;
3620               if (off > 4)
3621                 break;
3622             }
3623
3624           if (resolv_vma)
3625             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3626
3627           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3628           if (relplt != NULL)
3629             {
3630               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3631               if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3632                 goto free_contents_and_exit_err;
3633
3634               plt_count = relplt->size / sizeof (Elf64_External_Rela);
3635               size += plt_count * sizeof (asymbol);
3636
3637               p = relplt->relocation;
3638               for (i = 0; i < plt_count; i++, p++)
3639                 {
3640                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3641                   if (p->addend != 0)
3642                     size += sizeof ("+0x") - 1 + 16;
3643                 }
3644             }
3645         }
3646
3647       if (size == 0)
3648         goto free_contents_and_exit;
3649       s = *ret = bfd_malloc (size);
3650       if (s == NULL)
3651         goto free_contents_and_exit_err;
3652
3653       names = (char *) (s + count + plt_count + (resolv_vma != 0));
3654
3655       for (i = secsymend; i < opdsymend; ++i)
3656         {
3657           bfd_vma ent;
3658
3659           if (syms[i]->value > opd->size - 8)
3660             continue;
3661
3662           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3663           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3664             {
3665               size_t lo, hi;
3666               size_t len;
3667               asection *sec = abfd->sections;
3668
3669               *s = *syms[i];
3670               lo = codesecsym;
3671               hi = codesecsymend;
3672               while (lo < hi)
3673                 {
3674                   size_t mid = (lo + hi) >> 1;
3675                   if (syms[mid]->section->vma < ent)
3676                     lo = mid + 1;
3677                   else if (syms[mid]->section->vma > ent)
3678                     hi = mid;
3679                   else
3680                     {
3681                       sec = syms[mid]->section;
3682                       break;
3683                     }
3684                 }
3685
3686               if (lo >= hi && lo > codesecsym)
3687                 sec = syms[lo - 1]->section;
3688
3689               for (; sec != NULL; sec = sec->next)
3690                 {
3691                   if (sec->vma > ent)
3692                     break;
3693                   /* SEC_LOAD may not be set if SEC is from a separate debug
3694                      info file.  */
3695                   if ((sec->flags & SEC_ALLOC) == 0)
3696                     break;
3697                   if ((sec->flags & SEC_CODE) != 0)
3698                     s->section = sec;
3699                 }
3700               s->flags |= BSF_SYNTHETIC;
3701               s->value = ent - s->section->vma;
3702               s->name = names;
3703               *names++ = '.';
3704               len = strlen (syms[i]->name);
3705               memcpy (names, syms[i]->name, len + 1);
3706               names += len + 1;
3707               /* Have udata.p point back to the original symbol this
3708                  synthetic symbol was derived from.  */
3709               s->udata.p = syms[i];
3710               s++;
3711             }
3712         }
3713       free (contents);
3714
3715       if (glink != NULL && relplt != NULL)
3716         {
3717           if (resolv_vma)
3718             {
3719               /* Add a symbol for the main glink trampoline.  */
3720               memset (s, 0, sizeof *s);
3721               s->the_bfd = abfd;
3722               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3723               s->section = glink;
3724               s->value = resolv_vma - glink->vma;
3725               s->name = names;
3726               memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3727               names += sizeof ("__glink_PLTresolve");
3728               s++;
3729               count++;
3730             }
3731
3732           /* FIXME: It would be very much nicer to put sym@plt on the
3733              stub rather than on the glink branch table entry.  The
3734              objdump disassembler would then use a sensible symbol
3735              name on plt calls.  The difficulty in doing so is
3736              a) finding the stubs, and,
3737              b) matching stubs against plt entries, and,
3738              c) there can be multiple stubs for a given plt entry.
3739
3740              Solving (a) could be done by code scanning, but older
3741              ppc64 binaries used different stubs to current code.
3742              (b) is the tricky one since you need to known the toc
3743              pointer for at least one function that uses a pic stub to
3744              be able to calculate the plt address referenced.
3745              (c) means gdb would need to set multiple breakpoints (or
3746              find the glink branch itself) when setting breakpoints
3747              for pending shared library loads.  */
3748           p = relplt->relocation;
3749           for (i = 0; i < plt_count; i++, p++)
3750             {
3751               size_t len;
3752
3753               *s = **p->sym_ptr_ptr;
3754               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
3755                  we are defining a symbol, ensure one of them is set.  */
3756               if ((s->flags & BSF_LOCAL) == 0)
3757                 s->flags |= BSF_GLOBAL;
3758               s->flags |= BSF_SYNTHETIC;
3759               s->section = glink;
3760               s->value = glink_vma - glink->vma;
3761               s->name = names;
3762               s->udata.p = NULL;
3763               len = strlen ((*p->sym_ptr_ptr)->name);
3764               memcpy (names, (*p->sym_ptr_ptr)->name, len);
3765               names += len;
3766               if (p->addend != 0)
3767                 {
3768                   memcpy (names, "+0x", sizeof ("+0x") - 1);
3769                   names += sizeof ("+0x") - 1;
3770                   bfd_sprintf_vma (abfd, names, p->addend);
3771                   names += strlen (names);
3772                 }
3773               memcpy (names, "@plt", sizeof ("@plt"));
3774               names += sizeof ("@plt");
3775               s++;
3776               if (abi < 2)
3777                 {
3778                   glink_vma += 8;
3779                   if (i >= 0x8000)
3780                     glink_vma += 4;
3781                 }
3782               else
3783                 glink_vma += 4;
3784             }
3785           count += plt_count;
3786         }
3787     }
3788
3789  done:
3790   free (syms);
3791   return count;
3792 }
3793 \f
3794 /* The following functions are specific to the ELF linker, while
3795    functions above are used generally.  Those named ppc64_elf_* are
3796    called by the main ELF linker code.  They appear in this file more
3797    or less in the order in which they are called.  eg.
3798    ppc64_elf_check_relocs is called early in the link process,
3799    ppc64_elf_finish_dynamic_sections is one of the last functions
3800    called.
3801
3802    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3803    functions have both a function code symbol and a function descriptor
3804    symbol.  A call to foo in a relocatable object file looks like:
3805
3806    .            .text
3807    .    x:
3808    .            bl      .foo
3809    .            nop
3810
3811    The function definition in another object file might be:
3812
3813    .            .section .opd
3814    .    foo:    .quad   .foo
3815    .            .quad   .TOC.@tocbase
3816    .            .quad   0
3817    .
3818    .            .text
3819    .    .foo:   blr
3820
3821    When the linker resolves the call during a static link, the branch
3822    unsurprisingly just goes to .foo and the .opd information is unused.
3823    If the function definition is in a shared library, things are a little
3824    different:  The call goes via a plt call stub, the opd information gets
3825    copied to the plt, and the linker patches the nop.
3826
3827    .    x:
3828    .            bl      .foo_stub
3829    .            ld      2,40(1)
3830    .
3831    .
3832    .    .foo_stub:
3833    .            std     2,40(1)                 # in practice, the call stub
3834    .            addis   11,2,Lfoo@toc@ha        # is slightly optimized, but
3835    .            addi    11,11,Lfoo@toc@l        # this is the general idea
3836    .            ld      12,0(11)
3837    .            ld      2,8(11)
3838    .            mtctr   12
3839    .            ld      11,16(11)
3840    .            bctr
3841    .
3842    .            .section .plt
3843    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
3844
3845    The "reloc ()" notation is supposed to indicate that the linker emits
3846    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3847    copying.
3848
3849    What are the difficulties here?  Well, firstly, the relocations
3850    examined by the linker in check_relocs are against the function code
3851    sym .foo, while the dynamic relocation in the plt is emitted against
3852    the function descriptor symbol, foo.  Somewhere along the line, we need
3853    to carefully copy dynamic link information from one symbol to the other.
3854    Secondly, the generic part of the elf linker will make .foo a dynamic
3855    symbol as is normal for most other backends.  We need foo dynamic
3856    instead, at least for an application final link.  However, when
3857    creating a shared library containing foo, we need to have both symbols
3858    dynamic so that references to .foo are satisfied during the early
3859    stages of linking.  Otherwise the linker might decide to pull in a
3860    definition from some other object, eg. a static library.
3861
3862    Update: As of August 2004, we support a new convention.  Function
3863    calls may use the function descriptor symbol, ie. "bl foo".  This
3864    behaves exactly as "bl .foo".  */
3865
3866 /* Of those relocs that might be copied as dynamic relocs, this
3867    function selects those that must be copied when linking a shared
3868    library or PIE, even when the symbol is local.  */
3869
3870 static int
3871 must_be_dyn_reloc (struct bfd_link_info *info,
3872                    enum elf_ppc64_reloc_type r_type)
3873 {
3874   switch (r_type)
3875     {
3876     default:
3877       /* Only relative relocs can be resolved when the object load
3878          address isn't fixed.  DTPREL64 is excluded because the
3879          dynamic linker needs to differentiate global dynamic from
3880          local dynamic __tls_index pairs when PPC64_OPT_TLS is set.  */
3881       return 1;
3882
3883     case R_PPC64_REL32:
3884     case R_PPC64_REL64:
3885     case R_PPC64_REL30:
3886       return 0;
3887
3888     case R_PPC64_TPREL16:
3889     case R_PPC64_TPREL16_LO:
3890     case R_PPC64_TPREL16_HI:
3891     case R_PPC64_TPREL16_HA:
3892     case R_PPC64_TPREL16_DS:
3893     case R_PPC64_TPREL16_LO_DS:
3894     case R_PPC64_TPREL16_HIGH:
3895     case R_PPC64_TPREL16_HIGHA:
3896     case R_PPC64_TPREL16_HIGHER:
3897     case R_PPC64_TPREL16_HIGHERA:
3898     case R_PPC64_TPREL16_HIGHEST:
3899     case R_PPC64_TPREL16_HIGHESTA:
3900     case R_PPC64_TPREL64:
3901       /* These relocations are relative but in a shared library the
3902          linker doesn't know the thread pointer base.  */
3903       return bfd_link_dll (info);
3904     }
3905 }
3906
3907 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3908    copying dynamic variables from a shared lib into an app's dynbss
3909    section, and instead use a dynamic relocation to point into the
3910    shared lib.  With code that gcc generates, it's vital that this be
3911    enabled;  In the PowerPC64 ABI, the address of a function is actually
3912    the address of a function descriptor, which resides in the .opd
3913    section.  gcc uses the descriptor directly rather than going via the
3914    GOT as some other ABI's do, which means that initialized function
3915    pointers must reference the descriptor.  Thus, a function pointer
3916    initialized to the address of a function in a shared library will
3917    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3918    redefines the function descriptor symbol to point to the copy.  This
3919    presents a problem as a plt entry for that function is also
3920    initialized from the function descriptor symbol and the copy reloc
3921    may not be initialized first.  */
3922 #define ELIMINATE_COPY_RELOCS 1
3923
3924 /* Section name for stubs is the associated section name plus this
3925    string.  */
3926 #define STUB_SUFFIX ".stub"
3927
3928 /* Linker stubs.
3929    ppc_stub_long_branch:
3930    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3931    destination, but a 24 bit branch in a stub section will reach.
3932    .    b       dest
3933
3934    ppc_stub_plt_branch:
3935    Similar to the above, but a 24 bit branch in the stub section won't
3936    reach its destination.
3937    .    addis   %r11,%r2,xxx@toc@ha
3938    .    ld      %r12,xxx@toc@l(%r11)
3939    .    mtctr   %r12
3940    .    bctr
3941
3942    ppc_stub_plt_call:
3943    Used to call a function in a shared library.  If it so happens that
3944    the plt entry referenced crosses a 64k boundary, then an extra
3945    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3946    .    std     %r2,40(%r1)
3947    .    addis   %r11,%r2,xxx@toc@ha
3948    .    ld      %r12,xxx+0@toc@l(%r11)
3949    .    mtctr   %r12
3950    .    ld      %r2,xxx+8@toc@l(%r11)
3951    .    ld      %r11,xxx+16@toc@l(%r11)
3952    .    bctr
3953
3954    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3955    code to adjust the value and save r2 to support multiple toc sections.
3956    A ppc_stub_long_branch with an r2 offset looks like:
3957    .    std     %r2,40(%r1)
3958    .    addis   %r2,%r2,off@ha
3959    .    addi    %r2,%r2,off@l
3960    .    b       dest
3961
3962    A ppc_stub_plt_branch with an r2 offset looks like:
3963    .    std     %r2,40(%r1)
3964    .    addis   %r11,%r2,xxx@toc@ha
3965    .    ld      %r12,xxx@toc@l(%r11)
3966    .    addis   %r2,%r2,off@ha
3967    .    addi    %r2,%r2,off@l
3968    .    mtctr   %r12
3969    .    bctr
3970
3971    In cases where the "addis" instruction would add zero, the "addis" is
3972    omitted and following instructions modified slightly in some cases.
3973 */
3974
3975 enum ppc_stub_type {
3976   ppc_stub_none,
3977   ppc_stub_long_branch,
3978   ppc_stub_long_branch_r2off,
3979   ppc_stub_plt_branch,
3980   ppc_stub_plt_branch_r2off,
3981   ppc_stub_plt_call,
3982   ppc_stub_plt_call_r2save,
3983   ppc_stub_global_entry,
3984   ppc_stub_save_res
3985 };
3986
3987 /* Information on stub grouping.  */
3988 struct map_stub
3989 {
3990   /* The stub section.  */
3991   asection *stub_sec;
3992   /* This is the section to which stubs in the group will be attached.  */
3993   asection *link_sec;
3994   /* Next group.  */
3995   struct map_stub *next;
3996   /* Whether to emit a copy of register save/restore functions in this
3997      group.  */
3998   int needs_save_res;
3999   /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
4000      or -1u if no such stub with bctrl exists.  */
4001   unsigned int tls_get_addr_opt_bctrl;
4002 };
4003
4004 struct ppc_stub_hash_entry {
4005
4006   /* Base hash table entry structure.  */
4007   struct bfd_hash_entry root;
4008
4009   enum ppc_stub_type stub_type;
4010
4011   /* Group information.  */
4012   struct map_stub *group;
4013
4014   /* Offset within stub_sec of the beginning of this stub.  */
4015   bfd_vma stub_offset;
4016
4017   /* Given the symbol's value and its section we can determine its final
4018      value when building the stubs (so the stub knows where to jump.  */
4019   bfd_vma target_value;
4020   asection *target_section;
4021
4022   /* The symbol table entry, if any, that this was derived from.  */
4023   struct ppc_link_hash_entry *h;
4024   struct plt_entry *plt_ent;
4025
4026   /* Symbol type.  */
4027   unsigned char symtype;
4028
4029   /* Symbol st_other.  */
4030   unsigned char other;
4031 };
4032
4033 struct ppc_branch_hash_entry {
4034
4035   /* Base hash table entry structure.  */
4036   struct bfd_hash_entry root;
4037
4038   /* Offset within branch lookup table.  */
4039   unsigned int offset;
4040
4041   /* Generation marker.  */
4042   unsigned int iter;
4043 };
4044
4045 /* Used to track dynamic relocations for local symbols.  */
4046 struct ppc_dyn_relocs
4047 {
4048   struct ppc_dyn_relocs *next;
4049
4050   /* The input section of the reloc.  */
4051   asection *sec;
4052
4053   /* Total number of relocs copied for the input section.  */
4054   unsigned int count : 31;
4055
4056   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
4057   unsigned int ifunc : 1;
4058 };
4059
4060 struct ppc_link_hash_entry
4061 {
4062   struct elf_link_hash_entry elf;
4063
4064   union {
4065     /* A pointer to the most recently used stub hash entry against this
4066        symbol.  */
4067     struct ppc_stub_hash_entry *stub_cache;
4068
4069     /* A pointer to the next symbol starting with a '.'  */
4070     struct ppc_link_hash_entry *next_dot_sym;
4071   } u;
4072
4073   /* Track dynamic relocs copied for this symbol.  */
4074   struct elf_dyn_relocs *dyn_relocs;
4075
4076   /* Link between function code and descriptor symbols.  */
4077   struct ppc_link_hash_entry *oh;
4078
4079   /* Flag function code and descriptor symbols.  */
4080   unsigned int is_func:1;
4081   unsigned int is_func_descriptor:1;
4082   unsigned int fake:1;
4083
4084   /* Whether global opd/toc sym has been adjusted or not.
4085      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4086      should be set for all globals defined in any opd/toc section.  */
4087   unsigned int adjust_done:1;
4088
4089   /* Set if this is an out-of-line register save/restore function,
4090      with non-standard calling convention.  */
4091   unsigned int save_res:1;
4092
4093   /* Set if a duplicate symbol with non-zero localentry is detected,
4094      even when the duplicate symbol does not provide a definition.  */
4095   unsigned int non_zero_localentry:1;
4096
4097   /* Contexts in which symbol is used in the GOT (or TOC).
4098      Bits are or'd into the mask as the corresponding relocs are
4099      encountered during check_relocs, with TLS_TLS being set when any
4100      of the other TLS bits are set.  tls_optimize clears bits when
4101      optimizing to indicate the corresponding GOT entry type is not
4102      needed.  If set, TLS_TLS is never cleared.  tls_optimize may also
4103      set TLS_TPRELGD when a GD reloc turns into a TPREL one.  We use a
4104      separate flag rather than setting TPREL just for convenience in
4105      distinguishing the two cases.
4106      These flags are also kept for local symbols.  */
4107 #define TLS_TLS          1      /* Any TLS reloc.  */
4108 #define TLS_GD           2      /* GD reloc. */
4109 #define TLS_LD           4      /* LD reloc. */
4110 #define TLS_TPREL        8      /* TPREL reloc, => IE. */
4111 #define TLS_DTPREL      16      /* DTPREL reloc, => LD. */
4112 #define TLS_MARK        32      /* __tls_get_addr call marked. */
4113 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
4114 #define TLS_EXPLICIT   128      /* Marks TOC section TLS relocs. */
4115   unsigned char tls_mask;
4116
4117   /* The above field is also used to mark function symbols.  In which
4118      case TLS_TLS will be 0.  */
4119 #define PLT_IFUNC        2      /* STT_GNU_IFUNC.  */
4120 #define PLT_KEEP         4      /* inline plt call requires plt entry.  */
4121 #define NON_GOT        256      /* local symbol plt, not stored.  */
4122 };
4123
4124 /* ppc64 ELF linker hash table.  */
4125
4126 struct ppc_link_hash_table
4127 {
4128   struct elf_link_hash_table elf;
4129
4130   /* The stub hash table.  */
4131   struct bfd_hash_table stub_hash_table;
4132
4133   /* Another hash table for plt_branch stubs.  */
4134   struct bfd_hash_table branch_hash_table;
4135
4136   /* Hash table for function prologue tocsave.  */
4137   htab_t tocsave_htab;
4138
4139   /* Various options and other info passed from the linker.  */
4140   struct ppc64_elf_params *params;
4141
4142   /* The size of sec_info below.  */
4143   unsigned int sec_info_arr_size;
4144
4145   /* Per-section array of extra section info.  Done this way rather
4146      than as part of ppc64_elf_section_data so we have the info for
4147      non-ppc64 sections.  */
4148   struct
4149   {
4150     /* Along with elf_gp, specifies the TOC pointer used by this section.  */
4151     bfd_vma toc_off;
4152
4153     union
4154     {
4155       /* The section group that this section belongs to.  */
4156       struct map_stub *group;
4157       /* A temp section list pointer.  */
4158       asection *list;
4159     } u;
4160   } *sec_info;
4161
4162   /* Linked list of groups.  */
4163   struct map_stub *group;
4164
4165   /* Temp used when calculating TOC pointers.  */
4166   bfd_vma toc_curr;
4167   bfd *toc_bfd;
4168   asection *toc_first_sec;
4169
4170   /* Used when adding symbols.  */
4171   struct ppc_link_hash_entry *dot_syms;
4172
4173   /* Shortcuts to get to dynamic linker sections.  */
4174   asection *glink;
4175   asection *global_entry;
4176   asection *sfpr;
4177   asection *pltlocal;
4178   asection *relpltlocal;
4179   asection *brlt;
4180   asection *relbrlt;
4181   asection *glink_eh_frame;
4182
4183   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
4184   struct ppc_link_hash_entry *tls_get_addr;
4185   struct ppc_link_hash_entry *tls_get_addr_fd;
4186
4187   /* The size of reliplt used by got entry relocs.  */
4188   bfd_size_type got_reli_size;
4189
4190   /* Statistics.  */
4191   unsigned long stub_count[ppc_stub_global_entry];
4192
4193   /* Number of stubs against global syms.  */
4194   unsigned long stub_globals;
4195
4196   /* Set if we're linking code with function descriptors.  */
4197   unsigned int opd_abi:1;
4198
4199   /* Support for multiple toc sections.  */
4200   unsigned int do_multi_toc:1;
4201   unsigned int multi_toc_needed:1;
4202   unsigned int second_toc_pass:1;
4203   unsigned int do_toc_opt:1;
4204
4205   /* Set if tls optimization is enabled.  */
4206   unsigned int do_tls_opt:1;
4207
4208   /* Set if inline plt calls should be converted to direct calls.  */
4209   unsigned int can_convert_all_inline_plt:1;
4210
4211   /* Set on error.  */
4212   unsigned int stub_error:1;
4213
4214   /* Whether func_desc_adjust needs to be run over symbols.  */
4215   unsigned int need_func_desc_adj:1;
4216
4217   /* Whether there exist local gnu indirect function resolvers,
4218      referenced by dynamic relocations.  */
4219   unsigned int local_ifunc_resolver:1;
4220   unsigned int maybe_local_ifunc_resolver:1;
4221
4222   /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized.  */
4223   unsigned int has_plt_localentry0:1;
4224
4225   /* Incremented every time we size stubs.  */
4226   unsigned int stub_iteration;
4227
4228   /* Small local sym cache.  */
4229   struct sym_cache sym_cache;
4230 };
4231
4232 /* Rename some of the generic section flags to better document how they
4233    are used here.  */
4234
4235 /* Nonzero if this section has TLS related relocations.  */
4236 #define has_tls_reloc sec_flg0
4237
4238 /* Nonzero if this section has an old-style call to __tls_get_addr.  */
4239 #define has_tls_get_addr_call sec_flg1
4240
4241 /* Nonzero if this section has any toc or got relocs.  */
4242 #define has_toc_reloc sec_flg2
4243
4244 /* Nonzero if this section has a call to another section that uses
4245    the toc or got.  */
4246 #define makes_toc_func_call sec_flg3
4247
4248 /* Recursion protection when determining above flag.  */
4249 #define call_check_in_progress sec_flg4
4250 #define call_check_done sec_flg5
4251
4252 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
4253
4254 #define ppc_hash_table(p) \
4255   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4256   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4257
4258 #define ppc_stub_hash_lookup(table, string, create, copy) \
4259   ((struct ppc_stub_hash_entry *) \
4260    bfd_hash_lookup ((table), (string), (create), (copy)))
4261
4262 #define ppc_branch_hash_lookup(table, string, create, copy) \
4263   ((struct ppc_branch_hash_entry *) \
4264    bfd_hash_lookup ((table), (string), (create), (copy)))
4265
4266 /* Create an entry in the stub hash table.  */
4267
4268 static struct bfd_hash_entry *
4269 stub_hash_newfunc (struct bfd_hash_entry *entry,
4270                    struct bfd_hash_table *table,
4271                    const char *string)
4272 {
4273   /* Allocate the structure if it has not already been allocated by a
4274      subclass.  */
4275   if (entry == NULL)
4276     {
4277       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4278       if (entry == NULL)
4279         return entry;
4280     }
4281
4282   /* Call the allocation method of the superclass.  */
4283   entry = bfd_hash_newfunc (entry, table, string);
4284   if (entry != NULL)
4285     {
4286       struct ppc_stub_hash_entry *eh;
4287
4288       /* Initialize the local fields.  */
4289       eh = (struct ppc_stub_hash_entry *) entry;
4290       eh->stub_type = ppc_stub_none;
4291       eh->group = NULL;
4292       eh->stub_offset = 0;
4293       eh->target_value = 0;
4294       eh->target_section = NULL;
4295       eh->h = NULL;
4296       eh->plt_ent = NULL;
4297       eh->other = 0;
4298     }
4299
4300   return entry;
4301 }
4302
4303 /* Create an entry in the branch hash table.  */
4304
4305 static struct bfd_hash_entry *
4306 branch_hash_newfunc (struct bfd_hash_entry *entry,
4307                      struct bfd_hash_table *table,
4308                      const char *string)
4309 {
4310   /* Allocate the structure if it has not already been allocated by a
4311      subclass.  */
4312   if (entry == NULL)
4313     {
4314       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4315       if (entry == NULL)
4316         return entry;
4317     }
4318
4319   /* Call the allocation method of the superclass.  */
4320   entry = bfd_hash_newfunc (entry, table, string);
4321   if (entry != NULL)
4322     {
4323       struct ppc_branch_hash_entry *eh;
4324
4325       /* Initialize the local fields.  */
4326       eh = (struct ppc_branch_hash_entry *) entry;
4327       eh->offset = 0;
4328       eh->iter = 0;
4329     }
4330
4331   return entry;
4332 }
4333
4334 /* Create an entry in a ppc64 ELF linker hash table.  */
4335
4336 static struct bfd_hash_entry *
4337 link_hash_newfunc (struct bfd_hash_entry *entry,
4338                    struct bfd_hash_table *table,
4339                    const char *string)
4340 {
4341   /* Allocate the structure if it has not already been allocated by a
4342      subclass.  */
4343   if (entry == NULL)
4344     {
4345       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4346       if (entry == NULL)
4347         return entry;
4348     }
4349
4350   /* Call the allocation method of the superclass.  */
4351   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4352   if (entry != NULL)
4353     {
4354       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4355
4356       memset (&eh->u.stub_cache, 0,
4357               (sizeof (struct ppc_link_hash_entry)
4358                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4359
4360       /* When making function calls, old ABI code references function entry
4361          points (dot symbols), while new ABI code references the function
4362          descriptor symbol.  We need to make any combination of reference and
4363          definition work together, without breaking archive linking.
4364
4365          For a defined function "foo" and an undefined call to "bar":
4366          An old object defines "foo" and ".foo", references ".bar" (possibly
4367          "bar" too).
4368          A new object defines "foo" and references "bar".
4369
4370          A new object thus has no problem with its undefined symbols being
4371          satisfied by definitions in an old object.  On the other hand, the
4372          old object won't have ".bar" satisfied by a new object.
4373
4374          Keep a list of newly added dot-symbols.  */
4375
4376       if (string[0] == '.')
4377         {
4378           struct ppc_link_hash_table *htab;
4379
4380           htab = (struct ppc_link_hash_table *) table;
4381           eh->u.next_dot_sym = htab->dot_syms;
4382           htab->dot_syms = eh;
4383         }
4384     }
4385
4386   return entry;
4387 }
4388
4389 struct tocsave_entry {
4390   asection *sec;
4391   bfd_vma offset;
4392 };
4393
4394 static hashval_t
4395 tocsave_htab_hash (const void *p)
4396 {
4397   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4398   return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
4399 }
4400
4401 static int
4402 tocsave_htab_eq (const void *p1, const void *p2)
4403 {
4404   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4405   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4406   return e1->sec == e2->sec && e1->offset == e2->offset;
4407 }
4408
4409 /* Destroy a ppc64 ELF linker hash table.  */
4410
4411 static void
4412 ppc64_elf_link_hash_table_free (bfd *obfd)
4413 {
4414   struct ppc_link_hash_table *htab;
4415
4416   htab = (struct ppc_link_hash_table *) obfd->link.hash;
4417   if (htab->tocsave_htab)
4418     htab_delete (htab->tocsave_htab);
4419   bfd_hash_table_free (&htab->branch_hash_table);
4420   bfd_hash_table_free (&htab->stub_hash_table);
4421   _bfd_elf_link_hash_table_free (obfd);
4422 }
4423
4424 /* Create a ppc64 ELF linker hash table.  */
4425
4426 static struct bfd_link_hash_table *
4427 ppc64_elf_link_hash_table_create (bfd *abfd)
4428 {
4429   struct ppc_link_hash_table *htab;
4430   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4431
4432   htab = bfd_zmalloc (amt);
4433   if (htab == NULL)
4434     return NULL;
4435
4436   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4437                                       sizeof (struct ppc_link_hash_entry),
4438                                       PPC64_ELF_DATA))
4439     {
4440       free (htab);
4441       return NULL;
4442     }
4443
4444   /* Init the stub hash table too.  */
4445   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4446                             sizeof (struct ppc_stub_hash_entry)))
4447     {
4448       _bfd_elf_link_hash_table_free (abfd);
4449       return NULL;
4450     }
4451
4452   /* And the branch hash table.  */
4453   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4454                             sizeof (struct ppc_branch_hash_entry)))
4455     {
4456       bfd_hash_table_free (&htab->stub_hash_table);
4457       _bfd_elf_link_hash_table_free (abfd);
4458       return NULL;
4459     }
4460
4461   htab->tocsave_htab = htab_try_create (1024,
4462                                         tocsave_htab_hash,
4463                                         tocsave_htab_eq,
4464                                         NULL);
4465   if (htab->tocsave_htab == NULL)
4466     {
4467       ppc64_elf_link_hash_table_free (abfd);
4468       return NULL;
4469     }
4470   htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4471
4472   /* Initializing two fields of the union is just cosmetic.  We really
4473      only care about glist, but when compiled on a 32-bit host the
4474      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
4475      debugger inspection of these fields look nicer.  */
4476   htab->elf.init_got_refcount.refcount = 0;
4477   htab->elf.init_got_refcount.glist = NULL;
4478   htab->elf.init_plt_refcount.refcount = 0;
4479   htab->elf.init_plt_refcount.glist = NULL;
4480   htab->elf.init_got_offset.offset = 0;
4481   htab->elf.init_got_offset.glist = NULL;
4482   htab->elf.init_plt_offset.offset = 0;
4483   htab->elf.init_plt_offset.glist = NULL;
4484
4485   return &htab->elf.root;
4486 }
4487
4488 /* Create sections for linker generated code.  */
4489
4490 static bfd_boolean
4491 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4492 {
4493   struct ppc_link_hash_table *htab;
4494   flagword flags;
4495
4496   htab = ppc_hash_table (info);
4497
4498   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4499            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4500   if (htab->params->save_restore_funcs)
4501     {
4502       /* Create .sfpr for code to save and restore fp regs.  */
4503       htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4504                                                        flags);
4505       if (htab->sfpr == NULL
4506           || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4507         return FALSE;
4508     }
4509
4510   if (bfd_link_relocatable (info))
4511     return TRUE;
4512
4513   /* Create .glink for lazy dynamic linking support.  */
4514   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4515                                                     flags);
4516   if (htab->glink == NULL
4517       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4518     return FALSE;
4519
4520   /* The part of .glink used by global entry stubs, separate so that
4521      it can be aligned appropriately without affecting htab->glink.  */
4522   htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4523                                                            flags);
4524   if (htab->global_entry == NULL
4525       || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4526     return FALSE;
4527
4528   if (!info->no_ld_generated_unwind_info)
4529     {
4530       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4531                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4532       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4533                                                                  ".eh_frame",
4534                                                                  flags);
4535       if (htab->glink_eh_frame == NULL
4536           || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4537         return FALSE;
4538     }
4539
4540   flags = SEC_ALLOC | SEC_LINKER_CREATED;
4541   htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4542   if (htab->elf.iplt == NULL
4543       || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4544     return FALSE;
4545
4546   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4547            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4548   htab->elf.irelplt
4549     = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4550   if (htab->elf.irelplt == NULL
4551       || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4552     return FALSE;
4553
4554   /* Create branch lookup table for plt_branch stubs.  */
4555   flags = (SEC_ALLOC | SEC_LOAD
4556            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4557   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4558                                                    flags);
4559   if (htab->brlt == NULL
4560       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4561     return FALSE;
4562
4563   /* Local plt entries, put in .branch_lt but a separate section for
4564      convenience.  */
4565   htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4566                                                        flags);
4567   if (htab->pltlocal == NULL
4568       || ! bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
4569     return FALSE;
4570
4571   if (!bfd_link_pic (info))
4572     return TRUE;
4573
4574   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4575            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4576   htab->relbrlt
4577     = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4578   if (htab->relbrlt == NULL
4579       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4580     return FALSE;
4581
4582   htab->relpltlocal
4583     = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4584   if (htab->relpltlocal == NULL
4585       || ! bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
4586     return FALSE;
4587
4588   return TRUE;
4589 }
4590
4591 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
4592
4593 bfd_boolean
4594 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4595                          struct ppc64_elf_params *params)
4596 {
4597   struct ppc_link_hash_table *htab;
4598
4599   elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4600
4601 /* Always hook our dynamic sections into the first bfd, which is the
4602    linker created stub bfd.  This ensures that the GOT header is at
4603    the start of the output TOC section.  */
4604   htab = ppc_hash_table (info);
4605   htab->elf.dynobj = params->stub_bfd;
4606   htab->params = params;
4607
4608   return create_linkage_sections (htab->elf.dynobj, info);
4609 }
4610
4611 /* Build a name for an entry in the stub hash table.  */
4612
4613 static char *
4614 ppc_stub_name (const asection *input_section,
4615                const asection *sym_sec,
4616                const struct ppc_link_hash_entry *h,
4617                const Elf_Internal_Rela *rel)
4618 {
4619   char *stub_name;
4620   ssize_t len;
4621
4622   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4623      offsets from a sym as a branch target?  In fact, we could
4624      probably assume the addend is always zero.  */
4625   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4626
4627   if (h)
4628     {
4629       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4630       stub_name = bfd_malloc (len);
4631       if (stub_name == NULL)
4632         return stub_name;
4633
4634       len = sprintf (stub_name, "%08x.%s+%x",
4635                      input_section->id & 0xffffffff,
4636                      h->elf.root.root.string,
4637                      (int) rel->r_addend & 0xffffffff);
4638     }
4639   else
4640     {
4641       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4642       stub_name = bfd_malloc (len);
4643       if (stub_name == NULL)
4644         return stub_name;
4645
4646       len = sprintf (stub_name, "%08x.%x:%x+%x",
4647                      input_section->id & 0xffffffff,
4648                      sym_sec->id & 0xffffffff,
4649                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4650                      (int) rel->r_addend & 0xffffffff);
4651     }
4652   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4653     stub_name[len - 2] = 0;
4654   return stub_name;
4655 }
4656
4657 /* Look up an entry in the stub hash.  Stub entries are cached because
4658    creating the stub name takes a bit of time.  */
4659
4660 static struct ppc_stub_hash_entry *
4661 ppc_get_stub_entry (const asection *input_section,
4662                     const asection *sym_sec,
4663                     struct ppc_link_hash_entry *h,
4664                     const Elf_Internal_Rela *rel,
4665                     struct ppc_link_hash_table *htab)
4666 {
4667   struct ppc_stub_hash_entry *stub_entry;
4668   struct map_stub *group;
4669
4670   /* If this input section is part of a group of sections sharing one
4671      stub section, then use the id of the first section in the group.
4672      Stub names need to include a section id, as there may well be
4673      more than one stub used to reach say, printf, and we need to
4674      distinguish between them.  */
4675   group = htab->sec_info[input_section->id].u.group;
4676   if (group == NULL)
4677     return NULL;
4678
4679   if (h != NULL && h->u.stub_cache != NULL
4680       && h->u.stub_cache->h == h
4681       && h->u.stub_cache->group == group)
4682     {
4683       stub_entry = h->u.stub_cache;
4684     }
4685   else
4686     {
4687       char *stub_name;
4688
4689       stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4690       if (stub_name == NULL)
4691         return NULL;
4692
4693       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4694                                          stub_name, FALSE, FALSE);
4695       if (h != NULL)
4696         h->u.stub_cache = stub_entry;
4697
4698       free (stub_name);
4699     }
4700
4701   return stub_entry;
4702 }
4703
4704 /* Add a new stub entry to the stub hash.  Not all fields of the new
4705    stub entry are initialised.  */
4706
4707 static struct ppc_stub_hash_entry *
4708 ppc_add_stub (const char *stub_name,
4709               asection *section,
4710               struct bfd_link_info *info)
4711 {
4712   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4713   struct map_stub *group;
4714   asection *link_sec;
4715   asection *stub_sec;
4716   struct ppc_stub_hash_entry *stub_entry;
4717
4718   group = htab->sec_info[section->id].u.group;
4719   link_sec = group->link_sec;
4720   stub_sec = group->stub_sec;
4721   if (stub_sec == NULL)
4722     {
4723       size_t namelen;
4724       bfd_size_type len;
4725       char *s_name;
4726
4727       namelen = strlen (link_sec->name);
4728       len = namelen + sizeof (STUB_SUFFIX);
4729       s_name = bfd_alloc (htab->params->stub_bfd, len);
4730       if (s_name == NULL)
4731         return NULL;
4732
4733       memcpy (s_name, link_sec->name, namelen);
4734       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4735       stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4736       if (stub_sec == NULL)
4737         return NULL;
4738       group->stub_sec = stub_sec;
4739     }
4740
4741   /* Enter this entry into the linker stub hash table.  */
4742   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4743                                      TRUE, FALSE);
4744   if (stub_entry == NULL)
4745     {
4746       /* xgettext:c-format */
4747       _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4748                           section->owner, stub_name);
4749       return NULL;
4750     }
4751
4752   stub_entry->group = group;
4753   stub_entry->stub_offset = 0;
4754   return stub_entry;
4755 }
4756
4757 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4758    not already done.  */
4759
4760 static bfd_boolean
4761 create_got_section (bfd *abfd, struct bfd_link_info *info)
4762 {
4763   asection *got, *relgot;
4764   flagword flags;
4765   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4766
4767   if (!is_ppc64_elf (abfd))
4768     return FALSE;
4769   if (htab == NULL)
4770     return FALSE;
4771
4772   if (!htab->elf.sgot
4773       && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4774     return FALSE;
4775
4776   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4777            | SEC_LINKER_CREATED);
4778
4779   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4780   if (!got
4781       || !bfd_set_section_alignment (abfd, got, 3))
4782     return FALSE;
4783
4784   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4785                                                flags | SEC_READONLY);
4786   if (!relgot
4787       || ! bfd_set_section_alignment (abfd, relgot, 3))
4788     return FALSE;
4789
4790   ppc64_elf_tdata (abfd)->got = got;
4791   ppc64_elf_tdata (abfd)->relgot = relgot;
4792   return TRUE;
4793 }
4794
4795 /* Follow indirect and warning symbol links.  */
4796
4797 static inline struct bfd_link_hash_entry *
4798 follow_link (struct bfd_link_hash_entry *h)
4799 {
4800   while (h->type == bfd_link_hash_indirect
4801          || h->type == bfd_link_hash_warning)
4802     h = h->u.i.link;
4803   return h;
4804 }
4805
4806 static inline struct elf_link_hash_entry *
4807 elf_follow_link (struct elf_link_hash_entry *h)
4808 {
4809   return (struct elf_link_hash_entry *) follow_link (&h->root);
4810 }
4811
4812 static inline struct ppc_link_hash_entry *
4813 ppc_follow_link (struct ppc_link_hash_entry *h)
4814 {
4815   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4816 }
4817
4818 /* Merge PLT info on FROM with that on TO.  */
4819
4820 static void
4821 move_plt_plist (struct ppc_link_hash_entry *from,
4822                 struct ppc_link_hash_entry *to)
4823 {
4824   if (from->elf.plt.plist != NULL)
4825     {
4826       if (to->elf.plt.plist != NULL)
4827         {
4828           struct plt_entry **entp;
4829           struct plt_entry *ent;
4830
4831           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4832             {
4833               struct plt_entry *dent;
4834
4835               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4836                 if (dent->addend == ent->addend)
4837                   {
4838                     dent->plt.refcount += ent->plt.refcount;
4839                     *entp = ent->next;
4840                     break;
4841                   }
4842               if (dent == NULL)
4843                 entp = &ent->next;
4844             }
4845           *entp = to->elf.plt.plist;
4846         }
4847
4848       to->elf.plt.plist = from->elf.plt.plist;
4849       from->elf.plt.plist = NULL;
4850     }
4851 }
4852
4853 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
4854
4855 static void
4856 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4857                                 struct elf_link_hash_entry *dir,
4858                                 struct elf_link_hash_entry *ind)
4859 {
4860   struct ppc_link_hash_entry *edir, *eind;
4861
4862   edir = (struct ppc_link_hash_entry *) dir;
4863   eind = (struct ppc_link_hash_entry *) ind;
4864
4865   edir->is_func |= eind->is_func;
4866   edir->is_func_descriptor |= eind->is_func_descriptor;
4867   edir->tls_mask |= eind->tls_mask;
4868   if (eind->oh != NULL)
4869     edir->oh = ppc_follow_link (eind->oh);
4870
4871   if (edir->elf.versioned != versioned_hidden)
4872     edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4873   edir->elf.ref_regular |= eind->elf.ref_regular;
4874   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4875   edir->elf.non_got_ref |= eind->elf.non_got_ref;
4876   edir->elf.needs_plt |= eind->elf.needs_plt;
4877   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4878
4879   /* If we were called to copy over info for a weak sym, don't copy
4880      dyn_relocs, plt/got info, or dynindx.  We used to copy dyn_relocs
4881      in order to simplify readonly_dynrelocs and save a field in the
4882      symbol hash entry, but that means dyn_relocs can't be used in any
4883      tests about a specific symbol, or affect other symbol flags which
4884      are then tested.  */
4885   if (eind->elf.root.type != bfd_link_hash_indirect)
4886     return;
4887
4888   /* Copy over any dynamic relocs we may have on the indirect sym.  */
4889   if (eind->dyn_relocs != NULL)
4890     {
4891       if (edir->dyn_relocs != NULL)
4892         {
4893           struct elf_dyn_relocs **pp;
4894           struct elf_dyn_relocs *p;
4895
4896           /* Add reloc counts against the indirect sym to the direct sym
4897              list.  Merge any entries against the same section.  */
4898           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4899             {
4900               struct elf_dyn_relocs *q;
4901
4902               for (q = edir->dyn_relocs; q != NULL; q = q->next)
4903                 if (q->sec == p->sec)
4904                   {
4905                     q->pc_count += p->pc_count;
4906                     q->count += p->count;
4907                     *pp = p->next;
4908                     break;
4909                   }
4910               if (q == NULL)
4911                 pp = &p->next;
4912             }
4913           *pp = edir->dyn_relocs;
4914         }
4915
4916       edir->dyn_relocs = eind->dyn_relocs;
4917       eind->dyn_relocs = NULL;
4918     }
4919
4920   /* Copy over got entries that we may have already seen to the
4921      symbol which just became indirect.  */
4922   if (eind->elf.got.glist != NULL)
4923     {
4924       if (edir->elf.got.glist != NULL)
4925         {
4926           struct got_entry **entp;
4927           struct got_entry *ent;
4928
4929           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4930             {
4931               struct got_entry *dent;
4932
4933               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4934                 if (dent->addend == ent->addend
4935                     && dent->owner == ent->owner
4936                     && dent->tls_type == ent->tls_type)
4937                   {
4938                     dent->got.refcount += ent->got.refcount;
4939                     *entp = ent->next;
4940                     break;
4941                   }
4942               if (dent == NULL)
4943                 entp = &ent->next;
4944             }
4945           *entp = edir->elf.got.glist;
4946         }
4947
4948       edir->elf.got.glist = eind->elf.got.glist;
4949       eind->elf.got.glist = NULL;
4950     }
4951
4952   /* And plt entries.  */
4953   move_plt_plist (eind, edir);
4954
4955   if (eind->elf.dynindx != -1)
4956     {
4957       if (edir->elf.dynindx != -1)
4958         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4959                                 edir->elf.dynstr_index);
4960       edir->elf.dynindx = eind->elf.dynindx;
4961       edir->elf.dynstr_index = eind->elf.dynstr_index;
4962       eind->elf.dynindx = -1;
4963       eind->elf.dynstr_index = 0;
4964     }
4965 }
4966
4967 /* Find the function descriptor hash entry from the given function code
4968    hash entry FH.  Link the entries via their OH fields.  */
4969
4970 static struct ppc_link_hash_entry *
4971 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4972 {
4973   struct ppc_link_hash_entry *fdh = fh->oh;
4974
4975   if (fdh == NULL)
4976     {
4977       const char *fd_name = fh->elf.root.root.string + 1;
4978
4979       fdh = (struct ppc_link_hash_entry *)
4980         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4981       if (fdh == NULL)
4982         return fdh;
4983
4984       fdh->is_func_descriptor = 1;
4985       fdh->oh = fh;
4986       fh->is_func = 1;
4987       fh->oh = fdh;
4988     }
4989
4990   fdh = ppc_follow_link (fdh);
4991   fdh->is_func_descriptor = 1;
4992   fdh->oh = fh;
4993   return fdh;
4994 }
4995
4996 /* Make a fake function descriptor sym for the undefined code sym FH.  */
4997
4998 static struct ppc_link_hash_entry *
4999 make_fdh (struct bfd_link_info *info,
5000           struct ppc_link_hash_entry *fh)
5001 {
5002   bfd *abfd = fh->elf.root.u.undef.abfd;
5003   struct bfd_link_hash_entry *bh = NULL;
5004   struct ppc_link_hash_entry *fdh;
5005   flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
5006                     ? BSF_WEAK
5007                     : BSF_GLOBAL);
5008
5009   if (!_bfd_generic_link_add_one_symbol (info, abfd,
5010                                          fh->elf.root.root.string + 1,
5011                                          flags, bfd_und_section_ptr, 0,
5012                                          NULL, FALSE, FALSE, &bh))
5013     return NULL;
5014
5015   fdh = (struct ppc_link_hash_entry *) bh;
5016   fdh->elf.non_elf = 0;
5017   fdh->fake = 1;
5018   fdh->is_func_descriptor = 1;
5019   fdh->oh = fh;
5020   fh->is_func = 1;
5021   fh->oh = fdh;
5022   return fdh;
5023 }
5024
5025 /* Fix function descriptor symbols defined in .opd sections to be
5026    function type.  */
5027
5028 static bfd_boolean
5029 ppc64_elf_add_symbol_hook (bfd *ibfd,
5030                            struct bfd_link_info *info,
5031                            Elf_Internal_Sym *isym,
5032                            const char **name,
5033                            flagword *flags ATTRIBUTE_UNUSED,
5034                            asection **sec,
5035                            bfd_vma *value)
5036 {
5037   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
5038       && (ibfd->flags & DYNAMIC) == 0
5039       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
5040     elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
5041
5042   if (*sec != NULL
5043       && strcmp ((*sec)->name, ".opd") == 0)
5044     {
5045       asection *code_sec;
5046
5047       if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
5048             || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
5049         isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
5050
5051       /* If the symbol is a function defined in .opd, and the function
5052          code is in a discarded group, let it appear to be undefined.  */
5053       if (!bfd_link_relocatable (info)
5054           && (*sec)->reloc_count != 0
5055           && opd_entry_value (*sec, *value, &code_sec, NULL,
5056                               FALSE) != (bfd_vma) -1
5057           && discarded_section (code_sec))
5058         {
5059           *sec = bfd_und_section_ptr;
5060           isym->st_shndx = SHN_UNDEF;
5061         }
5062     }
5063   else if (*sec != NULL
5064            && strcmp ((*sec)->name, ".toc") == 0
5065            && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
5066     {
5067       struct ppc_link_hash_table *htab = ppc_hash_table (info);
5068       if (htab != NULL)
5069         htab->params->object_in_toc = 1;
5070     }
5071
5072   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5073     {
5074       if (abiversion (ibfd) == 0)
5075         set_abiversion (ibfd, 2);
5076       else if (abiversion (ibfd) == 1)
5077         {
5078           _bfd_error_handler (_("symbol '%s' has invalid st_other"
5079                                 " for ABI version 1"), *name);
5080           bfd_set_error (bfd_error_bad_value);
5081           return FALSE;
5082         }
5083     }
5084
5085   return TRUE;
5086 }
5087
5088 /* Merge non-visibility st_other attributes: local entry point.  */
5089
5090 static void
5091 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5092                                   const Elf_Internal_Sym *isym,
5093                                   bfd_boolean definition,
5094                                   bfd_boolean dynamic)
5095 {
5096   if (definition && (!dynamic || !h->def_regular))
5097     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5098                 | ELF_ST_VISIBILITY (h->other));
5099 }
5100
5101 /* Hook called on merging a symbol.  We use this to clear "fake" since
5102    we now have a real symbol.  */
5103
5104 static bfd_boolean
5105 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5106                         const Elf_Internal_Sym *isym,
5107                         asection **psec ATTRIBUTE_UNUSED,
5108                         bfd_boolean newdef ATTRIBUTE_UNUSED,
5109                         bfd_boolean olddef ATTRIBUTE_UNUSED,
5110                         bfd *oldbfd ATTRIBUTE_UNUSED,
5111                         const asection *oldsec ATTRIBUTE_UNUSED)
5112 {
5113   ((struct ppc_link_hash_entry *) h)->fake = 0;
5114   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5115     ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
5116   return TRUE;
5117 }
5118
5119 /* This function makes an old ABI object reference to ".bar" cause the
5120    inclusion of a new ABI object archive that defines "bar".
5121    NAME is a symbol defined in an archive.  Return a symbol in the hash
5122    table that might be satisfied by the archive symbols.  */
5123
5124 static struct elf_link_hash_entry *
5125 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5126                                  struct bfd_link_info *info,
5127                                  const char *name)
5128 {
5129   struct elf_link_hash_entry *h;
5130   char *dot_name;
5131   size_t len;
5132
5133   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5134   if (h != NULL
5135       /* Don't return this sym if it is a fake function descriptor
5136          created by add_symbol_adjust.  */
5137       && !((struct ppc_link_hash_entry *) h)->fake)
5138     return h;
5139
5140   if (name[0] == '.')
5141     return h;
5142
5143   len = strlen (name);
5144   dot_name = bfd_alloc (abfd, len + 2);
5145   if (dot_name == NULL)
5146     return (struct elf_link_hash_entry *) -1;
5147   dot_name[0] = '.';
5148   memcpy (dot_name + 1, name, len + 1);
5149   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5150   bfd_release (abfd, dot_name);
5151   return h;
5152 }
5153
5154 /* This function satisfies all old ABI object references to ".bar" if a
5155    new ABI object defines "bar".  Well, at least, undefined dot symbols
5156    are made weak.  This stops later archive searches from including an
5157    object if we already have a function descriptor definition.  It also
5158    prevents the linker complaining about undefined symbols.
5159    We also check and correct mismatched symbol visibility here.  The
5160    most restrictive visibility of the function descriptor and the
5161    function entry symbol is used.  */
5162
5163 static bfd_boolean
5164 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5165 {
5166   struct ppc_link_hash_table *htab;
5167   struct ppc_link_hash_entry *fdh;
5168
5169   if (eh->elf.root.type == bfd_link_hash_warning)
5170     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5171
5172   if (eh->elf.root.type == bfd_link_hash_indirect)
5173     return TRUE;
5174
5175   if (eh->elf.root.root.string[0] != '.')
5176     abort ();
5177
5178   htab = ppc_hash_table (info);
5179   if (htab == NULL)
5180     return FALSE;
5181
5182   fdh = lookup_fdh (eh, htab);
5183   if (fdh == NULL
5184       && !bfd_link_relocatable (info)
5185       && (eh->elf.root.type == bfd_link_hash_undefined
5186           || eh->elf.root.type == bfd_link_hash_undefweak)
5187       && eh->elf.ref_regular)
5188     {
5189       /* Make an undefined function descriptor sym, in order to
5190          pull in an --as-needed shared lib.  Archives are handled
5191          elsewhere.  */
5192       fdh = make_fdh (info, eh);
5193       if (fdh == NULL)
5194         return FALSE;
5195     }
5196
5197   if (fdh != NULL)
5198     {
5199       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5200       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5201
5202       /* Make both descriptor and entry symbol have the most
5203          constraining visibility of either symbol.  */
5204       if (entry_vis < descr_vis)
5205         fdh->elf.other += entry_vis - descr_vis;
5206       else if (entry_vis > descr_vis)
5207         eh->elf.other += descr_vis - entry_vis;
5208
5209       /* Propagate reference flags from entry symbol to function
5210          descriptor symbol.  */
5211       fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
5212       fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
5213       fdh->elf.ref_regular |= eh->elf.ref_regular;
5214       fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5215
5216       if (!fdh->elf.forced_local
5217           && fdh->elf.dynindx == -1
5218           && fdh->elf.versioned != versioned_hidden
5219           && (bfd_link_dll (info)
5220               || fdh->elf.def_dynamic
5221               || fdh->elf.ref_dynamic)
5222           && (eh->elf.ref_regular
5223               || eh->elf.def_regular))
5224         {
5225           if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5226             return FALSE;
5227         }
5228     }
5229
5230   return TRUE;
5231 }
5232
5233 /* Set up opd section info and abiversion for IBFD, and process list
5234    of dot-symbols we made in link_hash_newfunc.  */
5235
5236 static bfd_boolean
5237 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5238 {
5239   struct ppc_link_hash_table *htab;
5240   struct ppc_link_hash_entry **p, *eh;
5241   asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5242
5243   if (opd != NULL && opd->size != 0)
5244     {
5245       BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5246       ppc64_elf_section_data (opd)->sec_type = sec_opd;
5247
5248       if (abiversion (ibfd) == 0)
5249         set_abiversion (ibfd, 1);
5250       else if (abiversion (ibfd) >= 2)
5251         {
5252           /* xgettext:c-format */
5253           _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
5254                               ibfd, abiversion (ibfd));
5255           bfd_set_error (bfd_error_bad_value);
5256           return FALSE;
5257         }
5258     }
5259
5260   if (is_ppc64_elf (info->output_bfd))
5261     {
5262       /* For input files without an explicit abiversion in e_flags
5263          we should have flagged any with symbol st_other bits set
5264          as ELFv1 and above flagged those with .opd as ELFv2.
5265          Set the output abiversion if not yet set, and for any input
5266          still ambiguous, take its abiversion from the output.
5267          Differences in ABI are reported later.  */
5268       if (abiversion (info->output_bfd) == 0)
5269         set_abiversion (info->output_bfd, abiversion (ibfd));
5270       else if (abiversion (ibfd) == 0)
5271         set_abiversion (ibfd, abiversion (info->output_bfd));
5272     }
5273
5274   htab = ppc_hash_table (info);
5275   if (htab == NULL)
5276     return TRUE;
5277
5278   if (opd != NULL && opd->size != 0
5279       && (ibfd->flags & DYNAMIC) == 0
5280       && (opd->flags & SEC_RELOC) != 0
5281       && opd->reloc_count != 0
5282       && !bfd_is_abs_section (opd->output_section)
5283       && info->gc_sections)
5284     {
5285       /* Garbage collection needs some extra help with .opd sections.
5286          We don't want to necessarily keep everything referenced by
5287          relocs in .opd, as that would keep all functions.  Instead,
5288          if we reference an .opd symbol (a function descriptor), we
5289          want to keep the function code symbol's section.  This is
5290          easy for global symbols, but for local syms we need to keep
5291          information about the associated function section.  */
5292       bfd_size_type amt;
5293       asection **opd_sym_map;
5294       Elf_Internal_Shdr *symtab_hdr;
5295       Elf_Internal_Rela *relocs, *rel_end, *rel;
5296
5297       amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5298       opd_sym_map = bfd_zalloc (ibfd, amt);
5299       if (opd_sym_map == NULL)
5300         return FALSE;
5301       ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5302       relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5303                                           info->keep_memory);
5304       if (relocs == NULL)
5305         return FALSE;
5306       symtab_hdr = &elf_symtab_hdr (ibfd);
5307       rel_end = relocs + opd->reloc_count - 1;
5308       for (rel = relocs; rel < rel_end; rel++)
5309         {
5310           enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5311           unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5312
5313           if (r_type == R_PPC64_ADDR64
5314               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5315               && r_symndx < symtab_hdr->sh_info)
5316             {
5317               Elf_Internal_Sym *isym;
5318               asection *s;
5319
5320               isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5321               if (isym == NULL)
5322                 {
5323                   if (elf_section_data (opd)->relocs != relocs)
5324                     free (relocs);
5325                   return FALSE;
5326                 }
5327
5328               s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5329               if (s != NULL && s != opd)
5330                 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5331             }
5332         }
5333       if (elf_section_data (opd)->relocs != relocs)
5334         free (relocs);
5335     }
5336
5337   p = &htab->dot_syms;
5338   while ((eh = *p) != NULL)
5339     {
5340       *p = NULL;
5341       if (&eh->elf == htab->elf.hgot)
5342         ;
5343       else if (htab->elf.hgot == NULL
5344                && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5345         htab->elf.hgot = &eh->elf;
5346       else if (abiversion (ibfd) <= 1)
5347         {
5348           htab->need_func_desc_adj = 1;
5349           if (!add_symbol_adjust (eh, info))
5350             return FALSE;
5351         }
5352       p = &eh->u.next_dot_sym;
5353     }
5354   return TRUE;
5355 }
5356
5357 /* Undo hash table changes when an --as-needed input file is determined
5358    not to be needed.  */
5359
5360 static bfd_boolean
5361 ppc64_elf_notice_as_needed (bfd *ibfd,
5362                             struct bfd_link_info *info,
5363                             enum notice_asneeded_action act)
5364 {
5365   if (act == notice_not_needed)
5366     {
5367       struct ppc_link_hash_table *htab = ppc_hash_table (info);
5368
5369       if (htab == NULL)
5370         return FALSE;
5371
5372       htab->dot_syms = NULL;
5373     }
5374   return _bfd_elf_notice_as_needed (ibfd, info, act);
5375 }
5376
5377 /* If --just-symbols against a final linked binary, then assume we need
5378    toc adjusting stubs when calling functions defined there.  */
5379
5380 static void
5381 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5382 {
5383   if ((sec->flags & SEC_CODE) != 0
5384       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5385       && is_ppc64_elf (sec->owner))
5386     {
5387       if (abiversion (sec->owner) >= 2
5388           || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5389         sec->has_toc_reloc = 1;
5390     }
5391   _bfd_elf_link_just_syms (sec, info);
5392 }
5393
5394 static struct plt_entry **
5395 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5396                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5397 {
5398   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5399   struct plt_entry **local_plt;
5400   unsigned char *local_got_tls_masks;
5401
5402   if (local_got_ents == NULL)
5403     {
5404       bfd_size_type size = symtab_hdr->sh_info;
5405
5406       size *= (sizeof (*local_got_ents)
5407                + sizeof (*local_plt)
5408                + sizeof (*local_got_tls_masks));
5409       local_got_ents = bfd_zalloc (abfd, size);
5410       if (local_got_ents == NULL)
5411         return NULL;
5412       elf_local_got_ents (abfd) = local_got_ents;
5413     }
5414
5415   if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
5416     {
5417       struct got_entry *ent;
5418
5419       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5420         if (ent->addend == r_addend
5421             && ent->owner == abfd
5422             && ent->tls_type == tls_type)
5423           break;
5424       if (ent == NULL)
5425         {
5426           bfd_size_type amt = sizeof (*ent);
5427           ent = bfd_alloc (abfd, amt);
5428           if (ent == NULL)
5429             return FALSE;
5430           ent->next = local_got_ents[r_symndx];
5431           ent->addend = r_addend;
5432           ent->owner = abfd;
5433           ent->tls_type = tls_type;
5434           ent->is_indirect = FALSE;
5435           ent->got.refcount = 0;
5436           local_got_ents[r_symndx] = ent;
5437         }
5438       ent->got.refcount += 1;
5439     }
5440
5441   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5442   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5443   local_got_tls_masks[r_symndx] |= tls_type & 0xff;
5444
5445   return local_plt + r_symndx;
5446 }
5447
5448 static bfd_boolean
5449 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5450 {
5451   struct plt_entry *ent;
5452
5453   for (ent = *plist; ent != NULL; ent = ent->next)
5454     if (ent->addend == addend)
5455       break;
5456   if (ent == NULL)
5457     {
5458       bfd_size_type amt = sizeof (*ent);
5459       ent = bfd_alloc (abfd, amt);
5460       if (ent == NULL)
5461         return FALSE;
5462       ent->next = *plist;
5463       ent->addend = addend;
5464       ent->plt.refcount = 0;
5465       *plist = ent;
5466     }
5467   ent->plt.refcount += 1;
5468   return TRUE;
5469 }
5470
5471 static bfd_boolean
5472 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5473 {
5474   return (r_type == R_PPC64_REL24
5475           || r_type == R_PPC64_REL14
5476           || r_type == R_PPC64_REL14_BRTAKEN
5477           || r_type == R_PPC64_REL14_BRNTAKEN
5478           || r_type == R_PPC64_ADDR24
5479           || r_type == R_PPC64_ADDR14
5480           || r_type == R_PPC64_ADDR14_BRTAKEN
5481           || r_type == R_PPC64_ADDR14_BRNTAKEN
5482           || r_type == R_PPC64_PLTCALL);
5483 }
5484
5485 /* Relocs on inline plt call sequence insns prior to the call.  */
5486
5487 static bfd_boolean
5488 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
5489 {
5490   return (r_type == R_PPC64_PLT16_HA
5491           || r_type == R_PPC64_PLT16_HI
5492           || r_type == R_PPC64_PLT16_LO
5493           || r_type == R_PPC64_PLT16_LO_DS
5494           || r_type == R_PPC64_PLTSEQ);
5495 }
5496
5497 /* Look through the relocs for a section during the first phase, and
5498    calculate needed space in the global offset table, procedure
5499    linkage table, and dynamic reloc sections.  */
5500
5501 static bfd_boolean
5502 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5503                         asection *sec, const Elf_Internal_Rela *relocs)
5504 {
5505   struct ppc_link_hash_table *htab;
5506   Elf_Internal_Shdr *symtab_hdr;
5507   struct elf_link_hash_entry **sym_hashes;
5508   const Elf_Internal_Rela *rel;
5509   const Elf_Internal_Rela *rel_end;
5510   asection *sreloc;
5511   struct elf_link_hash_entry *tga, *dottga;
5512   bfd_boolean is_opd;
5513
5514   if (bfd_link_relocatable (info))
5515     return TRUE;
5516
5517   /* Don't do anything special with non-loaded, non-alloced sections.
5518      In particular, any relocs in such sections should not affect GOT
5519      and PLT reference counting (ie. we don't allow them to create GOT
5520      or PLT entries), there's no possibility or desire to optimize TLS
5521      relocs, and there's not much point in propagating relocs to shared
5522      libs that the dynamic linker won't relocate.  */
5523   if ((sec->flags & SEC_ALLOC) == 0)
5524     return TRUE;
5525
5526   BFD_ASSERT (is_ppc64_elf (abfd));
5527
5528   htab = ppc_hash_table (info);
5529   if (htab == NULL)
5530     return FALSE;
5531
5532   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5533                               FALSE, FALSE, TRUE);
5534   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5535                                  FALSE, FALSE, TRUE);
5536   symtab_hdr = &elf_symtab_hdr (abfd);
5537   sym_hashes = elf_sym_hashes (abfd);
5538   sreloc = NULL;
5539   is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5540   rel_end = relocs + sec->reloc_count;
5541   for (rel = relocs; rel < rel_end; rel++)
5542     {
5543       unsigned long r_symndx;
5544       struct elf_link_hash_entry *h;
5545       enum elf_ppc64_reloc_type r_type;
5546       int tls_type;
5547       struct _ppc64_elf_section_data *ppc64_sec;
5548       struct plt_entry **ifunc, **plt_list;
5549
5550       r_symndx = ELF64_R_SYM (rel->r_info);
5551       if (r_symndx < symtab_hdr->sh_info)
5552         h = NULL;
5553       else
5554         {
5555           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5556           h = elf_follow_link (h);
5557
5558           if (h == htab->elf.hgot)
5559             sec->has_toc_reloc = 1;
5560         }
5561
5562       tls_type = 0;
5563       ifunc = NULL;
5564       if (h != NULL)
5565         {
5566           if (h->type == STT_GNU_IFUNC)
5567             {
5568               h->needs_plt = 1;
5569               ifunc = &h->plt.plist;
5570             }
5571         }
5572       else
5573         {
5574           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5575                                                           abfd, r_symndx);
5576           if (isym == NULL)
5577             return FALSE;
5578
5579           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5580             {
5581               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5582                                              rel->r_addend,
5583                                              NON_GOT | PLT_IFUNC);
5584               if (ifunc == NULL)
5585                 return FALSE;
5586             }
5587         }
5588
5589       r_type = ELF64_R_TYPE (rel->r_info);
5590       switch (r_type)
5591         {
5592         case R_PPC64_TLSGD:
5593         case R_PPC64_TLSLD:
5594           /* These special tls relocs tie a call to __tls_get_addr with
5595              its parameter symbol.  */
5596           if (h != NULL)
5597             ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
5598           else
5599             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5600                                         rel->r_addend,
5601                                         NON_GOT | TLS_TLS | TLS_MARK))
5602               return FALSE;
5603           sec->has_tls_reloc = 1;
5604           break;
5605
5606         case R_PPC64_GOT_TLSLD16:
5607         case R_PPC64_GOT_TLSLD16_LO:
5608         case R_PPC64_GOT_TLSLD16_HI:
5609         case R_PPC64_GOT_TLSLD16_HA:
5610           tls_type = TLS_TLS | TLS_LD;
5611           goto dogottls;
5612
5613         case R_PPC64_GOT_TLSGD16:
5614         case R_PPC64_GOT_TLSGD16_LO:
5615         case R_PPC64_GOT_TLSGD16_HI:
5616         case R_PPC64_GOT_TLSGD16_HA:
5617           tls_type = TLS_TLS | TLS_GD;
5618           goto dogottls;
5619
5620         case R_PPC64_GOT_TPREL16_DS:
5621         case R_PPC64_GOT_TPREL16_LO_DS:
5622         case R_PPC64_GOT_TPREL16_HI:
5623         case R_PPC64_GOT_TPREL16_HA:
5624           if (bfd_link_dll (info))
5625             info->flags |= DF_STATIC_TLS;
5626           tls_type = TLS_TLS | TLS_TPREL;
5627           goto dogottls;
5628
5629         case R_PPC64_GOT_DTPREL16_DS:
5630         case R_PPC64_GOT_DTPREL16_LO_DS:
5631         case R_PPC64_GOT_DTPREL16_HI:
5632         case R_PPC64_GOT_DTPREL16_HA:
5633           tls_type = TLS_TLS | TLS_DTPREL;
5634         dogottls:
5635           sec->has_tls_reloc = 1;
5636           /* Fall through */
5637
5638         case R_PPC64_GOT16:
5639         case R_PPC64_GOT16_DS:
5640         case R_PPC64_GOT16_HA:
5641         case R_PPC64_GOT16_HI:
5642         case R_PPC64_GOT16_LO:
5643         case R_PPC64_GOT16_LO_DS:
5644           /* This symbol requires a global offset table entry.  */
5645           sec->has_toc_reloc = 1;
5646           if (r_type == R_PPC64_GOT_TLSLD16
5647               || r_type == R_PPC64_GOT_TLSGD16
5648               || r_type == R_PPC64_GOT_TPREL16_DS
5649               || r_type == R_PPC64_GOT_DTPREL16_DS
5650               || r_type == R_PPC64_GOT16
5651               || r_type == R_PPC64_GOT16_DS)
5652             {
5653               htab->do_multi_toc = 1;
5654               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5655             }
5656
5657           if (ppc64_elf_tdata (abfd)->got == NULL
5658               && !create_got_section (abfd, info))
5659             return FALSE;
5660
5661           if (h != NULL)
5662             {
5663               struct ppc_link_hash_entry *eh;
5664               struct got_entry *ent;
5665
5666               eh = (struct ppc_link_hash_entry *) h;
5667               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5668                 if (ent->addend == rel->r_addend
5669                     && ent->owner == abfd
5670                     && ent->tls_type == tls_type)
5671                   break;
5672               if (ent == NULL)
5673                 {
5674                   bfd_size_type amt = sizeof (*ent);
5675                   ent = bfd_alloc (abfd, amt);
5676                   if (ent == NULL)
5677                     return FALSE;
5678                   ent->next = eh->elf.got.glist;
5679                   ent->addend = rel->r_addend;
5680                   ent->owner = abfd;
5681                   ent->tls_type = tls_type;
5682                   ent->is_indirect = FALSE;
5683                   ent->got.refcount = 0;
5684                   eh->elf.got.glist = ent;
5685                 }
5686               ent->got.refcount += 1;
5687               eh->tls_mask |= tls_type;
5688             }
5689           else
5690             /* This is a global offset table entry for a local symbol.  */
5691             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5692                                         rel->r_addend, tls_type))
5693               return FALSE;
5694
5695           /* We may also need a plt entry if the symbol turns out to be
5696              an ifunc.  */
5697           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5698             {
5699               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5700                 return FALSE;
5701             }
5702           break;
5703
5704         case R_PPC64_PLT16_HA:
5705         case R_PPC64_PLT16_HI:
5706         case R_PPC64_PLT16_LO:
5707         case R_PPC64_PLT16_LO_DS:
5708         case R_PPC64_PLT32:
5709         case R_PPC64_PLT64:
5710           /* This symbol requires a procedure linkage table entry.  */
5711           plt_list = ifunc;
5712           if (h != NULL)
5713             {
5714               h->needs_plt = 1;
5715               if (h->root.root.string[0] == '.'
5716                   && h->root.root.string[1] != '\0')
5717                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5718               ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
5719               plt_list = &h->plt.plist;
5720             }
5721           if (plt_list == NULL)
5722             plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5723                                               rel->r_addend,
5724                                               NON_GOT | PLT_KEEP);
5725           if (!update_plt_info (abfd, plt_list, rel->r_addend))
5726             return FALSE;
5727           break;
5728
5729           /* The following relocations don't need to propagate the
5730              relocation if linking a shared object since they are
5731              section relative.  */
5732         case R_PPC64_SECTOFF:
5733         case R_PPC64_SECTOFF_LO:
5734         case R_PPC64_SECTOFF_HI:
5735         case R_PPC64_SECTOFF_HA:
5736         case R_PPC64_SECTOFF_DS:
5737         case R_PPC64_SECTOFF_LO_DS:
5738         case R_PPC64_DTPREL16:
5739         case R_PPC64_DTPREL16_LO:
5740         case R_PPC64_DTPREL16_HI:
5741         case R_PPC64_DTPREL16_HA:
5742         case R_PPC64_DTPREL16_DS:
5743         case R_PPC64_DTPREL16_LO_DS:
5744         case R_PPC64_DTPREL16_HIGH:
5745         case R_PPC64_DTPREL16_HIGHA:
5746         case R_PPC64_DTPREL16_HIGHER:
5747         case R_PPC64_DTPREL16_HIGHERA:
5748         case R_PPC64_DTPREL16_HIGHEST:
5749         case R_PPC64_DTPREL16_HIGHESTA:
5750           break;
5751
5752           /* Nor do these.  */
5753         case R_PPC64_REL16:
5754         case R_PPC64_REL16_LO:
5755         case R_PPC64_REL16_HI:
5756         case R_PPC64_REL16_HA:
5757         case R_PPC64_REL16DX_HA:
5758           break;
5759
5760           /* Not supported as a dynamic relocation.  */
5761         case R_PPC64_ADDR64_LOCAL:
5762           if (bfd_link_pic (info))
5763             {
5764               if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5765                 ppc_howto_init ();
5766               /* xgettext:c-format */
5767               info->callbacks->einfo (_("%H: %s reloc unsupported "
5768                                         "in shared libraries and PIEs\n"),
5769                                       abfd, sec, rel->r_offset,
5770                                       ppc64_elf_howto_table[r_type]->name);
5771               bfd_set_error (bfd_error_bad_value);
5772               return FALSE;
5773             }
5774           break;
5775
5776         case R_PPC64_TOC16:
5777         case R_PPC64_TOC16_DS:
5778           htab->do_multi_toc = 1;
5779           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5780           /* Fall through.  */
5781         case R_PPC64_TOC16_LO:
5782         case R_PPC64_TOC16_HI:
5783         case R_PPC64_TOC16_HA:
5784         case R_PPC64_TOC16_LO_DS:
5785           sec->has_toc_reloc = 1;
5786           break;
5787
5788           /* Marker reloc.  */
5789         case R_PPC64_ENTRY:
5790           break;
5791
5792           /* This relocation describes the C++ object vtable hierarchy.
5793              Reconstruct it for later use during GC.  */
5794         case R_PPC64_GNU_VTINHERIT:
5795           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5796             return FALSE;
5797           break;
5798
5799           /* This relocation describes which C++ vtable entries are actually
5800              used.  Record for later use during GC.  */
5801         case R_PPC64_GNU_VTENTRY:
5802           BFD_ASSERT (h != NULL);
5803           if (h != NULL
5804               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5805             return FALSE;
5806           break;
5807
5808         case R_PPC64_REL14:
5809         case R_PPC64_REL14_BRTAKEN:
5810         case R_PPC64_REL14_BRNTAKEN:
5811           {
5812             asection *dest = NULL;
5813
5814             /* Heuristic: If jumping outside our section, chances are
5815                we are going to need a stub.  */
5816             if (h != NULL)
5817               {
5818                 /* If the sym is weak it may be overridden later, so
5819                    don't assume we know where a weak sym lives.  */
5820                 if (h->root.type == bfd_link_hash_defined)
5821                   dest = h->root.u.def.section;
5822               }
5823             else
5824               {
5825                 Elf_Internal_Sym *isym;
5826
5827                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5828                                               abfd, r_symndx);
5829                 if (isym == NULL)
5830                   return FALSE;
5831
5832                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5833               }
5834
5835             if (dest != sec)
5836               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5837           }
5838           goto rel24;
5839
5840         case R_PPC64_PLTCALL:
5841           ppc64_elf_section_data (sec)->has_pltcall = 1;
5842           /* Fall through.  */
5843
5844         case R_PPC64_REL24:
5845         rel24:
5846           plt_list = ifunc;
5847           if (h != NULL)
5848             {
5849               h->needs_plt = 1;
5850               if (h->root.root.string[0] == '.'
5851                   && h->root.root.string[1] != '\0')
5852                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5853
5854               if (h == tga || h == dottga)
5855                 {
5856                   sec->has_tls_reloc = 1;
5857                   if (rel != relocs
5858                       && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5859                           || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5860                     /* We have a new-style __tls_get_addr call with
5861                        a marker reloc.  */
5862                     ;
5863                   else
5864                     /* Mark this section as having an old-style call.  */
5865                     sec->has_tls_get_addr_call = 1;
5866                 }
5867               plt_list = &h->plt.plist;
5868             }
5869
5870           /* We may need a .plt entry if the function this reloc
5871              refers to is in a shared lib.  */
5872           if (plt_list
5873               && !update_plt_info (abfd, plt_list, rel->r_addend))
5874             return FALSE;
5875           break;
5876
5877         case R_PPC64_ADDR14:
5878         case R_PPC64_ADDR14_BRNTAKEN:
5879         case R_PPC64_ADDR14_BRTAKEN:
5880         case R_PPC64_ADDR24:
5881           goto dodyn;
5882
5883         case R_PPC64_TPREL64:
5884           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5885           if (bfd_link_dll (info))
5886             info->flags |= DF_STATIC_TLS;
5887           goto dotlstoc;
5888
5889         case R_PPC64_DTPMOD64:
5890           if (rel + 1 < rel_end
5891               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5892               && rel[1].r_offset == rel->r_offset + 8)
5893             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5894           else
5895             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5896           goto dotlstoc;
5897
5898         case R_PPC64_DTPREL64:
5899           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5900           if (rel != relocs
5901               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5902               && rel[-1].r_offset == rel->r_offset - 8)
5903             /* This is the second reloc of a dtpmod, dtprel pair.
5904                Don't mark with TLS_DTPREL.  */
5905             goto dodyn;
5906
5907         dotlstoc:
5908           sec->has_tls_reloc = 1;
5909           if (h != NULL)
5910             {
5911               struct ppc_link_hash_entry *eh;
5912               eh = (struct ppc_link_hash_entry *) h;
5913               eh->tls_mask |= tls_type;
5914             }
5915           else
5916             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5917                                         rel->r_addend, tls_type))
5918               return FALSE;
5919
5920           ppc64_sec = ppc64_elf_section_data (sec);
5921           if (ppc64_sec->sec_type != sec_toc)
5922             {
5923               bfd_size_type amt;
5924
5925               /* One extra to simplify get_tls_mask.  */
5926               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5927               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5928               if (ppc64_sec->u.toc.symndx == NULL)
5929                 return FALSE;
5930               amt = sec->size * sizeof (bfd_vma) / 8;
5931               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5932               if (ppc64_sec->u.toc.add == NULL)
5933                 return FALSE;
5934               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5935               ppc64_sec->sec_type = sec_toc;
5936             }
5937           BFD_ASSERT (rel->r_offset % 8 == 0);
5938           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5939           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5940
5941           /* Mark the second slot of a GD or LD entry.
5942              -1 to indicate GD and -2 to indicate LD.  */
5943           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5944             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5945           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5946             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5947           goto dodyn;
5948
5949         case R_PPC64_TPREL16:
5950         case R_PPC64_TPREL16_LO:
5951         case R_PPC64_TPREL16_HI:
5952         case R_PPC64_TPREL16_HA:
5953         case R_PPC64_TPREL16_DS:
5954         case R_PPC64_TPREL16_LO_DS:
5955         case R_PPC64_TPREL16_HIGH:
5956         case R_PPC64_TPREL16_HIGHA:
5957         case R_PPC64_TPREL16_HIGHER:
5958         case R_PPC64_TPREL16_HIGHERA:
5959         case R_PPC64_TPREL16_HIGHEST:
5960         case R_PPC64_TPREL16_HIGHESTA:
5961           if (bfd_link_dll (info))
5962             info->flags |= DF_STATIC_TLS;
5963           goto dodyn;
5964
5965         case R_PPC64_ADDR64:
5966           if (is_opd
5967               && rel + 1 < rel_end
5968               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5969             {
5970               if (h != NULL)
5971                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5972             }
5973           /* Fall through.  */
5974
5975         case R_PPC64_ADDR16:
5976         case R_PPC64_ADDR16_DS:
5977         case R_PPC64_ADDR16_HA:
5978         case R_PPC64_ADDR16_HI:
5979         case R_PPC64_ADDR16_HIGH:
5980         case R_PPC64_ADDR16_HIGHA:
5981         case R_PPC64_ADDR16_HIGHER:
5982         case R_PPC64_ADDR16_HIGHERA:
5983         case R_PPC64_ADDR16_HIGHEST:
5984         case R_PPC64_ADDR16_HIGHESTA:
5985         case R_PPC64_ADDR16_LO:
5986         case R_PPC64_ADDR16_LO_DS:
5987           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5988               && rel->r_addend == 0)
5989             {
5990               /* We may need a .plt entry if this reloc refers to a
5991                  function in a shared lib.  */
5992               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5993                 return FALSE;
5994               h->pointer_equality_needed = 1;
5995             }
5996           /* Fall through.  */
5997
5998         case R_PPC64_REL30:
5999         case R_PPC64_REL32:
6000         case R_PPC64_REL64:
6001         case R_PPC64_ADDR32:
6002         case R_PPC64_UADDR16:
6003         case R_PPC64_UADDR32:
6004         case R_PPC64_UADDR64:
6005         case R_PPC64_TOC:
6006           if (h != NULL && !bfd_link_pic (info))
6007             /* We may need a copy reloc.  */
6008             h->non_got_ref = 1;
6009
6010           /* Don't propagate .opd relocs.  */
6011           if (NO_OPD_RELOCS && is_opd)
6012             break;
6013
6014           /* If we are creating a shared library, and this is a reloc
6015              against a global symbol, or a non PC relative reloc
6016              against a local symbol, then we need to copy the reloc
6017              into the shared library.  However, if we are linking with
6018              -Bsymbolic, we do not need to copy a reloc against a
6019              global symbol which is defined in an object we are
6020              including in the link (i.e., DEF_REGULAR is set).  At
6021              this point we have not seen all the input files, so it is
6022              possible that DEF_REGULAR is not set now but will be set
6023              later (it is never cleared).  In case of a weak definition,
6024              DEF_REGULAR may be cleared later by a strong definition in
6025              a shared library.  We account for that possibility below by
6026              storing information in the dyn_relocs field of the hash
6027              table entry.  A similar situation occurs when creating
6028              shared libraries and symbol visibility changes render the
6029              symbol local.
6030
6031              If on the other hand, we are creating an executable, we
6032              may need to keep relocations for symbols satisfied by a
6033              dynamic library if we manage to avoid copy relocs for the
6034              symbol.  */
6035         dodyn:
6036           if ((bfd_link_pic (info)
6037                && (must_be_dyn_reloc (info, r_type)
6038                    || (h != NULL
6039                        && (!SYMBOLIC_BIND (info, h)
6040                            || h->root.type == bfd_link_hash_defweak
6041                            || !h->def_regular))))
6042               || (ELIMINATE_COPY_RELOCS
6043                   && !bfd_link_pic (info)
6044                   && h != NULL
6045                   && (h->root.type == bfd_link_hash_defweak
6046                       || !h->def_regular))
6047               || (!bfd_link_pic (info)
6048                   && ifunc != NULL))
6049             {
6050               /* We must copy these reloc types into the output file.
6051                  Create a reloc section in dynobj and make room for
6052                  this reloc.  */
6053               if (sreloc == NULL)
6054                 {
6055                   sreloc = _bfd_elf_make_dynamic_reloc_section
6056                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
6057
6058                   if (sreloc == NULL)
6059                     return FALSE;
6060                 }
6061
6062               /* If this is a global symbol, we count the number of
6063                  relocations we need for this symbol.  */
6064               if (h != NULL)
6065                 {
6066                   struct elf_dyn_relocs *p;
6067                   struct elf_dyn_relocs **head;
6068
6069                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6070                   p = *head;
6071                   if (p == NULL || p->sec != sec)
6072                     {
6073                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6074                       if (p == NULL)
6075                         return FALSE;
6076                       p->next = *head;
6077                       *head = p;
6078                       p->sec = sec;
6079                       p->count = 0;
6080                       p->pc_count = 0;
6081                     }
6082                   p->count += 1;
6083                   if (!must_be_dyn_reloc (info, r_type))
6084                     p->pc_count += 1;
6085                 }
6086               else
6087                 {
6088                   /* Track dynamic relocs needed for local syms too.
6089                      We really need local syms available to do this
6090                      easily.  Oh well.  */
6091                   struct ppc_dyn_relocs *p;
6092                   struct ppc_dyn_relocs **head;
6093                   bfd_boolean is_ifunc;
6094                   asection *s;
6095                   void *vpp;
6096                   Elf_Internal_Sym *isym;
6097
6098                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6099                                                 abfd, r_symndx);
6100                   if (isym == NULL)
6101                     return FALSE;
6102
6103                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6104                   if (s == NULL)
6105                     s = sec;
6106
6107                   vpp = &elf_section_data (s)->local_dynrel;
6108                   head = (struct ppc_dyn_relocs **) vpp;
6109                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6110                   p = *head;
6111                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6112                     p = p->next;
6113                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6114                     {
6115                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6116                       if (p == NULL)
6117                         return FALSE;
6118                       p->next = *head;
6119                       *head = p;
6120                       p->sec = sec;
6121                       p->ifunc = is_ifunc;
6122                       p->count = 0;
6123                     }
6124                   p->count += 1;
6125                 }
6126             }
6127           break;
6128
6129         default:
6130           break;
6131         }
6132     }
6133
6134   return TRUE;
6135 }
6136
6137 /* Merge backend specific data from an object file to the output
6138    object file when linking.  */
6139
6140 static bfd_boolean
6141 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6142 {
6143   bfd *obfd = info->output_bfd;
6144   unsigned long iflags, oflags;
6145
6146   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6147     return TRUE;
6148
6149   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6150     return TRUE;
6151
6152   if (!_bfd_generic_verify_endian_match (ibfd, info))
6153     return FALSE;
6154
6155   iflags = elf_elfheader (ibfd)->e_flags;
6156   oflags = elf_elfheader (obfd)->e_flags;
6157
6158   if (iflags & ~EF_PPC64_ABI)
6159     {
6160       _bfd_error_handler
6161         /* xgettext:c-format */
6162         (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
6163       bfd_set_error (bfd_error_bad_value);
6164       return FALSE;
6165     }
6166   else if (iflags != oflags && iflags != 0)
6167     {
6168       _bfd_error_handler
6169         /* xgettext:c-format */
6170         (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
6171          ibfd, iflags, oflags);
6172       bfd_set_error (bfd_error_bad_value);
6173       return FALSE;
6174     }
6175
6176   _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
6177
6178   /* Merge Tag_compatibility attributes and any common GNU ones.  */
6179   _bfd_elf_merge_object_attributes (ibfd, info);
6180
6181   return TRUE;
6182 }
6183
6184 static bfd_boolean
6185 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6186 {
6187   /* Print normal ELF private data.  */
6188   _bfd_elf_print_private_bfd_data (abfd, ptr);
6189
6190   if (elf_elfheader (abfd)->e_flags != 0)
6191     {
6192       FILE *file = ptr;
6193
6194       fprintf (file, _("private flags = 0x%lx:"),
6195                elf_elfheader (abfd)->e_flags);
6196
6197       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6198         fprintf (file, _(" [abiv%ld]"),
6199                  elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6200       fputc ('\n', file);
6201     }
6202
6203   return TRUE;
6204 }
6205
6206 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
6207    of the code entry point, and its section, which must be in the same
6208    object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
6209
6210 static bfd_vma
6211 opd_entry_value (asection *opd_sec,
6212                  bfd_vma offset,
6213                  asection **code_sec,
6214                  bfd_vma *code_off,
6215                  bfd_boolean in_code_sec)
6216 {
6217   bfd *opd_bfd = opd_sec->owner;
6218   Elf_Internal_Rela *relocs;
6219   Elf_Internal_Rela *lo, *hi, *look;
6220   bfd_vma val;
6221
6222   /* No relocs implies we are linking a --just-symbols object, or looking
6223      at a final linked executable with addr2line or somesuch.  */
6224   if (opd_sec->reloc_count == 0)
6225     {
6226       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6227
6228       if (contents == NULL)
6229         {
6230           if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6231             return (bfd_vma) -1;
6232           ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6233         }
6234
6235       /* PR 17512: file: 64b9dfbb.  */
6236       if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6237         return (bfd_vma) -1;
6238
6239       val = bfd_get_64 (opd_bfd, contents + offset);
6240       if (code_sec != NULL)
6241         {
6242           asection *sec, *likely = NULL;
6243
6244           if (in_code_sec)
6245             {
6246               sec = *code_sec;
6247               if (sec->vma <= val
6248                   && val < sec->vma + sec->size)
6249                 likely = sec;
6250               else
6251                 val = -1;
6252             }
6253           else
6254             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6255               if (sec->vma <= val
6256                   && (sec->flags & SEC_LOAD) != 0
6257                   && (sec->flags & SEC_ALLOC) != 0)
6258                 likely = sec;
6259           if (likely != NULL)
6260             {
6261               *code_sec = likely;
6262               if (code_off != NULL)
6263                 *code_off = val - likely->vma;
6264             }
6265         }
6266       return val;
6267     }
6268
6269   BFD_ASSERT (is_ppc64_elf (opd_bfd));
6270
6271   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6272   if (relocs == NULL)
6273     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6274   /* PR 17512: file: df8e1fd6.  */
6275   if (relocs == NULL)
6276     return (bfd_vma) -1;
6277
6278   /* Go find the opd reloc at the sym address.  */
6279   lo = relocs;
6280   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6281   val = (bfd_vma) -1;
6282   while (lo < hi)
6283     {
6284       look = lo + (hi - lo) / 2;
6285       if (look->r_offset < offset)
6286         lo = look + 1;
6287       else if (look->r_offset > offset)
6288         hi = look;
6289       else
6290         {
6291           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6292
6293           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6294               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6295             {
6296               unsigned long symndx = ELF64_R_SYM (look->r_info);
6297               asection *sec = NULL;
6298
6299               if (symndx >= symtab_hdr->sh_info
6300                   && elf_sym_hashes (opd_bfd) != NULL)
6301                 {
6302                   struct elf_link_hash_entry **sym_hashes;
6303                   struct elf_link_hash_entry *rh;
6304
6305                   sym_hashes = elf_sym_hashes (opd_bfd);
6306                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
6307                   if (rh != NULL)
6308                     {
6309                       rh = elf_follow_link (rh);
6310                       if (rh->root.type != bfd_link_hash_defined
6311                           && rh->root.type != bfd_link_hash_defweak)
6312                         break;
6313                       if (rh->root.u.def.section->owner == opd_bfd)
6314                         {
6315                           val = rh->root.u.def.value;
6316                           sec = rh->root.u.def.section;
6317                         }
6318                     }
6319                 }
6320
6321               if (sec == NULL)
6322                 {
6323                   Elf_Internal_Sym *sym;
6324
6325                   if (symndx < symtab_hdr->sh_info)
6326                     {
6327                       sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6328                       if (sym == NULL)
6329                         {
6330                           size_t symcnt = symtab_hdr->sh_info;
6331                           sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6332                                                       symcnt, 0,
6333                                                       NULL, NULL, NULL);
6334                           if (sym == NULL)
6335                             break;
6336                           symtab_hdr->contents = (bfd_byte *) sym;
6337                         }
6338                       sym += symndx;
6339                     }
6340                   else
6341                     {
6342                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6343                                                   1, symndx,
6344                                                   NULL, NULL, NULL);
6345                       if (sym == NULL)
6346                         break;
6347                     }
6348                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6349                   if (sec == NULL)
6350                     break;
6351                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6352                   val = sym->st_value;
6353                 }
6354
6355               val += look->r_addend;
6356               if (code_off != NULL)
6357                 *code_off = val;
6358               if (code_sec != NULL)
6359                 {
6360                   if (in_code_sec && *code_sec != sec)
6361                     return -1;
6362                   else
6363                     *code_sec = sec;
6364                 }
6365               if (sec->output_section != NULL)
6366                 val += sec->output_section->vma + sec->output_offset;
6367             }
6368           break;
6369         }
6370     }
6371
6372   return val;
6373 }
6374
6375 /* If the ELF symbol SYM might be a function in SEC, return the
6376    function size and set *CODE_OFF to the function's entry point,
6377    otherwise return zero.  */
6378
6379 static bfd_size_type
6380 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6381                               bfd_vma *code_off)
6382 {
6383   bfd_size_type size;
6384
6385   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6386                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6387     return 0;
6388
6389   size = 0;
6390   if (!(sym->flags & BSF_SYNTHETIC))
6391     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6392
6393   if (strcmp (sym->section->name, ".opd") == 0)
6394     {
6395       struct _opd_sec_data *opd = get_opd_info (sym->section);
6396       bfd_vma symval = sym->value;
6397
6398       if (opd != NULL
6399           && opd->adjust != NULL
6400           && elf_section_data (sym->section)->relocs != NULL)
6401         {
6402           /* opd_entry_value will use cached relocs that have been
6403              adjusted, but with raw symbols.  That means both local
6404              and global symbols need adjusting.  */
6405           long adjust = opd->adjust[OPD_NDX (symval)];
6406           if (adjust == -1)
6407             return 0;
6408           symval += adjust;
6409         }
6410
6411       if (opd_entry_value (sym->section, symval,
6412                            &sec, code_off, TRUE) == (bfd_vma) -1)
6413         return 0;
6414       /* An old ABI binary with dot-syms has a size of 24 on the .opd
6415          symbol.  This size has nothing to do with the code size of the
6416          function, which is what we're supposed to return, but the
6417          code size isn't available without looking up the dot-sym.
6418          However, doing that would be a waste of time particularly
6419          since elf_find_function will look at the dot-sym anyway.
6420          Now, elf_find_function will keep the largest size of any
6421          function sym found at the code address of interest, so return
6422          1 here to avoid it incorrectly caching a larger function size
6423          for a small function.  This does mean we return the wrong
6424          size for a new-ABI function of size 24, but all that does is
6425          disable caching for such functions.  */
6426       if (size == 24)
6427         size = 1;
6428     }
6429   else
6430     {
6431       if (sym->section != sec)
6432         return 0;
6433       *code_off = sym->value;
6434     }
6435   if (size == 0)
6436     size = 1;
6437   return size;
6438 }
6439
6440 /* Return true if symbol is a strong function defined in an ELFv2
6441    object with st_other localentry bits of zero, ie. its local entry
6442    point coincides with its global entry point.  */
6443
6444 static bfd_boolean
6445 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6446 {
6447   return (h != NULL
6448           && h->type == STT_FUNC
6449           && h->root.type == bfd_link_hash_defined
6450           && (STO_PPC64_LOCAL_MASK & h->other) == 0
6451           && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
6452           && is_ppc64_elf (h->root.u.def.section->owner)
6453           && abiversion (h->root.u.def.section->owner) >= 2);
6454 }
6455
6456 /* Return true if symbol is defined in a regular object file.  */
6457
6458 static bfd_boolean
6459 is_static_defined (struct elf_link_hash_entry *h)
6460 {
6461   return ((h->root.type == bfd_link_hash_defined
6462            || h->root.type == bfd_link_hash_defweak)
6463           && h->root.u.def.section != NULL
6464           && h->root.u.def.section->output_section != NULL);
6465 }
6466
6467 /* If FDH is a function descriptor symbol, return the associated code
6468    entry symbol if it is defined.  Return NULL otherwise.  */
6469
6470 static struct ppc_link_hash_entry *
6471 defined_code_entry (struct ppc_link_hash_entry *fdh)
6472 {
6473   if (fdh->is_func_descriptor)
6474     {
6475       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6476       if (fh->elf.root.type == bfd_link_hash_defined
6477           || fh->elf.root.type == bfd_link_hash_defweak)
6478         return fh;
6479     }
6480   return NULL;
6481 }
6482
6483 /* If FH is a function code entry symbol, return the associated
6484    function descriptor symbol if it is defined.  Return NULL otherwise.  */
6485
6486 static struct ppc_link_hash_entry *
6487 defined_func_desc (struct ppc_link_hash_entry *fh)
6488 {
6489   if (fh->oh != NULL
6490       && fh->oh->is_func_descriptor)
6491     {
6492       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6493       if (fdh->elf.root.type == bfd_link_hash_defined
6494           || fdh->elf.root.type == bfd_link_hash_defweak)
6495         return fdh;
6496     }
6497   return NULL;
6498 }
6499
6500 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6501
6502 /* Garbage collect sections, after first dealing with dot-symbols.  */
6503
6504 static bfd_boolean
6505 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6506 {
6507   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6508
6509   if (htab != NULL && htab->need_func_desc_adj)
6510     {
6511       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6512       htab->need_func_desc_adj = 0;
6513     }
6514   return bfd_elf_gc_sections (abfd, info);
6515 }
6516
6517 /* Mark all our entry sym sections, both opd and code section.  */
6518
6519 static void
6520 ppc64_elf_gc_keep (struct bfd_link_info *info)
6521 {
6522   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6523   struct bfd_sym_chain *sym;
6524
6525   if (htab == NULL)
6526     return;
6527
6528   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6529     {
6530       struct ppc_link_hash_entry *eh, *fh;
6531       asection *sec;
6532
6533       eh = (struct ppc_link_hash_entry *)
6534         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6535       if (eh == NULL)
6536         continue;
6537       if (eh->elf.root.type != bfd_link_hash_defined
6538           && eh->elf.root.type != bfd_link_hash_defweak)
6539         continue;
6540
6541       fh = defined_code_entry (eh);
6542       if (fh != NULL)
6543         {
6544           sec = fh->elf.root.u.def.section;
6545           sec->flags |= SEC_KEEP;
6546         }
6547       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6548                && opd_entry_value (eh->elf.root.u.def.section,
6549                                    eh->elf.root.u.def.value,
6550                                    &sec, NULL, FALSE) != (bfd_vma) -1)
6551         sec->flags |= SEC_KEEP;
6552
6553       sec = eh->elf.root.u.def.section;
6554       sec->flags |= SEC_KEEP;
6555     }
6556 }
6557
6558 /* Mark sections containing dynamically referenced symbols.  When
6559    building shared libraries, we must assume that any visible symbol is
6560    referenced.  */
6561
6562 static bfd_boolean
6563 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6564 {
6565   struct bfd_link_info *info = (struct bfd_link_info *) inf;
6566   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6567   struct ppc_link_hash_entry *fdh;
6568   struct bfd_elf_dynamic_list *d = info->dynamic_list;
6569
6570   /* Dynamic linking info is on the func descriptor sym.  */
6571   fdh = defined_func_desc (eh);
6572   if (fdh != NULL)
6573     eh = fdh;
6574
6575   if ((eh->elf.root.type == bfd_link_hash_defined
6576        || eh->elf.root.type == bfd_link_hash_defweak)
6577       && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
6578           || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6579               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6580               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6581               && (!bfd_link_executable (info)
6582                   || info->gc_keep_exported
6583                   || info->export_dynamic
6584                   || (eh->elf.dynamic
6585                       && d != NULL
6586                       && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6587               && (eh->elf.versioned >= versioned
6588                   || !bfd_hide_sym_by_version (info->version_info,
6589                                                eh->elf.root.root.string)))))
6590     {
6591       asection *code_sec;
6592       struct ppc_link_hash_entry *fh;
6593
6594       eh->elf.root.u.def.section->flags |= SEC_KEEP;
6595
6596       /* Function descriptor syms cause the associated
6597          function code sym section to be marked.  */
6598       fh = defined_code_entry (eh);
6599       if (fh != NULL)
6600         {
6601           code_sec = fh->elf.root.u.def.section;
6602           code_sec->flags |= SEC_KEEP;
6603         }
6604       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6605                && opd_entry_value (eh->elf.root.u.def.section,
6606                                    eh->elf.root.u.def.value,
6607                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
6608         code_sec->flags |= SEC_KEEP;
6609     }
6610
6611   return TRUE;
6612 }
6613
6614 /* Return the section that should be marked against GC for a given
6615    relocation.  */
6616
6617 static asection *
6618 ppc64_elf_gc_mark_hook (asection *sec,
6619                         struct bfd_link_info *info,
6620                         Elf_Internal_Rela *rel,
6621                         struct elf_link_hash_entry *h,
6622                         Elf_Internal_Sym *sym)
6623 {
6624   asection *rsec;
6625
6626   /* Syms return NULL if we're marking .opd, so we avoid marking all
6627      function sections, as all functions are referenced in .opd.  */
6628   rsec = NULL;
6629   if (get_opd_info (sec) != NULL)
6630     return rsec;
6631
6632   if (h != NULL)
6633     {
6634       enum elf_ppc64_reloc_type r_type;
6635       struct ppc_link_hash_entry *eh, *fh, *fdh;
6636
6637       r_type = ELF64_R_TYPE (rel->r_info);
6638       switch (r_type)
6639         {
6640         case R_PPC64_GNU_VTINHERIT:
6641         case R_PPC64_GNU_VTENTRY:
6642           break;
6643
6644         default:
6645           switch (h->root.type)
6646             {
6647             case bfd_link_hash_defined:
6648             case bfd_link_hash_defweak:
6649               eh = (struct ppc_link_hash_entry *) h;
6650               fdh = defined_func_desc (eh);
6651               if (fdh != NULL)
6652                 {
6653                   /* -mcall-aixdesc code references the dot-symbol on
6654                      a call reloc.  Mark the function descriptor too
6655                      against garbage collection.  */
6656                   fdh->elf.mark = 1;
6657                   if (fdh->elf.is_weakalias)
6658                     weakdef (&fdh->elf)->mark = 1;
6659                   eh = fdh;
6660                 }
6661
6662               /* Function descriptor syms cause the associated
6663                  function code sym section to be marked.  */
6664               fh = defined_code_entry (eh);
6665               if (fh != NULL)
6666                 {
6667                   /* They also mark their opd section.  */
6668                   eh->elf.root.u.def.section->gc_mark = 1;
6669
6670                   rsec = fh->elf.root.u.def.section;
6671                 }
6672               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6673                        && opd_entry_value (eh->elf.root.u.def.section,
6674                                            eh->elf.root.u.def.value,
6675                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
6676                 eh->elf.root.u.def.section->gc_mark = 1;
6677               else
6678                 rsec = h->root.u.def.section;
6679               break;
6680
6681             case bfd_link_hash_common:
6682               rsec = h->root.u.c.p->section;
6683               break;
6684
6685             default:
6686               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6687             }
6688         }
6689     }
6690   else
6691     {
6692       struct _opd_sec_data *opd;
6693
6694       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6695       opd = get_opd_info (rsec);
6696       if (opd != NULL && opd->func_sec != NULL)
6697         {
6698           rsec->gc_mark = 1;
6699
6700           rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6701         }
6702     }
6703
6704   return rsec;
6705 }
6706
6707 /* The maximum size of .sfpr.  */
6708 #define SFPR_MAX (218*4)
6709
6710 struct sfpr_def_parms
6711 {
6712   const char name[12];
6713   unsigned char lo, hi;
6714   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6715   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6716 };
6717
6718 /* Auto-generate _save*, _rest* functions in .sfpr.
6719    If STUB_SEC is non-null, define alias symbols in STUB_SEC
6720    instead.  */
6721
6722 static bfd_boolean
6723 sfpr_define (struct bfd_link_info *info,
6724              const struct sfpr_def_parms *parm,
6725              asection *stub_sec)
6726 {
6727   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6728   unsigned int i;
6729   size_t len = strlen (parm->name);
6730   bfd_boolean writing = FALSE;
6731   char sym[16];
6732
6733   if (htab == NULL)
6734     return FALSE;
6735
6736   memcpy (sym, parm->name, len);
6737   sym[len + 2] = 0;
6738
6739   for (i = parm->lo; i <= parm->hi; i++)
6740     {
6741       struct ppc_link_hash_entry *h;
6742
6743       sym[len + 0] = i / 10 + '0';
6744       sym[len + 1] = i % 10 + '0';
6745       h = (struct ppc_link_hash_entry *)
6746         elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6747       if (stub_sec != NULL)
6748         {
6749           if (h != NULL
6750               && h->elf.root.type == bfd_link_hash_defined
6751               && h->elf.root.u.def.section == htab->sfpr)
6752             {
6753               struct elf_link_hash_entry *s;
6754               char buf[32];
6755               sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6756               s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6757               if (s == NULL)
6758                 return FALSE;
6759               if (s->root.type == bfd_link_hash_new
6760                   || (s->root.type = bfd_link_hash_defined
6761                       && s->root.u.def.section == stub_sec))
6762                 {
6763                   s->root.type = bfd_link_hash_defined;
6764                   s->root.u.def.section = stub_sec;
6765                   s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6766                                          + h->elf.root.u.def.value);
6767                   s->ref_regular = 1;
6768                   s->def_regular = 1;
6769                   s->ref_regular_nonweak = 1;
6770                   s->forced_local = 1;
6771                   s->non_elf = 0;
6772                   s->root.linker_def = 1;
6773                 }
6774             }
6775           continue;
6776         }
6777       if (h != NULL)
6778         {
6779           h->save_res = 1;
6780           if (!h->elf.def_regular)
6781             {
6782               h->elf.root.type = bfd_link_hash_defined;
6783               h->elf.root.u.def.section = htab->sfpr;
6784               h->elf.root.u.def.value = htab->sfpr->size;
6785               h->elf.type = STT_FUNC;
6786               h->elf.def_regular = 1;
6787               h->elf.non_elf = 0;
6788               _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6789               writing = TRUE;
6790               if (htab->sfpr->contents == NULL)
6791                 {
6792                   htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6793                   if (htab->sfpr->contents == NULL)
6794                     return FALSE;
6795                 }
6796             }
6797         }
6798       if (writing)
6799         {
6800           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6801           if (i != parm->hi)
6802             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6803           else
6804             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6805           htab->sfpr->size = p - htab->sfpr->contents;
6806         }
6807     }
6808
6809   return TRUE;
6810 }
6811
6812 static bfd_byte *
6813 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6814 {
6815   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6816   return p + 4;
6817 }
6818
6819 static bfd_byte *
6820 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6821 {
6822   p = savegpr0 (abfd, p, r);
6823   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6824   p = p + 4;
6825   bfd_put_32 (abfd, BLR, p);
6826   return p + 4;
6827 }
6828
6829 static bfd_byte *
6830 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6831 {
6832   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6833   return p + 4;
6834 }
6835
6836 static bfd_byte *
6837 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6838 {
6839   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6840   p = p + 4;
6841   p = restgpr0 (abfd, p, r);
6842   bfd_put_32 (abfd, MTLR_R0, p);
6843   p = p + 4;
6844   if (r == 29)
6845     {
6846       p = restgpr0 (abfd, p, 30);
6847       p = restgpr0 (abfd, p, 31);
6848     }
6849   bfd_put_32 (abfd, BLR, p);
6850   return p + 4;
6851 }
6852
6853 static bfd_byte *
6854 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6855 {
6856   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6857   return p + 4;
6858 }
6859
6860 static bfd_byte *
6861 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6862 {
6863   p = savegpr1 (abfd, p, r);
6864   bfd_put_32 (abfd, BLR, p);
6865   return p + 4;
6866 }
6867
6868 static bfd_byte *
6869 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6870 {
6871   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6872   return p + 4;
6873 }
6874
6875 static bfd_byte *
6876 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6877 {
6878   p = restgpr1 (abfd, p, r);
6879   bfd_put_32 (abfd, BLR, p);
6880   return p + 4;
6881 }
6882
6883 static bfd_byte *
6884 savefpr (bfd *abfd, bfd_byte *p, int r)
6885 {
6886   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6887   return p + 4;
6888 }
6889
6890 static bfd_byte *
6891 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6892 {
6893   p = savefpr (abfd, p, r);
6894   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6895   p = p + 4;
6896   bfd_put_32 (abfd, BLR, p);
6897   return p + 4;
6898 }
6899
6900 static bfd_byte *
6901 restfpr (bfd *abfd, bfd_byte *p, int r)
6902 {
6903   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6904   return p + 4;
6905 }
6906
6907 static bfd_byte *
6908 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6909 {
6910   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6911   p = p + 4;
6912   p = restfpr (abfd, p, r);
6913   bfd_put_32 (abfd, MTLR_R0, p);
6914   p = p + 4;
6915   if (r == 29)
6916     {
6917       p = restfpr (abfd, p, 30);
6918       p = restfpr (abfd, p, 31);
6919     }
6920   bfd_put_32 (abfd, BLR, p);
6921   return p + 4;
6922 }
6923
6924 static bfd_byte *
6925 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6926 {
6927   p = savefpr (abfd, p, r);
6928   bfd_put_32 (abfd, BLR, p);
6929   return p + 4;
6930 }
6931
6932 static bfd_byte *
6933 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6934 {
6935   p = restfpr (abfd, p, r);
6936   bfd_put_32 (abfd, BLR, p);
6937   return p + 4;
6938 }
6939
6940 static bfd_byte *
6941 savevr (bfd *abfd, bfd_byte *p, int r)
6942 {
6943   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6944   p = p + 4;
6945   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6946   return p + 4;
6947 }
6948
6949 static bfd_byte *
6950 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6951 {
6952   p = savevr (abfd, p, r);
6953   bfd_put_32 (abfd, BLR, p);
6954   return p + 4;
6955 }
6956
6957 static bfd_byte *
6958 restvr (bfd *abfd, bfd_byte *p, int r)
6959 {
6960   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6961   p = p + 4;
6962   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6963   return p + 4;
6964 }
6965
6966 static bfd_byte *
6967 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6968 {
6969   p = restvr (abfd, p, r);
6970   bfd_put_32 (abfd, BLR, p);
6971   return p + 4;
6972 }
6973
6974 /* Called via elf_link_hash_traverse to transfer dynamic linking
6975    information on function code symbol entries to their corresponding
6976    function descriptor symbol entries.  */
6977
6978 static bfd_boolean
6979 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6980 {
6981   struct bfd_link_info *info;
6982   struct ppc_link_hash_table *htab;
6983   struct ppc_link_hash_entry *fh;
6984   struct ppc_link_hash_entry *fdh;
6985   bfd_boolean force_local;
6986
6987   fh = (struct ppc_link_hash_entry *) h;
6988   if (fh->elf.root.type == bfd_link_hash_indirect)
6989     return TRUE;
6990
6991   if (!fh->is_func)
6992     return TRUE;
6993
6994   if (fh->elf.root.root.string[0] != '.'
6995       || fh->elf.root.root.string[1] == '\0')
6996     return TRUE;
6997
6998   info = inf;
6999   htab = ppc_hash_table (info);
7000   if (htab == NULL)
7001     return FALSE;
7002
7003   /* Find the corresponding function descriptor symbol.  */
7004   fdh = lookup_fdh (fh, htab);
7005
7006   /* Resolve undefined references to dot-symbols as the value
7007      in the function descriptor, if we have one in a regular object.
7008      This is to satisfy cases like ".quad .foo".  Calls to functions
7009      in dynamic objects are handled elsewhere.  */
7010   if ((fh->elf.root.type == bfd_link_hash_undefined
7011        || fh->elf.root.type == bfd_link_hash_undefweak)
7012       && (fdh->elf.root.type == bfd_link_hash_defined
7013           || fdh->elf.root.type == bfd_link_hash_defweak)
7014       && get_opd_info (fdh->elf.root.u.def.section) != NULL
7015       && opd_entry_value (fdh->elf.root.u.def.section,
7016                           fdh->elf.root.u.def.value,
7017                           &fh->elf.root.u.def.section,
7018                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
7019     {
7020       fh->elf.root.type = fdh->elf.root.type;
7021       fh->elf.forced_local = 1;
7022       fh->elf.def_regular = fdh->elf.def_regular;
7023       fh->elf.def_dynamic = fdh->elf.def_dynamic;
7024     }
7025
7026   if (!fh->elf.dynamic)
7027     {
7028       struct plt_entry *ent;
7029
7030       for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7031         if (ent->plt.refcount > 0)
7032           break;
7033       if (ent == NULL)
7034         return TRUE;
7035     }
7036
7037   /* Create a descriptor as undefined if necessary.  */
7038   if (fdh == NULL
7039       && !bfd_link_executable (info)
7040       && (fh->elf.root.type == bfd_link_hash_undefined
7041           || fh->elf.root.type == bfd_link_hash_undefweak))
7042     {
7043       fdh = make_fdh (info, fh);
7044       if (fdh == NULL)
7045         return FALSE;
7046     }
7047
7048   /* We can't support overriding of symbols on a fake descriptor.  */
7049   if (fdh != NULL
7050       && fdh->fake
7051       && (fh->elf.root.type == bfd_link_hash_defined
7052           || fh->elf.root.type == bfd_link_hash_defweak))
7053     _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
7054
7055   /* Transfer dynamic linking information to the function descriptor.  */
7056   if (fdh != NULL)
7057     {
7058       fdh->elf.ref_regular |= fh->elf.ref_regular;
7059       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7060       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7061       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
7062       fdh->elf.dynamic |= fh->elf.dynamic;
7063       fdh->elf.needs_plt |= (fh->elf.needs_plt
7064                              || fh->elf.type == STT_FUNC
7065                              || fh->elf.type == STT_GNU_IFUNC);
7066       move_plt_plist (fh, fdh);
7067
7068       if (!fdh->elf.forced_local
7069           && fh->elf.dynindx != -1)
7070         if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7071           return FALSE;
7072     }
7073
7074   /* Now that the info is on the function descriptor, clear the
7075      function code sym info.  Any function code syms for which we
7076      don't have a definition in a regular file, we force local.
7077      This prevents a shared library from exporting syms that have
7078      been imported from another library.  Function code syms that
7079      are really in the library we must leave global to prevent the
7080      linker dragging in a definition from a static library.  */
7081   force_local = (!fh->elf.def_regular
7082                  || fdh == NULL
7083                  || !fdh->elf.def_regular
7084                  || fdh->elf.forced_local);
7085   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7086
7087   return TRUE;
7088 }
7089
7090 static const struct sfpr_def_parms save_res_funcs[] =
7091   {
7092     { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7093     { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7094     { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7095     { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7096     { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7097     { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7098     { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7099     { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7100     { "._savef", 14, 31, savefpr, savefpr1_tail },
7101     { "._restf", 14, 31, restfpr, restfpr1_tail },
7102     { "_savevr_", 20, 31, savevr, savevr_tail },
7103     { "_restvr_", 20, 31, restvr, restvr_tail }
7104   };
7105
7106 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
7107    this hook to a) provide some gcc support functions, and b) transfer
7108    dynamic linking information gathered so far on function code symbol
7109    entries, to their corresponding function descriptor symbol entries.  */
7110
7111 static bfd_boolean
7112 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7113                             struct bfd_link_info *info)
7114 {
7115   struct ppc_link_hash_table *htab;
7116
7117   htab = ppc_hash_table (info);
7118   if (htab == NULL)
7119     return FALSE;
7120
7121   /* Provide any missing _save* and _rest* functions.  */
7122   if (htab->sfpr != NULL)
7123     {
7124       unsigned int i;
7125
7126       htab->sfpr->size = 0;
7127       for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7128         if (!sfpr_define (info, &save_res_funcs[i], NULL))
7129           return FALSE;
7130       if (htab->sfpr->size == 0)
7131         htab->sfpr->flags |= SEC_EXCLUDE;
7132     }
7133
7134   if (bfd_link_relocatable (info))
7135     return TRUE;
7136
7137   if (htab->elf.hgot != NULL)
7138     {
7139       _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7140       /* Make .TOC. defined so as to prevent it being made dynamic.
7141          The wrong value here is fixed later in ppc64_elf_set_toc.  */
7142       if (!htab->elf.hgot->def_regular
7143           || htab->elf.hgot->root.type != bfd_link_hash_defined)
7144         {
7145           htab->elf.hgot->root.type = bfd_link_hash_defined;
7146           htab->elf.hgot->root.u.def.value = 0;
7147           htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7148           htab->elf.hgot->def_regular = 1;
7149           htab->elf.hgot->root.linker_def = 1;
7150         }
7151       htab->elf.hgot->type = STT_OBJECT;
7152       htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7153                                | STV_HIDDEN);
7154     }
7155
7156   if (htab->need_func_desc_adj)
7157     {
7158       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7159       htab->need_func_desc_adj = 0;
7160     }
7161
7162   return TRUE;
7163 }
7164
7165 /* Find dynamic relocs for H that apply to read-only sections.  */
7166
7167 static asection *
7168 readonly_dynrelocs (struct elf_link_hash_entry *h)
7169 {
7170   struct ppc_link_hash_entry *eh;
7171   struct elf_dyn_relocs *p;
7172
7173   eh = (struct ppc_link_hash_entry *) h;
7174   for (p = eh->dyn_relocs; p != NULL; p = p->next)
7175     {
7176       asection *s = p->sec->output_section;
7177
7178       if (s != NULL && (s->flags & SEC_READONLY) != 0)
7179         return p->sec;
7180     }
7181   return NULL;
7182 }
7183
7184 /* Return true if we have dynamic relocs against H or any of its weak
7185    aliases, that apply to read-only sections.  Cannot be used after
7186    size_dynamic_sections.  */
7187
7188 static bfd_boolean
7189 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7190 {
7191   struct ppc_link_hash_entry *eh;
7192
7193   eh = (struct ppc_link_hash_entry *) h;
7194   do
7195     {
7196       if (readonly_dynrelocs (&eh->elf))
7197         return TRUE;
7198       eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
7199     } while (eh != NULL && &eh->elf != h);
7200
7201   return FALSE;
7202 }
7203
7204 /* Return whether EH has pc-relative dynamic relocs.  */
7205
7206 static bfd_boolean
7207 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7208 {
7209   struct elf_dyn_relocs *p;
7210
7211   for (p = eh->dyn_relocs; p != NULL; p = p->next)
7212     if (p->pc_count != 0)
7213       return TRUE;
7214   return FALSE;
7215 }
7216
7217 /* Return true if a global entry stub will be created for H.  Valid
7218    for ELFv2 before plt entries have been allocated.  */
7219
7220 static bfd_boolean
7221 global_entry_stub (struct elf_link_hash_entry *h)
7222 {
7223   struct plt_entry *pent;
7224
7225   if (!h->pointer_equality_needed
7226       || h->def_regular)
7227     return FALSE;
7228
7229   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7230     if (pent->plt.refcount > 0
7231         && pent->addend == 0)
7232       return TRUE;
7233
7234   return FALSE;
7235 }
7236
7237 /* Adjust a symbol defined by a dynamic object and referenced by a
7238    regular object.  The current definition is in some section of the
7239    dynamic object, but we're not including those sections.  We have to
7240    change the definition to something the rest of the link can
7241    understand.  */
7242
7243 static bfd_boolean
7244 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7245                                  struct elf_link_hash_entry *h)
7246 {
7247   struct ppc_link_hash_table *htab;
7248   asection *s, *srel;
7249
7250   htab = ppc_hash_table (info);
7251   if (htab == NULL)
7252     return FALSE;
7253
7254   /* Deal with function syms.  */
7255   if (h->type == STT_FUNC
7256       || h->type == STT_GNU_IFUNC
7257       || h->needs_plt)
7258     {
7259       bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7260                            || SYMBOL_CALLS_LOCAL (info, h)
7261                            || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7262       /* Discard dyn_relocs when non-pic if we've decided that a
7263          function symbol is local and not an ifunc.  We keep dynamic
7264          relocs for ifuncs when local rather than always emitting a
7265          plt call stub for them and defining the symbol on the call
7266          stub.  We can't do that for ELFv1 anyway (a function symbol
7267          is defined on a descriptor, not code) and it can be faster at
7268          run-time due to not needing to bounce through a stub.  The
7269          dyn_relocs for ifuncs will be applied even in a static
7270          executable.  */
7271       if (!bfd_link_pic (info)
7272           && h->type != STT_GNU_IFUNC
7273           && local)
7274         ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7275
7276       /* Clear procedure linkage table information for any symbol that
7277          won't need a .plt entry.  */
7278       struct plt_entry *ent;
7279       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7280         if (ent->plt.refcount > 0)
7281           break;
7282       if (ent == NULL
7283           || (h->type != STT_GNU_IFUNC
7284               && local
7285               && (htab->can_convert_all_inline_plt
7286                   || (((struct ppc_link_hash_entry *) h)->tls_mask
7287                       & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
7288         {
7289           h->plt.plist = NULL;
7290           h->needs_plt = 0;
7291           h->pointer_equality_needed = 0;
7292         }
7293       else if (abiversion (info->output_bfd) >= 2)
7294         {
7295           /* Taking a function's address in a read/write section
7296              doesn't require us to define the function symbol in the
7297              executable on a global entry stub.  A dynamic reloc can
7298              be used instead.  The reason we prefer a few more dynamic
7299              relocs is that calling via a global entry stub costs a
7300              few more instructions, and pointer_equality_needed causes
7301              extra work in ld.so when resolving these symbols.  */
7302           if (global_entry_stub (h))
7303             {
7304               if (!readonly_dynrelocs (h))
7305                 {
7306                   h->pointer_equality_needed = 0;
7307                   /* If we haven't seen a branch reloc and the symbol
7308                      isn't an ifunc then we don't need a plt entry.  */
7309                   if (!h->needs_plt)
7310                     h->plt.plist = NULL;
7311                 }
7312               else if (!bfd_link_pic (info))
7313                 /* We are going to be defining the function symbol on the
7314                    plt stub, so no dyn_relocs needed when non-pic.  */
7315                 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7316             }
7317
7318           /* ELFv2 function symbols can't have copy relocs.  */
7319           return TRUE;
7320         }
7321       else if (!h->needs_plt
7322                && !readonly_dynrelocs (h))
7323         {
7324           /* If we haven't seen a branch reloc and the symbol isn't an
7325              ifunc then we don't need a plt entry.  */
7326           h->plt.plist = NULL;
7327           h->pointer_equality_needed = 0;
7328           return TRUE;
7329         }
7330     }
7331   else
7332     h->plt.plist = NULL;
7333
7334   /* If this is a weak symbol, and there is a real definition, the
7335      processor independent code will have arranged for us to see the
7336      real definition first, and we can just use the same value.  */
7337   if (h->is_weakalias)
7338     {
7339       struct elf_link_hash_entry *def = weakdef (h);
7340       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7341       h->root.u.def.section = def->root.u.def.section;
7342       h->root.u.def.value = def->root.u.def.value;
7343       if (def->root.u.def.section == htab->elf.sdynbss
7344           || def->root.u.def.section == htab->elf.sdynrelro)
7345         ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7346       return TRUE;
7347     }
7348
7349   /* If we are creating a shared library, we must presume that the
7350      only references to the symbol are via the global offset table.
7351      For such cases we need not do anything here; the relocations will
7352      be handled correctly by relocate_section.  */
7353   if (bfd_link_pic (info))
7354     return TRUE;
7355
7356   /* If there are no references to this symbol that do not use the
7357      GOT, we don't need to generate a copy reloc.  */
7358   if (!h->non_got_ref)
7359     return TRUE;
7360
7361   /* Don't generate a copy reloc for symbols defined in the executable.  */
7362   if (!h->def_dynamic || !h->ref_regular || h->def_regular
7363
7364       /* If -z nocopyreloc was given, don't generate them either.  */
7365       || info->nocopyreloc
7366
7367       /* If we don't find any dynamic relocs in read-only sections, then
7368          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
7369       || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7370
7371       /* Protected variables do not work with .dynbss.  The copy in
7372          .dynbss won't be used by the shared library with the protected
7373          definition for the variable.  Text relocations are preferable
7374          to an incorrect program.  */
7375       || h->protected_def)
7376     return TRUE;
7377
7378   if (h->plt.plist != NULL)
7379     {
7380       /* We should never get here, but unfortunately there are versions
7381          of gcc out there that improperly (for this ABI) put initialized
7382          function pointers, vtable refs and suchlike in read-only
7383          sections.  Allow them to proceed, but warn that this might
7384          break at runtime.  */
7385       info->callbacks->einfo
7386         (_("%P: copy reloc against `%pT' requires lazy plt linking; "
7387            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7388          h->root.root.string);
7389     }
7390
7391   /* This is a reference to a symbol defined by a dynamic object which
7392      is not a function.  */
7393
7394   /* We must allocate the symbol in our .dynbss section, which will
7395      become part of the .bss section of the executable.  There will be
7396      an entry for this symbol in the .dynsym section.  The dynamic
7397      object will contain position independent code, so all references
7398      from the dynamic object to this symbol will go through the global
7399      offset table.  The dynamic linker will use the .dynsym entry to
7400      determine the address it must put in the global offset table, so
7401      both the dynamic object and the regular object will refer to the
7402      same memory location for the variable.  */
7403   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7404     {
7405       s = htab->elf.sdynrelro;
7406       srel = htab->elf.sreldynrelro;
7407     }
7408   else
7409     {
7410       s = htab->elf.sdynbss;
7411       srel = htab->elf.srelbss;
7412     }
7413   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7414     {
7415       /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7416          linker to copy the initial value out of the dynamic object
7417          and into the runtime process image.  */
7418       srel->size += sizeof (Elf64_External_Rela);
7419       h->needs_copy = 1;
7420     }
7421
7422   /* We no longer want dyn_relocs.  */
7423   ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7424   return _bfd_elf_adjust_dynamic_copy (info, h, s);
7425 }
7426
7427 /* If given a function descriptor symbol, hide both the function code
7428    sym and the descriptor.  */
7429 static void
7430 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7431                        struct elf_link_hash_entry *h,
7432                        bfd_boolean force_local)
7433 {
7434   struct ppc_link_hash_entry *eh;
7435   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7436
7437   eh = (struct ppc_link_hash_entry *) h;
7438   if (eh->is_func_descriptor)
7439     {
7440       struct ppc_link_hash_entry *fh = eh->oh;
7441
7442       if (fh == NULL)
7443         {
7444           const char *p, *q;
7445           struct elf_link_hash_table *htab = elf_hash_table (info);
7446           char save;
7447
7448           /* We aren't supposed to use alloca in BFD because on
7449              systems which do not have alloca the version in libiberty
7450              calls xmalloc, which might cause the program to crash
7451              when it runs out of memory.  This function doesn't have a
7452              return status, so there's no way to gracefully return an
7453              error.  So cheat.  We know that string[-1] can be safely
7454              accessed;  It's either a string in an ELF string table,
7455              or allocated in an objalloc structure.  */
7456
7457           p = eh->elf.root.root.string - 1;
7458           save = *p;
7459           *(char *) p = '.';
7460           fh = (struct ppc_link_hash_entry *)
7461             elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7462           *(char *) p = save;
7463
7464           /* Unfortunately, if it so happens that the string we were
7465              looking for was allocated immediately before this string,
7466              then we overwrote the string terminator.  That's the only
7467              reason the lookup should fail.  */
7468           if (fh == NULL)
7469             {
7470               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7471               while (q >= eh->elf.root.root.string && *q == *p)
7472                 --q, --p;
7473               if (q < eh->elf.root.root.string && *p == '.')
7474                 fh = (struct ppc_link_hash_entry *)
7475                   elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7476             }
7477           if (fh != NULL)
7478             {
7479               eh->oh = fh;
7480               fh->oh = eh;
7481             }
7482         }
7483       if (fh != NULL)
7484         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7485     }
7486 }
7487
7488 static bfd_boolean
7489 get_sym_h (struct elf_link_hash_entry **hp,
7490            Elf_Internal_Sym **symp,
7491            asection **symsecp,
7492            unsigned char **tls_maskp,
7493            Elf_Internal_Sym **locsymsp,
7494            unsigned long r_symndx,
7495            bfd *ibfd)
7496 {
7497   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7498
7499   if (r_symndx >= symtab_hdr->sh_info)
7500     {
7501       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7502       struct elf_link_hash_entry *h;
7503
7504       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7505       h = elf_follow_link (h);
7506
7507       if (hp != NULL)
7508         *hp = h;
7509
7510       if (symp != NULL)
7511         *symp = NULL;
7512
7513       if (symsecp != NULL)
7514         {
7515           asection *symsec = NULL;
7516           if (h->root.type == bfd_link_hash_defined
7517               || h->root.type == bfd_link_hash_defweak)
7518             symsec = h->root.u.def.section;
7519           *symsecp = symsec;
7520         }
7521
7522       if (tls_maskp != NULL)
7523         {
7524           struct ppc_link_hash_entry *eh;
7525
7526           eh = (struct ppc_link_hash_entry *) h;
7527           *tls_maskp = &eh->tls_mask;
7528         }
7529     }
7530   else
7531     {
7532       Elf_Internal_Sym *sym;
7533       Elf_Internal_Sym *locsyms = *locsymsp;
7534
7535       if (locsyms == NULL)
7536         {
7537           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7538           if (locsyms == NULL)
7539             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7540                                             symtab_hdr->sh_info,
7541                                             0, NULL, NULL, NULL);
7542           if (locsyms == NULL)
7543             return FALSE;
7544           *locsymsp = locsyms;
7545         }
7546       sym = locsyms + r_symndx;
7547
7548       if (hp != NULL)
7549         *hp = NULL;
7550
7551       if (symp != NULL)
7552         *symp = sym;
7553
7554       if (symsecp != NULL)
7555         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7556
7557       if (tls_maskp != NULL)
7558         {
7559           struct got_entry **lgot_ents;
7560           unsigned char *tls_mask;
7561
7562           tls_mask = NULL;
7563           lgot_ents = elf_local_got_ents (ibfd);
7564           if (lgot_ents != NULL)
7565             {
7566               struct plt_entry **local_plt = (struct plt_entry **)
7567                 (lgot_ents + symtab_hdr->sh_info);
7568               unsigned char *lgot_masks = (unsigned char *)
7569                 (local_plt + symtab_hdr->sh_info);
7570               tls_mask = &lgot_masks[r_symndx];
7571             }
7572           *tls_maskp = tls_mask;
7573         }
7574     }
7575   return TRUE;
7576 }
7577
7578 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
7579    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7580    type suitable for optimization, and 1 otherwise.  */
7581
7582 static int
7583 get_tls_mask (unsigned char **tls_maskp,
7584               unsigned long *toc_symndx,
7585               bfd_vma *toc_addend,
7586               Elf_Internal_Sym **locsymsp,
7587               const Elf_Internal_Rela *rel,
7588               bfd *ibfd)
7589 {
7590   unsigned long r_symndx;
7591   int next_r;
7592   struct elf_link_hash_entry *h;
7593   Elf_Internal_Sym *sym;
7594   asection *sec;
7595   bfd_vma off;
7596
7597   r_symndx = ELF64_R_SYM (rel->r_info);
7598   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7599     return 0;
7600
7601   if ((*tls_maskp != NULL
7602        && (**tls_maskp & TLS_TLS) != 0
7603        && **tls_maskp != (TLS_TLS | TLS_MARK))
7604       || sec == NULL
7605       || ppc64_elf_section_data (sec) == NULL
7606       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7607     return 1;
7608
7609   /* Look inside a TOC section too.  */
7610   if (h != NULL)
7611     {
7612       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7613       off = h->root.u.def.value;
7614     }
7615   else
7616     off = sym->st_value;
7617   off += rel->r_addend;
7618   BFD_ASSERT (off % 8 == 0);
7619   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7620   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7621   if (toc_symndx != NULL)
7622     *toc_symndx = r_symndx;
7623   if (toc_addend != NULL)
7624     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7625   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7626     return 0;
7627   if ((h == NULL || is_static_defined (h))
7628       && (next_r == -1 || next_r == -2))
7629     return 1 - next_r;
7630   return 1;
7631 }
7632
7633 /* Find (or create) an entry in the tocsave hash table.  */
7634
7635 static struct tocsave_entry *
7636 tocsave_find (struct ppc_link_hash_table *htab,
7637               enum insert_option insert,
7638               Elf_Internal_Sym **local_syms,
7639               const Elf_Internal_Rela *irela,
7640               bfd *ibfd)
7641 {
7642   unsigned long r_indx;
7643   struct elf_link_hash_entry *h;
7644   Elf_Internal_Sym *sym;
7645   struct tocsave_entry ent, *p;
7646   hashval_t hash;
7647   struct tocsave_entry **slot;
7648
7649   r_indx = ELF64_R_SYM (irela->r_info);
7650   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7651     return NULL;
7652   if (ent.sec == NULL || ent.sec->output_section == NULL)
7653     {
7654       _bfd_error_handler
7655         (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7656       return NULL;
7657     }
7658
7659   if (h != NULL)
7660     ent.offset = h->root.u.def.value;
7661   else
7662     ent.offset = sym->st_value;
7663   ent.offset += irela->r_addend;
7664
7665   hash = tocsave_htab_hash (&ent);
7666   slot = ((struct tocsave_entry **)
7667           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7668   if (slot == NULL)
7669     return NULL;
7670
7671   if (*slot == NULL)
7672     {
7673       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7674       if (p == NULL)
7675         return NULL;
7676       *p = ent;
7677       *slot = p;
7678     }
7679   return *slot;
7680 }
7681
7682 /* Adjust all global syms defined in opd sections.  In gcc generated
7683    code for the old ABI, these will already have been done.  */
7684
7685 static bfd_boolean
7686 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7687 {
7688   struct ppc_link_hash_entry *eh;
7689   asection *sym_sec;
7690   struct _opd_sec_data *opd;
7691
7692   if (h->root.type == bfd_link_hash_indirect)
7693     return TRUE;
7694
7695   if (h->root.type != bfd_link_hash_defined
7696       && h->root.type != bfd_link_hash_defweak)
7697     return TRUE;
7698
7699   eh = (struct ppc_link_hash_entry *) h;
7700   if (eh->adjust_done)
7701     return TRUE;
7702
7703   sym_sec = eh->elf.root.u.def.section;
7704   opd = get_opd_info (sym_sec);
7705   if (opd != NULL && opd->adjust != NULL)
7706     {
7707       long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7708       if (adjust == -1)
7709         {
7710           /* This entry has been deleted.  */
7711           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7712           if (dsec == NULL)
7713             {
7714               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7715                 if (discarded_section (dsec))
7716                   {
7717                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7718                     break;
7719                   }
7720             }
7721           eh->elf.root.u.def.value = 0;
7722           eh->elf.root.u.def.section = dsec;
7723         }
7724       else
7725         eh->elf.root.u.def.value += adjust;
7726       eh->adjust_done = 1;
7727     }
7728   return TRUE;
7729 }
7730
7731 /* Handles decrementing dynamic reloc counts for the reloc specified by
7732    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
7733    have already been determined.  */
7734
7735 static bfd_boolean
7736 dec_dynrel_count (bfd_vma r_info,
7737                   asection *sec,
7738                   struct bfd_link_info *info,
7739                   Elf_Internal_Sym **local_syms,
7740                   struct elf_link_hash_entry *h,
7741                   Elf_Internal_Sym *sym)
7742 {
7743   enum elf_ppc64_reloc_type r_type;
7744   asection *sym_sec = NULL;
7745
7746   /* Can this reloc be dynamic?  This switch, and later tests here
7747      should be kept in sync with the code in check_relocs.  */
7748   r_type = ELF64_R_TYPE (r_info);
7749   switch (r_type)
7750     {
7751     default:
7752       return TRUE;
7753
7754     case R_PPC64_TPREL16:
7755     case R_PPC64_TPREL16_LO:
7756     case R_PPC64_TPREL16_HI:
7757     case R_PPC64_TPREL16_HA:
7758     case R_PPC64_TPREL16_DS:
7759     case R_PPC64_TPREL16_LO_DS:
7760     case R_PPC64_TPREL16_HIGH:
7761     case R_PPC64_TPREL16_HIGHA:
7762     case R_PPC64_TPREL16_HIGHER:
7763     case R_PPC64_TPREL16_HIGHERA:
7764     case R_PPC64_TPREL16_HIGHEST:
7765     case R_PPC64_TPREL16_HIGHESTA:
7766     case R_PPC64_TPREL64:
7767     case R_PPC64_DTPMOD64:
7768     case R_PPC64_DTPREL64:
7769     case R_PPC64_ADDR64:
7770     case R_PPC64_REL30:
7771     case R_PPC64_REL32:
7772     case R_PPC64_REL64:
7773     case R_PPC64_ADDR14:
7774     case R_PPC64_ADDR14_BRNTAKEN:
7775     case R_PPC64_ADDR14_BRTAKEN:
7776     case R_PPC64_ADDR16:
7777     case R_PPC64_ADDR16_DS:
7778     case R_PPC64_ADDR16_HA:
7779     case R_PPC64_ADDR16_HI:
7780     case R_PPC64_ADDR16_HIGH:
7781     case R_PPC64_ADDR16_HIGHA:
7782     case R_PPC64_ADDR16_HIGHER:
7783     case R_PPC64_ADDR16_HIGHERA:
7784     case R_PPC64_ADDR16_HIGHEST:
7785     case R_PPC64_ADDR16_HIGHESTA:
7786     case R_PPC64_ADDR16_LO:
7787     case R_PPC64_ADDR16_LO_DS:
7788     case R_PPC64_ADDR24:
7789     case R_PPC64_ADDR32:
7790     case R_PPC64_UADDR16:
7791     case R_PPC64_UADDR32:
7792     case R_PPC64_UADDR64:
7793     case R_PPC64_TOC:
7794       break;
7795     }
7796
7797   if (local_syms != NULL)
7798     {
7799       unsigned long r_symndx;
7800       bfd *ibfd = sec->owner;
7801
7802       r_symndx = ELF64_R_SYM (r_info);
7803       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7804         return FALSE;
7805     }
7806
7807   if ((bfd_link_pic (info)
7808        && (must_be_dyn_reloc (info, r_type)
7809            || (h != NULL
7810                && (!SYMBOLIC_BIND (info, h)
7811                    || h->root.type == bfd_link_hash_defweak
7812                    || !h->def_regular))))
7813       || (ELIMINATE_COPY_RELOCS
7814           && !bfd_link_pic (info)
7815           && h != NULL
7816           && (h->root.type == bfd_link_hash_defweak
7817               || !h->def_regular)))
7818     ;
7819   else
7820     return TRUE;
7821
7822   if (h != NULL)
7823     {
7824       struct elf_dyn_relocs *p;
7825       struct elf_dyn_relocs **pp;
7826       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7827
7828       /* elf_gc_sweep may have already removed all dyn relocs associated
7829          with local syms for a given section.  Also, symbol flags are
7830          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
7831          report a dynreloc miscount.  */
7832       if (*pp == NULL && info->gc_sections)
7833         return TRUE;
7834
7835       while ((p = *pp) != NULL)
7836         {
7837           if (p->sec == sec)
7838             {
7839               if (!must_be_dyn_reloc (info, r_type))
7840                 p->pc_count -= 1;
7841               p->count -= 1;
7842               if (p->count == 0)
7843                 *pp = p->next;
7844               return TRUE;
7845             }
7846           pp = &p->next;
7847         }
7848     }
7849   else
7850     {
7851       struct ppc_dyn_relocs *p;
7852       struct ppc_dyn_relocs **pp;
7853       void *vpp;
7854       bfd_boolean is_ifunc;
7855
7856       if (local_syms == NULL)
7857         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7858       if (sym_sec == NULL)
7859         sym_sec = sec;
7860
7861       vpp = &elf_section_data (sym_sec)->local_dynrel;
7862       pp = (struct ppc_dyn_relocs **) vpp;
7863
7864       if (*pp == NULL && info->gc_sections)
7865         return TRUE;
7866
7867       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7868       while ((p = *pp) != NULL)
7869         {
7870           if (p->sec == sec && p->ifunc == is_ifunc)
7871             {
7872               p->count -= 1;
7873               if (p->count == 0)
7874                 *pp = p->next;
7875               return TRUE;
7876             }
7877           pp = &p->next;
7878         }
7879     }
7880
7881   /* xgettext:c-format */
7882   _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7883                       sec->owner, sec);
7884   bfd_set_error (bfd_error_bad_value);
7885   return FALSE;
7886 }
7887
7888 /* Remove unused Official Procedure Descriptor entries.  Currently we
7889    only remove those associated with functions in discarded link-once
7890    sections, or weakly defined functions that have been overridden.  It
7891    would be possible to remove many more entries for statically linked
7892    applications.  */
7893
7894 bfd_boolean
7895 ppc64_elf_edit_opd (struct bfd_link_info *info)
7896 {
7897   bfd *ibfd;
7898   bfd_boolean some_edited = FALSE;
7899   asection *need_pad = NULL;
7900   struct ppc_link_hash_table *htab;
7901
7902   htab = ppc_hash_table (info);
7903   if (htab == NULL)
7904     return FALSE;
7905
7906   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7907     {
7908       asection *sec;
7909       Elf_Internal_Rela *relstart, *rel, *relend;
7910       Elf_Internal_Shdr *symtab_hdr;
7911       Elf_Internal_Sym *local_syms;
7912       struct _opd_sec_data *opd;
7913       bfd_boolean need_edit, add_aux_fields, broken;
7914       bfd_size_type cnt_16b = 0;
7915
7916       if (!is_ppc64_elf (ibfd))
7917         continue;
7918
7919       sec = bfd_get_section_by_name (ibfd, ".opd");
7920       if (sec == NULL || sec->size == 0)
7921         continue;
7922
7923       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7924         continue;
7925
7926       if (sec->output_section == bfd_abs_section_ptr)
7927         continue;
7928
7929       /* Look through the section relocs.  */
7930       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7931         continue;
7932
7933       local_syms = NULL;
7934       symtab_hdr = &elf_symtab_hdr (ibfd);
7935
7936       /* Read the relocations.  */
7937       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7938                                             info->keep_memory);
7939       if (relstart == NULL)
7940         return FALSE;
7941
7942       /* First run through the relocs to check they are sane, and to
7943          determine whether we need to edit this opd section.  */
7944       need_edit = FALSE;
7945       broken = FALSE;
7946       need_pad = sec;
7947       relend = relstart + sec->reloc_count;
7948       for (rel = relstart; rel < relend; )
7949         {
7950           enum elf_ppc64_reloc_type r_type;
7951           unsigned long r_symndx;
7952           asection *sym_sec;
7953           struct elf_link_hash_entry *h;
7954           Elf_Internal_Sym *sym;
7955           bfd_vma offset;
7956
7957           /* .opd contains an array of 16 or 24 byte entries.  We're
7958              only interested in the reloc pointing to a function entry
7959              point.  */
7960           offset = rel->r_offset;
7961           if (rel + 1 == relend
7962               || rel[1].r_offset != offset + 8)
7963             {
7964               /* If someone messes with .opd alignment then after a
7965                  "ld -r" we might have padding in the middle of .opd.
7966                  Also, there's nothing to prevent someone putting
7967                  something silly in .opd with the assembler.  No .opd
7968                  optimization for them!  */
7969             broken_opd:
7970               _bfd_error_handler
7971                 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7972               broken = TRUE;
7973               break;
7974             }
7975
7976           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7977               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7978             {
7979               _bfd_error_handler
7980                 /* xgettext:c-format */
7981                 (_("%pB: unexpected reloc type %u in .opd section"),
7982                  ibfd, r_type);
7983               broken = TRUE;
7984               break;
7985             }
7986
7987           r_symndx = ELF64_R_SYM (rel->r_info);
7988           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7989                           r_symndx, ibfd))
7990             goto error_ret;
7991
7992           if (sym_sec == NULL || sym_sec->owner == NULL)
7993             {
7994               const char *sym_name;
7995               if (h != NULL)
7996                 sym_name = h->root.root.string;
7997               else
7998                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7999                                              sym_sec);
8000
8001               _bfd_error_handler
8002                 /* xgettext:c-format */
8003                 (_("%pB: undefined sym `%s' in .opd section"),
8004                  ibfd, sym_name);
8005               broken = TRUE;
8006               break;
8007             }
8008
8009           /* opd entries are always for functions defined in the
8010              current input bfd.  If the symbol isn't defined in the
8011              input bfd, then we won't be using the function in this
8012              bfd;  It must be defined in a linkonce section in another
8013              bfd, or is weak.  It's also possible that we are
8014              discarding the function due to a linker script /DISCARD/,
8015              which we test for via the output_section.  */
8016           if (sym_sec->owner != ibfd
8017               || sym_sec->output_section == bfd_abs_section_ptr)
8018             need_edit = TRUE;
8019
8020           rel += 2;
8021           if (rel + 1 == relend
8022               || (rel + 2 < relend
8023                   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
8024             ++rel;
8025
8026           if (rel == relend)
8027             {
8028               if (sec->size == offset + 24)
8029                 {
8030                   need_pad = NULL;
8031                   break;
8032                 }
8033               if (sec->size == offset + 16)
8034                 {
8035                   cnt_16b++;
8036                   break;
8037                 }
8038               goto broken_opd;
8039             }
8040           else if (rel + 1 < relend
8041                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8042                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8043             {
8044               if (rel[0].r_offset == offset + 16)
8045                 cnt_16b++;
8046               else if (rel[0].r_offset != offset + 24)
8047                 goto broken_opd;
8048             }
8049           else
8050             goto broken_opd;
8051         }
8052
8053       add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
8054
8055       if (!broken && (need_edit || add_aux_fields))
8056         {
8057           Elf_Internal_Rela *write_rel;
8058           Elf_Internal_Shdr *rel_hdr;
8059           bfd_byte *rptr, *wptr;
8060           bfd_byte *new_contents;
8061           bfd_size_type amt;
8062
8063           new_contents = NULL;
8064           amt = OPD_NDX (sec->size) * sizeof (long);
8065           opd = &ppc64_elf_section_data (sec)->u.opd;
8066           opd->adjust = bfd_zalloc (sec->owner, amt);
8067           if (opd->adjust == NULL)
8068             return FALSE;
8069
8070           /* This seems a waste of time as input .opd sections are all
8071              zeros as generated by gcc, but I suppose there's no reason
8072              this will always be so.  We might start putting something in
8073              the third word of .opd entries.  */
8074           if ((sec->flags & SEC_IN_MEMORY) == 0)
8075             {
8076               bfd_byte *loc;
8077               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
8078                 {
8079                   if (loc != NULL)
8080                     free (loc);
8081                 error_ret:
8082                   if (local_syms != NULL
8083                       && symtab_hdr->contents != (unsigned char *) local_syms)
8084                     free (local_syms);
8085                   if (elf_section_data (sec)->relocs != relstart)
8086                     free (relstart);
8087                   return FALSE;
8088                 }
8089               sec->contents = loc;
8090               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8091             }
8092
8093           elf_section_data (sec)->relocs = relstart;
8094
8095           new_contents = sec->contents;
8096           if (add_aux_fields)
8097             {
8098               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8099               if (new_contents == NULL)
8100                 return FALSE;
8101               need_pad = NULL;
8102             }
8103           wptr = new_contents;
8104           rptr = sec->contents;
8105           write_rel = relstart;
8106           for (rel = relstart; rel < relend; )
8107             {
8108               unsigned long r_symndx;
8109               asection *sym_sec;
8110               struct elf_link_hash_entry *h;
8111               struct ppc_link_hash_entry *fdh = NULL;
8112               Elf_Internal_Sym *sym;
8113               long opd_ent_size;
8114               Elf_Internal_Rela *next_rel;
8115               bfd_boolean skip;
8116
8117               r_symndx = ELF64_R_SYM (rel->r_info);
8118               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8119                               r_symndx, ibfd))
8120                 goto error_ret;
8121
8122               next_rel = rel + 2;
8123               if (next_rel + 1 == relend
8124                   || (next_rel + 2 < relend
8125                       && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8126                 ++next_rel;
8127
8128               /* See if the .opd entry is full 24 byte or
8129                  16 byte (with fd_aux entry overlapped with next
8130                  fd_func).  */
8131               opd_ent_size = 24;
8132               if (next_rel == relend)
8133                 {
8134                   if (sec->size == rel->r_offset + 16)
8135                     opd_ent_size = 16;
8136                 }
8137               else if (next_rel->r_offset == rel->r_offset + 16)
8138                 opd_ent_size = 16;
8139
8140               if (h != NULL
8141                   && h->root.root.string[0] == '.')
8142                 {
8143                   fdh = ((struct ppc_link_hash_entry *) h)->oh;
8144                   if (fdh != NULL)
8145                     {
8146                       fdh = ppc_follow_link (fdh);
8147                       if (fdh->elf.root.type != bfd_link_hash_defined
8148                           && fdh->elf.root.type != bfd_link_hash_defweak)
8149                         fdh = NULL;
8150                     }
8151                 }
8152
8153               skip = (sym_sec->owner != ibfd
8154                       || sym_sec->output_section == bfd_abs_section_ptr);
8155               if (skip)
8156                 {
8157                   if (fdh != NULL && sym_sec->owner == ibfd)
8158                     {
8159                       /* Arrange for the function descriptor sym
8160                          to be dropped.  */
8161                       fdh->elf.root.u.def.value = 0;
8162                       fdh->elf.root.u.def.section = sym_sec;
8163                     }
8164                   opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8165
8166                   if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8167                     rel = next_rel;
8168                   else
8169                     while (1)
8170                       {
8171                         if (!dec_dynrel_count (rel->r_info, sec, info,
8172                                                NULL, h, sym))
8173                           goto error_ret;
8174
8175                         if (++rel == next_rel)
8176                           break;
8177
8178                         r_symndx = ELF64_R_SYM (rel->r_info);
8179                         if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8180                                         r_symndx, ibfd))
8181                           goto error_ret;
8182                       }
8183                 }
8184               else
8185                 {
8186                   /* We'll be keeping this opd entry.  */
8187                   long adjust;
8188
8189                   if (fdh != NULL)
8190                     {
8191                       /* Redefine the function descriptor symbol to
8192                          this location in the opd section.  It is
8193                          necessary to update the value here rather
8194                          than using an array of adjustments as we do
8195                          for local symbols, because various places
8196                          in the generic ELF code use the value
8197                          stored in u.def.value.  */
8198                       fdh->elf.root.u.def.value = wptr - new_contents;
8199                       fdh->adjust_done = 1;
8200                     }
8201
8202                   /* Local syms are a bit tricky.  We could
8203                      tweak them as they can be cached, but
8204                      we'd need to look through the local syms
8205                      for the function descriptor sym which we
8206                      don't have at the moment.  So keep an
8207                      array of adjustments.  */
8208                   adjust = (wptr - new_contents) - (rptr - sec->contents);
8209                   opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8210
8211                   if (wptr != rptr)
8212                     memcpy (wptr, rptr, opd_ent_size);
8213                   wptr += opd_ent_size;
8214                   if (add_aux_fields && opd_ent_size == 16)
8215                     {
8216                       memset (wptr, '\0', 8);
8217                       wptr += 8;
8218                     }
8219
8220                   /* We need to adjust any reloc offsets to point to the
8221                      new opd entries.  */
8222                   for ( ; rel != next_rel; ++rel)
8223                     {
8224                       rel->r_offset += adjust;
8225                       if (write_rel != rel)
8226                         memcpy (write_rel, rel, sizeof (*rel));
8227                       ++write_rel;
8228                     }
8229                 }
8230
8231               rptr += opd_ent_size;
8232             }
8233
8234           sec->size = wptr - new_contents;
8235           sec->reloc_count = write_rel - relstart;
8236           if (add_aux_fields)
8237             {
8238               free (sec->contents);
8239               sec->contents = new_contents;
8240             }
8241
8242           /* Fudge the header size too, as this is used later in
8243              elf_bfd_final_link if we are emitting relocs.  */
8244           rel_hdr = _bfd_elf_single_rel_hdr (sec);
8245           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8246           some_edited = TRUE;
8247         }
8248       else if (elf_section_data (sec)->relocs != relstart)
8249         free (relstart);
8250
8251       if (local_syms != NULL
8252           && symtab_hdr->contents != (unsigned char *) local_syms)
8253         {
8254           if (!info->keep_memory)
8255             free (local_syms);
8256           else
8257             symtab_hdr->contents = (unsigned char *) local_syms;
8258         }
8259     }
8260
8261   if (some_edited)
8262     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8263
8264   /* If we are doing a final link and the last .opd entry is just 16 byte
8265      long, add a 8 byte padding after it.  */
8266   if (need_pad != NULL && !bfd_link_relocatable (info))
8267     {
8268       bfd_byte *p;
8269
8270       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8271         {
8272           BFD_ASSERT (need_pad->size > 0);
8273
8274           p = bfd_malloc (need_pad->size + 8);
8275           if (p == NULL)
8276             return FALSE;
8277
8278           if (! bfd_get_section_contents (need_pad->owner, need_pad,
8279                                           p, 0, need_pad->size))
8280             return FALSE;
8281
8282           need_pad->contents = p;
8283           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8284         }
8285       else
8286         {
8287           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8288           if (p == NULL)
8289             return FALSE;
8290
8291           need_pad->contents = p;
8292         }
8293
8294       memset (need_pad->contents + need_pad->size, 0, 8);
8295       need_pad->size += 8;
8296     }
8297
8298   return TRUE;
8299 }
8300
8301 /* Analyze inline PLT call relocations to see whether calls to locally
8302    defined functions can be converted to direct calls.  */
8303
8304 bfd_boolean
8305 ppc64_elf_inline_plt (struct bfd_link_info *info)
8306 {
8307   struct ppc_link_hash_table *htab;
8308   bfd *ibfd;
8309   asection *sec;
8310   bfd_vma low_vma, high_vma, limit;
8311
8312   htab = ppc_hash_table (info);
8313   if (htab == NULL)
8314     return FALSE;
8315
8316   /* A bl insn can reach -0x2000000 to 0x1fffffc.  The limit is
8317      reduced somewhat to cater for possible stubs that might be added
8318      between the call and its destination.  */
8319   if (htab->params->group_size < 0)
8320     {
8321       limit = -htab->params->group_size;
8322       if (limit == 1)
8323         limit = 0x1e00000;
8324     }
8325   else
8326     {
8327       limit = htab->params->group_size;
8328       if (limit == 1)
8329         limit = 0x1c00000;
8330     }
8331
8332   low_vma = -1;
8333   high_vma = 0;
8334   for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
8335     if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
8336       {
8337         if (low_vma > sec->vma)
8338           low_vma = sec->vma;
8339         if (high_vma < sec->vma + sec->size)
8340           high_vma = sec->vma + sec->size;
8341       }
8342
8343   /* If a "bl" can reach anywhere in local code sections, then we can
8344      convert all inline PLT sequences to direct calls when the symbol
8345      is local.  */
8346   if (high_vma - low_vma < limit)
8347     {
8348       htab->can_convert_all_inline_plt = 1;
8349       return TRUE;
8350     }
8351
8352   /* Otherwise, go looking through relocs for cases where a direct
8353      call won't reach.  Mark the symbol on any such reloc to disable
8354      the optimization and keep the PLT entry as it seems likely that
8355      this will be better than creating trampolines.  Note that this
8356      will disable the optimization for all inline PLT calls to a
8357      particular symbol, not just those that won't reach.  The
8358      difficulty in doing a more precise optimization is that the
8359      linker needs to make a decision depending on whether a
8360      particular R_PPC64_PLTCALL insn can be turned into a direct
8361      call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
8362      the sequence, and there is nothing that ties those relocs
8363      together except their symbol.  */
8364
8365   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8366     {
8367       Elf_Internal_Shdr *symtab_hdr;
8368       Elf_Internal_Sym *local_syms;
8369
8370       if (!is_ppc64_elf (ibfd))
8371         continue;
8372
8373       local_syms = NULL;
8374       symtab_hdr = &elf_symtab_hdr (ibfd);
8375
8376       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8377         if (ppc64_elf_section_data (sec)->has_pltcall
8378             && !bfd_is_abs_section (sec->output_section))
8379           {
8380             Elf_Internal_Rela *relstart, *rel, *relend;
8381
8382             /* Read the relocations.  */
8383             relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8384                                                   info->keep_memory);
8385             if (relstart == NULL)
8386               return FALSE;
8387
8388             relend = relstart + sec->reloc_count;
8389             for (rel = relstart; rel < relend; )
8390               {
8391                 enum elf_ppc64_reloc_type r_type;
8392                 unsigned long r_symndx;
8393                 asection *sym_sec;
8394                 struct elf_link_hash_entry *h;
8395                 Elf_Internal_Sym *sym;
8396                 unsigned char *tls_maskp;
8397
8398                 r_type = ELF64_R_TYPE (rel->r_info);
8399                 if (r_type != R_PPC64_PLTCALL)
8400                   continue;
8401
8402                 r_symndx = ELF64_R_SYM (rel->r_info);
8403                 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
8404                                 r_symndx, ibfd))
8405                   {
8406                     if (elf_section_data (sec)->relocs != relstart)
8407                       free (relstart);
8408                     if (local_syms != NULL
8409                         && symtab_hdr->contents != (unsigned char *) local_syms)
8410                       free (local_syms);
8411                     return FALSE;
8412                   }
8413
8414                 if (sym_sec != NULL && sym_sec->output_section != NULL)
8415                   {
8416                     bfd_vma from, to;
8417                     if (h != NULL)
8418                       to = h->root.u.def.value;
8419                     else
8420                       to = sym->st_value;
8421                     to += (rel->r_addend
8422                            + sym_sec->output_offset
8423                            + sym_sec->output_section->vma);
8424                     from = (rel->r_offset
8425                             + sec->output_offset
8426                             + sec->output_section->vma);
8427                     if (to - from + limit < 2 * limit)
8428                       *tls_maskp &= ~PLT_KEEP;
8429                   }
8430               }
8431             if (elf_section_data (sec)->relocs != relstart)
8432               free (relstart);
8433           }
8434
8435       if (local_syms != NULL
8436           && symtab_hdr->contents != (unsigned char *) local_syms)
8437         {
8438           if (!info->keep_memory)
8439             free (local_syms);
8440           else
8441             symtab_hdr->contents = (unsigned char *) local_syms;
8442         }
8443     }
8444
8445   return TRUE;
8446 }
8447
8448 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
8449
8450 asection *
8451 ppc64_elf_tls_setup (struct bfd_link_info *info)
8452 {
8453   struct ppc_link_hash_table *htab;
8454
8455   htab = ppc_hash_table (info);
8456   if (htab == NULL)
8457     return NULL;
8458
8459   if (abiversion (info->output_bfd) == 1)
8460     htab->opd_abi = 1;
8461
8462   if (htab->params->no_multi_toc)
8463     htab->do_multi_toc = 0;
8464   else if (!htab->do_multi_toc)
8465     htab->params->no_multi_toc = 1;
8466
8467   /* Default to --no-plt-localentry, as this option can cause problems
8468      with symbol interposition.  For example, glibc libpthread.so and
8469      libc.so duplicate many pthread symbols, with a fallback
8470      implementation in libc.so.  In some cases the fallback does more
8471      work than the pthread implementation.  __pthread_condattr_destroy
8472      is one such symbol: the libpthread.so implementation is
8473      localentry:0 while the libc.so implementation is localentry:8.
8474      An app that "cleverly" uses dlopen to only load necessary
8475      libraries at runtime may omit loading libpthread.so when not
8476      running multi-threaded, which then results in the libc.so
8477      fallback symbols being used and ld.so complaining.  Now there
8478      are workarounds in ld (see non_zero_localentry) to detect the
8479      pthread situation, but that may not be the only case where
8480      --plt-localentry can cause trouble.  */
8481   if (htab->params->plt_localentry0 < 0)
8482     htab->params->plt_localentry0 = 0;
8483   if (htab->params->plt_localentry0
8484       && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8485                                FALSE, FALSE, FALSE) == NULL)
8486     _bfd_error_handler
8487       (_("warning: --plt-localentry is especially dangerous without "
8488          "ld.so support to detect ABI violations"));
8489
8490   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8491                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8492                                               FALSE, FALSE, TRUE));
8493   /* Move dynamic linking info to the function descriptor sym.  */
8494   if (htab->tls_get_addr != NULL)
8495     func_desc_adjust (&htab->tls_get_addr->elf, info);
8496   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8497                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8498                                                  FALSE, FALSE, TRUE));
8499   if (htab->params->tls_get_addr_opt)
8500     {
8501       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8502
8503       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8504                                   FALSE, FALSE, TRUE);
8505       if (opt != NULL)
8506         func_desc_adjust (opt, info);
8507       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8508                                      FALSE, FALSE, TRUE);
8509       if (opt_fd != NULL
8510           && (opt_fd->root.type == bfd_link_hash_defined
8511               || opt_fd->root.type == bfd_link_hash_defweak))
8512         {
8513           /* If glibc supports an optimized __tls_get_addr call stub,
8514              signalled by the presence of __tls_get_addr_opt, and we'll
8515              be calling __tls_get_addr via a plt call stub, then
8516              make __tls_get_addr point to __tls_get_addr_opt.  */
8517           tga_fd = &htab->tls_get_addr_fd->elf;
8518           if (htab->elf.dynamic_sections_created
8519               && tga_fd != NULL
8520               && (tga_fd->type == STT_FUNC
8521                   || tga_fd->needs_plt)
8522               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8523                    || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
8524             {
8525               struct plt_entry *ent;
8526
8527               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8528                 if (ent->plt.refcount > 0)
8529                   break;
8530               if (ent != NULL)
8531                 {
8532                   tga_fd->root.type = bfd_link_hash_indirect;
8533                   tga_fd->root.u.i.link = &opt_fd->root;
8534                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8535                   opt_fd->mark = 1;
8536                   if (opt_fd->dynindx != -1)
8537                     {
8538                       /* Use __tls_get_addr_opt in dynamic relocations.  */
8539                       opt_fd->dynindx = -1;
8540                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8541                                               opt_fd->dynstr_index);
8542                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8543                         return NULL;
8544                     }
8545                   htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8546                   tga = &htab->tls_get_addr->elf;
8547                   if (opt != NULL && tga != NULL)
8548                     {
8549                       tga->root.type = bfd_link_hash_indirect;
8550                       tga->root.u.i.link = &opt->root;
8551                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
8552                       opt->mark = 1;
8553                       _bfd_elf_link_hash_hide_symbol (info, opt,
8554                                                       tga->forced_local);
8555                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8556                     }
8557                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8558                   htab->tls_get_addr_fd->is_func_descriptor = 1;
8559                   if (htab->tls_get_addr != NULL)
8560                     {
8561                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8562                       htab->tls_get_addr->is_func = 1;
8563                     }
8564                 }
8565             }
8566         }
8567       else if (htab->params->tls_get_addr_opt < 0)
8568         htab->params->tls_get_addr_opt = 0;
8569     }
8570   return _bfd_elf_tls_setup (info->output_bfd, info);
8571 }
8572
8573 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8574    HASH1 or HASH2.  */
8575
8576 static bfd_boolean
8577 branch_reloc_hash_match (const bfd *ibfd,
8578                          const Elf_Internal_Rela *rel,
8579                          const struct ppc_link_hash_entry *hash1,
8580                          const struct ppc_link_hash_entry *hash2)
8581 {
8582   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8583   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8584   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8585
8586   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8587     {
8588       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8589       struct elf_link_hash_entry *h;
8590
8591       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8592       h = elf_follow_link (h);
8593       if (h == &hash1->elf || h == &hash2->elf)
8594         return TRUE;
8595     }
8596   return FALSE;
8597 }
8598
8599 /* Run through all the TLS relocs looking for optimization
8600    opportunities.  The linker has been hacked (see ppc64elf.em) to do
8601    a preliminary section layout so that we know the TLS segment
8602    offsets.  We can't optimize earlier because some optimizations need
8603    to know the tp offset, and we need to optimize before allocating
8604    dynamic relocations.  */
8605
8606 bfd_boolean
8607 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8608 {
8609   bfd *ibfd;
8610   asection *sec;
8611   struct ppc_link_hash_table *htab;
8612   unsigned char *toc_ref;
8613   int pass;
8614
8615   if (!bfd_link_executable (info))
8616     return TRUE;
8617
8618   htab = ppc_hash_table (info);
8619   if (htab == NULL)
8620     return FALSE;
8621
8622   /* Make two passes over the relocs.  On the first pass, mark toc
8623      entries involved with tls relocs, and check that tls relocs
8624      involved in setting up a tls_get_addr call are indeed followed by
8625      such a call.  If they are not, we can't do any tls optimization.
8626      On the second pass twiddle tls_mask flags to notify
8627      relocate_section that optimization can be done, and adjust got
8628      and plt refcounts.  */
8629   toc_ref = NULL;
8630   for (pass = 0; pass < 2; ++pass)
8631     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8632       {
8633         Elf_Internal_Sym *locsyms = NULL;
8634         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8635
8636         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8637           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8638             {
8639               Elf_Internal_Rela *relstart, *rel, *relend;
8640               bfd_boolean found_tls_get_addr_arg = 0;
8641
8642               /* Read the relocations.  */
8643               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8644                                                     info->keep_memory);
8645               if (relstart == NULL)
8646                 {
8647                   free (toc_ref);
8648                   return FALSE;
8649                 }
8650
8651               relend = relstart + sec->reloc_count;
8652               for (rel = relstart; rel < relend; rel++)
8653                 {
8654                   enum elf_ppc64_reloc_type r_type;
8655                   unsigned long r_symndx;
8656                   struct elf_link_hash_entry *h;
8657                   Elf_Internal_Sym *sym;
8658                   asection *sym_sec;
8659                   unsigned char *tls_mask;
8660                   unsigned char tls_set, tls_clear, tls_type = 0;
8661                   bfd_vma value;
8662                   bfd_boolean ok_tprel, is_local;
8663                   long toc_ref_index = 0;
8664                   int expecting_tls_get_addr = 0;
8665                   bfd_boolean ret = FALSE;
8666
8667                   r_symndx = ELF64_R_SYM (rel->r_info);
8668                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8669                                   r_symndx, ibfd))
8670                     {
8671                     err_free_rel:
8672                       if (elf_section_data (sec)->relocs != relstart)
8673                         free (relstart);
8674                       if (toc_ref != NULL)
8675                         free (toc_ref);
8676                       if (locsyms != NULL
8677                           && (elf_symtab_hdr (ibfd).contents
8678                               != (unsigned char *) locsyms))
8679                         free (locsyms);
8680                       return ret;
8681                     }
8682
8683                   if (h != NULL)
8684                     {
8685                       if (h->root.type == bfd_link_hash_defined
8686                           || h->root.type == bfd_link_hash_defweak)
8687                         value = h->root.u.def.value;
8688                       else if (h->root.type == bfd_link_hash_undefweak)
8689                         value = 0;
8690                       else
8691                         {
8692                           found_tls_get_addr_arg = 0;
8693                           continue;
8694                         }
8695                     }
8696                   else
8697                     /* Symbols referenced by TLS relocs must be of type
8698                        STT_TLS.  So no need for .opd local sym adjust.  */
8699                     value = sym->st_value;
8700
8701                   ok_tprel = FALSE;
8702                   is_local = FALSE;
8703                   if (h == NULL
8704                       || !h->def_dynamic)
8705                     {
8706                       is_local = TRUE;
8707                       if (h != NULL
8708                           && h->root.type == bfd_link_hash_undefweak)
8709                         ok_tprel = TRUE;
8710                       else if (sym_sec != NULL
8711                                && sym_sec->output_section != NULL)
8712                         {
8713                           value += sym_sec->output_offset;
8714                           value += sym_sec->output_section->vma;
8715                           value -= htab->elf.tls_sec->vma;
8716                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8717                                       < (bfd_vma) 1 << 32);
8718                         }
8719                     }
8720
8721                   r_type = ELF64_R_TYPE (rel->r_info);
8722                   /* If this section has old-style __tls_get_addr calls
8723                      without marker relocs, then check that each
8724                      __tls_get_addr call reloc is preceded by a reloc
8725                      that conceivably belongs to the __tls_get_addr arg
8726                      setup insn.  If we don't find matching arg setup
8727                      relocs, don't do any tls optimization.  */
8728                   if (pass == 0
8729                       && sec->has_tls_get_addr_call
8730                       && h != NULL
8731                       && (h == &htab->tls_get_addr->elf
8732                           || h == &htab->tls_get_addr_fd->elf)
8733                       && !found_tls_get_addr_arg
8734                       && is_branch_reloc (r_type))
8735                     {
8736                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8737                                                 "TLS optimization disabled\n"),
8738                                               ibfd, sec, rel->r_offset);
8739                       ret = TRUE;
8740                       goto err_free_rel;
8741                     }
8742
8743                   found_tls_get_addr_arg = 0;
8744                   switch (r_type)
8745                     {
8746                     case R_PPC64_GOT_TLSLD16:
8747                     case R_PPC64_GOT_TLSLD16_LO:
8748                       expecting_tls_get_addr = 1;
8749                       found_tls_get_addr_arg = 1;
8750                       /* Fall through.  */
8751
8752                     case R_PPC64_GOT_TLSLD16_HI:
8753                     case R_PPC64_GOT_TLSLD16_HA:
8754                       /* These relocs should never be against a symbol
8755                          defined in a shared lib.  Leave them alone if
8756                          that turns out to be the case.  */
8757                       if (!is_local)
8758                         continue;
8759
8760                       /* LD -> LE */
8761                       tls_set = 0;
8762                       tls_clear = TLS_LD;
8763                       tls_type = TLS_TLS | TLS_LD;
8764                       break;
8765
8766                     case R_PPC64_GOT_TLSGD16:
8767                     case R_PPC64_GOT_TLSGD16_LO:
8768                       expecting_tls_get_addr = 1;
8769                       found_tls_get_addr_arg = 1;
8770                       /* Fall through. */
8771
8772                     case R_PPC64_GOT_TLSGD16_HI:
8773                     case R_PPC64_GOT_TLSGD16_HA:
8774                       if (ok_tprel)
8775                         /* GD -> LE */
8776                         tls_set = 0;
8777                       else
8778                         /* GD -> IE */
8779                         tls_set = TLS_TLS | TLS_TPRELGD;
8780                       tls_clear = TLS_GD;
8781                       tls_type = TLS_TLS | TLS_GD;
8782                       break;
8783
8784                     case R_PPC64_GOT_TPREL16_DS:
8785                     case R_PPC64_GOT_TPREL16_LO_DS:
8786                     case R_PPC64_GOT_TPREL16_HI:
8787                     case R_PPC64_GOT_TPREL16_HA:
8788                       if (ok_tprel)
8789                         {
8790                           /* IE -> LE */
8791                           tls_set = 0;
8792                           tls_clear = TLS_TPREL;
8793                           tls_type = TLS_TLS | TLS_TPREL;
8794                           break;
8795                         }
8796                       continue;
8797
8798                     case R_PPC64_TLSGD:
8799                     case R_PPC64_TLSLD:
8800                       if (rel + 1 < relend
8801                           && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8802                         {
8803                           if (pass != 0
8804                               && ELF64_R_TYPE (rel[1].r_info) != R_PPC64_PLTSEQ)
8805                             {
8806                               r_symndx = ELF64_R_SYM (rel[1].r_info);
8807                               if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8808                                   r_symndx, ibfd))
8809                                 goto err_free_rel;
8810                               if (h != NULL)
8811                                 {
8812                                   struct plt_entry *ent = NULL;
8813
8814                                   for (ent = h->plt.plist;
8815                                        ent != NULL;
8816                                        ent = ent->next)
8817                                     if (ent->addend == rel[1].r_addend)
8818                                       break;
8819
8820                                   if (ent != NULL
8821                                       && ent->plt.refcount > 0)
8822                                     ent->plt.refcount -= 1;
8823                                 }
8824                             }
8825                           continue;
8826                         }
8827                       found_tls_get_addr_arg = 1;
8828                       /* Fall through.  */
8829
8830                     case R_PPC64_TLS:
8831                     case R_PPC64_TOC16:
8832                     case R_PPC64_TOC16_LO:
8833                       if (sym_sec == NULL || sym_sec != toc)
8834                         continue;
8835
8836                       /* Mark this toc entry as referenced by a TLS
8837                          code sequence.  We can do that now in the
8838                          case of R_PPC64_TLS, and after checking for
8839                          tls_get_addr for the TOC16 relocs.  */
8840                       if (toc_ref == NULL)
8841                         toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8842                       if (toc_ref == NULL)
8843                         goto err_free_rel;
8844
8845                       if (h != NULL)
8846                         value = h->root.u.def.value;
8847                       else
8848                         value = sym->st_value;
8849                       value += rel->r_addend;
8850                       if (value % 8 != 0)
8851                         continue;
8852                       BFD_ASSERT (value < toc->size
8853                                   && toc->output_offset % 8 == 0);
8854                       toc_ref_index = (value + toc->output_offset) / 8;
8855                       if (r_type == R_PPC64_TLS
8856                           || r_type == R_PPC64_TLSGD
8857                           || r_type == R_PPC64_TLSLD)
8858                         {
8859                           toc_ref[toc_ref_index] = 1;
8860                           continue;
8861                         }
8862
8863                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
8864                         continue;
8865
8866                       tls_set = 0;
8867                       tls_clear = 0;
8868                       expecting_tls_get_addr = 2;
8869                       break;
8870
8871                     case R_PPC64_TPREL64:
8872                       if (pass == 0
8873                           || sec != toc
8874                           || toc_ref == NULL
8875                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8876                         continue;
8877                       if (ok_tprel)
8878                         {
8879                           /* IE -> LE */
8880                           tls_set = TLS_EXPLICIT;
8881                           tls_clear = TLS_TPREL;
8882                           break;
8883                         }
8884                       continue;
8885
8886                     case R_PPC64_DTPMOD64:
8887                       if (pass == 0
8888                           || sec != toc
8889                           || toc_ref == NULL
8890                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8891                         continue;
8892                       if (rel + 1 < relend
8893                           && (rel[1].r_info
8894                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8895                           && rel[1].r_offset == rel->r_offset + 8)
8896                         {
8897                           if (ok_tprel)
8898                             /* GD -> LE */
8899                             tls_set = TLS_EXPLICIT | TLS_GD;
8900                           else
8901                             /* GD -> IE */
8902                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8903                           tls_clear = TLS_GD;
8904                         }
8905                       else
8906                         {
8907                           if (!is_local)
8908                             continue;
8909
8910                           /* LD -> LE */
8911                           tls_set = TLS_EXPLICIT;
8912                           tls_clear = TLS_LD;
8913                         }
8914                       break;
8915
8916                     default:
8917                       continue;
8918                     }
8919
8920                   if (pass == 0)
8921                     {
8922                       if (!expecting_tls_get_addr
8923                           || !sec->has_tls_get_addr_call)
8924                         continue;
8925
8926                       if (rel + 1 < relend
8927                           && branch_reloc_hash_match (ibfd, rel + 1,
8928                                                       htab->tls_get_addr,
8929                                                       htab->tls_get_addr_fd))
8930                         {
8931                           if (expecting_tls_get_addr == 2)
8932                             {
8933                               /* Check for toc tls entries.  */
8934                               unsigned char *toc_tls;
8935                               int retval;
8936
8937                               retval = get_tls_mask (&toc_tls, NULL, NULL,
8938                                                      &locsyms,
8939                                                      rel, ibfd);
8940                               if (retval == 0)
8941                                 goto err_free_rel;
8942                               if (toc_tls != NULL)
8943                                 {
8944                                   if ((*toc_tls & TLS_TLS) != 0
8945                                       && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8946                                     found_tls_get_addr_arg = 1;
8947                                   if (retval > 1)
8948                                     toc_ref[toc_ref_index] = 1;
8949                                 }
8950                             }
8951                           continue;
8952                         }
8953
8954                       /* Uh oh, we didn't find the expected call.  We
8955                          could just mark this symbol to exclude it
8956                          from tls optimization but it's safer to skip
8957                          the entire optimization.  */
8958                       /* xgettext:c-format */
8959                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8960                                                 "TLS optimization disabled\n"),
8961                                               ibfd, sec, rel->r_offset);
8962                       ret = TRUE;
8963                       goto err_free_rel;
8964                     }
8965
8966                   /* If we don't have old-style __tls_get_addr calls
8967                      without TLSGD/TLSLD marker relocs, and we haven't
8968                      found a new-style __tls_get_addr call with a
8969                      marker for this symbol, then we either have a
8970                      broken object file or an -mlongcall style
8971                      indirect call to __tls_get_addr without a marker.
8972                      Disable optimization in this case.  */
8973                   if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8974                       && (tls_set & TLS_EXPLICIT) == 0
8975                       && !sec->has_tls_get_addr_call
8976                       && ((*tls_mask & (TLS_TLS | TLS_MARK))
8977                           != (TLS_TLS | TLS_MARK)))
8978                     continue;
8979
8980                   if (expecting_tls_get_addr)
8981                     {
8982                       struct plt_entry *ent = NULL;
8983
8984                       if (htab->tls_get_addr != NULL)
8985                         for (ent = htab->tls_get_addr->elf.plt.plist;
8986                              ent != NULL;
8987                              ent = ent->next)
8988                           if (ent->addend == 0)
8989                             break;
8990
8991                       if (ent == NULL && htab->tls_get_addr_fd != NULL)
8992                         for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8993                              ent != NULL;
8994                              ent = ent->next)
8995                           if (ent->addend == 0)
8996                             break;
8997
8998                       if (ent != NULL
8999                           && ent->plt.refcount > 0)
9000                         ent->plt.refcount -= 1;
9001                     }
9002
9003                   if (tls_clear == 0)
9004                     continue;
9005
9006                   if ((tls_set & TLS_EXPLICIT) == 0)
9007                     {
9008                       struct got_entry *ent;
9009
9010                       /* Adjust got entry for this reloc.  */
9011                       if (h != NULL)
9012                         ent = h->got.glist;
9013                       else
9014                         ent = elf_local_got_ents (ibfd)[r_symndx];
9015
9016                       for (; ent != NULL; ent = ent->next)
9017                         if (ent->addend == rel->r_addend
9018                             && ent->owner == ibfd
9019                             && ent->tls_type == tls_type)
9020                           break;
9021                       if (ent == NULL)
9022                         abort ();
9023
9024                       if (tls_set == 0)
9025                         {
9026                           /* We managed to get rid of a got entry.  */
9027                           if (ent->got.refcount > 0)
9028                             ent->got.refcount -= 1;
9029                         }
9030                     }
9031                   else
9032                     {
9033                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
9034                          we'll lose one or two dyn relocs.  */
9035                       if (!dec_dynrel_count (rel->r_info, sec, info,
9036                                              NULL, h, sym))
9037                         return FALSE;
9038
9039                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
9040                         {
9041                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
9042                                                  NULL, h, sym))
9043                             return FALSE;
9044                         }
9045                     }
9046
9047                   *tls_mask |= tls_set;
9048                   *tls_mask &= ~tls_clear;
9049                 }
9050
9051               if (elf_section_data (sec)->relocs != relstart)
9052                 free (relstart);
9053             }
9054
9055         if (locsyms != NULL
9056             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
9057           {
9058             if (!info->keep_memory)
9059               free (locsyms);
9060             else
9061               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
9062           }
9063       }
9064
9065   if (toc_ref != NULL)
9066     free (toc_ref);
9067   htab->do_tls_opt = 1;
9068   return TRUE;
9069 }
9070
9071 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
9072    the values of any global symbols in a toc section that has been
9073    edited.  Globals in toc sections should be a rarity, so this function
9074    sets a flag if any are found in toc sections other than the one just
9075    edited, so that further hash table traversals can be avoided.  */
9076
9077 struct adjust_toc_info
9078 {
9079   asection *toc;
9080   unsigned long *skip;
9081   bfd_boolean global_toc_syms;
9082 };
9083
9084 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
9085
9086 static bfd_boolean
9087 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
9088 {
9089   struct ppc_link_hash_entry *eh;
9090   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
9091   unsigned long i;
9092
9093   if (h->root.type != bfd_link_hash_defined
9094       && h->root.type != bfd_link_hash_defweak)
9095     return TRUE;
9096
9097   eh = (struct ppc_link_hash_entry *) h;
9098   if (eh->adjust_done)
9099     return TRUE;
9100
9101   if (eh->elf.root.u.def.section == toc_inf->toc)
9102     {
9103       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
9104         i = toc_inf->toc->rawsize >> 3;
9105       else
9106         i = eh->elf.root.u.def.value >> 3;
9107
9108       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
9109         {
9110           _bfd_error_handler
9111             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
9112           do
9113             ++i;
9114           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
9115           eh->elf.root.u.def.value = (bfd_vma) i << 3;
9116         }
9117
9118       eh->elf.root.u.def.value -= toc_inf->skip[i];
9119       eh->adjust_done = 1;
9120     }
9121   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
9122     toc_inf->global_toc_syms = TRUE;
9123
9124   return TRUE;
9125 }
9126
9127 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
9128    on a _LO variety toc/got reloc.  */
9129
9130 static bfd_boolean
9131 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
9132 {
9133   return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
9134           || (insn & (0x3f << 26)) == 14u << 26 /* addi */
9135           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
9136           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
9137           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
9138           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
9139           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
9140           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
9141           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
9142           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
9143           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
9144           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
9145           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
9146           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
9147           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
9148           || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
9149           || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
9150               /* Exclude lfqu by testing reloc.  If relocs are ever
9151                  defined for the reduced D field in psq_lu then those
9152                  will need testing too.  */
9153               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9154           || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
9155               && (insn & 1) == 0)
9156           || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
9157           || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
9158               /* Exclude stfqu.  psq_stu as above for psq_lu.  */
9159               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9160           || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
9161               && (insn & 1) == 0));
9162 }
9163
9164 /* Examine all relocs referencing .toc sections in order to remove
9165    unused .toc entries.  */
9166
9167 bfd_boolean
9168 ppc64_elf_edit_toc (struct bfd_link_info *info)
9169 {
9170   bfd *ibfd;
9171   struct adjust_toc_info toc_inf;
9172   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9173
9174   htab->do_toc_opt = 1;
9175   toc_inf.global_toc_syms = TRUE;
9176   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9177     {
9178       asection *toc, *sec;
9179       Elf_Internal_Shdr *symtab_hdr;
9180       Elf_Internal_Sym *local_syms;
9181       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
9182       unsigned long *skip, *drop;
9183       unsigned char *used;
9184       unsigned char *keep, last, some_unused;
9185
9186       if (!is_ppc64_elf (ibfd))
9187         continue;
9188
9189       toc = bfd_get_section_by_name (ibfd, ".toc");
9190       if (toc == NULL
9191           || toc->size == 0
9192           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
9193           || discarded_section (toc))
9194         continue;
9195
9196       toc_relocs = NULL;
9197       local_syms = NULL;
9198       symtab_hdr = &elf_symtab_hdr (ibfd);
9199
9200       /* Look at sections dropped from the final link.  */
9201       skip = NULL;
9202       relstart = NULL;
9203       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9204         {
9205           if (sec->reloc_count == 0
9206               || !discarded_section (sec)
9207               || get_opd_info (sec)
9208               || (sec->flags & SEC_ALLOC) == 0
9209               || (sec->flags & SEC_DEBUGGING) != 0)
9210             continue;
9211
9212           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
9213           if (relstart == NULL)
9214             goto error_ret;
9215
9216           /* Run through the relocs to see which toc entries might be
9217              unused.  */
9218           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9219             {
9220               enum elf_ppc64_reloc_type r_type;
9221               unsigned long r_symndx;
9222               asection *sym_sec;
9223               struct elf_link_hash_entry *h;
9224               Elf_Internal_Sym *sym;
9225               bfd_vma val;
9226
9227               r_type = ELF64_R_TYPE (rel->r_info);
9228               switch (r_type)
9229                 {
9230                 default:
9231                   continue;
9232
9233                 case R_PPC64_TOC16:
9234                 case R_PPC64_TOC16_LO:
9235                 case R_PPC64_TOC16_HI:
9236                 case R_PPC64_TOC16_HA:
9237                 case R_PPC64_TOC16_DS:
9238                 case R_PPC64_TOC16_LO_DS:
9239                   break;
9240                 }
9241
9242               r_symndx = ELF64_R_SYM (rel->r_info);
9243               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9244                               r_symndx, ibfd))
9245                 goto error_ret;
9246
9247               if (sym_sec != toc)
9248                 continue;
9249
9250               if (h != NULL)
9251                 val = h->root.u.def.value;
9252               else
9253                 val = sym->st_value;
9254               val += rel->r_addend;
9255
9256               if (val >= toc->size)
9257                 continue;
9258
9259               /* Anything in the toc ought to be aligned to 8 bytes.
9260                  If not, don't mark as unused.  */
9261               if (val & 7)
9262                 continue;
9263
9264               if (skip == NULL)
9265                 {
9266                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9267                   if (skip == NULL)
9268                     goto error_ret;
9269                 }
9270
9271               skip[val >> 3] = ref_from_discarded;
9272             }
9273
9274           if (elf_section_data (sec)->relocs != relstart)
9275             free (relstart);
9276         }
9277
9278       /* For largetoc loads of address constants, we can convert
9279          .  addis rx,2,addr@got@ha
9280          .  ld ry,addr@got@l(rx)
9281          to
9282          .  addis rx,2,addr@toc@ha
9283          .  addi ry,rx,addr@toc@l
9284          when addr is within 2G of the toc pointer.  This then means
9285          that the word storing "addr" in the toc is no longer needed.  */
9286
9287       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9288           && toc->output_section->rawsize < (bfd_vma) 1 << 31
9289           && toc->reloc_count != 0)
9290         {
9291           /* Read toc relocs.  */
9292           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9293                                                   info->keep_memory);
9294           if (toc_relocs == NULL)
9295             goto error_ret;
9296
9297           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9298             {
9299               enum elf_ppc64_reloc_type r_type;
9300               unsigned long r_symndx;
9301               asection *sym_sec;
9302               struct elf_link_hash_entry *h;
9303               Elf_Internal_Sym *sym;
9304               bfd_vma val, addr;
9305
9306               r_type = ELF64_R_TYPE (rel->r_info);
9307               if (r_type != R_PPC64_ADDR64)
9308                 continue;
9309
9310               r_symndx = ELF64_R_SYM (rel->r_info);
9311               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9312                               r_symndx, ibfd))
9313                 goto error_ret;
9314
9315               if (sym_sec == NULL
9316                   || sym_sec->output_section == NULL
9317                   || discarded_section (sym_sec))
9318                 continue;
9319
9320               if (!SYMBOL_REFERENCES_LOCAL (info, h))
9321                 continue;
9322
9323               if (h != NULL)
9324                 {
9325                   if (h->type == STT_GNU_IFUNC)
9326                     continue;
9327                   val = h->root.u.def.value;
9328                 }
9329               else
9330                 {
9331                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9332                     continue;
9333                   val = sym->st_value;
9334                 }
9335               val += rel->r_addend;
9336               val += sym_sec->output_section->vma + sym_sec->output_offset;
9337
9338               /* We don't yet know the exact toc pointer value, but we
9339                  know it will be somewhere in the toc section.  Don't
9340                  optimize if the difference from any possible toc
9341                  pointer is outside [ff..f80008000, 7fff7fff].  */
9342               addr = toc->output_section->vma + TOC_BASE_OFF;
9343               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9344                 continue;
9345
9346               addr = toc->output_section->vma + toc->output_section->rawsize;
9347               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9348                 continue;
9349
9350               if (skip == NULL)
9351                 {
9352                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9353                   if (skip == NULL)
9354                     goto error_ret;
9355                 }
9356
9357               skip[rel->r_offset >> 3]
9358                 |= can_optimize | ((rel - toc_relocs) << 2);
9359             }
9360         }
9361
9362       if (skip == NULL)
9363         continue;
9364
9365       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9366       if (used == NULL)
9367         {
9368         error_ret:
9369           if (local_syms != NULL
9370               && symtab_hdr->contents != (unsigned char *) local_syms)
9371             free (local_syms);
9372           if (sec != NULL
9373               && relstart != NULL
9374               && elf_section_data (sec)->relocs != relstart)
9375             free (relstart);
9376           if (toc_relocs != NULL
9377               && elf_section_data (toc)->relocs != toc_relocs)
9378             free (toc_relocs);
9379           if (skip != NULL)
9380             free (skip);
9381           return FALSE;
9382         }
9383
9384       /* Now check all kept sections that might reference the toc.
9385          Check the toc itself last.  */
9386       for (sec = (ibfd->sections == toc && toc->next ? toc->next
9387                   : ibfd->sections);
9388            sec != NULL;
9389            sec = (sec == toc ? NULL
9390                   : sec->next == NULL ? toc
9391                   : sec->next == toc && toc->next ? toc->next
9392                   : sec->next))
9393         {
9394           int repeat;
9395
9396           if (sec->reloc_count == 0
9397               || discarded_section (sec)
9398               || get_opd_info (sec)
9399               || (sec->flags & SEC_ALLOC) == 0
9400               || (sec->flags & SEC_DEBUGGING) != 0)
9401             continue;
9402
9403           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9404                                                 info->keep_memory);
9405           if (relstart == NULL)
9406             {
9407               free (used);
9408               goto error_ret;
9409             }
9410
9411           /* Mark toc entries referenced as used.  */
9412           do
9413             {
9414               repeat = 0;
9415               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9416                 {
9417                   enum elf_ppc64_reloc_type r_type;
9418                   unsigned long r_symndx;
9419                   asection *sym_sec;
9420                   struct elf_link_hash_entry *h;
9421                   Elf_Internal_Sym *sym;
9422                   bfd_vma val;
9423                   enum {no_check, check_lo, check_ha} insn_check;
9424
9425                   r_type = ELF64_R_TYPE (rel->r_info);
9426                   switch (r_type)
9427                     {
9428                     default:
9429                       insn_check = no_check;
9430                       break;
9431
9432                     case R_PPC64_GOT_TLSLD16_HA:
9433                     case R_PPC64_GOT_TLSGD16_HA:
9434                     case R_PPC64_GOT_TPREL16_HA:
9435                     case R_PPC64_GOT_DTPREL16_HA:
9436                     case R_PPC64_GOT16_HA:
9437                     case R_PPC64_TOC16_HA:
9438                       insn_check = check_ha;
9439                       break;
9440
9441                     case R_PPC64_GOT_TLSLD16_LO:
9442                     case R_PPC64_GOT_TLSGD16_LO:
9443                     case R_PPC64_GOT_TPREL16_LO_DS:
9444                     case R_PPC64_GOT_DTPREL16_LO_DS:
9445                     case R_PPC64_GOT16_LO:
9446                     case R_PPC64_GOT16_LO_DS:
9447                     case R_PPC64_TOC16_LO:
9448                     case R_PPC64_TOC16_LO_DS:
9449                       insn_check = check_lo;
9450                       break;
9451                     }
9452
9453                   if (insn_check != no_check)
9454                     {
9455                       bfd_vma off = rel->r_offset & ~3;
9456                       unsigned char buf[4];
9457                       unsigned int insn;
9458
9459                       if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9460                         {
9461                           free (used);
9462                           goto error_ret;
9463                         }
9464                       insn = bfd_get_32 (ibfd, buf);
9465                       if (insn_check == check_lo
9466                           ? !ok_lo_toc_insn (insn, r_type)
9467                           : ((insn & ((0x3f << 26) | 0x1f << 16))
9468                              != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9469                         {
9470                           char str[12];
9471
9472                           ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9473                           sprintf (str, "%#08x", insn);
9474                           info->callbacks->einfo
9475                             /* xgettext:c-format */
9476                             (_("%H: toc optimization is not supported for"
9477                                " %s instruction\n"),
9478                              ibfd, sec, rel->r_offset & ~3, str);
9479                         }
9480                     }
9481
9482                   switch (r_type)
9483                     {
9484                     case R_PPC64_TOC16:
9485                     case R_PPC64_TOC16_LO:
9486                     case R_PPC64_TOC16_HI:
9487                     case R_PPC64_TOC16_HA:
9488                     case R_PPC64_TOC16_DS:
9489                     case R_PPC64_TOC16_LO_DS:
9490                       /* In case we're taking addresses of toc entries.  */
9491                     case R_PPC64_ADDR64:
9492                       break;
9493
9494                     default:
9495                       continue;
9496                     }
9497
9498                   r_symndx = ELF64_R_SYM (rel->r_info);
9499                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9500                                   r_symndx, ibfd))
9501                     {
9502                       free (used);
9503                       goto error_ret;
9504                     }
9505
9506                   if (sym_sec != toc)
9507                     continue;
9508
9509                   if (h != NULL)
9510                     val = h->root.u.def.value;
9511                   else
9512                     val = sym->st_value;
9513                   val += rel->r_addend;
9514
9515                   if (val >= toc->size)
9516                     continue;
9517
9518                   if ((skip[val >> 3] & can_optimize) != 0)
9519                     {
9520                       bfd_vma off;
9521                       unsigned char opc;
9522
9523                       switch (r_type)
9524                         {
9525                         case R_PPC64_TOC16_HA:
9526                           break;
9527
9528                         case R_PPC64_TOC16_LO_DS:
9529                           off = rel->r_offset;
9530                           off += (bfd_big_endian (ibfd) ? -2 : 3);
9531                           if (!bfd_get_section_contents (ibfd, sec, &opc,
9532                                                          off, 1))
9533                             {
9534                               free (used);
9535                               goto error_ret;
9536                             }
9537                           if ((opc & (0x3f << 2)) == (58u << 2))
9538                             break;
9539                           /* Fall through.  */
9540
9541                         default:
9542                           /* Wrong sort of reloc, or not a ld.  We may
9543                              as well clear ref_from_discarded too.  */
9544                           skip[val >> 3] = 0;
9545                         }
9546                     }
9547
9548                   if (sec != toc)
9549                     used[val >> 3] = 1;
9550                   /* For the toc section, we only mark as used if this
9551                      entry itself isn't unused.  */
9552                   else if ((used[rel->r_offset >> 3]
9553                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9554                            && !used[val >> 3])
9555                     {
9556                       /* Do all the relocs again, to catch reference
9557                          chains.  */
9558                       repeat = 1;
9559                       used[val >> 3] = 1;
9560                     }
9561                 }
9562             }
9563           while (repeat);
9564
9565           if (elf_section_data (sec)->relocs != relstart)
9566             free (relstart);
9567         }
9568
9569       /* Merge the used and skip arrays.  Assume that TOC
9570          doublewords not appearing as either used or unused belong
9571          to an entry more than one doubleword in size.  */
9572       for (drop = skip, keep = used, last = 0, some_unused = 0;
9573            drop < skip + (toc->size + 7) / 8;
9574            ++drop, ++keep)
9575         {
9576           if (*keep)
9577             {
9578               *drop &= ~ref_from_discarded;
9579               if ((*drop & can_optimize) != 0)
9580                 some_unused = 1;
9581               last = 0;
9582             }
9583           else if ((*drop & ref_from_discarded) != 0)
9584             {
9585               some_unused = 1;
9586               last = ref_from_discarded;
9587             }
9588           else
9589             *drop = last;
9590         }
9591
9592       free (used);
9593
9594       if (some_unused)
9595         {
9596           bfd_byte *contents, *src;
9597           unsigned long off;
9598           Elf_Internal_Sym *sym;
9599           bfd_boolean local_toc_syms = FALSE;
9600
9601           /* Shuffle the toc contents, and at the same time convert the
9602              skip array from booleans into offsets.  */
9603           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9604             goto error_ret;
9605
9606           elf_section_data (toc)->this_hdr.contents = contents;
9607
9608           for (src = contents, off = 0, drop = skip;
9609                src < contents + toc->size;
9610                src += 8, ++drop)
9611             {
9612               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9613                 off += 8;
9614               else if (off != 0)
9615                 {
9616                   *drop = off;
9617                   memcpy (src - off, src, 8);
9618                 }
9619             }
9620           *drop = off;
9621           toc->rawsize = toc->size;
9622           toc->size = src - contents - off;
9623
9624           /* Adjust addends for relocs against the toc section sym,
9625              and optimize any accesses we can.  */
9626           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9627             {
9628               if (sec->reloc_count == 0
9629                   || discarded_section (sec))
9630                 continue;
9631
9632               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9633                                                     info->keep_memory);
9634               if (relstart == NULL)
9635                 goto error_ret;
9636
9637               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9638                 {
9639                   enum elf_ppc64_reloc_type r_type;
9640                   unsigned long r_symndx;
9641                   asection *sym_sec;
9642                   struct elf_link_hash_entry *h;
9643                   bfd_vma val;
9644
9645                   r_type = ELF64_R_TYPE (rel->r_info);
9646                   switch (r_type)
9647                     {
9648                     default:
9649                       continue;
9650
9651                     case R_PPC64_TOC16:
9652                     case R_PPC64_TOC16_LO:
9653                     case R_PPC64_TOC16_HI:
9654                     case R_PPC64_TOC16_HA:
9655                     case R_PPC64_TOC16_DS:
9656                     case R_PPC64_TOC16_LO_DS:
9657                     case R_PPC64_ADDR64:
9658                       break;
9659                     }
9660
9661                   r_symndx = ELF64_R_SYM (rel->r_info);
9662                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9663                                   r_symndx, ibfd))
9664                     goto error_ret;
9665
9666                   if (sym_sec != toc)
9667                     continue;
9668
9669                   if (h != NULL)
9670                     val = h->root.u.def.value;
9671                   else
9672                     {
9673                       val = sym->st_value;
9674                       if (val != 0)
9675                         local_toc_syms = TRUE;
9676                     }
9677
9678                   val += rel->r_addend;
9679
9680                   if (val > toc->rawsize)
9681                     val = toc->rawsize;
9682                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
9683                     continue;
9684                   else if ((skip[val >> 3] & can_optimize) != 0)
9685                     {
9686                       Elf_Internal_Rela *tocrel
9687                         = toc_relocs + (skip[val >> 3] >> 2);
9688                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9689
9690                       switch (r_type)
9691                         {
9692                         case R_PPC64_TOC16_HA:
9693                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9694                           break;
9695
9696                         case R_PPC64_TOC16_LO_DS:
9697                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9698                           break;
9699
9700                         default:
9701                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9702                             ppc_howto_init ();
9703                           info->callbacks->einfo
9704                             /* xgettext:c-format */
9705                             (_("%H: %s references "
9706                                "optimized away TOC entry\n"),
9707                              ibfd, sec, rel->r_offset,
9708                              ppc64_elf_howto_table[r_type]->name);
9709                           bfd_set_error (bfd_error_bad_value);
9710                           goto error_ret;
9711                         }
9712                       rel->r_addend = tocrel->r_addend;
9713                       elf_section_data (sec)->relocs = relstart;
9714                       continue;
9715                     }
9716
9717                   if (h != NULL || sym->st_value != 0)
9718                     continue;
9719
9720                   rel->r_addend -= skip[val >> 3];
9721                   elf_section_data (sec)->relocs = relstart;
9722                 }
9723
9724               if (elf_section_data (sec)->relocs != relstart)
9725                 free (relstart);
9726             }
9727
9728           /* We shouldn't have local or global symbols defined in the TOC,
9729              but handle them anyway.  */
9730           if (local_syms != NULL)
9731             for (sym = local_syms;
9732                  sym < local_syms + symtab_hdr->sh_info;
9733                  ++sym)
9734               if (sym->st_value != 0
9735                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9736                 {
9737                   unsigned long i;
9738
9739                   if (sym->st_value > toc->rawsize)
9740                     i = toc->rawsize >> 3;
9741                   else
9742                     i = sym->st_value >> 3;
9743
9744                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9745                     {
9746                       if (local_toc_syms)
9747                         _bfd_error_handler
9748                           (_("%s defined on removed toc entry"),
9749                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9750                       do
9751                         ++i;
9752                       while ((skip[i] & (ref_from_discarded | can_optimize)));
9753                       sym->st_value = (bfd_vma) i << 3;
9754                     }
9755
9756                   sym->st_value -= skip[i];
9757                   symtab_hdr->contents = (unsigned char *) local_syms;
9758                 }
9759
9760           /* Adjust any global syms defined in this toc input section.  */
9761           if (toc_inf.global_toc_syms)
9762             {
9763               toc_inf.toc = toc;
9764               toc_inf.skip = skip;
9765               toc_inf.global_toc_syms = FALSE;
9766               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9767                                       &toc_inf);
9768             }
9769
9770           if (toc->reloc_count != 0)
9771             {
9772               Elf_Internal_Shdr *rel_hdr;
9773               Elf_Internal_Rela *wrel;
9774               bfd_size_type sz;
9775
9776               /* Remove unused toc relocs, and adjust those we keep.  */
9777               if (toc_relocs == NULL)
9778                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9779                                                         info->keep_memory);
9780               if (toc_relocs == NULL)
9781                 goto error_ret;
9782
9783               wrel = toc_relocs;
9784               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9785                 if ((skip[rel->r_offset >> 3]
9786                      & (ref_from_discarded | can_optimize)) == 0)
9787                   {
9788                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9789                     wrel->r_info = rel->r_info;
9790                     wrel->r_addend = rel->r_addend;
9791                     ++wrel;
9792                   }
9793                 else if (!dec_dynrel_count (rel->r_info, toc, info,
9794                                             &local_syms, NULL, NULL))
9795                   goto error_ret;
9796
9797               elf_section_data (toc)->relocs = toc_relocs;
9798               toc->reloc_count = wrel - toc_relocs;
9799               rel_hdr = _bfd_elf_single_rel_hdr (toc);
9800               sz = rel_hdr->sh_entsize;
9801               rel_hdr->sh_size = toc->reloc_count * sz;
9802             }
9803         }
9804       else if (toc_relocs != NULL
9805                && elf_section_data (toc)->relocs != toc_relocs)
9806         free (toc_relocs);
9807
9808       if (local_syms != NULL
9809           && symtab_hdr->contents != (unsigned char *) local_syms)
9810         {
9811           if (!info->keep_memory)
9812             free (local_syms);
9813           else
9814             symtab_hdr->contents = (unsigned char *) local_syms;
9815         }
9816       free (skip);
9817     }
9818
9819   return TRUE;
9820 }
9821
9822 /* Return true iff input section I references the TOC using
9823    instructions limited to +/-32k offsets.  */
9824
9825 bfd_boolean
9826 ppc64_elf_has_small_toc_reloc (asection *i)
9827 {
9828   return (is_ppc64_elf (i->owner)
9829           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9830 }
9831
9832 /* Allocate space for one GOT entry.  */
9833
9834 static void
9835 allocate_got (struct elf_link_hash_entry *h,
9836               struct bfd_link_info *info,
9837               struct got_entry *gent)
9838 {
9839   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9840   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9841   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9842                  ? 16 : 8);
9843   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9844                   ? 2 : 1) * sizeof (Elf64_External_Rela);
9845   asection *got = ppc64_elf_tdata (gent->owner)->got;
9846
9847   gent->got.offset = got->size;
9848   got->size += entsize;
9849
9850   if (h->type == STT_GNU_IFUNC)
9851     {
9852       htab->elf.irelplt->size += rentsize;
9853       htab->got_reli_size += rentsize;
9854     }
9855   else if (((bfd_link_pic (info)
9856              && !((gent->tls_type & TLS_TPREL) != 0
9857                   && bfd_link_executable (info)
9858                   && SYMBOL_REFERENCES_LOCAL (info, h)))
9859             || (htab->elf.dynamic_sections_created
9860                 && h->dynindx != -1
9861                 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9862            && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9863     {
9864       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9865       relgot->size += rentsize;
9866     }
9867 }
9868
9869 /* This function merges got entries in the same toc group.  */
9870
9871 static void
9872 merge_got_entries (struct got_entry **pent)
9873 {
9874   struct got_entry *ent, *ent2;
9875
9876   for (ent = *pent; ent != NULL; ent = ent->next)
9877     if (!ent->is_indirect)
9878       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9879         if (!ent2->is_indirect
9880             && ent2->addend == ent->addend
9881             && ent2->tls_type == ent->tls_type
9882             && elf_gp (ent2->owner) == elf_gp (ent->owner))
9883           {
9884             ent2->is_indirect = TRUE;
9885             ent2->got.ent = ent;
9886           }
9887 }
9888
9889 /* If H is undefined, make it dynamic if that makes sense.  */
9890
9891 static bfd_boolean
9892 ensure_undef_dynamic (struct bfd_link_info *info,
9893                       struct elf_link_hash_entry *h)
9894 {
9895   struct elf_link_hash_table *htab = elf_hash_table (info);
9896
9897   if (htab->dynamic_sections_created
9898       && ((info->dynamic_undefined_weak != 0
9899            && h->root.type == bfd_link_hash_undefweak)
9900           || h->root.type == bfd_link_hash_undefined)
9901       && h->dynindx == -1
9902       && !h->forced_local
9903       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9904     return bfd_elf_link_record_dynamic_symbol (info, h);
9905   return TRUE;
9906 }
9907
9908 /* Allocate space in .plt, .got and associated reloc sections for
9909    dynamic relocs.  */
9910
9911 static bfd_boolean
9912 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9913 {
9914   struct bfd_link_info *info;
9915   struct ppc_link_hash_table *htab;
9916   asection *s;
9917   struct ppc_link_hash_entry *eh;
9918   struct got_entry **pgent, *gent;
9919
9920   if (h->root.type == bfd_link_hash_indirect)
9921     return TRUE;
9922
9923   info = (struct bfd_link_info *) inf;
9924   htab = ppc_hash_table (info);
9925   if (htab == NULL)
9926     return FALSE;
9927
9928   eh = (struct ppc_link_hash_entry *) h;
9929   /* Run through the TLS GD got entries first if we're changing them
9930      to TPREL.  */
9931   if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
9932     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9933       if (gent->got.refcount > 0
9934           && (gent->tls_type & TLS_GD) != 0)
9935         {
9936           /* This was a GD entry that has been converted to TPREL.  If
9937              there happens to be a TPREL entry we can use that one.  */
9938           struct got_entry *ent;
9939           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9940             if (ent->got.refcount > 0
9941                 && (ent->tls_type & TLS_TPREL) != 0
9942                 && ent->addend == gent->addend
9943                 && ent->owner == gent->owner)
9944               {
9945                 gent->got.refcount = 0;
9946                 break;
9947               }
9948
9949           /* If not, then we'll be using our own TPREL entry.  */
9950           if (gent->got.refcount != 0)
9951             gent->tls_type = TLS_TLS | TLS_TPREL;
9952         }
9953
9954   /* Remove any list entry that won't generate a word in the GOT before
9955      we call merge_got_entries.  Otherwise we risk merging to empty
9956      entries.  */
9957   pgent = &h->got.glist;
9958   while ((gent = *pgent) != NULL)
9959     if (gent->got.refcount > 0)
9960       {
9961         if ((gent->tls_type & TLS_LD) != 0
9962             && !h->def_dynamic)
9963           {
9964             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9965             *pgent = gent->next;
9966           }
9967         else
9968           pgent = &gent->next;
9969       }
9970     else
9971       *pgent = gent->next;
9972
9973   if (!htab->do_multi_toc)
9974     merge_got_entries (&h->got.glist);
9975
9976   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9977     if (!gent->is_indirect)
9978       {
9979         /* Make sure this symbol is output as a dynamic symbol.  */
9980         if (!ensure_undef_dynamic (info, h))
9981           return FALSE;
9982
9983         if (!is_ppc64_elf (gent->owner))
9984           abort ();
9985
9986         allocate_got (h, info, gent);
9987       }
9988
9989   /* If no dynamic sections we can't have dynamic relocs, except for
9990      IFUNCs which are handled even in static executables.  */
9991   if (!htab->elf.dynamic_sections_created
9992       && h->type != STT_GNU_IFUNC)
9993     eh->dyn_relocs = NULL;
9994
9995   /* Discard relocs on undefined symbols that must be local.  */
9996   else if (h->root.type == bfd_link_hash_undefined
9997            && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9998     eh->dyn_relocs = NULL;
9999
10000   /* Also discard relocs on undefined weak syms with non-default
10001      visibility, or when dynamic_undefined_weak says so.  */
10002   else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
10003     eh->dyn_relocs = NULL;
10004
10005   if (eh->dyn_relocs != NULL)
10006     {
10007       struct elf_dyn_relocs *p, **pp;
10008
10009       /* In the shared -Bsymbolic case, discard space allocated for
10010          dynamic pc-relative relocs against symbols which turn out to
10011          be defined in regular objects.  For the normal shared case,
10012          discard space for relocs that have become local due to symbol
10013          visibility changes.  */
10014
10015       if (bfd_link_pic (info))
10016         {
10017           /* Relocs that use pc_count are those that appear on a call
10018              insn, or certain REL relocs (see must_be_dyn_reloc) that
10019              can be generated via assembly.  We want calls to
10020              protected symbols to resolve directly to the function
10021              rather than going via the plt.  If people want function
10022              pointer comparisons to work as expected then they should
10023              avoid writing weird assembly.  */
10024           if (SYMBOL_CALLS_LOCAL (info, h))
10025             {
10026               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
10027                 {
10028                   p->count -= p->pc_count;
10029                   p->pc_count = 0;
10030                   if (p->count == 0)
10031                     *pp = p->next;
10032                   else
10033                     pp = &p->next;
10034                 }
10035             }
10036
10037           if (eh->dyn_relocs != NULL)
10038             {
10039               /* Make sure this symbol is output as a dynamic symbol.  */
10040               if (!ensure_undef_dynamic (info, h))
10041                 return FALSE;
10042             }
10043         }
10044       else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
10045         {
10046           /* For the non-pic case, discard space for relocs against
10047              symbols which turn out to need copy relocs or are not
10048              dynamic.  */
10049           if (h->dynamic_adjusted
10050               && !h->def_regular
10051               && !ELF_COMMON_DEF_P (h))
10052             {
10053               /* Make sure this symbol is output as a dynamic symbol.  */
10054               if (!ensure_undef_dynamic (info, h))
10055                 return FALSE;
10056
10057               if (h->dynindx == -1)
10058                 eh->dyn_relocs = NULL;
10059             }
10060           else
10061             eh->dyn_relocs = NULL;
10062         }
10063
10064       /* Finally, allocate space.  */
10065       for (p = eh->dyn_relocs; p != NULL; p = p->next)
10066         {
10067           asection *sreloc = elf_section_data (p->sec)->sreloc;
10068           if (eh->elf.type == STT_GNU_IFUNC)
10069             sreloc = htab->elf.irelplt;
10070           sreloc->size += p->count * sizeof (Elf64_External_Rela);
10071         }
10072     }
10073
10074   /* We might need a PLT entry when the symbol
10075      a) is dynamic, or
10076      b) is an ifunc, or
10077      c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10078      d) has plt16 relocs and we are linking statically.  */
10079   if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10080       || h->type == STT_GNU_IFUNC
10081       || (h->needs_plt && h->dynamic_adjusted)
10082       || (h->needs_plt
10083           && h->def_regular
10084           && !htab->elf.dynamic_sections_created
10085           && !htab->can_convert_all_inline_plt
10086           && (((struct ppc_link_hash_entry *) h)->tls_mask
10087               & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10088     {
10089       struct plt_entry *pent;
10090       bfd_boolean doneone = FALSE;
10091       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10092         if (pent->plt.refcount > 0)
10093           {
10094             if (!htab->elf.dynamic_sections_created
10095                 || h->dynindx == -1)
10096               {
10097                 if (h->type == STT_GNU_IFUNC)
10098                   {
10099                     s = htab->elf.iplt;
10100                     pent->plt.offset = s->size;
10101                     s->size += PLT_ENTRY_SIZE (htab);
10102                     s = htab->elf.irelplt;
10103                   }
10104                 else
10105                   {
10106                     s = htab->pltlocal;
10107                     pent->plt.offset = s->size;
10108                     s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10109                     s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
10110                   }
10111               }
10112             else
10113               {
10114                 /* If this is the first .plt entry, make room for the special
10115                    first entry.  */
10116                 s = htab->elf.splt;
10117                 if (s->size == 0)
10118                   s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10119
10120                 pent->plt.offset = s->size;
10121
10122                 /* Make room for this entry.  */
10123                 s->size += PLT_ENTRY_SIZE (htab);
10124
10125                 /* Make room for the .glink code.  */
10126                 s = htab->glink;
10127                 if (s->size == 0)
10128                   s->size += GLINK_PLTRESOLVE_SIZE (htab);
10129                 if (htab->opd_abi)
10130                   {
10131                     /* We need bigger stubs past index 32767.  */
10132                     if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10133                       s->size += 4;
10134                     s->size += 2*4;
10135                   }
10136                 else
10137                   s->size += 4;
10138
10139                 /* We also need to make an entry in the .rela.plt section.  */
10140                 s = htab->elf.srelplt;
10141               }
10142             if (s != NULL)
10143               s->size += sizeof (Elf64_External_Rela);
10144             doneone = TRUE;
10145           }
10146         else
10147           pent->plt.offset = (bfd_vma) -1;
10148       if (!doneone)
10149         {
10150           h->plt.plist = NULL;
10151           h->needs_plt = 0;
10152         }
10153     }
10154   else
10155     {
10156       h->plt.plist = NULL;
10157       h->needs_plt = 0;
10158     }
10159
10160   return TRUE;
10161 }
10162
10163 #define PPC_LO(v) ((v) & 0xffff)
10164 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10165 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10166
10167 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10168    to set up space for global entry stubs.  These are put in glink,
10169    after the branch table.  */
10170
10171 static bfd_boolean
10172 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10173 {
10174   struct bfd_link_info *info;
10175   struct ppc_link_hash_table *htab;
10176   struct plt_entry *pent;
10177   asection *s, *plt;
10178
10179   if (h->root.type == bfd_link_hash_indirect)
10180     return TRUE;
10181
10182   if (!h->pointer_equality_needed)
10183     return TRUE;
10184
10185   if (h->def_regular)
10186     return TRUE;
10187
10188   info = inf;
10189   htab = ppc_hash_table (info);
10190   if (htab == NULL)
10191     return FALSE;
10192
10193   s = htab->global_entry;
10194   plt = htab->elf.splt;
10195   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10196     if (pent->plt.offset != (bfd_vma) -1
10197         && pent->addend == 0)
10198       {
10199         /* For ELFv2, if this symbol is not defined in a regular file
10200            and we are not generating a shared library or pie, then we
10201            need to define the symbol in the executable on a call stub.
10202            This is to avoid text relocations.  */
10203         bfd_vma off, stub_align, stub_off, stub_size;
10204         unsigned int align_power;
10205
10206         stub_size = 16;
10207         stub_off = s->size;
10208         if (htab->params->plt_stub_align >= 0)
10209           align_power = htab->params->plt_stub_align;
10210         else
10211           align_power = -htab->params->plt_stub_align;
10212         /* Setting section alignment is delayed until we know it is
10213            non-empty.  Otherwise the .text output section will be
10214            aligned at least to plt_stub_align even when no global
10215            entry stubs are needed.  */
10216         if (s->alignment_power < align_power)
10217           s->alignment_power = align_power;
10218         stub_align = (bfd_vma) 1 << align_power;
10219         if (htab->params->plt_stub_align >= 0
10220             || ((((stub_off + stub_size - 1) & -stub_align)
10221                  - (stub_off & -stub_align))
10222                 > ((stub_size - 1) & -stub_align)))
10223           stub_off = (stub_off + stub_align - 1) & -stub_align;
10224         off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10225         off -= stub_off + s->output_offset + s->output_section->vma;
10226         /* Note that for --plt-stub-align negative we have a possible
10227            dependency between stub offset and size.  Break that
10228            dependency by assuming the max stub size when calculating
10229            the stub offset.  */
10230         if (PPC_HA (off) == 0)
10231           stub_size -= 4;
10232         h->root.type = bfd_link_hash_defined;
10233         h->root.u.def.section = s;
10234         h->root.u.def.value = stub_off;
10235         s->size = stub_off + stub_size;
10236         break;
10237       }
10238   return TRUE;
10239 }
10240
10241 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
10242    read-only sections.  */
10243
10244 static bfd_boolean
10245 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
10246 {
10247   asection *sec;
10248
10249   if (h->root.type == bfd_link_hash_indirect)
10250     return TRUE;
10251
10252   sec = readonly_dynrelocs (h);
10253   if (sec != NULL)
10254     {
10255       struct bfd_link_info *info = (struct bfd_link_info *) inf;
10256
10257       info->flags |= DF_TEXTREL;
10258       info->callbacks->minfo
10259         (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
10260          sec->owner, h->root.root.string, sec);
10261
10262       /* Not an error, just cut short the traversal.  */
10263       return FALSE;
10264     }
10265   return TRUE;
10266 }
10267
10268 /* Set the sizes of the dynamic sections.  */
10269
10270 static bfd_boolean
10271 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10272                                  struct bfd_link_info *info)
10273 {
10274   struct ppc_link_hash_table *htab;
10275   bfd *dynobj;
10276   asection *s;
10277   bfd_boolean relocs;
10278   bfd *ibfd;
10279   struct got_entry *first_tlsld;
10280
10281   htab = ppc_hash_table (info);
10282   if (htab == NULL)
10283     return FALSE;
10284
10285   dynobj = htab->elf.dynobj;
10286   if (dynobj == NULL)
10287     abort ();
10288
10289   if (htab->elf.dynamic_sections_created)
10290     {
10291       /* Set the contents of the .interp section to the interpreter.  */
10292       if (bfd_link_executable (info) && !info->nointerp)
10293         {
10294           s = bfd_get_linker_section (dynobj, ".interp");
10295           if (s == NULL)
10296             abort ();
10297           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10298           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10299         }
10300     }
10301
10302   /* Set up .got offsets for local syms, and space for local dynamic
10303      relocs.  */
10304   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10305     {
10306       struct got_entry **lgot_ents;
10307       struct got_entry **end_lgot_ents;
10308       struct plt_entry **local_plt;
10309       struct plt_entry **end_local_plt;
10310       unsigned char *lgot_masks;
10311       bfd_size_type locsymcount;
10312       Elf_Internal_Shdr *symtab_hdr;
10313
10314       if (!is_ppc64_elf (ibfd))
10315         continue;
10316
10317       for (s = ibfd->sections; s != NULL; s = s->next)
10318         {
10319           struct ppc_dyn_relocs *p;
10320
10321           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10322             {
10323               if (!bfd_is_abs_section (p->sec)
10324                   && bfd_is_abs_section (p->sec->output_section))
10325                 {
10326                   /* Input section has been discarded, either because
10327                      it is a copy of a linkonce section or due to
10328                      linker script /DISCARD/, so we'll be discarding
10329                      the relocs too.  */
10330                 }
10331               else if (p->count != 0)
10332                 {
10333                   asection *srel = elf_section_data (p->sec)->sreloc;
10334                   if (p->ifunc)
10335                     srel = htab->elf.irelplt;
10336                   srel->size += p->count * sizeof (Elf64_External_Rela);
10337                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10338                     info->flags |= DF_TEXTREL;
10339                 }
10340             }
10341         }
10342
10343       lgot_ents = elf_local_got_ents (ibfd);
10344       if (!lgot_ents)
10345         continue;
10346
10347       symtab_hdr = &elf_symtab_hdr (ibfd);
10348       locsymcount = symtab_hdr->sh_info;
10349       end_lgot_ents = lgot_ents + locsymcount;
10350       local_plt = (struct plt_entry **) end_lgot_ents;
10351       end_local_plt = local_plt + locsymcount;
10352       lgot_masks = (unsigned char *) end_local_plt;
10353       s = ppc64_elf_tdata (ibfd)->got;
10354       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10355         {
10356           struct got_entry **pent, *ent;
10357
10358           pent = lgot_ents;
10359           while ((ent = *pent) != NULL)
10360             if (ent->got.refcount > 0)
10361               {
10362                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10363                   {
10364                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
10365                     *pent = ent->next;
10366                   }
10367                 else
10368                   {
10369                     unsigned int ent_size = 8;
10370                     unsigned int rel_size = sizeof (Elf64_External_Rela);
10371
10372                     ent->got.offset = s->size;
10373                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10374                       {
10375                         ent_size *= 2;
10376                         rel_size *= 2;
10377                       }
10378                     s->size += ent_size;
10379                     if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10380                       {
10381                         htab->elf.irelplt->size += rel_size;
10382                         htab->got_reli_size += rel_size;
10383                       }
10384                     else if (bfd_link_pic (info)
10385                              && !((ent->tls_type & TLS_TPREL) != 0
10386                                   && bfd_link_executable (info)))
10387                       {
10388                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10389                         srel->size += rel_size;
10390                       }
10391                     pent = &ent->next;
10392                   }
10393               }
10394             else
10395               *pent = ent->next;
10396         }
10397
10398       /* Allocate space for plt calls to local syms.  */
10399       lgot_masks = (unsigned char *) end_local_plt;
10400       for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10401         {
10402           struct plt_entry *ent;
10403
10404           for (ent = *local_plt; ent != NULL; ent = ent->next)
10405             if (ent->plt.refcount > 0)
10406               {
10407                 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10408                   {
10409                     s = htab->elf.iplt;
10410                     ent->plt.offset = s->size;
10411                     s->size += PLT_ENTRY_SIZE (htab);
10412                     htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10413                   }
10414                 else if (htab->can_convert_all_inline_plt
10415                          || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10416                   ent->plt.offset = (bfd_vma) -1;
10417                 else
10418                   {
10419                     s = htab->pltlocal;
10420                     ent->plt.offset = s->size;
10421                     s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10422                     if (bfd_link_pic (info))
10423                       htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10424                   }
10425               }
10426             else
10427               ent->plt.offset = (bfd_vma) -1;
10428         }
10429     }
10430
10431   /* Allocate global sym .plt and .got entries, and space for global
10432      sym dynamic relocs.  */
10433   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10434
10435   if (!htab->opd_abi && !bfd_link_pic (info))
10436     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10437
10438   first_tlsld = NULL;
10439   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10440     {
10441       struct got_entry *ent;
10442
10443       if (!is_ppc64_elf (ibfd))
10444         continue;
10445
10446       ent = ppc64_tlsld_got (ibfd);
10447       if (ent->got.refcount > 0)
10448         {
10449           if (!htab->do_multi_toc && first_tlsld != NULL)
10450             {
10451               ent->is_indirect = TRUE;
10452               ent->got.ent = first_tlsld;
10453             }
10454           else
10455             {
10456               if (first_tlsld == NULL)
10457                 first_tlsld = ent;
10458               s = ppc64_elf_tdata (ibfd)->got;
10459               ent->got.offset = s->size;
10460               ent->owner = ibfd;
10461               s->size += 16;
10462               if (bfd_link_pic (info))
10463                 {
10464                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10465                   srel->size += sizeof (Elf64_External_Rela);
10466                 }
10467             }
10468         }
10469       else
10470         ent->got.offset = (bfd_vma) -1;
10471     }
10472
10473   /* We now have determined the sizes of the various dynamic sections.
10474      Allocate memory for them.  */
10475   relocs = FALSE;
10476   for (s = dynobj->sections; s != NULL; s = s->next)
10477     {
10478       if ((s->flags & SEC_LINKER_CREATED) == 0)
10479         continue;
10480
10481       if (s == htab->brlt || s == htab->relbrlt)
10482         /* These haven't been allocated yet;  don't strip.  */
10483         continue;
10484       else if (s == htab->elf.sgot
10485                || s == htab->elf.splt
10486                || s == htab->elf.iplt
10487                || s == htab->pltlocal
10488                || s == htab->glink
10489                || s == htab->global_entry
10490                || s == htab->elf.sdynbss
10491                || s == htab->elf.sdynrelro)
10492         {
10493           /* Strip this section if we don't need it; see the
10494              comment below.  */
10495         }
10496       else if (s == htab->glink_eh_frame)
10497         {
10498           if (!bfd_is_abs_section (s->output_section))
10499             /* Not sized yet.  */
10500             continue;
10501         }
10502       else if (CONST_STRNEQ (s->name, ".rela"))
10503         {
10504           if (s->size != 0)
10505             {
10506               if (s != htab->elf.srelplt)
10507                 relocs = TRUE;
10508
10509               /* We use the reloc_count field as a counter if we need
10510                  to copy relocs into the output file.  */
10511               s->reloc_count = 0;
10512             }
10513         }
10514       else
10515         {
10516           /* It's not one of our sections, so don't allocate space.  */
10517           continue;
10518         }
10519
10520       if (s->size == 0)
10521         {
10522           /* If we don't need this section, strip it from the
10523              output file.  This is mostly to handle .rela.bss and
10524              .rela.plt.  We must create both sections in
10525              create_dynamic_sections, because they must be created
10526              before the linker maps input sections to output
10527              sections.  The linker does that before
10528              adjust_dynamic_symbol is called, and it is that
10529              function which decides whether anything needs to go
10530              into these sections.  */
10531           s->flags |= SEC_EXCLUDE;
10532           continue;
10533         }
10534
10535       if (bfd_is_abs_section (s->output_section))
10536         _bfd_error_handler (_("warning: discarding dynamic section %s"),
10537                             s->name);
10538
10539       if ((s->flags & SEC_HAS_CONTENTS) == 0)
10540         continue;
10541
10542       /* Allocate memory for the section contents.  We use bfd_zalloc
10543          here in case unused entries are not reclaimed before the
10544          section's contents are written out.  This should not happen,
10545          but this way if it does we get a R_PPC64_NONE reloc in .rela
10546          sections instead of garbage.
10547          We also rely on the section contents being zero when writing
10548          the GOT and .dynrelro.  */
10549       s->contents = bfd_zalloc (dynobj, s->size);
10550       if (s->contents == NULL)
10551         return FALSE;
10552     }
10553
10554   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10555     {
10556       if (!is_ppc64_elf (ibfd))
10557         continue;
10558
10559       s = ppc64_elf_tdata (ibfd)->got;
10560       if (s != NULL && s != htab->elf.sgot)
10561         {
10562           if (s->size == 0)
10563             s->flags |= SEC_EXCLUDE;
10564           else
10565             {
10566               s->contents = bfd_zalloc (ibfd, s->size);
10567               if (s->contents == NULL)
10568                 return FALSE;
10569             }
10570         }
10571       s = ppc64_elf_tdata (ibfd)->relgot;
10572       if (s != NULL)
10573         {
10574           if (s->size == 0)
10575             s->flags |= SEC_EXCLUDE;
10576           else
10577             {
10578               s->contents = bfd_zalloc (ibfd, s->size);
10579               if (s->contents == NULL)
10580                 return FALSE;
10581               relocs = TRUE;
10582               s->reloc_count = 0;
10583             }
10584         }
10585     }
10586
10587   if (htab->elf.dynamic_sections_created)
10588     {
10589       bfd_boolean tls_opt;
10590
10591       /* Add some entries to the .dynamic section.  We fill in the
10592          values later, in ppc64_elf_finish_dynamic_sections, but we
10593          must add the entries now so that we get the correct size for
10594          the .dynamic section.  The DT_DEBUG entry is filled in by the
10595          dynamic linker and used by the debugger.  */
10596 #define add_dynamic_entry(TAG, VAL) \
10597   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10598
10599       if (bfd_link_executable (info))
10600         {
10601           if (!add_dynamic_entry (DT_DEBUG, 0))
10602             return FALSE;
10603         }
10604
10605       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10606         {
10607           if (!add_dynamic_entry (DT_PLTGOT, 0)
10608               || !add_dynamic_entry (DT_PLTRELSZ, 0)
10609               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10610               || !add_dynamic_entry (DT_JMPREL, 0)
10611               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10612             return FALSE;
10613         }
10614
10615       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10616         {
10617           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10618               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10619             return FALSE;
10620         }
10621
10622       tls_opt = (htab->params->tls_get_addr_opt
10623                  && htab->tls_get_addr_fd != NULL
10624                  && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10625       if (tls_opt || !htab->opd_abi)
10626         {
10627           if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10628             return FALSE;
10629         }
10630
10631       if (relocs)
10632         {
10633           if (!add_dynamic_entry (DT_RELA, 0)
10634               || !add_dynamic_entry (DT_RELASZ, 0)
10635               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10636             return FALSE;
10637
10638           /* If any dynamic relocs apply to a read-only section,
10639              then we need a DT_TEXTREL entry.  */
10640           if ((info->flags & DF_TEXTREL) == 0)
10641             elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10642
10643           if ((info->flags & DF_TEXTREL) != 0)
10644             {
10645               if (!add_dynamic_entry (DT_TEXTREL, 0))
10646                 return FALSE;
10647             }
10648         }
10649     }
10650 #undef add_dynamic_entry
10651
10652   return TRUE;
10653 }
10654
10655 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
10656
10657 static bfd_boolean
10658 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10659 {
10660   if (h->plt.plist != NULL
10661       && !h->def_regular
10662       && !h->pointer_equality_needed)
10663     return FALSE;
10664
10665   return _bfd_elf_hash_symbol (h);
10666 }
10667
10668 /* Determine the type of stub needed, if any, for a call.  */
10669
10670 static inline enum ppc_stub_type
10671 ppc_type_of_stub (asection *input_sec,
10672                   const Elf_Internal_Rela *rel,
10673                   struct ppc_link_hash_entry **hash,
10674                   struct plt_entry **plt_ent,
10675                   bfd_vma destination,
10676                   unsigned long local_off)
10677 {
10678   struct ppc_link_hash_entry *h = *hash;
10679   bfd_vma location;
10680   bfd_vma branch_offset;
10681   bfd_vma max_branch_offset;
10682   enum elf_ppc64_reloc_type r_type;
10683
10684   if (h != NULL)
10685     {
10686       struct plt_entry *ent;
10687       struct ppc_link_hash_entry *fdh = h;
10688       if (h->oh != NULL
10689           && h->oh->is_func_descriptor)
10690         {
10691           fdh = ppc_follow_link (h->oh);
10692           *hash = fdh;
10693         }
10694
10695       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10696         if (ent->addend == rel->r_addend
10697             && ent->plt.offset != (bfd_vma) -1)
10698           {
10699             *plt_ent = ent;
10700             return ppc_stub_plt_call;
10701           }
10702
10703       /* Here, we know we don't have a plt entry.  If we don't have a
10704          either a defined function descriptor or a defined entry symbol
10705          in a regular object file, then it is pointless trying to make
10706          any other type of stub.  */
10707       if (!is_static_defined (&fdh->elf)
10708           && !is_static_defined (&h->elf))
10709         return ppc_stub_none;
10710     }
10711   else if (elf_local_got_ents (input_sec->owner) != NULL)
10712     {
10713       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10714       struct plt_entry **local_plt = (struct plt_entry **)
10715         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10716       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10717
10718       if (local_plt[r_symndx] != NULL)
10719         {
10720           struct plt_entry *ent;
10721
10722           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10723             if (ent->addend == rel->r_addend
10724                 && ent->plt.offset != (bfd_vma) -1)
10725               {
10726                 *plt_ent = ent;
10727                 return ppc_stub_plt_call;
10728               }
10729         }
10730     }
10731
10732   /* Determine where the call point is.  */
10733   location = (input_sec->output_offset
10734               + input_sec->output_section->vma
10735               + rel->r_offset);
10736
10737   branch_offset = destination - location;
10738   r_type = ELF64_R_TYPE (rel->r_info);
10739
10740   /* Determine if a long branch stub is needed.  */
10741   max_branch_offset = 1 << 25;
10742   if (r_type == R_PPC64_REL14
10743       || r_type == R_PPC64_REL14_BRTAKEN
10744       || r_type == R_PPC64_REL14_BRNTAKEN)
10745     max_branch_offset = 1 << 15;
10746
10747   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10748     /* We need a stub.  Figure out whether a long_branch or plt_branch
10749        is needed later.  */
10750     return ppc_stub_long_branch;
10751
10752   return ppc_stub_none;
10753 }
10754
10755 /* With power7 weakly ordered memory model, it is possible for ld.so
10756    to update a plt entry in one thread and have another thread see a
10757    stale zero toc entry.  To avoid this we need some sort of acquire
10758    barrier in the call stub.  One solution is to make the load of the
10759    toc word seem to appear to depend on the load of the function entry
10760    word.  Another solution is to test for r2 being zero, and branch to
10761    the appropriate glink entry if so.
10762
10763    .    fake dep barrier        compare
10764    .    ld 12,xxx(2)            ld 12,xxx(2)
10765    .    mtctr 12                mtctr 12
10766    .    xor 11,12,12            ld 2,xxx+8(2)
10767    .    add 2,2,11              cmpldi 2,0
10768    .    ld 2,xxx+8(2)           bnectr+
10769    .    bctr                    b <glink_entry>
10770
10771    The solution involving the compare turns out to be faster, so
10772    that's what we use unless the branch won't reach.  */
10773
10774 #define ALWAYS_USE_FAKE_DEP 0
10775 #define ALWAYS_EMIT_R2SAVE 0
10776
10777 static inline unsigned int
10778 plt_stub_size (struct ppc_link_hash_table *htab,
10779                struct ppc_stub_hash_entry *stub_entry,
10780                bfd_vma off)
10781 {
10782   unsigned size = 12;
10783
10784   if (ALWAYS_EMIT_R2SAVE
10785       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10786     size += 4;
10787   if (PPC_HA (off) != 0)
10788     size += 4;
10789   if (htab->opd_abi)
10790     {
10791       size += 4;
10792       if (htab->params->plt_static_chain)
10793         size += 4;
10794       if (htab->params->plt_thread_safe
10795           && htab->elf.dynamic_sections_created
10796           && stub_entry->h != NULL
10797           && stub_entry->h->elf.dynindx != -1)
10798         size += 8;
10799       if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10800         size += 4;
10801     }
10802   if (stub_entry->h != NULL
10803       && (stub_entry->h == htab->tls_get_addr_fd
10804           || stub_entry->h == htab->tls_get_addr)
10805       && htab->params->tls_get_addr_opt)
10806     {
10807       size += 7 * 4;
10808       if (ALWAYS_EMIT_R2SAVE
10809           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10810         size += 6 * 4;
10811     }
10812   return size;
10813 }
10814
10815 /* Depending on the sign of plt_stub_align:
10816    If positive, return the padding to align to a 2**plt_stub_align
10817    boundary.
10818    If negative, if this stub would cross fewer 2**plt_stub_align
10819    boundaries if we align, then return the padding needed to do so.  */
10820
10821 static inline unsigned int
10822 plt_stub_pad (struct ppc_link_hash_table *htab,
10823               struct ppc_stub_hash_entry *stub_entry,
10824               bfd_vma plt_off)
10825 {
10826   int stub_align;
10827   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10828   bfd_vma stub_off = stub_entry->group->stub_sec->size;
10829
10830   if (htab->params->plt_stub_align >= 0)
10831     {
10832       stub_align = 1 << htab->params->plt_stub_align;
10833       if ((stub_off & (stub_align - 1)) != 0)
10834         return stub_align - (stub_off & (stub_align - 1));
10835       return 0;
10836     }
10837
10838   stub_align = 1 << -htab->params->plt_stub_align;
10839   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10840       > ((stub_size - 1) & -stub_align))
10841     return stub_align - (stub_off & (stub_align - 1));
10842   return 0;
10843 }
10844
10845 /* Build a .plt call stub.  */
10846
10847 static inline bfd_byte *
10848 build_plt_stub (struct ppc_link_hash_table *htab,
10849                 struct ppc_stub_hash_entry *stub_entry,
10850                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10851 {
10852   bfd *obfd = htab->params->stub_bfd;
10853   bfd_boolean plt_load_toc = htab->opd_abi;
10854   bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10855   bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10856                                  && htab->elf.dynamic_sections_created
10857                                  && stub_entry->h != NULL
10858                                  && stub_entry->h->elf.dynindx != -1);
10859   bfd_boolean use_fake_dep = plt_thread_safe;
10860   bfd_vma cmp_branch_off = 0;
10861
10862   if (!ALWAYS_USE_FAKE_DEP
10863       && plt_load_toc
10864       && plt_thread_safe
10865       && !((stub_entry->h == htab->tls_get_addr_fd
10866             || stub_entry->h == htab->tls_get_addr)
10867            && htab->params->tls_get_addr_opt))
10868     {
10869       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10870       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10871                           / PLT_ENTRY_SIZE (htab));
10872       bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10873       bfd_vma to, from;
10874
10875       if (pltindex > 32768)
10876         glinkoff += (pltindex - 32768) * 4;
10877       to = (glinkoff
10878             + htab->glink->output_offset
10879             + htab->glink->output_section->vma);
10880       from = (p - stub_entry->group->stub_sec->contents
10881               + 4 * (ALWAYS_EMIT_R2SAVE
10882                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10883               + 4 * (PPC_HA (offset) != 0)
10884               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10885                      != PPC_HA (offset))
10886               + 4 * (plt_static_chain != 0)
10887               + 20
10888               + stub_entry->group->stub_sec->output_offset
10889               + stub_entry->group->stub_sec->output_section->vma);
10890       cmp_branch_off = to - from;
10891       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10892     }
10893
10894   if (PPC_HA (offset) != 0)
10895     {
10896       if (r != NULL)
10897         {
10898           if (ALWAYS_EMIT_R2SAVE
10899               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10900             r[0].r_offset += 4;
10901           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10902           r[1].r_offset = r[0].r_offset + 4;
10903           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10904           r[1].r_addend = r[0].r_addend;
10905           if (plt_load_toc)
10906             {
10907               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10908                 {
10909                   r[2].r_offset = r[1].r_offset + 4;
10910                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10911                   r[2].r_addend = r[0].r_addend;
10912                 }
10913               else
10914                 {
10915                   r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10916                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10917                   r[2].r_addend = r[0].r_addend + 8;
10918                   if (plt_static_chain)
10919                     {
10920                       r[3].r_offset = r[2].r_offset + 4;
10921                       r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10922                       r[3].r_addend = r[0].r_addend + 16;
10923                     }
10924                 }
10925             }
10926         }
10927       if (ALWAYS_EMIT_R2SAVE
10928           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10929         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10930       if (plt_load_toc)
10931         {
10932           bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10933           bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),  p += 4;
10934         }
10935       else
10936         {
10937           bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10938           bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),  p += 4;
10939         }
10940       if (plt_load_toc
10941           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10942         {
10943           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10944           offset = 0;
10945         }
10946       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10947       if (plt_load_toc)
10948         {
10949           if (use_fake_dep)
10950             {
10951               bfd_put_32 (obfd, XOR_R2_R12_R12, p),             p += 4;
10952               bfd_put_32 (obfd, ADD_R11_R11_R2, p),             p += 4;
10953             }
10954           bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10955           if (plt_static_chain)
10956             bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10957         }
10958     }
10959   else
10960     {
10961       if (r != NULL)
10962         {
10963           if (ALWAYS_EMIT_R2SAVE
10964               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10965             r[0].r_offset += 4;
10966           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10967           if (plt_load_toc)
10968             {
10969               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10970                 {
10971                   r[1].r_offset = r[0].r_offset + 4;
10972                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10973                   r[1].r_addend = r[0].r_addend;
10974                 }
10975               else
10976                 {
10977                   r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10978                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10979                   r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10980                   if (plt_static_chain)
10981                     {
10982                       r[2].r_offset = r[1].r_offset + 4;
10983                       r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10984                       r[2].r_addend = r[0].r_addend + 8;
10985                     }
10986                 }
10987             }
10988         }
10989       if (ALWAYS_EMIT_R2SAVE
10990           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10991         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10992       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
10993       if (plt_load_toc
10994           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10995         {
10996           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
10997           offset = 0;
10998         }
10999       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
11000       if (plt_load_toc)
11001         {
11002           if (use_fake_dep)
11003             {
11004               bfd_put_32 (obfd, XOR_R11_R12_R12, p),            p += 4;
11005               bfd_put_32 (obfd, ADD_R2_R2_R11, p),              p += 4;
11006             }
11007           if (plt_static_chain)
11008             bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11009           bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11010         }
11011     }
11012   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11013     {
11014       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
11015       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
11016       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11017     }
11018   else
11019     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
11020   return p;
11021 }
11022
11023 /* Build a special .plt call stub for __tls_get_addr.  */
11024
11025 #define LD_R11_0R3      0xe9630000
11026 #define LD_R12_0R3      0xe9830000
11027 #define MR_R0_R3        0x7c601b78
11028 #define CMPDI_R11_0     0x2c2b0000
11029 #define ADD_R3_R12_R13  0x7c6c6a14
11030 #define BEQLR           0x4d820020
11031 #define MR_R3_R0        0x7c030378
11032 #define STD_R11_0R1     0xf9610000
11033 #define BCTRL           0x4e800421
11034 #define LD_R11_0R1      0xe9610000
11035 #define MTLR_R11        0x7d6803a6
11036
11037 static inline bfd_byte *
11038 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
11039                          struct ppc_stub_hash_entry *stub_entry,
11040                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11041 {
11042   bfd *obfd = htab->params->stub_bfd;
11043
11044   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
11045   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
11046   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
11047   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
11048   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
11049   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
11050   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
11051   if (r != NULL)
11052     r[0].r_offset += 7 * 4;
11053   if (!ALWAYS_EMIT_R2SAVE
11054       && stub_entry->stub_type != ppc_stub_plt_call_r2save)
11055     return build_plt_stub (htab, stub_entry, p, offset, r);
11056
11057   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
11058   bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
11059
11060   if (r != NULL)
11061     r[0].r_offset += 2 * 4;
11062   p = build_plt_stub (htab, stub_entry, p, offset, r);
11063   bfd_put_32 (obfd, BCTRL, p - 4);
11064
11065   bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p),     p += 4;
11066   bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
11067   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
11068   bfd_put_32 (obfd, BLR, p),                    p += 4;
11069
11070   return p;
11071 }
11072
11073 static Elf_Internal_Rela *
11074 get_relocs (asection *sec, int count)
11075 {
11076   Elf_Internal_Rela *relocs;
11077   struct bfd_elf_section_data *elfsec_data;
11078
11079   elfsec_data = elf_section_data (sec);
11080   relocs = elfsec_data->relocs;
11081   if (relocs == NULL)
11082     {
11083       bfd_size_type relsize;
11084       relsize = sec->reloc_count * sizeof (*relocs);
11085       relocs = bfd_alloc (sec->owner, relsize);
11086       if (relocs == NULL)
11087         return NULL;
11088       elfsec_data->relocs = relocs;
11089       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11090                                           sizeof (Elf_Internal_Shdr));
11091       if (elfsec_data->rela.hdr == NULL)
11092         return NULL;
11093       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11094                                         * sizeof (Elf64_External_Rela));
11095       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11096       sec->reloc_count = 0;
11097     }
11098   relocs += sec->reloc_count;
11099   sec->reloc_count += count;
11100   return relocs;
11101 }
11102
11103 static bfd_vma
11104 get_r2off (struct bfd_link_info *info,
11105            struct ppc_stub_hash_entry *stub_entry)
11106 {
11107   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11108   bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11109
11110   if (r2off == 0)
11111     {
11112       /* Support linking -R objects.  Get the toc pointer from the
11113          opd entry.  */
11114       char buf[8];
11115       if (!htab->opd_abi)
11116         return r2off;
11117       asection *opd = stub_entry->h->elf.root.u.def.section;
11118       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11119
11120       if (strcmp (opd->name, ".opd") != 0
11121           || opd->reloc_count != 0)
11122         {
11123           info->callbacks->einfo (_("%P: cannot find opd entry toc for `%pT'\n"),
11124                                   stub_entry->h->elf.root.root.string);
11125           bfd_set_error (bfd_error_bad_value);
11126           return (bfd_vma) -1;
11127         }
11128       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11129         return (bfd_vma) -1;
11130       r2off = bfd_get_64 (opd->owner, buf);
11131       r2off -= elf_gp (info->output_bfd);
11132     }
11133   r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11134   return r2off;
11135 }
11136
11137 static bfd_boolean
11138 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11139 {
11140   struct ppc_stub_hash_entry *stub_entry;
11141   struct ppc_branch_hash_entry *br_entry;
11142   struct bfd_link_info *info;
11143   struct ppc_link_hash_table *htab;
11144   bfd_byte *loc;
11145   bfd_byte *p;
11146   bfd_vma dest, off;
11147   Elf_Internal_Rela *r;
11148   asection *plt;
11149
11150   /* Massage our args to the form they really have.  */
11151   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11152   info = in_arg;
11153
11154   htab = ppc_hash_table (info);
11155   if (htab == NULL)
11156     return FALSE;
11157
11158   /* Make a note of the offset within the stubs for this entry.  */
11159   stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11160   loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11161
11162   htab->stub_count[stub_entry->stub_type - 1] += 1;
11163   switch (stub_entry->stub_type)
11164     {
11165     case ppc_stub_long_branch:
11166     case ppc_stub_long_branch_r2off:
11167       /* Branches are relative.  This is where we are going to.  */
11168       dest = (stub_entry->target_value
11169               + stub_entry->target_section->output_offset
11170               + stub_entry->target_section->output_section->vma);
11171       dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11172       off = dest;
11173
11174       /* And this is where we are coming from.  */
11175       off -= (stub_entry->stub_offset
11176               + stub_entry->group->stub_sec->output_offset
11177               + stub_entry->group->stub_sec->output_section->vma);
11178
11179       p = loc;
11180       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11181         {
11182           bfd_vma r2off = get_r2off (info, stub_entry);
11183
11184           if (r2off == (bfd_vma) -1)
11185             {
11186               htab->stub_error = TRUE;
11187               return FALSE;
11188             }
11189           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11190           p += 4;
11191           if (PPC_HA (r2off) != 0)
11192             {
11193               bfd_put_32 (htab->params->stub_bfd,
11194                           ADDIS_R2_R2 | PPC_HA (r2off), p);
11195               p += 4;
11196             }
11197           if (PPC_LO (r2off) != 0)
11198             {
11199               bfd_put_32 (htab->params->stub_bfd,
11200                           ADDI_R2_R2 | PPC_LO (r2off), p);
11201               p += 4;
11202             }
11203           off -= p - loc;
11204         }
11205       bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11206       p += 4;
11207
11208       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11209         {
11210           _bfd_error_handler
11211             (_("long branch stub `%s' offset overflow"),
11212              stub_entry->root.string);
11213           htab->stub_error = TRUE;
11214           return FALSE;
11215         }
11216
11217       if (info->emitrelocations)
11218         {
11219           r = get_relocs (stub_entry->group->stub_sec, 1);
11220           if (r == NULL)
11221             return FALSE;
11222           r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11223           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11224           r->r_addend = dest;
11225           if (stub_entry->h != NULL)
11226             {
11227               struct elf_link_hash_entry **hashes;
11228               unsigned long symndx;
11229               struct ppc_link_hash_entry *h;
11230
11231               hashes = elf_sym_hashes (htab->params->stub_bfd);
11232               if (hashes == NULL)
11233                 {
11234                   bfd_size_type hsize;
11235
11236                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11237                   hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11238                   if (hashes == NULL)
11239                     return FALSE;
11240                   elf_sym_hashes (htab->params->stub_bfd) = hashes;
11241                   htab->stub_globals = 1;
11242                 }
11243               symndx = htab->stub_globals++;
11244               h = stub_entry->h;
11245               hashes[symndx] = &h->elf;
11246               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
11247               if (h->oh != NULL && h->oh->is_func)
11248                 h = ppc_follow_link (h->oh);
11249               if (h->elf.root.u.def.section != stub_entry->target_section)
11250                 /* H is an opd symbol.  The addend must be zero.  */
11251                 r->r_addend = 0;
11252               else
11253                 {
11254                   off = (h->elf.root.u.def.value
11255                          + h->elf.root.u.def.section->output_offset
11256                          + h->elf.root.u.def.section->output_section->vma);
11257                   r->r_addend -= off;
11258                 }
11259             }
11260         }
11261       break;
11262
11263     case ppc_stub_plt_branch:
11264     case ppc_stub_plt_branch_r2off:
11265       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11266                                          stub_entry->root.string + 9,
11267                                          FALSE, FALSE);
11268       if (br_entry == NULL)
11269         {
11270           _bfd_error_handler (_("can't find branch stub `%s'"),
11271                               stub_entry->root.string);
11272           htab->stub_error = TRUE;
11273           return FALSE;
11274         }
11275
11276       dest = (stub_entry->target_value
11277               + stub_entry->target_section->output_offset
11278               + stub_entry->target_section->output_section->vma);
11279       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11280         dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11281
11282       bfd_put_64 (htab->brlt->owner, dest,
11283                   htab->brlt->contents + br_entry->offset);
11284
11285       if (br_entry->iter == htab->stub_iteration)
11286         {
11287           br_entry->iter = 0;
11288
11289           if (htab->relbrlt != NULL)
11290             {
11291               /* Create a reloc for the branch lookup table entry.  */
11292               Elf_Internal_Rela rela;
11293               bfd_byte *rl;
11294
11295               rela.r_offset = (br_entry->offset
11296                                + htab->brlt->output_offset
11297                                + htab->brlt->output_section->vma);
11298               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11299               rela.r_addend = dest;
11300
11301               rl = htab->relbrlt->contents;
11302               rl += (htab->relbrlt->reloc_count++
11303                      * sizeof (Elf64_External_Rela));
11304               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11305             }
11306           else if (info->emitrelocations)
11307             {
11308               r = get_relocs (htab->brlt, 1);
11309               if (r == NULL)
11310                 return FALSE;
11311               /* brlt, being SEC_LINKER_CREATED does not go through the
11312                  normal reloc processing.  Symbols and offsets are not
11313                  translated from input file to output file form, so
11314                  set up the offset per the output file.  */
11315               r->r_offset = (br_entry->offset
11316                              + htab->brlt->output_offset
11317                              + htab->brlt->output_section->vma);
11318               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11319               r->r_addend = dest;
11320             }
11321         }
11322
11323       dest = (br_entry->offset
11324               + htab->brlt->output_offset
11325               + htab->brlt->output_section->vma);
11326
11327       off = (dest
11328              - elf_gp (info->output_bfd)
11329              - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11330
11331       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11332         {
11333           info->callbacks->einfo
11334             (_("%P: linkage table error against `%pT'\n"),
11335              stub_entry->root.string);
11336           bfd_set_error (bfd_error_bad_value);
11337           htab->stub_error = TRUE;
11338           return FALSE;
11339         }
11340
11341       if (info->emitrelocations)
11342         {
11343           r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11344           if (r == NULL)
11345             return FALSE;
11346           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11347           if (bfd_big_endian (info->output_bfd))
11348             r[0].r_offset += 2;
11349           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11350             r[0].r_offset += 4;
11351           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11352           r[0].r_addend = dest;
11353           if (PPC_HA (off) != 0)
11354             {
11355               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11356               r[1].r_offset = r[0].r_offset + 4;
11357               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11358               r[1].r_addend = r[0].r_addend;
11359             }
11360         }
11361
11362       p = loc;
11363       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11364         {
11365           if (PPC_HA (off) != 0)
11366             {
11367               bfd_put_32 (htab->params->stub_bfd,
11368                           ADDIS_R12_R2 | PPC_HA (off), p);
11369               p += 4;
11370               bfd_put_32 (htab->params->stub_bfd,
11371                           LD_R12_0R12 | PPC_LO (off), p);
11372             }
11373           else
11374             bfd_put_32 (htab->params->stub_bfd,
11375                         LD_R12_0R2 | PPC_LO (off), p);
11376         }
11377       else
11378         {
11379           bfd_vma r2off = get_r2off (info, stub_entry);
11380
11381           if (r2off == (bfd_vma) -1)
11382             {
11383               htab->stub_error = TRUE;
11384               return FALSE;
11385             }
11386
11387           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11388           p += 4;
11389           if (PPC_HA (off) != 0)
11390             {
11391               bfd_put_32 (htab->params->stub_bfd,
11392                           ADDIS_R12_R2 | PPC_HA (off), p);
11393               p += 4;
11394               bfd_put_32 (htab->params->stub_bfd,
11395                           LD_R12_0R12 | PPC_LO (off), p);
11396             }
11397           else
11398             bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11399
11400           if (PPC_HA (r2off) != 0)
11401             {
11402               p += 4;
11403               bfd_put_32 (htab->params->stub_bfd,
11404                           ADDIS_R2_R2 | PPC_HA (r2off), p);
11405             }
11406           if (PPC_LO (r2off) != 0)
11407             {
11408               p += 4;
11409               bfd_put_32 (htab->params->stub_bfd,
11410                           ADDI_R2_R2 | PPC_LO (r2off), p);
11411             }
11412         }
11413       p += 4;
11414       bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11415       p += 4;
11416       bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11417       p += 4;
11418       break;
11419
11420     case ppc_stub_plt_call:
11421     case ppc_stub_plt_call_r2save:
11422       if (stub_entry->h != NULL
11423           && stub_entry->h->is_func_descriptor
11424           && stub_entry->h->oh != NULL)
11425         {
11426           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11427
11428           /* If the old-ABI "dot-symbol" is undefined make it weak so
11429              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
11430           if (fh->elf.root.type == bfd_link_hash_undefined
11431               && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11432                   || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11433             fh->elf.root.type = bfd_link_hash_undefweak;
11434         }
11435
11436       /* Now build the stub.  */
11437       dest = stub_entry->plt_ent->plt.offset & ~1;
11438       if (dest >= (bfd_vma) -2)
11439         abort ();
11440
11441       plt = htab->elf.splt;
11442       if (!htab->elf.dynamic_sections_created
11443           || stub_entry->h == NULL
11444           || stub_entry->h->elf.dynindx == -1)
11445         {
11446           if (stub_entry->symtype == STT_GNU_IFUNC)
11447             plt = htab->elf.iplt;
11448           else
11449             plt = htab->pltlocal;
11450         }
11451
11452       dest += plt->output_offset + plt->output_section->vma;
11453
11454       off = (dest
11455              - elf_gp (info->output_bfd)
11456              - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11457
11458       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11459         {
11460           info->callbacks->einfo
11461             /* xgettext:c-format */
11462             (_("%P: linkage table error against `%pT'\n"),
11463              stub_entry->h != NULL
11464              ? stub_entry->h->elf.root.root.string
11465              : "<local sym>");
11466           bfd_set_error (bfd_error_bad_value);
11467           htab->stub_error = TRUE;
11468           return FALSE;
11469         }
11470
11471       if (htab->params->plt_stub_align != 0)
11472         {
11473           unsigned pad = plt_stub_pad (htab, stub_entry, off);
11474
11475           stub_entry->group->stub_sec->size += pad;
11476           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11477           loc += pad;
11478         }
11479
11480       r = NULL;
11481       if (info->emitrelocations)
11482         {
11483           r = get_relocs (stub_entry->group->stub_sec,
11484                           ((PPC_HA (off) != 0)
11485                            + (htab->opd_abi
11486                               ? 2 + (htab->params->plt_static_chain
11487                                      && PPC_HA (off + 16) == PPC_HA (off))
11488                               : 1)));
11489           if (r == NULL)
11490             return FALSE;
11491           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11492           if (bfd_big_endian (info->output_bfd))
11493             r[0].r_offset += 2;
11494           r[0].r_addend = dest;
11495         }
11496       if (stub_entry->h != NULL
11497           && (stub_entry->h == htab->tls_get_addr_fd
11498               || stub_entry->h == htab->tls_get_addr)
11499           && htab->params->tls_get_addr_opt)
11500         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11501       else
11502         p = build_plt_stub (htab, stub_entry, loc, off, r);
11503       break;
11504
11505     case ppc_stub_save_res:
11506       return TRUE;
11507
11508     default:
11509       BFD_FAIL ();
11510       return FALSE;
11511     }
11512
11513   stub_entry->group->stub_sec->size += p - loc;
11514
11515   if (htab->params->emit_stub_syms)
11516     {
11517       struct elf_link_hash_entry *h;
11518       size_t len1, len2;
11519       char *name;
11520       const char *const stub_str[] = { "long_branch",
11521                                        "long_branch_r2off",
11522                                        "plt_branch",
11523                                        "plt_branch_r2off",
11524                                        "plt_call",
11525                                        "plt_call" };
11526
11527       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11528       len2 = strlen (stub_entry->root.string);
11529       name = bfd_malloc (len1 + len2 + 2);
11530       if (name == NULL)
11531         return FALSE;
11532       memcpy (name, stub_entry->root.string, 9);
11533       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11534       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11535       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11536       if (h == NULL)
11537         return FALSE;
11538       if (h->root.type == bfd_link_hash_new)
11539         {
11540           h->root.type = bfd_link_hash_defined;
11541           h->root.u.def.section = stub_entry->group->stub_sec;
11542           h->root.u.def.value = stub_entry->stub_offset;
11543           h->ref_regular = 1;
11544           h->def_regular = 1;
11545           h->ref_regular_nonweak = 1;
11546           h->forced_local = 1;
11547           h->non_elf = 0;
11548           h->root.linker_def = 1;
11549         }
11550     }
11551
11552   return TRUE;
11553 }
11554
11555 /* As above, but don't actually build the stub.  Just bump offset so
11556    we know stub section sizes, and select plt_branch stubs where
11557    long_branch stubs won't do.  */
11558
11559 static bfd_boolean
11560 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11561 {
11562   struct ppc_stub_hash_entry *stub_entry;
11563   struct bfd_link_info *info;
11564   struct ppc_link_hash_table *htab;
11565   bfd_vma off;
11566   int size;
11567
11568   /* Massage our args to the form they really have.  */
11569   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11570   info = in_arg;
11571
11572   htab = ppc_hash_table (info);
11573   if (htab == NULL)
11574     return FALSE;
11575
11576   if (stub_entry->h != NULL
11577       && stub_entry->h->save_res
11578       && stub_entry->h->elf.root.type == bfd_link_hash_defined
11579       && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11580     {
11581       /* Don't make stubs to out-of-line register save/restore
11582          functions.  Instead, emit copies of the functions.  */
11583       stub_entry->group->needs_save_res = 1;
11584       stub_entry->stub_type = ppc_stub_save_res;
11585       return TRUE;
11586     }
11587
11588   if (stub_entry->stub_type == ppc_stub_plt_call
11589       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11590     {
11591       asection *plt;
11592       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11593       if (off >= (bfd_vma) -2)
11594         abort ();
11595       plt = htab->elf.splt;
11596       if (!htab->elf.dynamic_sections_created
11597           || stub_entry->h == NULL
11598           || stub_entry->h->elf.dynindx == -1)
11599         {
11600           if (stub_entry->symtype == STT_GNU_IFUNC)
11601             plt = htab->elf.iplt;
11602           else
11603             plt = htab->pltlocal;
11604         }
11605       off += (plt->output_offset
11606               + plt->output_section->vma
11607               - elf_gp (info->output_bfd)
11608               - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11609
11610       size = plt_stub_size (htab, stub_entry, off);
11611       if (stub_entry->h != NULL
11612           && (stub_entry->h == htab->tls_get_addr_fd
11613               || stub_entry->h == htab->tls_get_addr)
11614           && htab->params->tls_get_addr_opt
11615           && (ALWAYS_EMIT_R2SAVE
11616               || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11617         stub_entry->group->tls_get_addr_opt_bctrl
11618           = stub_entry->group->stub_sec->size + size - 5 * 4;
11619
11620       if (htab->params->plt_stub_align)
11621         size += plt_stub_pad (htab, stub_entry, off);
11622       if (info->emitrelocations)
11623         {
11624           stub_entry->group->stub_sec->reloc_count
11625             += ((PPC_HA (off) != 0)
11626                 + (htab->opd_abi
11627                    ? 2 + (htab->params->plt_static_chain
11628                           && PPC_HA (off + 16) == PPC_HA (off))
11629                    : 1));
11630           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11631         }
11632     }
11633   else
11634     {
11635       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11636          variants.  */
11637       bfd_vma r2off = 0;
11638       bfd_vma local_off = 0;
11639
11640       off = (stub_entry->target_value
11641              + stub_entry->target_section->output_offset
11642              + stub_entry->target_section->output_section->vma);
11643       off -= (stub_entry->group->stub_sec->size
11644               + stub_entry->group->stub_sec->output_offset
11645               + stub_entry->group->stub_sec->output_section->vma);
11646
11647       /* Reset the stub type from the plt variant in case we now
11648          can reach with a shorter stub.  */
11649       if (stub_entry->stub_type >= ppc_stub_plt_branch)
11650         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11651
11652       size = 4;
11653       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11654         {
11655           r2off = get_r2off (info, stub_entry);
11656           if (r2off == (bfd_vma) -1)
11657             {
11658               htab->stub_error = TRUE;
11659               return FALSE;
11660             }
11661           size = 8;
11662           if (PPC_HA (r2off) != 0)
11663             size += 4;
11664           if (PPC_LO (r2off) != 0)
11665             size += 4;
11666           off -= size - 4;
11667         }
11668
11669       local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11670
11671       /* If the branch offset is too big, use a ppc_stub_plt_branch.
11672          Do the same for -R objects without function descriptors.  */
11673       if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11674           || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11675               && r2off == 0
11676               && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11677         {
11678           struct ppc_branch_hash_entry *br_entry;
11679
11680           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11681                                              stub_entry->root.string + 9,
11682                                              TRUE, FALSE);
11683           if (br_entry == NULL)
11684             {
11685               _bfd_error_handler (_("can't build branch stub `%s'"),
11686                                   stub_entry->root.string);
11687               htab->stub_error = TRUE;
11688               return FALSE;
11689             }
11690
11691           if (br_entry->iter != htab->stub_iteration)
11692             {
11693               br_entry->iter = htab->stub_iteration;
11694               br_entry->offset = htab->brlt->size;
11695               htab->brlt->size += 8;
11696
11697               if (htab->relbrlt != NULL)
11698                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11699               else if (info->emitrelocations)
11700                 {
11701                   htab->brlt->reloc_count += 1;
11702                   htab->brlt->flags |= SEC_RELOC;
11703                 }
11704             }
11705
11706           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11707           off = (br_entry->offset
11708                  + htab->brlt->output_offset
11709                  + htab->brlt->output_section->vma
11710                  - elf_gp (info->output_bfd)
11711                  - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11712
11713           if (info->emitrelocations)
11714             {
11715               stub_entry->group->stub_sec->reloc_count
11716                 += 1 + (PPC_HA (off) != 0);
11717               stub_entry->group->stub_sec->flags |= SEC_RELOC;
11718             }
11719
11720           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11721             {
11722               size = 12;
11723               if (PPC_HA (off) != 0)
11724                 size = 16;
11725             }
11726           else
11727             {
11728               size = 16;
11729               if (PPC_HA (off) != 0)
11730                 size += 4;
11731
11732               if (PPC_HA (r2off) != 0)
11733                 size += 4;
11734               if (PPC_LO (r2off) != 0)
11735                 size += 4;
11736             }
11737         }
11738       else if (info->emitrelocations)
11739         {
11740           stub_entry->group->stub_sec->reloc_count += 1;
11741           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11742         }
11743     }
11744
11745   stub_entry->group->stub_sec->size += size;
11746   return TRUE;
11747 }
11748
11749 /* Set up various things so that we can make a list of input sections
11750    for each output section included in the link.  Returns -1 on error,
11751    0 when no stubs will be needed, and 1 on success.  */
11752
11753 int
11754 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11755 {
11756   unsigned int id;
11757   bfd_size_type amt;
11758   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11759
11760   if (htab == NULL)
11761     return -1;
11762
11763   htab->sec_info_arr_size = _bfd_section_id;
11764   amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11765   htab->sec_info = bfd_zmalloc (amt);
11766   if (htab->sec_info == NULL)
11767     return -1;
11768
11769   /* Set toc_off for com, und, abs and ind sections.  */
11770   for (id = 0; id < 3; id++)
11771     htab->sec_info[id].toc_off = TOC_BASE_OFF;
11772
11773   return 1;
11774 }
11775
11776 /* Set up for first pass at multitoc partitioning.  */
11777
11778 void
11779 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11780 {
11781   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11782
11783   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11784   htab->toc_bfd = NULL;
11785   htab->toc_first_sec = NULL;
11786 }
11787
11788 /* The linker repeatedly calls this function for each TOC input section
11789    and linker generated GOT section.  Group input bfds such that the toc
11790    within a group is less than 64k in size.  */
11791
11792 bfd_boolean
11793 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11794 {
11795   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11796   bfd_vma addr, off, limit;
11797
11798   if (htab == NULL)
11799     return FALSE;
11800
11801   if (!htab->second_toc_pass)
11802     {
11803       /* Keep track of the first .toc or .got section for this input bfd.  */
11804       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11805
11806       if (new_bfd)
11807         {
11808           htab->toc_bfd = isec->owner;
11809           htab->toc_first_sec = isec;
11810         }
11811
11812       addr = isec->output_offset + isec->output_section->vma;
11813       off = addr - htab->toc_curr;
11814       limit = 0x80008000;
11815       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11816         limit = 0x10000;
11817       if (off + isec->size > limit)
11818         {
11819           addr = (htab->toc_first_sec->output_offset
11820                   + htab->toc_first_sec->output_section->vma);
11821           htab->toc_curr = addr;
11822           htab->toc_curr &= -TOC_BASE_ALIGN;
11823         }
11824
11825       /* toc_curr is the base address of this toc group.  Set elf_gp
11826          for the input section to be the offset relative to the
11827          output toc base plus 0x8000.  Making the input elf_gp an
11828          offset allows us to move the toc as a whole without
11829          recalculating input elf_gp.  */
11830       off = htab->toc_curr - elf_gp (info->output_bfd);
11831       off += TOC_BASE_OFF;
11832
11833       /* Die if someone uses a linker script that doesn't keep input
11834          file .toc and .got together.  */
11835       if (new_bfd
11836           && elf_gp (isec->owner) != 0
11837           && elf_gp (isec->owner) != off)
11838         return FALSE;
11839
11840       elf_gp (isec->owner) = off;
11841       return TRUE;
11842     }
11843
11844   /* During the second pass toc_first_sec points to the start of
11845      a toc group, and toc_curr is used to track the old elf_gp.
11846      We use toc_bfd to ensure we only look at each bfd once.  */
11847   if (htab->toc_bfd == isec->owner)
11848     return TRUE;
11849   htab->toc_bfd = isec->owner;
11850
11851   if (htab->toc_first_sec == NULL
11852       || htab->toc_curr != elf_gp (isec->owner))
11853     {
11854       htab->toc_curr = elf_gp (isec->owner);
11855       htab->toc_first_sec = isec;
11856     }
11857   addr = (htab->toc_first_sec->output_offset
11858           + htab->toc_first_sec->output_section->vma);
11859   off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
11860   elf_gp (isec->owner) = off;
11861
11862   return TRUE;
11863 }
11864
11865 /* Called via elf_link_hash_traverse to merge GOT entries for global
11866    symbol H.  */
11867
11868 static bfd_boolean
11869 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11870 {
11871   if (h->root.type == bfd_link_hash_indirect)
11872     return TRUE;
11873
11874   merge_got_entries (&h->got.glist);
11875
11876   return TRUE;
11877 }
11878
11879 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11880    symbol H.  */
11881
11882 static bfd_boolean
11883 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11884 {
11885   struct got_entry *gent;
11886
11887   if (h->root.type == bfd_link_hash_indirect)
11888     return TRUE;
11889
11890   for (gent = h->got.glist; gent != NULL; gent = gent->next)
11891     if (!gent->is_indirect)
11892       allocate_got (h, (struct bfd_link_info *) inf, gent);
11893   return TRUE;
11894 }
11895
11896 /* Called on the first multitoc pass after the last call to
11897    ppc64_elf_next_toc_section.  This function removes duplicate GOT
11898    entries.  */
11899
11900 bfd_boolean
11901 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11902 {
11903   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11904   struct bfd *ibfd, *ibfd2;
11905   bfd_boolean done_something;
11906
11907   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11908
11909   if (!htab->do_multi_toc)
11910     return FALSE;
11911
11912   /* Merge global sym got entries within a toc group.  */
11913   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11914
11915   /* And tlsld_got.  */
11916   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11917     {
11918       struct got_entry *ent, *ent2;
11919
11920       if (!is_ppc64_elf (ibfd))
11921         continue;
11922
11923       ent = ppc64_tlsld_got (ibfd);
11924       if (!ent->is_indirect
11925           && ent->got.offset != (bfd_vma) -1)
11926         {
11927           for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11928             {
11929               if (!is_ppc64_elf (ibfd2))
11930                 continue;
11931
11932               ent2 = ppc64_tlsld_got (ibfd2);
11933               if (!ent2->is_indirect
11934                   && ent2->got.offset != (bfd_vma) -1
11935                   && elf_gp (ibfd2) == elf_gp (ibfd))
11936                 {
11937                   ent2->is_indirect = TRUE;
11938                   ent2->got.ent = ent;
11939                 }
11940             }
11941         }
11942     }
11943
11944   /* Zap sizes of got sections.  */
11945   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11946   htab->elf.irelplt->size -= htab->got_reli_size;
11947   htab->got_reli_size = 0;
11948
11949   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11950     {
11951       asection *got, *relgot;
11952
11953       if (!is_ppc64_elf (ibfd))
11954         continue;
11955
11956       got = ppc64_elf_tdata (ibfd)->got;
11957       if (got != NULL)
11958         {
11959           got->rawsize = got->size;
11960           got->size = 0;
11961           relgot = ppc64_elf_tdata (ibfd)->relgot;
11962           relgot->rawsize = relgot->size;
11963           relgot->size = 0;
11964         }
11965     }
11966
11967   /* Now reallocate the got, local syms first.  We don't need to
11968      allocate section contents again since we never increase size.  */
11969   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11970     {
11971       struct got_entry **lgot_ents;
11972       struct got_entry **end_lgot_ents;
11973       struct plt_entry **local_plt;
11974       struct plt_entry **end_local_plt;
11975       unsigned char *lgot_masks;
11976       bfd_size_type locsymcount;
11977       Elf_Internal_Shdr *symtab_hdr;
11978       asection *s;
11979
11980       if (!is_ppc64_elf (ibfd))
11981         continue;
11982
11983       lgot_ents = elf_local_got_ents (ibfd);
11984       if (!lgot_ents)
11985         continue;
11986
11987       symtab_hdr = &elf_symtab_hdr (ibfd);
11988       locsymcount = symtab_hdr->sh_info;
11989       end_lgot_ents = lgot_ents + locsymcount;
11990       local_plt = (struct plt_entry **) end_lgot_ents;
11991       end_local_plt = local_plt + locsymcount;
11992       lgot_masks = (unsigned char *) end_local_plt;
11993       s = ppc64_elf_tdata (ibfd)->got;
11994       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11995         {
11996           struct got_entry *ent;
11997
11998           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11999             {
12000               unsigned int ent_size = 8;
12001               unsigned int rel_size = sizeof (Elf64_External_Rela);
12002
12003               ent->got.offset = s->size;
12004               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12005                 {
12006                   ent_size *= 2;
12007                   rel_size *= 2;
12008                 }
12009               s->size += ent_size;
12010               if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12011                 {
12012                   htab->elf.irelplt->size += rel_size;
12013                   htab->got_reli_size += rel_size;
12014                 }
12015               else if (bfd_link_pic (info)
12016                        && !((ent->tls_type & TLS_TPREL) != 0
12017                             && bfd_link_executable (info)))
12018                 {
12019                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12020                   srel->size += rel_size;
12021                 }
12022             }
12023         }
12024     }
12025
12026   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12027
12028   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12029     {
12030       struct got_entry *ent;
12031
12032       if (!is_ppc64_elf (ibfd))
12033         continue;
12034
12035       ent = ppc64_tlsld_got (ibfd);
12036       if (!ent->is_indirect
12037           && ent->got.offset != (bfd_vma) -1)
12038         {
12039           asection *s = ppc64_elf_tdata (ibfd)->got;
12040           ent->got.offset = s->size;
12041           s->size += 16;
12042           if (bfd_link_pic (info))
12043             {
12044               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12045               srel->size += sizeof (Elf64_External_Rela);
12046             }
12047         }
12048     }
12049
12050   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12051   if (!done_something)
12052     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12053       {
12054         asection *got;
12055
12056         if (!is_ppc64_elf (ibfd))
12057           continue;
12058
12059         got = ppc64_elf_tdata (ibfd)->got;
12060         if (got != NULL)
12061           {
12062             done_something = got->rawsize != got->size;
12063             if (done_something)
12064               break;
12065           }
12066       }
12067
12068   if (done_something)
12069     (*htab->params->layout_sections_again) ();
12070
12071   /* Set up for second pass over toc sections to recalculate elf_gp
12072      on input sections.  */
12073   htab->toc_bfd = NULL;
12074   htab->toc_first_sec = NULL;
12075   htab->second_toc_pass = TRUE;
12076   return done_something;
12077 }
12078
12079 /* Called after second pass of multitoc partitioning.  */
12080
12081 void
12082 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12083 {
12084   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12085
12086   /* After the second pass, toc_curr tracks the TOC offset used
12087      for code sections below in ppc64_elf_next_input_section.  */
12088   htab->toc_curr = TOC_BASE_OFF;
12089 }
12090
12091 /* No toc references were found in ISEC.  If the code in ISEC makes no
12092    calls, then there's no need to use toc adjusting stubs when branching
12093    into ISEC.  Actually, indirect calls from ISEC are OK as they will
12094    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
12095    needed, and 2 if a cyclical call-graph was found but no other reason
12096    for a stub was detected.  If called from the top level, a return of
12097    2 means the same as a return of 0.  */
12098
12099 static int
12100 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12101 {
12102   int ret;
12103
12104   /* Mark this section as checked.  */
12105   isec->call_check_done = 1;
12106
12107   /* We know none of our code bearing sections will need toc stubs.  */
12108   if ((isec->flags & SEC_LINKER_CREATED) != 0)
12109     return 0;
12110
12111   if (isec->size == 0)
12112     return 0;
12113
12114   if (isec->output_section == NULL)
12115     return 0;
12116
12117   ret = 0;
12118   if (isec->reloc_count != 0)
12119     {
12120       Elf_Internal_Rela *relstart, *rel;
12121       Elf_Internal_Sym *local_syms;
12122       struct ppc_link_hash_table *htab;
12123
12124       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12125                                             info->keep_memory);
12126       if (relstart == NULL)
12127         return -1;
12128
12129       /* Look for branches to outside of this section.  */
12130       local_syms = NULL;
12131       htab = ppc_hash_table (info);
12132       if (htab == NULL)
12133         return -1;
12134
12135       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12136         {
12137           enum elf_ppc64_reloc_type r_type;
12138           unsigned long r_symndx;
12139           struct elf_link_hash_entry *h;
12140           struct ppc_link_hash_entry *eh;
12141           Elf_Internal_Sym *sym;
12142           asection *sym_sec;
12143           struct _opd_sec_data *opd;
12144           bfd_vma sym_value;
12145           bfd_vma dest;
12146
12147           r_type = ELF64_R_TYPE (rel->r_info);
12148           if (r_type != R_PPC64_REL24
12149               && r_type != R_PPC64_REL14
12150               && r_type != R_PPC64_REL14_BRTAKEN
12151               && r_type != R_PPC64_REL14_BRNTAKEN
12152               && r_type != R_PPC64_PLTCALL)
12153             continue;
12154
12155           r_symndx = ELF64_R_SYM (rel->r_info);
12156           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12157                           isec->owner))
12158             {
12159               ret = -1;
12160               break;
12161             }
12162
12163           /* Calls to dynamic lib functions go through a plt call stub
12164              that uses r2.  */
12165           eh = (struct ppc_link_hash_entry *) h;
12166           if (eh != NULL
12167               && (eh->elf.plt.plist != NULL
12168                   || (eh->oh != NULL
12169                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12170             {
12171               ret = 1;
12172               break;
12173             }
12174
12175           if (sym_sec == NULL)
12176             /* Ignore other undefined symbols.  */
12177             continue;
12178
12179           /* Assume branches to other sections not included in the
12180              link need stubs too, to cover -R and absolute syms.  */
12181           if (sym_sec->output_section == NULL)
12182             {
12183               ret = 1;
12184               break;
12185             }
12186
12187           if (h == NULL)
12188             sym_value = sym->st_value;
12189           else
12190             {
12191               if (h->root.type != bfd_link_hash_defined
12192                   && h->root.type != bfd_link_hash_defweak)
12193                 abort ();
12194               sym_value = h->root.u.def.value;
12195             }
12196           sym_value += rel->r_addend;
12197
12198           /* If this branch reloc uses an opd sym, find the code section.  */
12199           opd = get_opd_info (sym_sec);
12200           if (opd != NULL)
12201             {
12202               if (h == NULL && opd->adjust != NULL)
12203                 {
12204                   long adjust;
12205
12206                   adjust = opd->adjust[OPD_NDX (sym_value)];
12207                   if (adjust == -1)
12208                     /* Assume deleted functions won't ever be called.  */
12209                     continue;
12210                   sym_value += adjust;
12211                 }
12212
12213               dest = opd_entry_value (sym_sec, sym_value,
12214                                       &sym_sec, NULL, FALSE);
12215               if (dest == (bfd_vma) -1)
12216                 continue;
12217             }
12218           else
12219             dest = (sym_value
12220                     + sym_sec->output_offset
12221                     + sym_sec->output_section->vma);
12222
12223           /* Ignore branch to self.  */
12224           if (sym_sec == isec)
12225             continue;
12226
12227           /* If the called function uses the toc, we need a stub.  */
12228           if (sym_sec->has_toc_reloc
12229               || sym_sec->makes_toc_func_call)
12230             {
12231               ret = 1;
12232               break;
12233             }
12234
12235           /* Assume any branch that needs a long branch stub might in fact
12236              need a plt_branch stub.  A plt_branch stub uses r2.  */
12237           else if (dest - (isec->output_offset
12238                            + isec->output_section->vma
12239                            + rel->r_offset) + (1 << 25)
12240                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12241                                                              ? h->other
12242                                                              : sym->st_other))
12243             {
12244               ret = 1;
12245               break;
12246             }
12247
12248           /* If calling back to a section in the process of being
12249              tested, we can't say for sure that no toc adjusting stubs
12250              are needed, so don't return zero.  */
12251           else if (sym_sec->call_check_in_progress)
12252             ret = 2;
12253
12254           /* Branches to another section that itself doesn't have any TOC
12255              references are OK.  Recursively call ourselves to check.  */
12256           else if (!sym_sec->call_check_done)
12257             {
12258               int recur;
12259
12260               /* Mark current section as indeterminate, so that other
12261                  sections that call back to current won't be marked as
12262                  known.  */
12263               isec->call_check_in_progress = 1;
12264               recur = toc_adjusting_stub_needed (info, sym_sec);
12265               isec->call_check_in_progress = 0;
12266
12267               if (recur != 0)
12268                 {
12269                   ret = recur;
12270                   if (recur != 2)
12271                     break;
12272                 }
12273             }
12274         }
12275
12276       if (local_syms != NULL
12277           && (elf_symtab_hdr (isec->owner).contents
12278               != (unsigned char *) local_syms))
12279         free (local_syms);
12280       if (elf_section_data (isec)->relocs != relstart)
12281         free (relstart);
12282     }
12283
12284   if ((ret & 1) == 0
12285       && isec->map_head.s != NULL
12286       && (strcmp (isec->output_section->name, ".init") == 0
12287           || strcmp (isec->output_section->name, ".fini") == 0))
12288     {
12289       if (isec->map_head.s->has_toc_reloc
12290           || isec->map_head.s->makes_toc_func_call)
12291         ret = 1;
12292       else if (!isec->map_head.s->call_check_done)
12293         {
12294           int recur;
12295           isec->call_check_in_progress = 1;
12296           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12297           isec->call_check_in_progress = 0;
12298           if (recur != 0)
12299             ret = recur;
12300         }
12301     }
12302
12303   if (ret == 1)
12304     isec->makes_toc_func_call = 1;
12305
12306   return ret;
12307 }
12308
12309 /* The linker repeatedly calls this function for each input section,
12310    in the order that input sections are linked into output sections.
12311    Build lists of input sections to determine groupings between which
12312    we may insert linker stubs.  */
12313
12314 bfd_boolean
12315 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12316 {
12317   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12318
12319   if (htab == NULL)
12320     return FALSE;
12321
12322   if ((isec->output_section->flags & SEC_CODE) != 0
12323       && isec->output_section->id < htab->sec_info_arr_size)
12324     {
12325       /* This happens to make the list in reverse order,
12326          which is what we want.  */
12327       htab->sec_info[isec->id].u.list
12328         = htab->sec_info[isec->output_section->id].u.list;
12329       htab->sec_info[isec->output_section->id].u.list = isec;
12330     }
12331
12332   if (htab->multi_toc_needed)
12333     {
12334       /* Analyse sections that aren't already flagged as needing a
12335          valid toc pointer.  Exclude .fixup for the linux kernel.
12336          .fixup contains branches, but only back to the function that
12337          hit an exception.  */
12338       if (!(isec->has_toc_reloc
12339             || (isec->flags & SEC_CODE) == 0
12340             || strcmp (isec->name, ".fixup") == 0
12341             || isec->call_check_done))
12342         {
12343           if (toc_adjusting_stub_needed (info, isec) < 0)
12344             return FALSE;
12345         }
12346       /* Make all sections use the TOC assigned for this object file.
12347          This will be wrong for pasted sections;  We fix that in
12348          check_pasted_section().  */
12349       if (elf_gp (isec->owner) != 0)
12350         htab->toc_curr = elf_gp (isec->owner);
12351     }
12352
12353   htab->sec_info[isec->id].toc_off = htab->toc_curr;
12354   return TRUE;
12355 }
12356
12357 /* Check that all .init and .fini sections use the same toc, if they
12358    have toc relocs.  */
12359
12360 static bfd_boolean
12361 check_pasted_section (struct bfd_link_info *info, const char *name)
12362 {
12363   asection *o = bfd_get_section_by_name (info->output_bfd, name);
12364
12365   if (o != NULL)
12366     {
12367       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12368       bfd_vma toc_off = 0;
12369       asection *i;
12370
12371       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12372         if (i->has_toc_reloc)
12373           {
12374             if (toc_off == 0)
12375               toc_off = htab->sec_info[i->id].toc_off;
12376             else if (toc_off != htab->sec_info[i->id].toc_off)
12377               return FALSE;
12378           }
12379
12380       if (toc_off == 0)
12381         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12382           if (i->makes_toc_func_call)
12383             {
12384               toc_off = htab->sec_info[i->id].toc_off;
12385               break;
12386             }
12387
12388       /* Make sure the whole pasted function uses the same toc offset.  */
12389       if (toc_off != 0)
12390         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12391           htab->sec_info[i->id].toc_off = toc_off;
12392     }
12393   return TRUE;
12394 }
12395
12396 bfd_boolean
12397 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12398 {
12399   return (check_pasted_section (info, ".init")
12400           & check_pasted_section (info, ".fini"));
12401 }
12402
12403 /* See whether we can group stub sections together.  Grouping stub
12404    sections may result in fewer stubs.  More importantly, we need to
12405    put all .init* and .fini* stubs at the beginning of the .init or
12406    .fini output sections respectively, because glibc splits the
12407    _init and _fini functions into multiple parts.  Putting a stub in
12408    the middle of a function is not a good idea.  */
12409
12410 static bfd_boolean
12411 group_sections (struct bfd_link_info *info,
12412                 bfd_size_type stub_group_size,
12413                 bfd_boolean stubs_always_before_branch)
12414 {
12415   struct ppc_link_hash_table *htab;
12416   asection *osec;
12417   bfd_boolean suppress_size_errors;
12418
12419   htab = ppc_hash_table (info);
12420   if (htab == NULL)
12421     return FALSE;
12422
12423   suppress_size_errors = FALSE;
12424   if (stub_group_size == 1)
12425     {
12426       /* Default values.  */
12427       if (stubs_always_before_branch)
12428         stub_group_size = 0x1e00000;
12429       else
12430         stub_group_size = 0x1c00000;
12431       suppress_size_errors = TRUE;
12432     }
12433
12434   for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12435     {
12436       asection *tail;
12437
12438       if (osec->id >= htab->sec_info_arr_size)
12439         continue;
12440
12441       tail = htab->sec_info[osec->id].u.list;
12442       while (tail != NULL)
12443         {
12444           asection *curr;
12445           asection *prev;
12446           bfd_size_type total;
12447           bfd_boolean big_sec;
12448           bfd_vma curr_toc;
12449           struct map_stub *group;
12450           bfd_size_type group_size;
12451
12452           curr = tail;
12453           total = tail->size;
12454           group_size = (ppc64_elf_section_data (tail) != NULL
12455                         && ppc64_elf_section_data (tail)->has_14bit_branch
12456                         ? stub_group_size >> 10 : stub_group_size);
12457
12458           big_sec = total > group_size;
12459           if (big_sec && !suppress_size_errors)
12460             /* xgettext:c-format */
12461             _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12462                                 tail->owner, tail);
12463           curr_toc = htab->sec_info[tail->id].toc_off;
12464
12465           while ((prev = htab->sec_info[curr->id].u.list) != NULL
12466                  && ((total += curr->output_offset - prev->output_offset)
12467                      < (ppc64_elf_section_data (prev) != NULL
12468                         && ppc64_elf_section_data (prev)->has_14bit_branch
12469                         ? (group_size = stub_group_size >> 10) : group_size))
12470                  && htab->sec_info[prev->id].toc_off == curr_toc)
12471             curr = prev;
12472
12473           /* OK, the size from the start of CURR to the end is less
12474              than group_size and thus can be handled by one stub
12475              section.  (or the tail section is itself larger than
12476              group_size, in which case we may be toast.)  We should
12477              really be keeping track of the total size of stubs added
12478              here, as stubs contribute to the final output section
12479              size.  That's a little tricky, and this way will only
12480              break if stubs added make the total size more than 2^25,
12481              ie. for the default stub_group_size, if stubs total more
12482              than 2097152 bytes, or nearly 75000 plt call stubs.  */
12483           group = bfd_alloc (curr->owner, sizeof (*group));
12484           if (group == NULL)
12485             return FALSE;
12486           group->link_sec = curr;
12487           group->stub_sec = NULL;
12488           group->needs_save_res = 0;
12489           group->tls_get_addr_opt_bctrl = -1u;
12490           group->next = htab->group;
12491           htab->group = group;
12492           do
12493             {
12494               prev = htab->sec_info[tail->id].u.list;
12495               /* Set up this stub group.  */
12496               htab->sec_info[tail->id].u.group = group;
12497             }
12498           while (tail != curr && (tail = prev) != NULL);
12499
12500           /* But wait, there's more!  Input sections up to group_size
12501              bytes before the stub section can be handled by it too.
12502              Don't do this if we have a really large section after the
12503              stubs, as adding more stubs increases the chance that
12504              branches may not reach into the stub section.  */
12505           if (!stubs_always_before_branch && !big_sec)
12506             {
12507               total = 0;
12508               while (prev != NULL
12509                      && ((total += tail->output_offset - prev->output_offset)
12510                          < (ppc64_elf_section_data (prev) != NULL
12511                             && ppc64_elf_section_data (prev)->has_14bit_branch
12512                             ? (group_size = stub_group_size >> 10) : group_size))
12513                      && htab->sec_info[prev->id].toc_off == curr_toc)
12514                 {
12515                   tail = prev;
12516                   prev = htab->sec_info[tail->id].u.list;
12517                   htab->sec_info[tail->id].u.group = group;
12518                 }
12519             }
12520           tail = prev;
12521         }
12522     }
12523   return TRUE;
12524 }
12525
12526 static const unsigned char glink_eh_frame_cie[] =
12527 {
12528   0, 0, 0, 16,                          /* length.  */
12529   0, 0, 0, 0,                           /* id.  */
12530   1,                                    /* CIE version.  */
12531   'z', 'R', 0,                          /* Augmentation string.  */
12532   4,                                    /* Code alignment.  */
12533   0x78,                                 /* Data alignment.  */
12534   65,                                   /* RA reg.  */
12535   1,                                    /* Augmentation size.  */
12536   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
12537   DW_CFA_def_cfa, 1, 0                  /* def_cfa: r1 offset 0.  */
12538 };
12539
12540 static size_t
12541 stub_eh_frame_size (struct map_stub *group, size_t align)
12542 {
12543   size_t this_size = 17;
12544   if (group->tls_get_addr_opt_bctrl != -1u)
12545     {
12546       unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12547       if (to_bctrl < 64)
12548         this_size += 1;
12549       else if (to_bctrl < 256)
12550         this_size += 2;
12551       else if (to_bctrl < 65536)
12552         this_size += 3;
12553       else
12554         this_size += 5;
12555       this_size += 6;
12556     }
12557   this_size = (this_size + align - 1) & -align;
12558   return this_size;
12559 }
12560
12561 /* Stripping output sections is normally done before dynamic section
12562    symbols have been allocated.  This function is called later, and
12563    handles cases like htab->brlt which is mapped to its own output
12564    section.  */
12565
12566 static void
12567 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12568 {
12569   if (isec->size == 0
12570       && isec->output_section->size == 0
12571       && !(isec->output_section->flags & SEC_KEEP)
12572       && !bfd_section_removed_from_list (info->output_bfd,
12573                                          isec->output_section)
12574       && elf_section_data (isec->output_section)->dynindx == 0)
12575     {
12576       isec->output_section->flags |= SEC_EXCLUDE;
12577       bfd_section_list_remove (info->output_bfd, isec->output_section);
12578       info->output_bfd->section_count--;
12579     }
12580 }
12581
12582 /* Determine and set the size of the stub section for a final link.
12583
12584    The basic idea here is to examine all the relocations looking for
12585    PC-relative calls to a target that is unreachable with a "bl"
12586    instruction.  */
12587
12588 bfd_boolean
12589 ppc64_elf_size_stubs (struct bfd_link_info *info)
12590 {
12591   bfd_size_type stub_group_size;
12592   bfd_boolean stubs_always_before_branch;
12593   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12594
12595   if (htab == NULL)
12596     return FALSE;
12597
12598   if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12599     htab->params->plt_thread_safe = 1;
12600   if (!htab->opd_abi)
12601     htab->params->plt_thread_safe = 0;
12602   else if (htab->params->plt_thread_safe == -1)
12603     {
12604       static const char *const thread_starter[] =
12605         {
12606           "pthread_create",
12607           /* libstdc++ */
12608           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12609           /* librt */
12610           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12611           "mq_notify", "create_timer",
12612           /* libanl */
12613           "getaddrinfo_a",
12614           /* libgomp */
12615           "GOMP_parallel",
12616           "GOMP_parallel_start",
12617           "GOMP_parallel_loop_static",
12618           "GOMP_parallel_loop_static_start",
12619           "GOMP_parallel_loop_dynamic",
12620           "GOMP_parallel_loop_dynamic_start",
12621           "GOMP_parallel_loop_guided",
12622           "GOMP_parallel_loop_guided_start",
12623           "GOMP_parallel_loop_runtime",
12624           "GOMP_parallel_loop_runtime_start",
12625           "GOMP_parallel_sections",
12626           "GOMP_parallel_sections_start",
12627           /* libgo */
12628           "__go_go",
12629         };
12630       unsigned i;
12631
12632       for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12633         {
12634           struct elf_link_hash_entry *h;
12635           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12636                                     FALSE, FALSE, TRUE);
12637           htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12638           if (htab->params->plt_thread_safe)
12639             break;
12640         }
12641     }
12642   stubs_always_before_branch = htab->params->group_size < 0;
12643   if (htab->params->group_size < 0)
12644     stub_group_size = -htab->params->group_size;
12645   else
12646     stub_group_size = htab->params->group_size;
12647
12648   if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12649     return FALSE;
12650
12651 #define STUB_SHRINK_ITER 20
12652   /* Loop until no stubs added.  After iteration 20 of this loop we may
12653      exit on a stub section shrinking.  This is to break out of a
12654      pathological case where adding stubs on one iteration decreases
12655      section gaps (perhaps due to alignment), which then requires
12656      fewer or smaller stubs on the next iteration.  */
12657
12658   while (1)
12659     {
12660       bfd *input_bfd;
12661       unsigned int bfd_indx;
12662       struct map_stub *group;
12663
12664       htab->stub_iteration += 1;
12665
12666       for (input_bfd = info->input_bfds, bfd_indx = 0;
12667            input_bfd != NULL;
12668            input_bfd = input_bfd->link.next, bfd_indx++)
12669         {
12670           Elf_Internal_Shdr *symtab_hdr;
12671           asection *section;
12672           Elf_Internal_Sym *local_syms = NULL;
12673
12674           if (!is_ppc64_elf (input_bfd))
12675             continue;
12676
12677           /* We'll need the symbol table in a second.  */
12678           symtab_hdr = &elf_symtab_hdr (input_bfd);
12679           if (symtab_hdr->sh_info == 0)
12680             continue;
12681
12682           /* Walk over each section attached to the input bfd.  */
12683           for (section = input_bfd->sections;
12684                section != NULL;
12685                section = section->next)
12686             {
12687               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12688
12689               /* If there aren't any relocs, then there's nothing more
12690                  to do.  */
12691               if ((section->flags & SEC_RELOC) == 0
12692                   || (section->flags & SEC_ALLOC) == 0
12693                   || (section->flags & SEC_LOAD) == 0
12694                   || (section->flags & SEC_CODE) == 0
12695                   || section->reloc_count == 0)
12696                 continue;
12697
12698               /* If this section is a link-once section that will be
12699                  discarded, then don't create any stubs.  */
12700               if (section->output_section == NULL
12701                   || section->output_section->owner != info->output_bfd)
12702                 continue;
12703
12704               /* Get the relocs.  */
12705               internal_relocs
12706                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12707                                              info->keep_memory);
12708               if (internal_relocs == NULL)
12709                 goto error_ret_free_local;
12710
12711               /* Now examine each relocation.  */
12712               irela = internal_relocs;
12713               irelaend = irela + section->reloc_count;
12714               for (; irela < irelaend; irela++)
12715                 {
12716                   enum elf_ppc64_reloc_type r_type;
12717                   unsigned int r_indx;
12718                   enum ppc_stub_type stub_type;
12719                   struct ppc_stub_hash_entry *stub_entry;
12720                   asection *sym_sec, *code_sec;
12721                   bfd_vma sym_value, code_value;
12722                   bfd_vma destination;
12723                   unsigned long local_off;
12724                   bfd_boolean ok_dest;
12725                   struct ppc_link_hash_entry *hash;
12726                   struct ppc_link_hash_entry *fdh;
12727                   struct elf_link_hash_entry *h;
12728                   Elf_Internal_Sym *sym;
12729                   char *stub_name;
12730                   const asection *id_sec;
12731                   struct _opd_sec_data *opd;
12732                   struct plt_entry *plt_ent;
12733
12734                   r_type = ELF64_R_TYPE (irela->r_info);
12735                   r_indx = ELF64_R_SYM (irela->r_info);
12736
12737                   if (r_type >= R_PPC64_max)
12738                     {
12739                       bfd_set_error (bfd_error_bad_value);
12740                       goto error_ret_free_internal;
12741                     }
12742
12743                   /* Only look for stubs on branch instructions.  */
12744                   if (r_type != R_PPC64_REL24
12745                       && r_type != R_PPC64_REL14
12746                       && r_type != R_PPC64_REL14_BRTAKEN
12747                       && r_type != R_PPC64_REL14_BRNTAKEN)
12748                     continue;
12749
12750                   /* Now determine the call target, its name, value,
12751                      section.  */
12752                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12753                                   r_indx, input_bfd))
12754                     goto error_ret_free_internal;
12755                   hash = (struct ppc_link_hash_entry *) h;
12756
12757                   ok_dest = FALSE;
12758                   fdh = NULL;
12759                   sym_value = 0;
12760                   if (hash == NULL)
12761                     {
12762                       sym_value = sym->st_value;
12763                       if (sym_sec != NULL
12764                           && sym_sec->output_section != NULL)
12765                         ok_dest = TRUE;
12766                     }
12767                   else if (hash->elf.root.type == bfd_link_hash_defined
12768                            || hash->elf.root.type == bfd_link_hash_defweak)
12769                     {
12770                       sym_value = hash->elf.root.u.def.value;
12771                       if (sym_sec->output_section != NULL)
12772                         ok_dest = TRUE;
12773                     }
12774                   else if (hash->elf.root.type == bfd_link_hash_undefweak
12775                            || hash->elf.root.type == bfd_link_hash_undefined)
12776                     {
12777                       /* Recognise an old ABI func code entry sym, and
12778                          use the func descriptor sym instead if it is
12779                          defined.  */
12780                       if (hash->elf.root.root.string[0] == '.'
12781                           && hash->oh != NULL)
12782                         {
12783                           fdh = ppc_follow_link (hash->oh);
12784                           if (fdh->elf.root.type == bfd_link_hash_defined
12785                               || fdh->elf.root.type == bfd_link_hash_defweak)
12786                             {
12787                               sym_sec = fdh->elf.root.u.def.section;
12788                               sym_value = fdh->elf.root.u.def.value;
12789                               if (sym_sec->output_section != NULL)
12790                                 ok_dest = TRUE;
12791                             }
12792                           else
12793                             fdh = NULL;
12794                         }
12795                     }
12796                   else
12797                     {
12798                       bfd_set_error (bfd_error_bad_value);
12799                       goto error_ret_free_internal;
12800                     }
12801
12802                   destination = 0;
12803                   local_off = 0;
12804                   if (ok_dest)
12805                     {
12806                       sym_value += irela->r_addend;
12807                       destination = (sym_value
12808                                      + sym_sec->output_offset
12809                                      + sym_sec->output_section->vma);
12810                       local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12811                                                             ? hash->elf.other
12812                                                             : sym->st_other);
12813                     }
12814
12815                   code_sec = sym_sec;
12816                   code_value = sym_value;
12817                   opd = get_opd_info (sym_sec);
12818                   if (opd != NULL)
12819                     {
12820                       bfd_vma dest;
12821
12822                       if (hash == NULL && opd->adjust != NULL)
12823                         {
12824                           long adjust = opd->adjust[OPD_NDX (sym_value)];
12825                           if (adjust == -1)
12826                             continue;
12827                           code_value += adjust;
12828                           sym_value += adjust;
12829                         }
12830                       dest = opd_entry_value (sym_sec, sym_value,
12831                                               &code_sec, &code_value, FALSE);
12832                       if (dest != (bfd_vma) -1)
12833                         {
12834                           destination = dest;
12835                           if (fdh != NULL)
12836                             {
12837                               /* Fixup old ABI sym to point at code
12838                                  entry.  */
12839                               hash->elf.root.type = bfd_link_hash_defweak;
12840                               hash->elf.root.u.def.section = code_sec;
12841                               hash->elf.root.u.def.value = code_value;
12842                             }
12843                         }
12844                     }
12845
12846                   /* Determine what (if any) linker stub is needed.  */
12847                   plt_ent = NULL;
12848                   stub_type = ppc_type_of_stub (section, irela, &hash,
12849                                                 &plt_ent, destination,
12850                                                 local_off);
12851
12852                   if (stub_type != ppc_stub_plt_call)
12853                     {
12854                       /* Check whether we need a TOC adjusting stub.
12855                          Since the linker pastes together pieces from
12856                          different object files when creating the
12857                          _init and _fini functions, it may be that a
12858                          call to what looks like a local sym is in
12859                          fact a call needing a TOC adjustment.  */
12860                       if (code_sec != NULL
12861                           && code_sec->output_section != NULL
12862                           && (htab->sec_info[code_sec->id].toc_off
12863                               != htab->sec_info[section->id].toc_off)
12864                           && (code_sec->has_toc_reloc
12865                               || code_sec->makes_toc_func_call))
12866                         stub_type = ppc_stub_long_branch_r2off;
12867                     }
12868
12869                   if (stub_type == ppc_stub_none)
12870                     continue;
12871
12872                   /* __tls_get_addr calls might be eliminated.  */
12873                   if (stub_type != ppc_stub_plt_call
12874                       && hash != NULL
12875                       && (hash == htab->tls_get_addr
12876                           || hash == htab->tls_get_addr_fd)
12877                       && section->has_tls_reloc
12878                       && irela != internal_relocs)
12879                     {
12880                       /* Get tls info.  */
12881                       unsigned char *tls_mask;
12882
12883                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12884                                          irela - 1, input_bfd))
12885                         goto error_ret_free_internal;
12886                       if ((*tls_mask & TLS_TLS) != 0)
12887                         continue;
12888                     }
12889
12890                   if (stub_type == ppc_stub_plt_call)
12891                     {
12892                       if (!htab->opd_abi
12893                           && htab->params->plt_localentry0 != 0
12894                           && is_elfv2_localentry0 (&hash->elf))
12895                         htab->has_plt_localentry0 = 1;
12896                       else if (irela + 1 < irelaend
12897                                && irela[1].r_offset == irela->r_offset + 4
12898                                && (ELF64_R_TYPE (irela[1].r_info)
12899                                    == R_PPC64_TOCSAVE))
12900                         {
12901                           if (!tocsave_find (htab, INSERT,
12902                                              &local_syms, irela + 1, input_bfd))
12903                             goto error_ret_free_internal;
12904                         }
12905                       else
12906                         stub_type = ppc_stub_plt_call_r2save;
12907                     }
12908
12909                   /* Support for grouping stub sections.  */
12910                   id_sec = htab->sec_info[section->id].u.group->link_sec;
12911
12912                   /* Get the name of this stub.  */
12913                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12914                   if (!stub_name)
12915                     goto error_ret_free_internal;
12916
12917                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12918                                                      stub_name, FALSE, FALSE);
12919                   if (stub_entry != NULL)
12920                     {
12921                       /* The proper stub has already been created.  */
12922                       free (stub_name);
12923                       if (stub_type == ppc_stub_plt_call_r2save)
12924                         stub_entry->stub_type = stub_type;
12925                       continue;
12926                     }
12927
12928                   stub_entry = ppc_add_stub (stub_name, section, info);
12929                   if (stub_entry == NULL)
12930                     {
12931                       free (stub_name);
12932                     error_ret_free_internal:
12933                       if (elf_section_data (section)->relocs == NULL)
12934                         free (internal_relocs);
12935                     error_ret_free_local:
12936                       if (local_syms != NULL
12937                           && (symtab_hdr->contents
12938                               != (unsigned char *) local_syms))
12939                         free (local_syms);
12940                       return FALSE;
12941                     }
12942
12943                   stub_entry->stub_type = stub_type;
12944                   if (stub_type != ppc_stub_plt_call
12945                       && stub_type != ppc_stub_plt_call_r2save)
12946                     {
12947                       stub_entry->target_value = code_value;
12948                       stub_entry->target_section = code_sec;
12949                     }
12950                   else
12951                     {
12952                       stub_entry->target_value = sym_value;
12953                       stub_entry->target_section = sym_sec;
12954                     }
12955                   stub_entry->h = hash;
12956                   stub_entry->plt_ent = plt_ent;
12957                   stub_entry->symtype
12958                     = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
12959                   stub_entry->other = hash ? hash->elf.other : sym->st_other;
12960
12961                   if (stub_entry->h != NULL)
12962                     htab->stub_globals += 1;
12963                 }
12964
12965               /* We're done with the internal relocs, free them.  */
12966               if (elf_section_data (section)->relocs != internal_relocs)
12967                 free (internal_relocs);
12968             }
12969
12970           if (local_syms != NULL
12971               && symtab_hdr->contents != (unsigned char *) local_syms)
12972             {
12973               if (!info->keep_memory)
12974                 free (local_syms);
12975               else
12976                 symtab_hdr->contents = (unsigned char *) local_syms;
12977             }
12978         }
12979
12980       /* We may have added some stubs.  Find out the new size of the
12981          stub sections.  */
12982       for (group = htab->group; group != NULL; group = group->next)
12983         if (group->stub_sec != NULL)
12984           {
12985             asection *stub_sec = group->stub_sec;
12986
12987             if (htab->stub_iteration <= STUB_SHRINK_ITER
12988                 || stub_sec->rawsize < stub_sec->size)
12989               /* Past STUB_SHRINK_ITER, rawsize is the max size seen.  */
12990               stub_sec->rawsize = stub_sec->size;
12991             stub_sec->size = 0;
12992             stub_sec->reloc_count = 0;
12993             stub_sec->flags &= ~SEC_RELOC;
12994           }
12995
12996       if (htab->stub_iteration <= STUB_SHRINK_ITER
12997           || htab->brlt->rawsize < htab->brlt->size)
12998         htab->brlt->rawsize = htab->brlt->size;
12999       htab->brlt->size = 0;
13000       htab->brlt->reloc_count = 0;
13001       htab->brlt->flags &= ~SEC_RELOC;
13002       if (htab->relbrlt != NULL)
13003         htab->relbrlt->size = 0;
13004
13005       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
13006
13007       for (group = htab->group; group != NULL; group = group->next)
13008         if (group->needs_save_res)
13009           group->stub_sec->size += htab->sfpr->size;
13010
13011       if (info->emitrelocations
13012           && htab->glink != NULL && htab->glink->size != 0)
13013         {
13014           htab->glink->reloc_count = 1;
13015           htab->glink->flags |= SEC_RELOC;
13016         }
13017
13018       if (htab->glink_eh_frame != NULL
13019           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
13020           && htab->glink_eh_frame->output_section->size > 8)
13021         {
13022           size_t size = 0, align = 4;
13023
13024           for (group = htab->group; group != NULL; group = group->next)
13025             if (group->stub_sec != NULL)
13026               size += stub_eh_frame_size (group, align);
13027           if (htab->glink != NULL && htab->glink->size != 0)
13028             size += (24 + align - 1) & -align;
13029           if (size != 0)
13030             size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13031           align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13032           size = (size + align - 1) & -align;
13033           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13034           htab->glink_eh_frame->size = size;
13035         }
13036
13037       if (htab->params->plt_stub_align != 0)
13038         for (group = htab->group; group != NULL; group = group->next)
13039           if (group->stub_sec != NULL)
13040             {
13041               int align = abs (htab->params->plt_stub_align);
13042               group->stub_sec->size
13043                 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13044             }
13045
13046       for (group = htab->group; group != NULL; group = group->next)
13047         if (group->stub_sec != NULL
13048             && group->stub_sec->rawsize != group->stub_sec->size
13049             && (htab->stub_iteration <= STUB_SHRINK_ITER
13050                 || group->stub_sec->rawsize < group->stub_sec->size))
13051           break;
13052
13053       if (group == NULL
13054           && (htab->brlt->rawsize == htab->brlt->size
13055               || (htab->stub_iteration > STUB_SHRINK_ITER
13056                   && htab->brlt->rawsize > htab->brlt->size))
13057           && (htab->glink_eh_frame == NULL
13058               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
13059         break;
13060
13061       /* Ask the linker to do its stuff.  */
13062       (*htab->params->layout_sections_again) ();
13063     }
13064
13065   if (htab->glink_eh_frame != NULL
13066       && htab->glink_eh_frame->size != 0)
13067     {
13068       bfd_vma val;
13069       bfd_byte *p, *last_fde;
13070       size_t last_fde_len, size, align, pad;
13071       struct map_stub *group;
13072
13073       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13074       if (p == NULL)
13075         return FALSE;
13076       htab->glink_eh_frame->contents = p;
13077       last_fde = p;
13078       align = 4;
13079
13080       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13081       /* CIE length (rewrite in case little-endian).  */
13082       last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
13083       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13084       p += last_fde_len + 4;
13085
13086       for (group = htab->group; group != NULL; group = group->next)
13087         if (group->stub_sec != NULL)
13088           {
13089             last_fde = p;
13090             last_fde_len = stub_eh_frame_size (group, align) - 4;
13091             /* FDE length.  */
13092             bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13093             p += 4;
13094             /* CIE pointer.  */
13095             val = p - htab->glink_eh_frame->contents;
13096             bfd_put_32 (htab->elf.dynobj, val, p);
13097             p += 4;
13098             /* Offset to stub section, written later.  */
13099             p += 4;
13100             /* stub section size.  */
13101             bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
13102             p += 4;
13103             /* Augmentation.  */
13104             p += 1;
13105             if (group->tls_get_addr_opt_bctrl != -1u)
13106               {
13107                 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
13108
13109                 /* This FDE needs more than just the default.
13110                    Describe __tls_get_addr_opt stub LR.  */
13111                 if (to_bctrl < 64)
13112                   *p++ = DW_CFA_advance_loc + to_bctrl;
13113                 else if (to_bctrl < 256)
13114                   {
13115                     *p++ = DW_CFA_advance_loc1;
13116                     *p++ = to_bctrl;
13117                   }
13118                 else if (to_bctrl < 65536)
13119                   {
13120                     *p++ = DW_CFA_advance_loc2;
13121                     bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
13122                     p += 2;
13123                   }
13124                 else
13125                   {
13126                     *p++ = DW_CFA_advance_loc4;
13127                     bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
13128                     p += 4;
13129                   }
13130                 *p++ = DW_CFA_offset_extended_sf;
13131                 *p++ = 65;
13132                 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
13133                 *p++ = DW_CFA_advance_loc + 4;
13134                 *p++ = DW_CFA_restore_extended;
13135                 *p++ = 65;
13136               }
13137             /* Pad.  */
13138             p = last_fde + last_fde_len + 4;
13139           }
13140       if (htab->glink != NULL && htab->glink->size != 0)
13141         {
13142           last_fde = p;
13143           last_fde_len = ((24 + align - 1) & -align) - 4;
13144           /* FDE length.  */
13145           bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13146           p += 4;
13147           /* CIE pointer.  */
13148           val = p - htab->glink_eh_frame->contents;
13149           bfd_put_32 (htab->elf.dynobj, val, p);
13150           p += 4;
13151           /* Offset to .glink, written later.  */
13152           p += 4;
13153           /* .glink size.  */
13154           bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13155           p += 4;
13156           /* Augmentation.  */
13157           p += 1;
13158
13159           *p++ = DW_CFA_advance_loc + 1;
13160           *p++ = DW_CFA_register;
13161           *p++ = 65;
13162           *p++ = htab->opd_abi ? 12 : 0;
13163           *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
13164           *p++ = DW_CFA_restore_extended;
13165           *p++ = 65;
13166           p += ((24 + align - 1) & -align) - 24;
13167         }
13168       /* Subsume any padding into the last FDE if user .eh_frame
13169          sections are aligned more than glink_eh_frame.  Otherwise any
13170          zero padding will be seen as a terminator.  */
13171       align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13172       size = p - htab->glink_eh_frame->contents;
13173       pad = ((size + align - 1) & -align) - size;
13174       htab->glink_eh_frame->size = size + pad;
13175       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13176     }
13177
13178   maybe_strip_output (info, htab->brlt);
13179   if (htab->glink_eh_frame != NULL)
13180     maybe_strip_output (info, htab->glink_eh_frame);
13181
13182   return TRUE;
13183 }
13184
13185 /* Called after we have determined section placement.  If sections
13186    move, we'll be called again.  Provide a value for TOCstart.  */
13187
13188 bfd_vma
13189 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13190 {
13191   asection *s;
13192   bfd_vma TOCstart, adjust;
13193
13194   if (info != NULL)
13195     {
13196       struct elf_link_hash_entry *h;
13197       struct elf_link_hash_table *htab = elf_hash_table (info);
13198
13199       if (is_elf_hash_table (htab)
13200           && htab->hgot != NULL)
13201         h = htab->hgot;
13202       else
13203         {
13204           h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13205           if (is_elf_hash_table (htab))
13206             htab->hgot = h;
13207         }
13208       if (h != NULL
13209           && h->root.type == bfd_link_hash_defined
13210           && !h->root.linker_def
13211           && (!is_elf_hash_table (htab)
13212               || h->def_regular))
13213         {
13214           TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13215                       + h->root.u.def.section->output_offset
13216                       + h->root.u.def.section->output_section->vma);
13217           _bfd_set_gp_value (obfd, TOCstart);
13218           return TOCstart;
13219         }
13220     }
13221
13222   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13223      order.  The TOC starts where the first of these sections starts.  */
13224   s = bfd_get_section_by_name (obfd, ".got");
13225   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13226     s = bfd_get_section_by_name (obfd, ".toc");
13227   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13228     s = bfd_get_section_by_name (obfd, ".tocbss");
13229   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13230     s = bfd_get_section_by_name (obfd, ".plt");
13231   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13232     {
13233       /* This may happen for
13234          o  references to TOC base (SYM@toc / TOC[tc0]) without a
13235          .toc directive
13236          o  bad linker script
13237          o --gc-sections and empty TOC sections
13238
13239          FIXME: Warn user?  */
13240
13241       /* Look for a likely section.  We probably won't even be
13242          using TOCstart.  */
13243       for (s = obfd->sections; s != NULL; s = s->next)
13244         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13245                          | SEC_EXCLUDE))
13246             == (SEC_ALLOC | SEC_SMALL_DATA))
13247           break;
13248       if (s == NULL)
13249         for (s = obfd->sections; s != NULL; s = s->next)
13250           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13251               == (SEC_ALLOC | SEC_SMALL_DATA))
13252             break;
13253       if (s == NULL)
13254         for (s = obfd->sections; s != NULL; s = s->next)
13255           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13256               == SEC_ALLOC)
13257             break;
13258       if (s == NULL)
13259         for (s = obfd->sections; s != NULL; s = s->next)
13260           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13261             break;
13262     }
13263
13264   TOCstart = 0;
13265   if (s != NULL)
13266     TOCstart = s->output_section->vma + s->output_offset;
13267
13268   /* Force alignment.  */
13269   adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13270   TOCstart -= adjust;
13271   _bfd_set_gp_value (obfd, TOCstart);
13272
13273   if (info != NULL && s != NULL)
13274     {
13275       struct ppc_link_hash_table *htab = ppc_hash_table (info);
13276
13277       if (htab != NULL)
13278         {
13279           if (htab->elf.hgot != NULL)
13280             {
13281               htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13282               htab->elf.hgot->root.u.def.section = s;
13283             }
13284         }
13285       else
13286         {
13287           struct bfd_link_hash_entry *bh = NULL;
13288           _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13289                                             s, TOC_BASE_OFF - adjust,
13290                                             NULL, FALSE, FALSE, &bh);
13291         }
13292     }
13293   return TOCstart;
13294 }
13295
13296 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13297    write out any global entry stubs, and PLT relocations.  */
13298
13299 static bfd_boolean
13300 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13301 {
13302   struct bfd_link_info *info;
13303   struct ppc_link_hash_table *htab;
13304   struct plt_entry *ent;
13305   asection *s;
13306
13307   if (h->root.type == bfd_link_hash_indirect)
13308     return TRUE;
13309
13310   info = inf;
13311   htab = ppc_hash_table (info);
13312   if (htab == NULL)
13313     return FALSE;
13314
13315   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13316     if (ent->plt.offset != (bfd_vma) -1)
13317       {
13318         /* This symbol has an entry in the procedure linkage
13319            table.  Set it up.  */
13320         Elf_Internal_Rela rela;
13321         asection *plt, *relplt;
13322         bfd_byte *loc;
13323
13324         if (!htab->elf.dynamic_sections_created
13325             || h->dynindx == -1)
13326           {
13327             if (!(h->def_regular
13328                   && (h->root.type == bfd_link_hash_defined
13329                       || h->root.type == bfd_link_hash_defweak)))
13330               continue;
13331             if (h->type == STT_GNU_IFUNC)
13332               {
13333                 plt = htab->elf.iplt;
13334                 relplt = htab->elf.irelplt;
13335                 htab->local_ifunc_resolver = 1;
13336                 if (htab->opd_abi)
13337                   rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13338                 else
13339                   rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13340               }
13341             else
13342               {
13343                 plt = htab->pltlocal;
13344                 if (bfd_link_pic (info))
13345                   {
13346                     relplt = htab->relpltlocal;
13347                     if (htab->opd_abi)
13348                       rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13349                     else
13350                       rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13351                   }
13352                 else
13353                   relplt = NULL;
13354               }
13355             rela.r_addend = (h->root.u.def.value
13356                              + h->root.u.def.section->output_offset
13357                              + h->root.u.def.section->output_section->vma
13358                              + ent->addend);
13359
13360             if (relplt == NULL)
13361               {
13362                 loc = plt->contents + ent->plt.offset;
13363                 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13364                 if (htab->opd_abi)
13365                   {
13366                     bfd_vma toc = elf_gp (info->output_bfd);
13367                     toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13368                     bfd_put_64 (info->output_bfd, toc, loc + 8);
13369                   }
13370               }
13371             else
13372               {
13373                 rela.r_offset = (plt->output_section->vma
13374                                  + plt->output_offset
13375                                  + ent->plt.offset);
13376                 loc = relplt->contents + (relplt->reloc_count++
13377                                           * sizeof (Elf64_External_Rela));
13378                 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13379               }
13380           }
13381         else
13382           {
13383             rela.r_offset = (htab->elf.splt->output_section->vma
13384                              + htab->elf.splt->output_offset
13385                              + ent->plt.offset);
13386             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13387             rela.r_addend = ent->addend;
13388             loc = (htab->elf.srelplt->contents
13389                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13390                       / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13391             if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13392               htab->maybe_local_ifunc_resolver = 1;
13393             bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13394           }
13395       }
13396
13397   if (!h->pointer_equality_needed)
13398     return TRUE;
13399
13400   if (h->def_regular)
13401     return TRUE;
13402
13403   s = htab->global_entry;
13404   if (s == NULL || s->size == 0)
13405     return TRUE;
13406
13407   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13408     if (ent->plt.offset != (bfd_vma) -1
13409         && ent->addend == 0)
13410       {
13411         bfd_byte *p;
13412         asection *plt;
13413         bfd_vma off;
13414
13415         p = s->contents + h->root.u.def.value;
13416         plt = htab->elf.splt;
13417         if (!htab->elf.dynamic_sections_created
13418             || h->dynindx == -1)
13419           {
13420             if (h->type == STT_GNU_IFUNC)
13421               plt = htab->elf.iplt;
13422             else
13423               plt = htab->pltlocal;
13424           }
13425         off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13426         off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13427
13428         if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13429           {
13430             info->callbacks->einfo
13431               (_("%P: linkage table error against `%pT'\n"),
13432                h->root.root.string);
13433             bfd_set_error (bfd_error_bad_value);
13434             htab->stub_error = TRUE;
13435           }
13436
13437         htab->stub_count[ppc_stub_global_entry - 1] += 1;
13438         if (htab->params->emit_stub_syms)
13439           {
13440             size_t len = strlen (h->root.root.string);
13441             char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13442
13443             if (name == NULL)
13444               return FALSE;
13445
13446             sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13447             h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13448             if (h == NULL)
13449               return FALSE;
13450             if (h->root.type == bfd_link_hash_new)
13451               {
13452                 h->root.type = bfd_link_hash_defined;
13453                 h->root.u.def.section = s;
13454                 h->root.u.def.value = p - s->contents;
13455                 h->ref_regular = 1;
13456                 h->def_regular = 1;
13457                 h->ref_regular_nonweak = 1;
13458                 h->forced_local = 1;
13459                 h->non_elf = 0;
13460                 h->root.linker_def = 1;
13461               }
13462           }
13463
13464         if (PPC_HA (off) != 0)
13465           {
13466             bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13467             p += 4;
13468           }
13469         bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13470         p += 4;
13471         bfd_put_32 (s->owner, MTCTR_R12, p);
13472         p += 4;
13473         bfd_put_32 (s->owner, BCTR, p);
13474         break;
13475       }
13476   return TRUE;
13477 }
13478
13479 /* Write PLT relocs for locals.  */
13480
13481 static bfd_boolean
13482 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13483 {
13484   struct ppc_link_hash_table *htab = ppc_hash_table (info);
13485   bfd *ibfd;
13486
13487   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13488     {
13489       struct got_entry **lgot_ents, **end_lgot_ents;
13490       struct plt_entry **local_plt, **lplt, **end_local_plt;
13491       Elf_Internal_Shdr *symtab_hdr;
13492       bfd_size_type locsymcount;
13493       Elf_Internal_Sym *local_syms = NULL;
13494       struct plt_entry *ent;
13495
13496       if (!is_ppc64_elf (ibfd))
13497         continue;
13498
13499       lgot_ents = elf_local_got_ents (ibfd);
13500       if (!lgot_ents)
13501         continue;
13502
13503       symtab_hdr = &elf_symtab_hdr (ibfd);
13504       locsymcount = symtab_hdr->sh_info;
13505       end_lgot_ents = lgot_ents + locsymcount;
13506       local_plt = (struct plt_entry **) end_lgot_ents;
13507       end_local_plt = local_plt + locsymcount;
13508       for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13509         for (ent = *lplt; ent != NULL; ent = ent->next)
13510           if (ent->plt.offset != (bfd_vma) -1)
13511             {
13512               Elf_Internal_Sym *sym;
13513               asection *sym_sec;
13514               asection *plt, *relplt;
13515               bfd_byte *loc;
13516               bfd_vma val;
13517
13518               if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13519                               lplt - local_plt, ibfd))
13520                 {
13521                   if (local_syms != NULL
13522                       && symtab_hdr->contents != (unsigned char *) local_syms)
13523                     free (local_syms);
13524                   return FALSE;
13525                 }
13526
13527               val = sym->st_value + ent->addend;
13528               val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13529               if (sym_sec != NULL && sym_sec->output_section != NULL)
13530                 val += sym_sec->output_offset + sym_sec->output_section->vma;
13531
13532               if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13533                 {
13534                   htab->local_ifunc_resolver = 1;
13535                   plt = htab->elf.iplt;
13536                   relplt = htab->elf.irelplt;
13537                 }
13538               else
13539                 {
13540                   plt = htab->pltlocal;
13541                   relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13542                 }
13543
13544               if (relplt == NULL)
13545                 {
13546                   loc = plt->contents + ent->plt.offset;
13547                   bfd_put_64 (info->output_bfd, val, loc);
13548                   if (htab->opd_abi)
13549                     {
13550                       bfd_vma toc = elf_gp (ibfd);
13551                       bfd_put_64 (info->output_bfd, toc, loc + 8);
13552                     }
13553                 }
13554               else
13555                 {
13556                   Elf_Internal_Rela rela;
13557                   rela.r_offset = (ent->plt.offset
13558                                    + plt->output_offset
13559                                    + plt->output_section->vma);
13560                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13561                     {
13562                       if (htab->opd_abi)
13563                         rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13564                       else
13565                         rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13566                     }
13567                   else
13568                     {
13569                       if (htab->opd_abi)
13570                         rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13571                       else
13572                         rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13573                     }
13574                   rela.r_addend = val;
13575                   loc = relplt->contents + (relplt->reloc_count++
13576                                             * sizeof (Elf64_External_Rela));
13577                   bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13578                 }
13579             }
13580
13581       if (local_syms != NULL
13582           && symtab_hdr->contents != (unsigned char *) local_syms)
13583         {
13584           if (!info->keep_memory)
13585             free (local_syms);
13586           else
13587             symtab_hdr->contents = (unsigned char *) local_syms;
13588         }
13589     }
13590   return TRUE;
13591 }
13592
13593 /* Build all the stubs associated with the current output file.
13594    The stubs are kept in a hash table attached to the main linker
13595    hash table.  This function is called via gldelf64ppc_finish.  */
13596
13597 bfd_boolean
13598 ppc64_elf_build_stubs (struct bfd_link_info *info,
13599                        char **stats)
13600 {
13601   struct ppc_link_hash_table *htab = ppc_hash_table (info);
13602   struct map_stub *group;
13603   asection *stub_sec;
13604   bfd_byte *p;
13605   int stub_sec_count = 0;
13606
13607   if (htab == NULL)
13608     return FALSE;
13609
13610   /* Allocate memory to hold the linker stubs.  */
13611   for (group = htab->group; group != NULL; group = group->next)
13612     if ((stub_sec = group->stub_sec) != NULL
13613         && stub_sec->size != 0)
13614       {
13615         stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
13616         if (stub_sec->contents == NULL)
13617           return FALSE;
13618         stub_sec->size = 0;
13619       }
13620
13621   if (htab->glink != NULL && htab->glink->size != 0)
13622     {
13623       unsigned int indx;
13624       bfd_vma plt0;
13625
13626       /* Build the .glink plt call stub.  */
13627       if (htab->params->emit_stub_syms)
13628         {
13629           struct elf_link_hash_entry *h;
13630           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13631                                     TRUE, FALSE, FALSE);
13632           if (h == NULL)
13633             return FALSE;
13634           if (h->root.type == bfd_link_hash_new)
13635             {
13636               h->root.type = bfd_link_hash_defined;
13637               h->root.u.def.section = htab->glink;
13638               h->root.u.def.value = 8;
13639               h->ref_regular = 1;
13640               h->def_regular = 1;
13641               h->ref_regular_nonweak = 1;
13642               h->forced_local = 1;
13643               h->non_elf = 0;
13644               h->root.linker_def = 1;
13645             }
13646         }
13647       plt0 = (htab->elf.splt->output_section->vma
13648               + htab->elf.splt->output_offset
13649               - 16);
13650       if (info->emitrelocations)
13651         {
13652           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13653           if (r == NULL)
13654             return FALSE;
13655           r->r_offset = (htab->glink->output_offset
13656                          + htab->glink->output_section->vma);
13657           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13658           r->r_addend = plt0;
13659         }
13660       p = htab->glink->contents;
13661       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13662       bfd_put_64 (htab->glink->owner, plt0, p);
13663       p += 8;
13664       if (htab->opd_abi)
13665         {
13666           bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13667           p += 4;
13668           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13669           p += 4;
13670           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13671           p += 4;
13672           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13673           p += 4;
13674           bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13675           p += 4;
13676           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13677           p += 4;
13678           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13679           p += 4;
13680           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13681           p += 4;
13682           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13683           p += 4;
13684           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13685           p += 4;
13686         }
13687       else
13688         {
13689           bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13690           p += 4;
13691           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13692           p += 4;
13693           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13694           p += 4;
13695           bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13696           p += 4;
13697           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13698           p += 4;
13699           bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13700           p += 4;
13701           bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13702           p += 4;
13703           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13704           p += 4;
13705           bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13706           p += 4;
13707           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13708           p += 4;
13709           bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13710           p += 4;
13711           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13712           p += 4;
13713           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13714           p += 4;
13715         }
13716       bfd_put_32 (htab->glink->owner, BCTR, p);
13717       p += 4;
13718       BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13719
13720       /* Build the .glink lazy link call stubs.  */
13721       indx = 0;
13722       while (p < htab->glink->contents + htab->glink->size)
13723         {
13724           if (htab->opd_abi)
13725             {
13726               if (indx < 0x8000)
13727                 {
13728                   bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13729                   p += 4;
13730                 }
13731               else
13732                 {
13733                   bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13734                   p += 4;
13735                   bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13736                               p);
13737                   p += 4;
13738                 }
13739             }
13740           bfd_put_32 (htab->glink->owner,
13741                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13742           indx++;
13743           p += 4;
13744         }
13745     }
13746
13747   /* Build .glink global entry stubs, and PLT relocs for globals.  */
13748   elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13749
13750   if (!write_plt_relocs_for_local_syms (info))
13751     return FALSE;
13752
13753   if (htab->brlt != NULL && htab->brlt->size != 0)
13754     {
13755       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13756                                          htab->brlt->size);
13757       if (htab->brlt->contents == NULL)
13758         return FALSE;
13759     }
13760   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13761     {
13762       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13763                                             htab->relbrlt->size);
13764       if (htab->relbrlt->contents == NULL)
13765         return FALSE;
13766     }
13767
13768   /* Build the stubs as directed by the stub hash table.  */
13769   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13770
13771   for (group = htab->group; group != NULL; group = group->next)
13772     if (group->needs_save_res)
13773       group->stub_sec->size += htab->sfpr->size;
13774
13775   if (htab->relbrlt != NULL)
13776     htab->relbrlt->reloc_count = 0;
13777
13778   if (htab->params->plt_stub_align != 0)
13779     for (group = htab->group; group != NULL; group = group->next)
13780       if ((stub_sec = group->stub_sec) != NULL)
13781         {
13782           int align = abs (htab->params->plt_stub_align);
13783           stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13784         }
13785
13786   for (group = htab->group; group != NULL; group = group->next)
13787     if (group->needs_save_res)
13788       {
13789         stub_sec = group->stub_sec;
13790         memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13791                 htab->sfpr->contents, htab->sfpr->size);
13792         if (htab->params->emit_stub_syms)
13793           {
13794             unsigned int i;
13795
13796             for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13797               if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13798                 return FALSE;
13799           }
13800       }
13801
13802   for (group = htab->group; group != NULL; group = group->next)
13803     if ((stub_sec = group->stub_sec) != NULL)
13804       {
13805         stub_sec_count += 1;
13806         if (stub_sec->rawsize != stub_sec->size
13807             && (htab->stub_iteration <= STUB_SHRINK_ITER
13808                 || stub_sec->rawsize < stub_sec->size))
13809           break;
13810       }
13811
13812   if (group != NULL)
13813     {
13814       htab->stub_error = TRUE;
13815       _bfd_error_handler (_("stubs don't match calculated size"));
13816     }
13817
13818   if (htab->stub_error)
13819     return FALSE;
13820
13821   if (stats != NULL)
13822     {
13823       size_t len;
13824       *stats = bfd_malloc (500);
13825       if (*stats == NULL)
13826         return FALSE;
13827
13828       len = sprintf (*stats,
13829                      ngettext ("linker stubs in %u group\n",
13830                                "linker stubs in %u groups\n",
13831                                stub_sec_count),
13832                      stub_sec_count);
13833       sprintf (*stats + len, _("  branch       %lu\n"
13834                                "  toc adjust   %lu\n"
13835                                "  long branch  %lu\n"
13836                                "  long toc adj %lu\n"
13837                                "  plt call     %lu\n"
13838                                "  plt call toc %lu\n"
13839                                "  global entry %lu"),
13840                htab->stub_count[ppc_stub_long_branch - 1],
13841                htab->stub_count[ppc_stub_long_branch_r2off - 1],
13842                htab->stub_count[ppc_stub_plt_branch - 1],
13843                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13844                htab->stub_count[ppc_stub_plt_call - 1],
13845                htab->stub_count[ppc_stub_plt_call_r2save - 1],
13846                htab->stub_count[ppc_stub_global_entry - 1]);
13847     }
13848   return TRUE;
13849 }
13850
13851 /* What to do when ld finds relocations against symbols defined in
13852    discarded sections.  */
13853
13854 static unsigned int
13855 ppc64_elf_action_discarded (asection *sec)
13856 {
13857   if (strcmp (".opd", sec->name) == 0)
13858     return 0;
13859
13860   if (strcmp (".toc", sec->name) == 0)
13861     return 0;
13862
13863   if (strcmp (".toc1", sec->name) == 0)
13864     return 0;
13865
13866   return _bfd_elf_default_action_discarded (sec);
13867 }
13868
13869 /* The RELOCATE_SECTION function is called by the ELF backend linker
13870    to handle the relocations for a section.
13871
13872    The relocs are always passed as Rela structures; if the section
13873    actually uses Rel structures, the r_addend field will always be
13874    zero.
13875
13876    This function is responsible for adjust the section contents as
13877    necessary, and (if using Rela relocs and generating a
13878    relocatable output file) adjusting the reloc addend as
13879    necessary.
13880
13881    This function does not have to worry about setting the reloc
13882    address or the reloc symbol index.
13883
13884    LOCAL_SYMS is a pointer to the swapped in local symbols.
13885
13886    LOCAL_SECTIONS is an array giving the section in the input file
13887    corresponding to the st_shndx field of each local symbol.
13888
13889    The global hash table entry for the global symbols can be found
13890    via elf_sym_hashes (input_bfd).
13891
13892    When generating relocatable output, this function must handle
13893    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
13894    going to be the section symbol corresponding to the output
13895    section, which means that the addend must be adjusted
13896    accordingly.  */
13897
13898 static bfd_boolean
13899 ppc64_elf_relocate_section (bfd *output_bfd,
13900                             struct bfd_link_info *info,
13901                             bfd *input_bfd,
13902                             asection *input_section,
13903                             bfd_byte *contents,
13904                             Elf_Internal_Rela *relocs,
13905                             Elf_Internal_Sym *local_syms,
13906                             asection **local_sections)
13907 {
13908   struct ppc_link_hash_table *htab;
13909   Elf_Internal_Shdr *symtab_hdr;
13910   struct elf_link_hash_entry **sym_hashes;
13911   Elf_Internal_Rela *rel;
13912   Elf_Internal_Rela *wrel;
13913   Elf_Internal_Rela *relend;
13914   Elf_Internal_Rela outrel;
13915   bfd_byte *loc;
13916   struct got_entry **local_got_ents;
13917   bfd_vma TOCstart;
13918   bfd_boolean ret = TRUE;
13919   bfd_boolean is_opd;
13920   /* Assume 'at' branch hints.  */
13921   bfd_boolean is_isa_v2 = TRUE;
13922   bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13923
13924   /* Initialize howto table if needed.  */
13925   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13926     ppc_howto_init ();
13927
13928   htab = ppc_hash_table (info);
13929   if (htab == NULL)
13930     return FALSE;
13931
13932   /* Don't relocate stub sections.  */
13933   if (input_section->owner == htab->params->stub_bfd)
13934     return TRUE;
13935
13936   BFD_ASSERT (is_ppc64_elf (input_bfd));
13937
13938   local_got_ents = elf_local_got_ents (input_bfd);
13939   TOCstart = elf_gp (output_bfd);
13940   symtab_hdr = &elf_symtab_hdr (input_bfd);
13941   sym_hashes = elf_sym_hashes (input_bfd);
13942   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13943
13944   rel = wrel = relocs;
13945   relend = relocs + input_section->reloc_count;
13946   for (; rel < relend; wrel++, rel++)
13947     {
13948       enum elf_ppc64_reloc_type r_type;
13949       bfd_vma addend;
13950       bfd_reloc_status_type r;
13951       Elf_Internal_Sym *sym;
13952       asection *sec;
13953       struct elf_link_hash_entry *h_elf;
13954       struct ppc_link_hash_entry *h;
13955       struct ppc_link_hash_entry *fdh;
13956       const char *sym_name;
13957       unsigned long r_symndx, toc_symndx;
13958       bfd_vma toc_addend;
13959       unsigned char tls_mask, tls_gd, tls_type;
13960       unsigned char sym_type;
13961       bfd_vma relocation;
13962       bfd_boolean unresolved_reloc, save_unresolved_reloc;
13963       bfd_boolean warned;
13964       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13965       unsigned int insn;
13966       unsigned int mask;
13967       struct ppc_stub_hash_entry *stub_entry;
13968       bfd_vma max_br_offset;
13969       bfd_vma from;
13970       Elf_Internal_Rela orig_rel;
13971       reloc_howto_type *howto;
13972       struct reloc_howto_struct alt_howto;
13973
13974     again:
13975       orig_rel = *rel;
13976
13977       r_type = ELF64_R_TYPE (rel->r_info);
13978       r_symndx = ELF64_R_SYM (rel->r_info);
13979
13980       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13981          symbol of the previous ADDR64 reloc.  The symbol gives us the
13982          proper TOC base to use.  */
13983       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13984           && wrel != relocs
13985           && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13986           && is_opd)
13987         r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13988
13989       sym = NULL;
13990       sec = NULL;
13991       h_elf = NULL;
13992       sym_name = NULL;
13993       unresolved_reloc = FALSE;
13994       warned = FALSE;
13995
13996       if (r_symndx < symtab_hdr->sh_info)
13997         {
13998           /* It's a local symbol.  */
13999           struct _opd_sec_data *opd;
14000
14001           sym = local_syms + r_symndx;
14002           sec = local_sections[r_symndx];
14003           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
14004           sym_type = ELF64_ST_TYPE (sym->st_info);
14005           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
14006           opd = get_opd_info (sec);
14007           if (opd != NULL && opd->adjust != NULL)
14008             {
14009               long adjust = opd->adjust[OPD_NDX (sym->st_value
14010                                                  + rel->r_addend)];
14011               if (adjust == -1)
14012                 relocation = 0;
14013               else
14014                 {
14015                   /* If this is a relocation against the opd section sym
14016                      and we have edited .opd, adjust the reloc addend so
14017                      that ld -r and ld --emit-relocs output is correct.
14018                      If it is a reloc against some other .opd symbol,
14019                      then the symbol value will be adjusted later.  */
14020                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14021                     rel->r_addend += adjust;
14022                   else
14023                     relocation += adjust;
14024                 }
14025             }
14026         }
14027       else
14028         {
14029           bfd_boolean ignored;
14030
14031           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14032                                    r_symndx, symtab_hdr, sym_hashes,
14033                                    h_elf, sec, relocation,
14034                                    unresolved_reloc, warned, ignored);
14035           sym_name = h_elf->root.root.string;
14036           sym_type = h_elf->type;
14037           if (sec != NULL
14038               && sec->owner == output_bfd
14039               && strcmp (sec->name, ".opd") == 0)
14040             {
14041               /* This is a symbol defined in a linker script.  All
14042                  such are defined in output sections, even those
14043                  defined by simple assignment from a symbol defined in
14044                  an input section.  Transfer the symbol to an
14045                  appropriate input .opd section, so that a branch to
14046                  this symbol will be mapped to the location specified
14047                  by the opd entry.  */
14048               struct bfd_link_order *lo;
14049               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14050                 if (lo->type == bfd_indirect_link_order)
14051                   {
14052                     asection *isec = lo->u.indirect.section;
14053                     if (h_elf->root.u.def.value >= isec->output_offset
14054                         && h_elf->root.u.def.value < (isec->output_offset
14055                                                       + isec->size))
14056                       {
14057                         h_elf->root.u.def.value -= isec->output_offset;
14058                         h_elf->root.u.def.section = isec;
14059                         sec = isec;
14060                         break;
14061                       }
14062                   }
14063             }
14064         }
14065       h = (struct ppc_link_hash_entry *) h_elf;
14066
14067       if (sec != NULL && discarded_section (sec))
14068         {
14069           _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14070                                input_bfd, input_section,
14071                                contents + rel->r_offset);
14072           wrel->r_offset = rel->r_offset;
14073           wrel->r_info = 0;
14074           wrel->r_addend = 0;
14075
14076           /* For ld -r, remove relocations in debug sections against
14077              symbols defined in discarded sections.  Not done for
14078              non-debug to preserve relocs in .eh_frame which the
14079              eh_frame editing code expects to be present.  */
14080           if (bfd_link_relocatable (info)
14081               && (input_section->flags & SEC_DEBUGGING))
14082             wrel--;
14083
14084           continue;
14085         }
14086
14087       if (bfd_link_relocatable (info))
14088         goto copy_reloc;
14089
14090       if (h != NULL && &h->elf == htab->elf.hgot)
14091         {
14092           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14093           sec = bfd_abs_section_ptr;
14094           unresolved_reloc = FALSE;
14095         }
14096
14097       /* TLS optimizations.  Replace instruction sequences and relocs
14098          based on information we collected in tls_optimize.  We edit
14099          RELOCS so that --emit-relocs will output something sensible
14100          for the final instruction stream.  */
14101       tls_mask = 0;
14102       tls_gd = 0;
14103       toc_symndx = 0;
14104       if (h != NULL)
14105         tls_mask = h->tls_mask;
14106       else if (local_got_ents != NULL)
14107         {
14108           struct plt_entry **local_plt = (struct plt_entry **)
14109             (local_got_ents + symtab_hdr->sh_info);
14110           unsigned char *lgot_masks = (unsigned char *)
14111             (local_plt + symtab_hdr->sh_info);
14112           tls_mask = lgot_masks[r_symndx];
14113         }
14114       if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
14115           && (r_type == R_PPC64_TLS
14116               || r_type == R_PPC64_TLSGD
14117               || r_type == R_PPC64_TLSLD))
14118         {
14119           /* Check for toc tls entries.  */
14120           unsigned char *toc_tls;
14121
14122           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14123                              &local_syms, rel, input_bfd))
14124             return FALSE;
14125
14126           if (toc_tls)
14127             tls_mask = *toc_tls;
14128         }
14129
14130       /* Check that tls relocs are used with tls syms, and non-tls
14131          relocs are used with non-tls syms.  */
14132       if (r_symndx != STN_UNDEF
14133           && r_type != R_PPC64_NONE
14134           && (h == NULL
14135               || h->elf.root.type == bfd_link_hash_defined
14136               || h->elf.root.type == bfd_link_hash_defweak)
14137           && (IS_PPC64_TLS_RELOC (r_type)
14138               != (sym_type == STT_TLS
14139                   || (sym_type == STT_SECTION
14140                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
14141         {
14142           if ((tls_mask & TLS_TLS) != 0
14143               && (r_type == R_PPC64_TLS
14144                   || r_type == R_PPC64_TLSGD
14145                   || r_type == R_PPC64_TLSLD))
14146             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
14147             ;
14148           else
14149             info->callbacks->einfo
14150               (!IS_PPC64_TLS_RELOC (r_type)
14151                /* xgettext:c-format */
14152                ? _("%H: %s used with TLS symbol `%pT'\n")
14153                /* xgettext:c-format */
14154                : _("%H: %s used with non-TLS symbol `%pT'\n"),
14155                input_bfd, input_section, rel->r_offset,
14156                ppc64_elf_howto_table[r_type]->name,
14157                sym_name);
14158         }
14159
14160       /* Ensure reloc mapping code below stays sane.  */
14161       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14162           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14163           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
14164           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14165           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14166           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14167           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
14168           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14169           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14170           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14171         abort ();
14172
14173       switch (r_type)
14174         {
14175         default:
14176           break;
14177
14178         case R_PPC64_LO_DS_OPT:
14179           insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
14180           if ((insn & (0x3f << 26)) != 58u << 26)
14181             abort ();
14182           insn += (14u << 26) - (58u << 26);
14183           bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
14184           r_type = R_PPC64_TOC16_LO;
14185           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14186           break;
14187
14188         case R_PPC64_TOC16:
14189         case R_PPC64_TOC16_LO:
14190         case R_PPC64_TOC16_DS:
14191         case R_PPC64_TOC16_LO_DS:
14192           {
14193             /* Check for toc tls entries.  */
14194             unsigned char *toc_tls;
14195             int retval;
14196
14197             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14198                                    &local_syms, rel, input_bfd);
14199             if (retval == 0)
14200               return FALSE;
14201
14202             if (toc_tls)
14203               {
14204                 tls_mask = *toc_tls;
14205                 if (r_type == R_PPC64_TOC16_DS
14206                     || r_type == R_PPC64_TOC16_LO_DS)
14207                   {
14208                     if ((tls_mask & TLS_TLS) != 0
14209                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14210                       goto toctprel;
14211                   }
14212                 else
14213                   {
14214                     /* If we found a GD reloc pair, then we might be
14215                        doing a GD->IE transition.  */
14216                     if (retval == 2)
14217                       {
14218                         tls_gd = TLS_TPRELGD;
14219                         if ((tls_mask & TLS_TLS) != 0
14220                             && (tls_mask & TLS_GD) == 0)
14221                           goto tls_ldgd_opt;
14222                       }
14223                     else if (retval == 3)
14224                       {
14225                         if ((tls_mask & TLS_TLS) != 0
14226                             && (tls_mask & TLS_LD) == 0)
14227                           goto tls_ldgd_opt;
14228                       }
14229                   }
14230               }
14231           }
14232           break;
14233
14234         case R_PPC64_GOT_TPREL16_HI:
14235         case R_PPC64_GOT_TPREL16_HA:
14236           if ((tls_mask & TLS_TLS) != 0
14237               && (tls_mask & TLS_TPREL) == 0)
14238             {
14239               rel->r_offset -= d_offset;
14240               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14241               r_type = R_PPC64_NONE;
14242               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14243             }
14244           break;
14245
14246         case R_PPC64_GOT_TPREL16_DS:
14247         case R_PPC64_GOT_TPREL16_LO_DS:
14248           if ((tls_mask & TLS_TLS) != 0
14249               && (tls_mask & TLS_TPREL) == 0)
14250             {
14251             toctprel:
14252               insn = bfd_get_32 (input_bfd,
14253                                  contents + rel->r_offset - d_offset);
14254               insn &= 31 << 21;
14255               insn |= 0x3c0d0000;       /* addis 0,13,0 */
14256               bfd_put_32 (input_bfd, insn,
14257                           contents + rel->r_offset - d_offset);
14258               r_type = R_PPC64_TPREL16_HA;
14259               if (toc_symndx != 0)
14260                 {
14261                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14262                   rel->r_addend = toc_addend;
14263                   /* We changed the symbol.  Start over in order to
14264                      get h, sym, sec etc. right.  */
14265                   goto again;
14266                 }
14267               else
14268                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14269             }
14270           break;
14271
14272         case R_PPC64_TLS:
14273           if ((tls_mask & TLS_TLS) != 0
14274               && (tls_mask & TLS_TPREL) == 0)
14275             {
14276               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14277               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14278               if (insn == 0)
14279                 abort ();
14280               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14281               /* Was PPC64_TLS which sits on insn boundary, now
14282                  PPC64_TPREL16_LO which is at low-order half-word.  */
14283               rel->r_offset += d_offset;
14284               r_type = R_PPC64_TPREL16_LO;
14285               if (toc_symndx != 0)
14286                 {
14287                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14288                   rel->r_addend = toc_addend;
14289                   /* We changed the symbol.  Start over in order to
14290                      get h, sym, sec etc. right.  */
14291                   goto again;
14292                 }
14293               else
14294                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14295             }
14296           break;
14297
14298         case R_PPC64_GOT_TLSGD16_HI:
14299         case R_PPC64_GOT_TLSGD16_HA:
14300           tls_gd = TLS_TPRELGD;
14301           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14302             goto tls_gdld_hi;
14303           break;
14304
14305         case R_PPC64_GOT_TLSLD16_HI:
14306         case R_PPC64_GOT_TLSLD16_HA:
14307           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14308             {
14309             tls_gdld_hi:
14310               if ((tls_mask & tls_gd) != 0)
14311                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14312                           + R_PPC64_GOT_TPREL16_DS);
14313               else
14314                 {
14315                   rel->r_offset -= d_offset;
14316                   bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14317                   r_type = R_PPC64_NONE;
14318                 }
14319               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14320             }
14321           break;
14322
14323         case R_PPC64_GOT_TLSGD16:
14324         case R_PPC64_GOT_TLSGD16_LO:
14325           tls_gd = TLS_TPRELGD;
14326           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14327             goto tls_ldgd_opt;
14328           break;
14329
14330         case R_PPC64_GOT_TLSLD16:
14331         case R_PPC64_GOT_TLSLD16_LO:
14332           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14333             {
14334               unsigned int insn1, insn2;
14335               bfd_vma offset;
14336
14337             tls_ldgd_opt:
14338               offset = (bfd_vma) -1;
14339               /* If not using the newer R_PPC64_TLSGD/LD to mark
14340                  __tls_get_addr calls, we must trust that the call
14341                  stays with its arg setup insns, ie. that the next
14342                  reloc is the __tls_get_addr call associated with
14343                  the current reloc.  Edit both insns.  */
14344               if (input_section->has_tls_get_addr_call
14345                   && rel + 1 < relend
14346                   && branch_reloc_hash_match (input_bfd, rel + 1,
14347                                               htab->tls_get_addr,
14348                                               htab->tls_get_addr_fd))
14349                 offset = rel[1].r_offset;
14350               /* We read the low GOT_TLS (or TOC16) insn because we
14351                  need to keep the destination reg.  It may be
14352                  something other than the usual r3, and moved to r3
14353                  before the call by intervening code.  */
14354               insn1 = bfd_get_32 (input_bfd,
14355                                   contents + rel->r_offset - d_offset);
14356               if ((tls_mask & tls_gd) != 0)
14357                 {
14358                   /* IE */
14359                   insn1 &= (0x1f << 21) | (0x1f << 16);
14360                   insn1 |= 58 << 26;    /* ld */
14361                   insn2 = 0x7c636a14;   /* add 3,3,13 */
14362                   if (offset != (bfd_vma) -1)
14363                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14364                   if ((tls_mask & TLS_EXPLICIT) == 0)
14365                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14366                               + R_PPC64_GOT_TPREL16_DS);
14367                   else
14368                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14369                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14370                 }
14371               else
14372                 {
14373                   /* LE */
14374                   insn1 &= 0x1f << 21;
14375                   insn1 |= 0x3c0d0000;  /* addis r,13,0 */
14376                   insn2 = 0x38630000;   /* addi 3,3,0 */
14377                   if (tls_gd == 0)
14378                     {
14379                       /* Was an LD reloc.  */
14380                       if (toc_symndx)
14381                         sec = local_sections[toc_symndx];
14382                       for (r_symndx = 0;
14383                            r_symndx < symtab_hdr->sh_info;
14384                            r_symndx++)
14385                         if (local_sections[r_symndx] == sec)
14386                           break;
14387                       if (r_symndx >= symtab_hdr->sh_info)
14388                         r_symndx = STN_UNDEF;
14389                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14390                       if (r_symndx != STN_UNDEF)
14391                         rel->r_addend -= (local_syms[r_symndx].st_value
14392                                           + sec->output_offset
14393                                           + sec->output_section->vma);
14394                     }
14395                   else if (toc_symndx != 0)
14396                     {
14397                       r_symndx = toc_symndx;
14398                       rel->r_addend = toc_addend;
14399                     }
14400                   r_type = R_PPC64_TPREL16_HA;
14401                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14402                   if (offset != (bfd_vma) -1)
14403                     {
14404                       rel[1].r_info = ELF64_R_INFO (r_symndx,
14405                                                     R_PPC64_TPREL16_LO);
14406                       rel[1].r_offset = offset + d_offset;
14407                       rel[1].r_addend = rel->r_addend;
14408                     }
14409                 }
14410               bfd_put_32 (input_bfd, insn1,
14411                           contents + rel->r_offset - d_offset);
14412               if (offset != (bfd_vma) -1)
14413                 bfd_put_32 (input_bfd, insn2, contents + offset);
14414               if ((tls_mask & tls_gd) == 0
14415                   && (tls_gd == 0 || toc_symndx != 0))
14416                 {
14417                   /* We changed the symbol.  Start over in order
14418                      to get h, sym, sec etc. right.  */
14419                   goto again;
14420                 }
14421             }
14422           break;
14423
14424         case R_PPC64_TLSGD:
14425           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
14426               && rel + 1 < relend)
14427             {
14428               unsigned int insn2;
14429               bfd_vma offset = rel->r_offset;
14430
14431               if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14432                 {
14433                   bfd_put_32 (output_bfd, NOP, contents + offset);
14434                   rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14435                   break;
14436                 }
14437
14438               if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14439                 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14440
14441               if ((tls_mask & TLS_TPRELGD) != 0)
14442                 {
14443                   /* IE */
14444                   r_type = R_PPC64_NONE;
14445                   insn2 = 0x7c636a14;   /* add 3,3,13 */
14446                 }
14447               else
14448                 {
14449                   /* LE */
14450                   if (toc_symndx != 0)
14451                     {
14452                       r_symndx = toc_symndx;
14453                       rel->r_addend = toc_addend;
14454                     }
14455                   r_type = R_PPC64_TPREL16_LO;
14456                   rel->r_offset = offset + d_offset;
14457                   insn2 = 0x38630000;   /* addi 3,3,0 */
14458                 }
14459               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14460               /* Zap the reloc on the _tls_get_addr call too.  */
14461               BFD_ASSERT (offset == rel[1].r_offset);
14462               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14463               bfd_put_32 (input_bfd, insn2, contents + offset);
14464               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
14465                 goto again;
14466             }
14467           break;
14468
14469         case R_PPC64_TLSLD:
14470           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
14471               && rel + 1 < relend)
14472             {
14473               unsigned int insn2;
14474               bfd_vma offset = rel->r_offset;
14475
14476               if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14477                 {
14478                   bfd_put_32 (output_bfd, NOP, contents + offset);
14479                   rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14480                   break;
14481                 }
14482
14483               if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14484                 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14485
14486               if (toc_symndx)
14487                 sec = local_sections[toc_symndx];
14488               for (r_symndx = 0;
14489                    r_symndx < symtab_hdr->sh_info;
14490                    r_symndx++)
14491                 if (local_sections[r_symndx] == sec)
14492                   break;
14493               if (r_symndx >= symtab_hdr->sh_info)
14494                 r_symndx = STN_UNDEF;
14495               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14496               if (r_symndx != STN_UNDEF)
14497                 rel->r_addend -= (local_syms[r_symndx].st_value
14498                                   + sec->output_offset
14499                                   + sec->output_section->vma);
14500
14501               r_type = R_PPC64_TPREL16_LO;
14502               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14503               rel->r_offset = offset + d_offset;
14504               /* Zap the reloc on the _tls_get_addr call too.  */
14505               BFD_ASSERT (offset == rel[1].r_offset);
14506               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14507               insn2 = 0x38630000;       /* addi 3,3,0 */
14508               bfd_put_32 (input_bfd, insn2, contents + offset);
14509               goto again;
14510             }
14511           break;
14512
14513         case R_PPC64_DTPMOD64:
14514           if (rel + 1 < relend
14515               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14516               && rel[1].r_offset == rel->r_offset + 8)
14517             {
14518               if ((tls_mask & TLS_GD) == 0)
14519                 {
14520                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14521                   if ((tls_mask & TLS_TPRELGD) != 0)
14522                     r_type = R_PPC64_TPREL64;
14523                   else
14524                     {
14525                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14526                       r_type = R_PPC64_NONE;
14527                     }
14528                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14529                 }
14530             }
14531           else
14532             {
14533               if ((tls_mask & TLS_LD) == 0)
14534                 {
14535                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14536                   r_type = R_PPC64_NONE;
14537                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14538                 }
14539             }
14540           break;
14541
14542         case R_PPC64_TPREL64:
14543           if ((tls_mask & TLS_TPREL) == 0)
14544             {
14545               r_type = R_PPC64_NONE;
14546               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14547             }
14548           break;
14549
14550         case R_PPC64_ENTRY:
14551           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14552           if (!bfd_link_pic (info)
14553               && !info->traditional_format
14554               && relocation + 0x80008000 <= 0xffffffff)
14555             {
14556               unsigned int insn1, insn2;
14557
14558               insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14559               insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14560               if ((insn1 & ~0xfffc) == LD_R2_0R12
14561                   && insn2 == ADD_R2_R2_R12)
14562                 {
14563                   bfd_put_32 (input_bfd,
14564                               LIS_R2 + PPC_HA (relocation),
14565                               contents + rel->r_offset);
14566                   bfd_put_32 (input_bfd,
14567                               ADDI_R2_R2 + PPC_LO (relocation),
14568                               contents + rel->r_offset + 4);
14569                 }
14570             }
14571           else
14572             {
14573               relocation -= (rel->r_offset
14574                              + input_section->output_offset
14575                              + input_section->output_section->vma);
14576               if (relocation + 0x80008000 <= 0xffffffff)
14577                 {
14578                   unsigned int insn1, insn2;
14579
14580                   insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14581                   insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14582                   if ((insn1 & ~0xfffc) == LD_R2_0R12
14583                       && insn2 == ADD_R2_R2_R12)
14584                     {
14585                       bfd_put_32 (input_bfd,
14586                                   ADDIS_R2_R12 + PPC_HA (relocation),
14587                                   contents + rel->r_offset);
14588                       bfd_put_32 (input_bfd,
14589                                   ADDI_R2_R2 + PPC_LO (relocation),
14590                                   contents + rel->r_offset + 4);
14591                     }
14592                 }
14593             }
14594           break;
14595
14596         case R_PPC64_REL16_HA:
14597           /* If we are generating a non-PIC executable, edit
14598              .  0:      addis 2,12,.TOC.-0b@ha
14599              .          addi 2,2,.TOC.-0b@l
14600              used by ELFv2 global entry points to set up r2, to
14601              .          lis 2,.TOC.@ha
14602              .          addi 2,2,.TOC.@l
14603              if .TOC. is in range.  */
14604           if (!bfd_link_pic (info)
14605               && !info->traditional_format
14606               && !htab->opd_abi
14607               && rel->r_addend == d_offset
14608               && h != NULL && &h->elf == htab->elf.hgot
14609               && rel + 1 < relend
14610               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14611               && rel[1].r_offset == rel->r_offset + 4
14612               && rel[1].r_addend == rel->r_addend + 4
14613               && relocation + 0x80008000 <= 0xffffffff)
14614             {
14615               unsigned int insn1, insn2;
14616               bfd_vma offset = rel->r_offset - d_offset;
14617               insn1 = bfd_get_32 (input_bfd, contents + offset);
14618               insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14619               if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14620                   && (insn2 & 0xffff0000) == ADDI_R2_R2)
14621                 {
14622                   r_type = R_PPC64_ADDR16_HA;
14623                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14624                   rel->r_addend -= d_offset;
14625                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14626                   rel[1].r_addend -= d_offset + 4;
14627                   bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14628                 }
14629             }
14630           break;
14631         }
14632
14633       /* Handle other relocations that tweak non-addend part of insn.  */
14634       insn = 0;
14635       max_br_offset = 1 << 25;
14636       addend = rel->r_addend;
14637       reloc_dest = DEST_NORMAL;
14638       switch (r_type)
14639         {
14640         default:
14641           break;
14642
14643         case R_PPC64_TOCSAVE:
14644           if (relocation + addend == (rel->r_offset
14645                                       + input_section->output_offset
14646                                       + input_section->output_section->vma)
14647               && tocsave_find (htab, NO_INSERT,
14648                                &local_syms, rel, input_bfd))
14649             {
14650               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14651               if (insn == NOP
14652                   || insn == CROR_151515 || insn == CROR_313131)
14653                 bfd_put_32 (input_bfd,
14654                             STD_R2_0R1 + STK_TOC (htab),
14655                             contents + rel->r_offset);
14656             }
14657           break;
14658
14659           /* Branch taken prediction relocations.  */
14660         case R_PPC64_ADDR14_BRTAKEN:
14661         case R_PPC64_REL14_BRTAKEN:
14662           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
14663           /* Fall through.  */
14664
14665           /* Branch not taken prediction relocations.  */
14666         case R_PPC64_ADDR14_BRNTAKEN:
14667         case R_PPC64_REL14_BRNTAKEN:
14668           insn |= bfd_get_32 (input_bfd,
14669                               contents + rel->r_offset) & ~(0x01 << 21);
14670           /* Fall through.  */
14671
14672         case R_PPC64_REL14:
14673           max_br_offset = 1 << 15;
14674           /* Fall through.  */
14675
14676         case R_PPC64_REL24:
14677         case R_PPC64_PLTCALL:
14678           /* Calls to functions with a different TOC, such as calls to
14679              shared objects, need to alter the TOC pointer.  This is
14680              done using a linkage stub.  A REL24 branching to these
14681              linkage stubs needs to be followed by a nop, as the nop
14682              will be replaced with an instruction to restore the TOC
14683              base pointer.  */
14684           fdh = h;
14685           if (h != NULL
14686               && h->oh != NULL
14687               && h->oh->is_func_descriptor)
14688             fdh = ppc_follow_link (h->oh);
14689           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14690                                            htab);
14691           if (r_type == R_PPC64_PLTCALL
14692               && stub_entry != NULL
14693               && (stub_entry->stub_type == ppc_stub_plt_call
14694                   || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14695             stub_entry = NULL;
14696
14697           if (stub_entry != NULL
14698               && (stub_entry->stub_type == ppc_stub_plt_call
14699                   || stub_entry->stub_type == ppc_stub_plt_call_r2save
14700                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14701                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
14702             {
14703               bfd_boolean can_plt_call = FALSE;
14704
14705               if (stub_entry->stub_type == ppc_stub_plt_call
14706                   && !htab->opd_abi
14707                   && htab->params->plt_localentry0 != 0
14708                   && is_elfv2_localentry0 (&h->elf))
14709                 {
14710                   /* The function doesn't use or change r2.  */
14711                   can_plt_call = TRUE;
14712                 }
14713
14714               /* All of these stubs may modify r2, so there must be a
14715                  branch and link followed by a nop.  The nop is
14716                  replaced by an insn to restore r2.  */
14717               else if (rel->r_offset + 8 <= input_section->size)
14718                 {
14719                   unsigned long br;
14720
14721                   br = bfd_get_32 (input_bfd,
14722                                    contents + rel->r_offset);
14723                   if ((br & 1) != 0)
14724                     {
14725                       unsigned long nop;
14726
14727                       nop = bfd_get_32 (input_bfd,
14728                                         contents + rel->r_offset + 4);
14729                       if (nop == LD_R2_0R1 + STK_TOC (htab))
14730                         can_plt_call = TRUE;
14731                       else if (nop == NOP
14732                                || nop == CROR_151515
14733                                || nop == CROR_313131)
14734                         {
14735                           if (h != NULL
14736                               && (h == htab->tls_get_addr_fd
14737                                   || h == htab->tls_get_addr)
14738                               && htab->params->tls_get_addr_opt)
14739                             {
14740                               /* Special stub used, leave nop alone.  */
14741                             }
14742                           else
14743                             bfd_put_32 (input_bfd,
14744                                         LD_R2_0R1 + STK_TOC (htab),
14745                                         contents + rel->r_offset + 4);
14746                           can_plt_call = TRUE;
14747                         }
14748                     }
14749                 }
14750
14751               if (!can_plt_call && h != NULL)
14752                 {
14753                   const char *name = h->elf.root.root.string;
14754
14755                   if (*name == '.')
14756                     ++name;
14757
14758                   if (strncmp (name, "__libc_start_main", 17) == 0
14759                       && (name[17] == 0 || name[17] == '@'))
14760                     {
14761                       /* Allow crt1 branch to go via a toc adjusting
14762                          stub.  Other calls that never return could do
14763                          the same, if we could detect such.  */
14764                       can_plt_call = TRUE;
14765                     }
14766                 }
14767
14768               if (!can_plt_call)
14769                 {
14770                   /* g++ as of 20130507 emits self-calls without a
14771                      following nop.  This is arguably wrong since we
14772                      have conflicting information.  On the one hand a
14773                      global symbol and on the other a local call
14774                      sequence, but don't error for this special case.
14775                      It isn't possible to cheaply verify we have
14776                      exactly such a call.  Allow all calls to the same
14777                      section.  */
14778                   asection *code_sec = sec;
14779
14780                   if (get_opd_info (sec) != NULL)
14781                     {
14782                       bfd_vma off = (relocation + addend
14783                                      - sec->output_section->vma
14784                                      - sec->output_offset);
14785
14786                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14787                     }
14788                   if (code_sec == input_section)
14789                     can_plt_call = TRUE;
14790                 }
14791
14792               if (!can_plt_call)
14793                 {
14794                   if (stub_entry->stub_type == ppc_stub_plt_call
14795                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14796                     info->callbacks->einfo
14797                       /* xgettext:c-format */
14798                       (_("%H: call to `%pT' lacks nop, can't restore toc; "
14799                          "recompile with -fPIC\n"),
14800                        input_bfd, input_section, rel->r_offset, sym_name);
14801                   else
14802                     info->callbacks->einfo
14803                       /* xgettext:c-format */
14804                       (_("%H: call to `%pT' lacks nop, can't restore toc; "
14805                          "(-mcmodel=small toc adjust stub)\n"),
14806                        input_bfd, input_section, rel->r_offset, sym_name);
14807
14808                   bfd_set_error (bfd_error_bad_value);
14809                   ret = FALSE;
14810                 }
14811
14812               if (can_plt_call
14813                   && (stub_entry->stub_type == ppc_stub_plt_call
14814                       || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14815                 unresolved_reloc = FALSE;
14816             }
14817
14818           if ((stub_entry == NULL
14819                || stub_entry->stub_type == ppc_stub_long_branch
14820                || stub_entry->stub_type == ppc_stub_plt_branch)
14821               && get_opd_info (sec) != NULL)
14822             {
14823               /* The branch destination is the value of the opd entry. */
14824               bfd_vma off = (relocation + addend
14825                              - sec->output_section->vma
14826                              - sec->output_offset);
14827               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14828               if (dest != (bfd_vma) -1)
14829                 {
14830                   relocation = dest;
14831                   addend = 0;
14832                   reloc_dest = DEST_OPD;
14833                 }
14834             }
14835
14836           /* If the branch is out of reach we ought to have a long
14837              branch stub.  */
14838           from = (rel->r_offset
14839                   + input_section->output_offset
14840                   + input_section->output_section->vma);
14841
14842           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14843                                                   ? fdh->elf.other
14844                                                   : sym->st_other);
14845
14846           if (stub_entry != NULL
14847               && (stub_entry->stub_type == ppc_stub_long_branch
14848                   || stub_entry->stub_type == ppc_stub_plt_branch)
14849               && (r_type == R_PPC64_ADDR14_BRTAKEN
14850                   || r_type == R_PPC64_ADDR14_BRNTAKEN
14851                   || (relocation + addend - from + max_br_offset
14852                       < 2 * max_br_offset)))
14853             /* Don't use the stub if this branch is in range.  */
14854             stub_entry = NULL;
14855
14856           if (stub_entry != NULL)
14857             {
14858               /* Munge up the value and addend so that we call the stub
14859                  rather than the procedure directly.  */
14860               asection *stub_sec = stub_entry->group->stub_sec;
14861
14862               if (stub_entry->stub_type == ppc_stub_save_res)
14863                 relocation += (stub_sec->output_offset
14864                                + stub_sec->output_section->vma
14865                                + stub_sec->size - htab->sfpr->size
14866                                - htab->sfpr->output_offset
14867                                - htab->sfpr->output_section->vma);
14868               else
14869                 relocation = (stub_entry->stub_offset
14870                               + stub_sec->output_offset
14871                               + stub_sec->output_section->vma);
14872               addend = 0;
14873               reloc_dest = DEST_STUB;
14874
14875               if ((stub_entry->stub_type == ppc_stub_plt_call
14876                    || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14877                   && (ALWAYS_EMIT_R2SAVE
14878                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14879                   && rel + 1 < relend
14880                   && rel[1].r_offset == rel->r_offset + 4
14881                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14882                 relocation += 4;
14883             }
14884
14885           if (insn != 0)
14886             {
14887               if (is_isa_v2)
14888                 {
14889                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
14890                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
14891                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
14892                   if ((insn & (0x14 << 21)) == (0x04 << 21))
14893                     insn |= 0x02 << 21;
14894                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
14895                     insn |= 0x08 << 21;
14896                   else
14897                     break;
14898                 }
14899               else
14900                 {
14901                   /* Invert 'y' bit if not the default.  */
14902                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
14903                     insn ^= 0x01 << 21;
14904                 }
14905
14906               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14907             }
14908
14909           /* NOP out calls to undefined weak functions.
14910              We can thus call a weak function without first
14911              checking whether the function is defined.  */
14912           else if (h != NULL
14913                    && h->elf.root.type == bfd_link_hash_undefweak
14914                    && h->elf.dynindx == -1
14915                    && r_type == R_PPC64_REL24
14916                    && relocation == 0
14917                    && addend == 0)
14918             {
14919               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14920               goto copy_reloc;
14921             }
14922           break;
14923         }
14924
14925       /* Set `addend'.  */
14926       tls_type = 0;
14927       save_unresolved_reloc = unresolved_reloc;
14928       switch (r_type)
14929         {
14930         default:
14931           /* xgettext:c-format */
14932           _bfd_error_handler (_("%pB: %s unsupported"),
14933                               input_bfd, ppc64_elf_howto_table[r_type]->name);
14934
14935           bfd_set_error (bfd_error_bad_value);
14936           ret = FALSE;
14937           goto copy_reloc;
14938
14939         case R_PPC64_NONE:
14940         case R_PPC64_TLS:
14941         case R_PPC64_TLSGD:
14942         case R_PPC64_TLSLD:
14943         case R_PPC64_TOCSAVE:
14944         case R_PPC64_GNU_VTINHERIT:
14945         case R_PPC64_GNU_VTENTRY:
14946         case R_PPC64_ENTRY:
14947           goto copy_reloc;
14948
14949           /* GOT16 relocations.  Like an ADDR16 using the symbol's
14950              address in the GOT as relocation value instead of the
14951              symbol's value itself.  Also, create a GOT entry for the
14952              symbol and put the symbol value there.  */
14953         case R_PPC64_GOT_TLSGD16:
14954         case R_PPC64_GOT_TLSGD16_LO:
14955         case R_PPC64_GOT_TLSGD16_HI:
14956         case R_PPC64_GOT_TLSGD16_HA:
14957           tls_type = TLS_TLS | TLS_GD;
14958           goto dogot;
14959
14960         case R_PPC64_GOT_TLSLD16:
14961         case R_PPC64_GOT_TLSLD16_LO:
14962         case R_PPC64_GOT_TLSLD16_HI:
14963         case R_PPC64_GOT_TLSLD16_HA:
14964           tls_type = TLS_TLS | TLS_LD;
14965           goto dogot;
14966
14967         case R_PPC64_GOT_TPREL16_DS:
14968         case R_PPC64_GOT_TPREL16_LO_DS:
14969         case R_PPC64_GOT_TPREL16_HI:
14970         case R_PPC64_GOT_TPREL16_HA:
14971           tls_type = TLS_TLS | TLS_TPREL;
14972           goto dogot;
14973
14974         case R_PPC64_GOT_DTPREL16_DS:
14975         case R_PPC64_GOT_DTPREL16_LO_DS:
14976         case R_PPC64_GOT_DTPREL16_HI:
14977         case R_PPC64_GOT_DTPREL16_HA:
14978           tls_type = TLS_TLS | TLS_DTPREL;
14979           goto dogot;
14980
14981         case R_PPC64_GOT16:
14982         case R_PPC64_GOT16_LO:
14983         case R_PPC64_GOT16_HI:
14984         case R_PPC64_GOT16_HA:
14985         case R_PPC64_GOT16_DS:
14986         case R_PPC64_GOT16_LO_DS:
14987         dogot:
14988           {
14989             /* Relocation is to the entry for this symbol in the global
14990                offset table.  */
14991             asection *got;
14992             bfd_vma *offp;
14993             bfd_vma off;
14994             unsigned long indx = 0;
14995             struct got_entry *ent;
14996
14997             if (tls_type == (TLS_TLS | TLS_LD)
14998                 && (h == NULL
14999                     || !h->elf.def_dynamic))
15000               ent = ppc64_tlsld_got (input_bfd);
15001             else
15002               {
15003                 if (h != NULL)
15004                   {
15005                     if (!htab->elf.dynamic_sections_created
15006                         || h->elf.dynindx == -1
15007                         || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15008                         || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
15009                       /* This is actually a static link, or it is a
15010                          -Bsymbolic link and the symbol is defined
15011                          locally, or the symbol was forced to be local
15012                          because of a version file.  */
15013                       ;
15014                     else
15015                       {
15016                         indx = h->elf.dynindx;
15017                         unresolved_reloc = FALSE;
15018                       }
15019                     ent = h->elf.got.glist;
15020                   }
15021                 else
15022                   {
15023                     if (local_got_ents == NULL)
15024                       abort ();
15025                     ent = local_got_ents[r_symndx];
15026                   }
15027
15028                 for (; ent != NULL; ent = ent->next)
15029                   if (ent->addend == orig_rel.r_addend
15030                       && ent->owner == input_bfd
15031                       && ent->tls_type == tls_type)
15032                     break;
15033               }
15034
15035             if (ent == NULL)
15036               abort ();
15037             if (ent->is_indirect)
15038               ent = ent->got.ent;
15039             offp = &ent->got.offset;
15040             got = ppc64_elf_tdata (ent->owner)->got;
15041             if (got == NULL)
15042               abort ();
15043
15044             /* The offset must always be a multiple of 8.  We use the
15045                least significant bit to record whether we have already
15046                processed this entry.  */
15047             off = *offp;
15048             if ((off & 1) != 0)
15049               off &= ~1;
15050             else
15051               {
15052                 /* Generate relocs for the dynamic linker, except in
15053                    the case of TLSLD where we'll use one entry per
15054                    module.  */
15055                 asection *relgot;
15056                 bfd_boolean ifunc;
15057
15058                 *offp = off | 1;
15059                 relgot = NULL;
15060                 ifunc = (h != NULL
15061                          ? h->elf.type == STT_GNU_IFUNC
15062                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
15063                 if (ifunc)
15064                   {
15065                     relgot = htab->elf.irelplt;
15066                     if (indx == 0)
15067                       htab->local_ifunc_resolver = 1;
15068                     else if (is_static_defined (&h->elf))
15069                       htab->maybe_local_ifunc_resolver = 1;
15070                   }
15071                 else if (indx != 0
15072                          || (bfd_link_pic (info)
15073                              && (h == NULL
15074                                  || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
15075                                  || (tls_type == (TLS_TLS | TLS_LD)
15076                                      && !h->elf.def_dynamic))
15077                              && !(tls_type == (TLS_TLS | TLS_TPREL)
15078                                   && bfd_link_executable (info)
15079                                   && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
15080                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
15081                 if (relgot != NULL)
15082                   {
15083                     outrel.r_offset = (got->output_section->vma
15084                                        + got->output_offset
15085                                        + off);
15086                     outrel.r_addend = addend;
15087                     if (tls_type & (TLS_LD | TLS_GD))
15088                       {
15089                         outrel.r_addend = 0;
15090                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
15091                         if (tls_type == (TLS_TLS | TLS_GD))
15092                           {
15093                             loc = relgot->contents;
15094                             loc += (relgot->reloc_count++
15095                                     * sizeof (Elf64_External_Rela));
15096                             bfd_elf64_swap_reloca_out (output_bfd,
15097                                                        &outrel, loc);
15098                             outrel.r_offset += 8;
15099                             outrel.r_addend = addend;
15100                             outrel.r_info
15101                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15102                           }
15103                       }
15104                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
15105                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15106                     else if (tls_type == (TLS_TLS | TLS_TPREL))
15107                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
15108                     else if (indx != 0)
15109                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15110                     else
15111                       {
15112                         if (ifunc)
15113                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15114                         else
15115                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15116
15117                         /* Write the .got section contents for the sake
15118                            of prelink.  */
15119                         loc = got->contents + off;
15120                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15121                                     loc);
15122                       }
15123
15124                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
15125                       {
15126                         outrel.r_addend += relocation;
15127                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
15128                           {
15129                             if (htab->elf.tls_sec == NULL)
15130                               outrel.r_addend = 0;
15131                             else
15132                               outrel.r_addend -= htab->elf.tls_sec->vma;
15133                           }
15134                       }
15135                     loc = relgot->contents;
15136                     loc += (relgot->reloc_count++
15137                             * sizeof (Elf64_External_Rela));
15138                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15139                   }
15140
15141                 /* Init the .got section contents here if we're not
15142                    emitting a reloc.  */
15143                 else
15144                   {
15145                     relocation += addend;
15146                     if (tls_type != 0)
15147                       {
15148                         if (htab->elf.tls_sec == NULL)
15149                           relocation = 0;
15150                         else
15151                           {
15152                             if (tls_type & TLS_LD)
15153                               relocation = 0;
15154                             else
15155                               relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
15156                             if (tls_type & TLS_TPREL)
15157                               relocation += DTP_OFFSET - TP_OFFSET;
15158                           }
15159
15160                         if (tls_type & (TLS_GD | TLS_LD))
15161                           {
15162                             bfd_put_64 (output_bfd, relocation,
15163                                         got->contents + off + 8);
15164                             relocation = 1;
15165                           }
15166                       }
15167                     bfd_put_64 (output_bfd, relocation,
15168                                 got->contents + off);
15169                   }
15170               }
15171
15172             if (off >= (bfd_vma) -2)
15173               abort ();
15174
15175             relocation = got->output_section->vma + got->output_offset + off;
15176             addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
15177           }
15178           break;
15179
15180         case R_PPC64_PLT16_HA:
15181         case R_PPC64_PLT16_HI:
15182         case R_PPC64_PLT16_LO:
15183         case R_PPC64_PLT16_LO_DS:
15184         case R_PPC64_PLT32:
15185         case R_PPC64_PLT64:
15186         case R_PPC64_PLTSEQ:
15187         case R_PPC64_PLTCALL:
15188           /* Relocation is to the entry for this symbol in the
15189              procedure linkage table.  */
15190           unresolved_reloc = TRUE;
15191           {
15192             struct plt_entry **plt_list = NULL;
15193             if (h != NULL)
15194               plt_list = &h->elf.plt.plist;
15195             else if (local_got_ents != NULL)
15196               {
15197                 struct plt_entry **local_plt = (struct plt_entry **)
15198                   (local_got_ents + symtab_hdr->sh_info);
15199                 plt_list = local_plt + r_symndx;
15200               }
15201             if (plt_list)
15202               {
15203                 struct plt_entry *ent;
15204
15205                 for (ent = *plt_list; ent != NULL; ent = ent->next)
15206                   if (ent->plt.offset != (bfd_vma) -1
15207                       && ent->addend == orig_rel.r_addend)
15208                     {
15209                       asection *plt;
15210                       bfd_vma got;
15211
15212                       plt = htab->elf.splt;
15213                       if (!htab->elf.dynamic_sections_created
15214                           || h == NULL
15215                           || h->elf.dynindx == -1)
15216                         {
15217                           if (h != NULL
15218                               ? h->elf.type == STT_GNU_IFUNC
15219                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15220                             plt = htab->elf.iplt;
15221                           else
15222                             plt = htab->pltlocal;
15223                         }
15224                       relocation = (plt->output_section->vma
15225                                     + plt->output_offset
15226                                     + ent->plt.offset);
15227                       if (r_type == R_PPC64_PLT16_HA
15228                           || r_type ==R_PPC64_PLT16_HI
15229                           || r_type ==R_PPC64_PLT16_LO
15230                           || r_type ==R_PPC64_PLT16_LO_DS)
15231                         {
15232                           got = (elf_gp (output_bfd)
15233                                  + htab->sec_info[input_section->id].toc_off);
15234                           relocation -= got;
15235                         }
15236                       addend = 0;
15237                       unresolved_reloc = FALSE;
15238                       break;
15239                     }
15240               }
15241           }
15242           break;
15243
15244         case R_PPC64_TOC:
15245           /* Relocation value is TOC base.  */
15246           relocation = TOCstart;
15247           if (r_symndx == STN_UNDEF)
15248             relocation += htab->sec_info[input_section->id].toc_off;
15249           else if (unresolved_reloc)
15250             ;
15251           else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15252             relocation += htab->sec_info[sec->id].toc_off;
15253           else
15254             unresolved_reloc = TRUE;
15255           goto dodyn;
15256
15257           /* TOC16 relocs.  We want the offset relative to the TOC base,
15258              which is the address of the start of the TOC plus 0x8000.
15259              The TOC consists of sections .got, .toc, .tocbss, and .plt,
15260              in this order.  */
15261         case R_PPC64_TOC16:
15262         case R_PPC64_TOC16_LO:
15263         case R_PPC64_TOC16_HI:
15264         case R_PPC64_TOC16_DS:
15265         case R_PPC64_TOC16_LO_DS:
15266         case R_PPC64_TOC16_HA:
15267           addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
15268           break;
15269
15270           /* Relocate against the beginning of the section.  */
15271         case R_PPC64_SECTOFF:
15272         case R_PPC64_SECTOFF_LO:
15273         case R_PPC64_SECTOFF_HI:
15274         case R_PPC64_SECTOFF_DS:
15275         case R_PPC64_SECTOFF_LO_DS:
15276         case R_PPC64_SECTOFF_HA:
15277           if (sec != NULL)
15278             addend -= sec->output_section->vma;
15279           break;
15280
15281         case R_PPC64_REL16:
15282         case R_PPC64_REL16_LO:
15283         case R_PPC64_REL16_HI:
15284         case R_PPC64_REL16_HA:
15285         case R_PPC64_REL16DX_HA:
15286           break;
15287
15288         case R_PPC64_REL14:
15289         case R_PPC64_REL14_BRNTAKEN:
15290         case R_PPC64_REL14_BRTAKEN:
15291         case R_PPC64_REL24:
15292           break;
15293
15294         case R_PPC64_TPREL16:
15295         case R_PPC64_TPREL16_LO:
15296         case R_PPC64_TPREL16_HI:
15297         case R_PPC64_TPREL16_HA:
15298         case R_PPC64_TPREL16_DS:
15299         case R_PPC64_TPREL16_LO_DS:
15300         case R_PPC64_TPREL16_HIGH:
15301         case R_PPC64_TPREL16_HIGHA:
15302         case R_PPC64_TPREL16_HIGHER:
15303         case R_PPC64_TPREL16_HIGHERA:
15304         case R_PPC64_TPREL16_HIGHEST:
15305         case R_PPC64_TPREL16_HIGHESTA:
15306           if (h != NULL
15307               && h->elf.root.type == bfd_link_hash_undefweak
15308               && h->elf.dynindx == -1)
15309             {
15310               /* Make this relocation against an undefined weak symbol
15311                  resolve to zero.  This is really just a tweak, since
15312                  code using weak externs ought to check that they are
15313                  defined before using them.  */
15314               bfd_byte *p = contents + rel->r_offset - d_offset;
15315
15316               insn = bfd_get_32 (input_bfd, p);
15317               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15318               if (insn != 0)
15319                 bfd_put_32 (input_bfd, insn, p);
15320               break;
15321             }
15322           if (htab->elf.tls_sec != NULL)
15323             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15324           /* The TPREL16 relocs shouldn't really be used in shared
15325              libs or with non-local symbols as that will result in
15326              DT_TEXTREL being set, but support them anyway.  */
15327           goto dodyn;
15328
15329         case R_PPC64_DTPREL16:
15330         case R_PPC64_DTPREL16_LO:
15331         case R_PPC64_DTPREL16_HI:
15332         case R_PPC64_DTPREL16_HA:
15333         case R_PPC64_DTPREL16_DS:
15334         case R_PPC64_DTPREL16_LO_DS:
15335         case R_PPC64_DTPREL16_HIGH:
15336         case R_PPC64_DTPREL16_HIGHA:
15337         case R_PPC64_DTPREL16_HIGHER:
15338         case R_PPC64_DTPREL16_HIGHERA:
15339         case R_PPC64_DTPREL16_HIGHEST:
15340         case R_PPC64_DTPREL16_HIGHESTA:
15341           if (htab->elf.tls_sec != NULL)
15342             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15343           break;
15344
15345         case R_PPC64_ADDR64_LOCAL:
15346           addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15347                                               ? h->elf.other
15348                                               : sym->st_other);
15349           break;
15350
15351         case R_PPC64_DTPMOD64:
15352           relocation = 1;
15353           addend = 0;
15354           goto dodyn;
15355
15356         case R_PPC64_TPREL64:
15357           if (htab->elf.tls_sec != NULL)
15358             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15359           goto dodyn;
15360
15361         case R_PPC64_DTPREL64:
15362           if (htab->elf.tls_sec != NULL)
15363             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15364           /* Fall through.  */
15365
15366           /* Relocations that may need to be propagated if this is a
15367              dynamic object.  */
15368         case R_PPC64_REL30:
15369         case R_PPC64_REL32:
15370         case R_PPC64_REL64:
15371         case R_PPC64_ADDR14:
15372         case R_PPC64_ADDR14_BRNTAKEN:
15373         case R_PPC64_ADDR14_BRTAKEN:
15374         case R_PPC64_ADDR16:
15375         case R_PPC64_ADDR16_DS:
15376         case R_PPC64_ADDR16_HA:
15377         case R_PPC64_ADDR16_HI:
15378         case R_PPC64_ADDR16_HIGH:
15379         case R_PPC64_ADDR16_HIGHA:
15380         case R_PPC64_ADDR16_HIGHER:
15381         case R_PPC64_ADDR16_HIGHERA:
15382         case R_PPC64_ADDR16_HIGHEST:
15383         case R_PPC64_ADDR16_HIGHESTA:
15384         case R_PPC64_ADDR16_LO:
15385         case R_PPC64_ADDR16_LO_DS:
15386         case R_PPC64_ADDR24:
15387         case R_PPC64_ADDR32:
15388         case R_PPC64_ADDR64:
15389         case R_PPC64_UADDR16:
15390         case R_PPC64_UADDR32:
15391         case R_PPC64_UADDR64:
15392         dodyn:
15393           if ((input_section->flags & SEC_ALLOC) == 0)
15394             break;
15395
15396           if (NO_OPD_RELOCS && is_opd)
15397             break;
15398
15399           if (bfd_link_pic (info)
15400               ? ((h == NULL
15401                   || h->dyn_relocs != NULL)
15402                  && ((h != NULL && pc_dynrelocs (h))
15403                      || must_be_dyn_reloc (info, r_type)))
15404               : (h != NULL
15405                  ? h->dyn_relocs != NULL
15406                  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15407             {
15408               bfd_boolean skip, relocate;
15409               asection *sreloc;
15410               bfd_vma out_off;
15411               long indx = 0;
15412
15413               /* When generating a dynamic object, these relocations
15414                  are copied into the output file to be resolved at run
15415                  time.  */
15416
15417               skip = FALSE;
15418               relocate = FALSE;
15419
15420               out_off = _bfd_elf_section_offset (output_bfd, info,
15421                                                  input_section, rel->r_offset);
15422               if (out_off == (bfd_vma) -1)
15423                 skip = TRUE;
15424               else if (out_off == (bfd_vma) -2)
15425                 skip = TRUE, relocate = TRUE;
15426               out_off += (input_section->output_section->vma
15427                           + input_section->output_offset);
15428               outrel.r_offset = out_off;
15429               outrel.r_addend = rel->r_addend;
15430
15431               /* Optimize unaligned reloc use.  */
15432               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15433                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15434                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15435               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15436                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15437                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15438               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15439                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15440                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15441
15442               if (skip)
15443                 memset (&outrel, 0, sizeof outrel);
15444               else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15445                        && !is_opd
15446                        && r_type != R_PPC64_TOC)
15447                 {
15448                   indx = h->elf.dynindx;
15449                   BFD_ASSERT (indx != -1);
15450                   outrel.r_info = ELF64_R_INFO (indx, r_type);
15451                 }
15452               else
15453                 {
15454                   /* This symbol is local, or marked to become local,
15455                      or this is an opd section reloc which must point
15456                      at a local function.  */
15457                   outrel.r_addend += relocation;
15458                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
15459                     {
15460                       if (is_opd && h != NULL)
15461                         {
15462                           /* Lie about opd entries.  This case occurs
15463                              when building shared libraries and we
15464                              reference a function in another shared
15465                              lib.  The same thing happens for a weak
15466                              definition in an application that's
15467                              overridden by a strong definition in a
15468                              shared lib.  (I believe this is a generic
15469                              bug in binutils handling of weak syms.)
15470                              In these cases we won't use the opd
15471                              entry in this lib.  */
15472                           unresolved_reloc = FALSE;
15473                         }
15474                       if (!is_opd
15475                           && r_type == R_PPC64_ADDR64
15476                           && (h != NULL
15477                               ? h->elf.type == STT_GNU_IFUNC
15478                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15479                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15480                       else
15481                         {
15482                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15483
15484                           /* We need to relocate .opd contents for ld.so.
15485                              Prelink also wants simple and consistent rules
15486                              for relocs.  This make all RELATIVE relocs have
15487                              *r_offset equal to r_addend.  */
15488                           relocate = TRUE;
15489                         }
15490                     }
15491                   else
15492                     {
15493                       if (h != NULL
15494                           ? h->elf.type == STT_GNU_IFUNC
15495                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15496                         {
15497                           info->callbacks->einfo
15498                             /* xgettext:c-format */
15499                             (_("%H: %s for indirect "
15500                                "function `%pT' unsupported\n"),
15501                              input_bfd, input_section, rel->r_offset,
15502                              ppc64_elf_howto_table[r_type]->name,
15503                              sym_name);
15504                           ret = FALSE;
15505                         }
15506                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
15507                         ;
15508                       else if (sec == NULL || sec->owner == NULL)
15509                         {
15510                           bfd_set_error (bfd_error_bad_value);
15511                           return FALSE;
15512                         }
15513                       else
15514                         {
15515                           asection *osec;
15516
15517                           osec = sec->output_section;
15518                           indx = elf_section_data (osec)->dynindx;
15519
15520                           if (indx == 0)
15521                             {
15522                               if ((osec->flags & SEC_READONLY) == 0
15523                                   && htab->elf.data_index_section != NULL)
15524                                 osec = htab->elf.data_index_section;
15525                               else
15526                                 osec = htab->elf.text_index_section;
15527                               indx = elf_section_data (osec)->dynindx;
15528                             }
15529                           BFD_ASSERT (indx != 0);
15530
15531                           /* We are turning this relocation into one
15532                              against a section symbol, so subtract out
15533                              the output section's address but not the
15534                              offset of the input section in the output
15535                              section.  */
15536                           outrel.r_addend -= osec->vma;
15537                         }
15538
15539                       outrel.r_info = ELF64_R_INFO (indx, r_type);
15540                     }
15541                 }
15542
15543               sreloc = elf_section_data (input_section)->sreloc;
15544               if (h != NULL
15545                   ? h->elf.type == STT_GNU_IFUNC
15546                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15547                 {
15548                   sreloc = htab->elf.irelplt;
15549                   if (indx == 0)
15550                     htab->local_ifunc_resolver = 1;
15551                   else if (is_static_defined (&h->elf))
15552                     htab->maybe_local_ifunc_resolver = 1;
15553                 }
15554               if (sreloc == NULL)
15555                 abort ();
15556
15557               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15558                   >= sreloc->size)
15559                 abort ();
15560               loc = sreloc->contents;
15561               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
15562               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15563
15564               /* If this reloc is against an external symbol, it will
15565                  be computed at runtime, so there's no need to do
15566                  anything now.  However, for the sake of prelink ensure
15567                  that the section contents are a known value.  */
15568               if (! relocate)
15569                 {
15570                   unresolved_reloc = FALSE;
15571                   /* The value chosen here is quite arbitrary as ld.so
15572                      ignores section contents except for the special
15573                      case of .opd where the contents might be accessed
15574                      before relocation.  Choose zero, as that won't
15575                      cause reloc overflow.  */
15576                   relocation = 0;
15577                   addend = 0;
15578                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15579                      to improve backward compatibility with older
15580                      versions of ld.  */
15581                   if (r_type == R_PPC64_ADDR64)
15582                     addend = outrel.r_addend;
15583                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
15584                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
15585                     addend = outrel.r_offset;
15586                 }
15587             }
15588           break;
15589
15590         case R_PPC64_COPY:
15591         case R_PPC64_GLOB_DAT:
15592         case R_PPC64_JMP_SLOT:
15593         case R_PPC64_JMP_IREL:
15594         case R_PPC64_RELATIVE:
15595           /* We shouldn't ever see these dynamic relocs in relocatable
15596              files.  */
15597           /* Fall through.  */
15598
15599         case R_PPC64_PLTGOT16:
15600         case R_PPC64_PLTGOT16_DS:
15601         case R_PPC64_PLTGOT16_HA:
15602         case R_PPC64_PLTGOT16_HI:
15603         case R_PPC64_PLTGOT16_LO:
15604         case R_PPC64_PLTGOT16_LO_DS:
15605         case R_PPC64_PLTREL32:
15606         case R_PPC64_PLTREL64:
15607           /* These ones haven't been implemented yet.  */
15608
15609           info->callbacks->einfo
15610             /* xgettext:c-format */
15611             (_("%P: %pB: %s is not supported for `%pT'\n"),
15612              input_bfd,
15613              ppc64_elf_howto_table[r_type]->name, sym_name);
15614
15615           bfd_set_error (bfd_error_invalid_operation);
15616           ret = FALSE;
15617           goto copy_reloc;
15618         }
15619
15620       /* Multi-instruction sequences that access the TOC can be
15621          optimized, eg. addis ra,r2,0; addi rb,ra,x;
15622          to             nop;           addi rb,r2,x;  */
15623       switch (r_type)
15624         {
15625         default:
15626           break;
15627
15628         case R_PPC64_GOT_TLSLD16_HI:
15629         case R_PPC64_GOT_TLSGD16_HI:
15630         case R_PPC64_GOT_TPREL16_HI:
15631         case R_PPC64_GOT_DTPREL16_HI:
15632         case R_PPC64_GOT16_HI:
15633         case R_PPC64_TOC16_HI:
15634           /* These relocs would only be useful if building up an
15635              offset to later add to r2, perhaps in an indexed
15636              addressing mode instruction.  Don't try to optimize.
15637              Unfortunately, the possibility of someone building up an
15638              offset like this or even with the HA relocs, means that
15639              we need to check the high insn when optimizing the low
15640              insn.  */
15641           break;
15642
15643         case R_PPC64_PLTCALL:
15644           if (unresolved_reloc)
15645             {
15646               /* No plt entry.  Make this into a direct call.  */
15647               bfd_byte *p = contents + rel->r_offset;
15648               insn = bfd_get_32 (input_bfd, p);
15649               insn &= 1;
15650               bfd_put_32 (input_bfd, B_DOT | insn, p);
15651               bfd_put_32 (input_bfd, NOP, p + 4);
15652               unresolved_reloc = save_unresolved_reloc;
15653               r_type = R_PPC64_REL24;
15654             }
15655           break;
15656
15657         case R_PPC64_PLTSEQ:
15658           if (unresolved_reloc)
15659             {
15660               unresolved_reloc = FALSE;
15661               goto nop_it;
15662             }
15663           break;
15664
15665         case R_PPC64_PLT16_HA:
15666           if (unresolved_reloc)
15667             {
15668               unresolved_reloc = FALSE;
15669               goto nop_it;
15670             }
15671           /* Fall through.  */
15672         case R_PPC64_GOT_TLSLD16_HA:
15673         case R_PPC64_GOT_TLSGD16_HA:
15674         case R_PPC64_GOT_TPREL16_HA:
15675         case R_PPC64_GOT_DTPREL16_HA:
15676         case R_PPC64_GOT16_HA:
15677         case R_PPC64_TOC16_HA:
15678           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15679               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15680             {
15681               bfd_byte *p;
15682             nop_it:
15683               p = contents + (rel->r_offset & ~3);
15684               bfd_put_32 (input_bfd, NOP, p);
15685               goto copy_reloc;
15686             }
15687           break;
15688
15689         case R_PPC64_PLT16_LO:
15690         case R_PPC64_PLT16_LO_DS:
15691           if (unresolved_reloc)
15692             {
15693               unresolved_reloc = FALSE;
15694               goto nop_it;
15695             }
15696           /* Fall through.  */
15697         case R_PPC64_GOT_TLSLD16_LO:
15698         case R_PPC64_GOT_TLSGD16_LO:
15699         case R_PPC64_GOT_TPREL16_LO_DS:
15700         case R_PPC64_GOT_DTPREL16_LO_DS:
15701         case R_PPC64_GOT16_LO:
15702         case R_PPC64_GOT16_LO_DS:
15703         case R_PPC64_TOC16_LO:
15704         case R_PPC64_TOC16_LO_DS:
15705           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15706               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15707             {
15708               bfd_byte *p = contents + (rel->r_offset & ~3);
15709               insn = bfd_get_32 (input_bfd, p);
15710               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15711                 {
15712                   /* Transform addic to addi when we change reg.  */
15713                   insn &= ~((0x3f << 26) | (0x1f << 16));
15714                   insn |= (14u << 26) | (2 << 16);
15715                 }
15716               else
15717                 {
15718                   insn &= ~(0x1f << 16);
15719                   insn |= 2 << 16;
15720                 }
15721               bfd_put_32 (input_bfd, insn, p);
15722             }
15723           break;
15724
15725         case R_PPC64_TPREL16_HA:
15726           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15727             {
15728               bfd_byte *p = contents + (rel->r_offset & ~3);
15729               insn = bfd_get_32 (input_bfd, p);
15730               if ((insn & ((0x3f << 26) | 0x1f << 16))
15731                   != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15732                 /* xgettext:c-format */
15733                 info->callbacks->minfo
15734                   (_("%H: warning: %s unexpected insn %#x.\n"),
15735                    input_bfd, input_section, rel->r_offset,
15736                    ppc64_elf_howto_table[r_type]->name, insn);
15737               else
15738                 {
15739                   bfd_put_32 (input_bfd, NOP, p);
15740                   goto copy_reloc;
15741                 }
15742             }
15743           break;
15744
15745         case R_PPC64_TPREL16_LO:
15746         case R_PPC64_TPREL16_LO_DS:
15747           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15748             {
15749               bfd_byte *p = contents + (rel->r_offset & ~3);
15750               insn = bfd_get_32 (input_bfd, p);
15751               insn &= ~(0x1f << 16);
15752               insn |= 13 << 16;
15753               bfd_put_32 (input_bfd, insn, p);
15754             }
15755           break;
15756         }
15757
15758       /* Do any further special processing.  */
15759       switch (r_type)
15760         {
15761         default:
15762           break;
15763
15764         case R_PPC64_REL16_HA:
15765         case R_PPC64_REL16DX_HA:
15766         case R_PPC64_ADDR16_HA:
15767         case R_PPC64_ADDR16_HIGHA:
15768         case R_PPC64_ADDR16_HIGHERA:
15769         case R_PPC64_ADDR16_HIGHESTA:
15770         case R_PPC64_TOC16_HA:
15771         case R_PPC64_SECTOFF_HA:
15772         case R_PPC64_TPREL16_HA:
15773         case R_PPC64_TPREL16_HIGHA:
15774         case R_PPC64_TPREL16_HIGHERA:
15775         case R_PPC64_TPREL16_HIGHESTA:
15776         case R_PPC64_DTPREL16_HA:
15777         case R_PPC64_DTPREL16_HIGHA:
15778         case R_PPC64_DTPREL16_HIGHERA:
15779         case R_PPC64_DTPREL16_HIGHESTA:
15780           /* It's just possible that this symbol is a weak symbol
15781              that's not actually defined anywhere. In that case,
15782              'sec' would be NULL, and we should leave the symbol
15783              alone (it will be set to zero elsewhere in the link).  */
15784           if (sec == NULL)
15785             break;
15786           /* Fall through.  */
15787
15788         case R_PPC64_GOT16_HA:
15789         case R_PPC64_PLTGOT16_HA:
15790         case R_PPC64_PLT16_HA:
15791         case R_PPC64_GOT_TLSGD16_HA:
15792         case R_PPC64_GOT_TLSLD16_HA:
15793         case R_PPC64_GOT_TPREL16_HA:
15794         case R_PPC64_GOT_DTPREL16_HA:
15795           /* Add 0x10000 if sign bit in 0:15 is set.
15796              Bits 0:15 are not used.  */
15797           addend += 0x8000;
15798           break;
15799
15800         case R_PPC64_ADDR16_DS:
15801         case R_PPC64_ADDR16_LO_DS:
15802         case R_PPC64_GOT16_DS:
15803         case R_PPC64_GOT16_LO_DS:
15804         case R_PPC64_PLT16_LO_DS:
15805         case R_PPC64_SECTOFF_DS:
15806         case R_PPC64_SECTOFF_LO_DS:
15807         case R_PPC64_TOC16_DS:
15808         case R_PPC64_TOC16_LO_DS:
15809         case R_PPC64_PLTGOT16_DS:
15810         case R_PPC64_PLTGOT16_LO_DS:
15811         case R_PPC64_GOT_TPREL16_DS:
15812         case R_PPC64_GOT_TPREL16_LO_DS:
15813         case R_PPC64_GOT_DTPREL16_DS:
15814         case R_PPC64_GOT_DTPREL16_LO_DS:
15815         case R_PPC64_TPREL16_DS:
15816         case R_PPC64_TPREL16_LO_DS:
15817         case R_PPC64_DTPREL16_DS:
15818         case R_PPC64_DTPREL16_LO_DS:
15819           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15820           mask = 3;
15821           /* If this reloc is against an lq, lxv, or stxv insn, then
15822              the value must be a multiple of 16.  This is somewhat of
15823              a hack, but the "correct" way to do this by defining _DQ
15824              forms of all the _DS relocs bloats all reloc switches in
15825              this file.  It doesn't make much sense to use these
15826              relocs in data, so testing the insn should be safe.  */
15827           if ((insn & (0x3f << 26)) == (56u << 26)
15828               || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15829             mask = 15;
15830           relocation += addend;
15831           addend = insn & (mask ^ 3);
15832           if ((relocation & mask) != 0)
15833             {
15834               relocation ^= relocation & mask;
15835               info->callbacks->einfo
15836                 /* xgettext:c-format */
15837                 (_("%H: error: %s not a multiple of %u\n"),
15838                  input_bfd, input_section, rel->r_offset,
15839                  ppc64_elf_howto_table[r_type]->name,
15840                  mask + 1);
15841               bfd_set_error (bfd_error_bad_value);
15842               ret = FALSE;
15843               goto copy_reloc;
15844             }
15845           break;
15846         }
15847
15848       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15849          because such sections are not SEC_ALLOC and thus ld.so will
15850          not process them.  */
15851       howto = ppc64_elf_howto_table[(int) r_type];
15852       if (unresolved_reloc
15853           && !((input_section->flags & SEC_DEBUGGING) != 0
15854                && h->elf.def_dynamic)
15855           && _bfd_elf_section_offset (output_bfd, info, input_section,
15856                                       rel->r_offset) != (bfd_vma) -1)
15857         {
15858           info->callbacks->einfo
15859             /* xgettext:c-format */
15860             (_("%H: unresolvable %s against `%pT'\n"),
15861              input_bfd, input_section, rel->r_offset,
15862              howto->name,
15863              h->elf.root.root.string);
15864           ret = FALSE;
15865         }
15866
15867       /* 16-bit fields in insns mostly have signed values, but a
15868          few insns have 16-bit unsigned values.  Really, we should
15869          have different reloc types.  */
15870       if (howto->complain_on_overflow != complain_overflow_dont
15871           && howto->dst_mask == 0xffff
15872           && (input_section->flags & SEC_CODE) != 0)
15873         {
15874           enum complain_overflow complain = complain_overflow_signed;
15875
15876           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15877           if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15878             complain = complain_overflow_bitfield;
15879           else if (howto->rightshift == 0
15880                    ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15881                       || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15882                       || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15883                    : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15884                       || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15885                       || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15886             complain = complain_overflow_unsigned;
15887           if (howto->complain_on_overflow != complain)
15888             {
15889               alt_howto = *howto;
15890               alt_howto.complain_on_overflow = complain;
15891               howto = &alt_howto;
15892             }
15893         }
15894
15895       if (r_type == R_PPC64_REL16DX_HA)
15896         {
15897           /* Split field reloc isn't handled by _bfd_final_link_relocate.  */
15898           if (rel->r_offset + 4 > input_section->size)
15899             r = bfd_reloc_outofrange;
15900           else
15901             {
15902               relocation += addend;
15903               relocation -= (rel->r_offset
15904                              + input_section->output_offset
15905                              + input_section->output_section->vma);
15906               relocation = (bfd_signed_vma) relocation >> 16;
15907               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15908               insn &= ~0x1fffc1;
15909               insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15910               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15911               r = bfd_reloc_ok;
15912               if (relocation + 0x8000 > 0xffff)
15913                 r = bfd_reloc_overflow;
15914             }
15915         }
15916       else
15917         r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15918                                       rel->r_offset, relocation, addend);
15919
15920       if (r != bfd_reloc_ok)
15921         {
15922           char *more_info = NULL;
15923           const char *reloc_name = howto->name;
15924
15925           if (reloc_dest != DEST_NORMAL)
15926             {
15927               more_info = bfd_malloc (strlen (reloc_name) + 8);
15928               if (more_info != NULL)
15929                 {
15930                   strcpy (more_info, reloc_name);
15931                   strcat (more_info, (reloc_dest == DEST_OPD
15932                                       ? " (OPD)" : " (stub)"));
15933                   reloc_name = more_info;
15934                 }
15935             }
15936
15937           if (r == bfd_reloc_overflow)
15938             {
15939               /* On code like "if (foo) foo();" don't report overflow
15940                  on a branch to zero when foo is undefined.  */
15941               if (!warned
15942                   && (reloc_dest == DEST_STUB
15943                       || !(h != NULL
15944                            && (h->elf.root.type == bfd_link_hash_undefweak
15945                                || h->elf.root.type == bfd_link_hash_undefined)
15946                            && is_branch_reloc (r_type))))
15947                 info->callbacks->reloc_overflow (info, &h->elf.root,
15948                                                  sym_name, reloc_name,
15949                                                  orig_rel.r_addend,
15950                                                  input_bfd, input_section,
15951                                                  rel->r_offset);
15952             }
15953           else
15954             {
15955               info->callbacks->einfo
15956                 /* xgettext:c-format */
15957                 (_("%H: %s against `%pT': error %d\n"),
15958                  input_bfd, input_section, rel->r_offset,
15959                  reloc_name, sym_name, (int) r);
15960               ret = FALSE;
15961             }
15962           if (more_info != NULL)
15963             free (more_info);
15964         }
15965     copy_reloc:
15966       if (wrel != rel)
15967         *wrel = *rel;
15968     }
15969
15970   if (wrel != rel)
15971     {
15972       Elf_Internal_Shdr *rel_hdr;
15973       size_t deleted = rel - wrel;
15974
15975       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15976       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15977       if (rel_hdr->sh_size == 0)
15978         {
15979           /* It is too late to remove an empty reloc section.  Leave
15980              one NONE reloc.
15981              ??? What is wrong with an empty section???  */
15982           rel_hdr->sh_size = rel_hdr->sh_entsize;
15983           deleted -= 1;
15984         }
15985       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15986       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15987       input_section->reloc_count -= deleted;
15988     }
15989
15990   /* If we're emitting relocations, then shortly after this function
15991      returns, reloc offsets and addends for this section will be
15992      adjusted.  Worse, reloc symbol indices will be for the output
15993      file rather than the input.  Save a copy of the relocs for
15994      opd_entry_value.  */
15995   if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15996     {
15997       bfd_size_type amt;
15998       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15999       rel = bfd_alloc (input_bfd, amt);
16000       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16001       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
16002       if (rel == NULL)
16003         return FALSE;
16004       memcpy (rel, relocs, amt);
16005     }
16006   return ret;
16007 }
16008
16009 /* Adjust the value of any local symbols in opd sections.  */
16010
16011 static int
16012 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16013                               const char *name ATTRIBUTE_UNUSED,
16014                               Elf_Internal_Sym *elfsym,
16015                               asection *input_sec,
16016                               struct elf_link_hash_entry *h)
16017 {
16018   struct _opd_sec_data *opd;
16019   long adjust;
16020   bfd_vma value;
16021
16022   if (h != NULL)
16023     return 1;
16024
16025   opd = get_opd_info (input_sec);
16026   if (opd == NULL || opd->adjust == NULL)
16027     return 1;
16028
16029   value = elfsym->st_value - input_sec->output_offset;
16030   if (!bfd_link_relocatable (info))
16031     value -= input_sec->output_section->vma;
16032
16033   adjust = opd->adjust[OPD_NDX (value)];
16034   if (adjust == -1)
16035     return 2;
16036
16037   elfsym->st_value += adjust;
16038   return 1;
16039 }
16040
16041 /* Finish up dynamic symbol handling.  We set the contents of various
16042    dynamic sections here.  */
16043
16044 static bfd_boolean
16045 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16046                                  struct bfd_link_info *info,
16047                                  struct elf_link_hash_entry *h,
16048                                  Elf_Internal_Sym *sym)
16049 {
16050   struct ppc_link_hash_table *htab;
16051   struct plt_entry *ent;
16052
16053   htab = ppc_hash_table (info);
16054   if (htab == NULL)
16055     return FALSE;
16056
16057   if (!htab->opd_abi && !h->def_regular)
16058     for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16059       if (ent->plt.offset != (bfd_vma) -1)
16060         {
16061           /* Mark the symbol as undefined, rather than as
16062              defined in glink.  Leave the value if there were
16063              any relocations where pointer equality matters
16064              (this is a clue for the dynamic linker, to make
16065              function pointer comparisons work between an
16066              application and shared library), otherwise set it
16067              to zero.  */
16068           sym->st_shndx = SHN_UNDEF;
16069           if (!h->pointer_equality_needed)
16070             sym->st_value = 0;
16071           else if (!h->ref_regular_nonweak)
16072             {
16073               /* This breaks function pointer comparisons, but
16074                  that is better than breaking tests for a NULL
16075                  function pointer.  */
16076               sym->st_value = 0;
16077             }
16078           break;
16079         }
16080
16081   if (h->needs_copy)
16082     {
16083       /* This symbol needs a copy reloc.  Set it up.  */
16084       Elf_Internal_Rela rela;
16085       asection *srel;
16086       bfd_byte *loc;
16087
16088       if (h->dynindx == -1
16089           || (h->root.type != bfd_link_hash_defined
16090               && h->root.type != bfd_link_hash_defweak)
16091           || htab->elf.srelbss == NULL
16092           || htab->elf.sreldynrelro == NULL)
16093         abort ();
16094
16095       rela.r_offset = (h->root.u.def.value
16096                        + h->root.u.def.section->output_section->vma
16097                        + h->root.u.def.section->output_offset);
16098       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16099       rela.r_addend = 0;
16100       if (h->root.u.def.section == htab->elf.sdynrelro)
16101         srel = htab->elf.sreldynrelro;
16102       else
16103         srel = htab->elf.srelbss;
16104       loc = srel->contents;
16105       loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
16106       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
16107     }
16108
16109   return TRUE;
16110 }
16111
16112 /* Used to decide how to sort relocs in an optimal manner for the
16113    dynamic linker, before writing them out.  */
16114
16115 static enum elf_reloc_type_class
16116 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16117                             const asection *rel_sec,
16118                             const Elf_Internal_Rela *rela)
16119 {
16120   enum elf_ppc64_reloc_type r_type;
16121   struct ppc_link_hash_table *htab = ppc_hash_table (info);
16122
16123   if (rel_sec == htab->elf.irelplt)
16124     return reloc_class_ifunc;
16125
16126   r_type = ELF64_R_TYPE (rela->r_info);
16127   switch (r_type)
16128     {
16129     case R_PPC64_RELATIVE:
16130       return reloc_class_relative;
16131     case R_PPC64_JMP_SLOT:
16132       return reloc_class_plt;
16133     case R_PPC64_COPY:
16134       return reloc_class_copy;
16135     default:
16136       return reloc_class_normal;
16137     }
16138 }
16139
16140 /* Finish up the dynamic sections.  */
16141
16142 static bfd_boolean
16143 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16144                                    struct bfd_link_info *info)
16145 {
16146   struct ppc_link_hash_table *htab;
16147   bfd *dynobj;
16148   asection *sdyn;
16149
16150   htab = ppc_hash_table (info);
16151   if (htab == NULL)
16152     return FALSE;
16153
16154   dynobj = htab->elf.dynobj;
16155   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
16156
16157   if (htab->elf.dynamic_sections_created)
16158     {
16159       Elf64_External_Dyn *dyncon, *dynconend;
16160
16161       if (sdyn == NULL || htab->elf.sgot == NULL)
16162         abort ();
16163
16164       dyncon = (Elf64_External_Dyn *) sdyn->contents;
16165       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
16166       for (; dyncon < dynconend; dyncon++)
16167         {
16168           Elf_Internal_Dyn dyn;
16169           asection *s;
16170
16171           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16172
16173           switch (dyn.d_tag)
16174             {
16175             default:
16176               continue;
16177
16178             case DT_PPC64_GLINK:
16179               s = htab->glink;
16180               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16181               /* We stupidly defined DT_PPC64_GLINK to be the start
16182                  of glink rather than the first entry point, which is
16183                  what ld.so needs, and now have a bigger stub to
16184                  support automatic multiple TOCs.  */
16185               dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
16186               break;
16187
16188             case DT_PPC64_OPD:
16189               s = bfd_get_section_by_name (output_bfd, ".opd");
16190               if (s == NULL)
16191                 continue;
16192               dyn.d_un.d_ptr = s->vma;
16193               break;
16194
16195             case DT_PPC64_OPT:
16196               if (htab->do_multi_toc && htab->multi_toc_needed)
16197                 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
16198               if (htab->has_plt_localentry0)
16199                 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
16200               break;
16201
16202             case DT_PPC64_OPDSZ:
16203               s = bfd_get_section_by_name (output_bfd, ".opd");
16204               if (s == NULL)
16205                 continue;
16206               dyn.d_un.d_val = s->size;
16207               break;
16208
16209             case DT_PLTGOT:
16210               s = htab->elf.splt;
16211               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16212               break;
16213
16214             case DT_JMPREL:
16215               s = htab->elf.srelplt;
16216               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16217               break;
16218
16219             case DT_PLTRELSZ:
16220               dyn.d_un.d_val = htab->elf.srelplt->size;
16221               break;
16222
16223             case DT_TEXTREL:
16224               if (htab->local_ifunc_resolver)
16225                 info->callbacks->einfo
16226                   (_("%X%P: text relocations and GNU indirect "
16227                      "functions will result in a segfault at runtime\n"));
16228               else if (htab->maybe_local_ifunc_resolver)
16229                 info->callbacks->einfo
16230                   (_("%P: warning: text relocations and GNU indirect "
16231                      "functions may result in a segfault at runtime\n"));
16232               continue;
16233             }
16234
16235           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
16236         }
16237     }
16238
16239   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16240       && htab->elf.sgot->output_section != bfd_abs_section_ptr)
16241     {
16242       /* Fill in the first entry in the global offset table.
16243          We use it to hold the link-time TOCbase.  */
16244       bfd_put_64 (output_bfd,
16245                   elf_gp (output_bfd) + TOC_BASE_OFF,
16246                   htab->elf.sgot->contents);
16247
16248       /* Set .got entry size.  */
16249       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
16250     }
16251
16252   if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16253       && htab->elf.splt->output_section != bfd_abs_section_ptr)
16254     {
16255       /* Set .plt entry size.  */
16256       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
16257         = PLT_ENTRY_SIZE (htab);
16258     }
16259
16260   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16261      brlt ourselves if emitrelocations.  */
16262   if (htab->brlt != NULL
16263       && htab->brlt->reloc_count != 0
16264       && !_bfd_elf_link_output_relocs (output_bfd,
16265                                        htab->brlt,
16266                                        elf_section_data (htab->brlt)->rela.hdr,
16267                                        elf_section_data (htab->brlt)->relocs,
16268                                        NULL))
16269     return FALSE;
16270
16271   if (htab->glink != NULL
16272       && htab->glink->reloc_count != 0
16273       && !_bfd_elf_link_output_relocs (output_bfd,
16274                                        htab->glink,
16275                                        elf_section_data (htab->glink)->rela.hdr,
16276                                        elf_section_data (htab->glink)->relocs,
16277                                        NULL))
16278     return FALSE;
16279
16280   if (htab->glink_eh_frame != NULL
16281       && htab->glink_eh_frame->size != 0)
16282     {
16283       bfd_vma val;
16284       bfd_byte *p;
16285       struct map_stub *group;
16286       size_t align = 4;
16287
16288       p = htab->glink_eh_frame->contents;
16289       p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
16290
16291       for (group = htab->group; group != NULL; group = group->next)
16292         if (group->stub_sec != NULL)
16293           {
16294             /* Offset to stub section.  */
16295             val = (group->stub_sec->output_section->vma
16296                    + group->stub_sec->output_offset);
16297             val -= (htab->glink_eh_frame->output_section->vma
16298                     + htab->glink_eh_frame->output_offset
16299                     + (p + 8 - htab->glink_eh_frame->contents));
16300             if (val + 0x80000000 > 0xffffffff)
16301               {
16302                 _bfd_error_handler
16303                   (_("%s offset too large for .eh_frame sdata4 encoding"),
16304                    group->stub_sec->name);
16305                 return FALSE;
16306               }
16307             bfd_put_32 (dynobj, val, p + 8);
16308             p += stub_eh_frame_size (group, align);
16309           }
16310       if (htab->glink != NULL && htab->glink->size != 0)
16311         {
16312           /* Offset to .glink.  */
16313           val = (htab->glink->output_section->vma
16314                  + htab->glink->output_offset
16315                  + 8);
16316           val -= (htab->glink_eh_frame->output_section->vma
16317                   + htab->glink_eh_frame->output_offset
16318                   + (p + 8 - htab->glink_eh_frame->contents));
16319           if (val + 0x80000000 > 0xffffffff)
16320             {
16321               _bfd_error_handler
16322                 (_("%s offset too large for .eh_frame sdata4 encoding"),
16323                  htab->glink->name);
16324               return FALSE;
16325             }
16326           bfd_put_32 (dynobj, val, p + 8);
16327           p += (24 + align - 1) & -align;
16328         }
16329
16330       if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16331           && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16332                                                htab->glink_eh_frame,
16333                                                htab->glink_eh_frame->contents))
16334         return FALSE;
16335     }
16336
16337   /* We need to handle writing out multiple GOT sections ourselves,
16338      since we didn't add them to DYNOBJ.  We know dynobj is the first
16339      bfd.  */
16340   while ((dynobj = dynobj->link.next) != NULL)
16341     {
16342       asection *s;
16343
16344       if (!is_ppc64_elf (dynobj))
16345         continue;
16346
16347       s = ppc64_elf_tdata (dynobj)->got;
16348       if (s != NULL
16349           && s->size != 0
16350           && s->output_section != bfd_abs_section_ptr
16351           && !bfd_set_section_contents (output_bfd, s->output_section,
16352                                         s->contents, s->output_offset,
16353                                         s->size))
16354         return FALSE;
16355       s = ppc64_elf_tdata (dynobj)->relgot;
16356       if (s != NULL
16357           && s->size != 0
16358           && s->output_section != bfd_abs_section_ptr
16359           && !bfd_set_section_contents (output_bfd, s->output_section,
16360                                         s->contents, s->output_offset,
16361                                         s->size))
16362         return FALSE;
16363     }
16364
16365   return TRUE;
16366 }
16367
16368 #include "elf64-target.h"
16369
16370 /* FreeBSD support */
16371
16372 #undef  TARGET_LITTLE_SYM
16373 #undef  TARGET_LITTLE_NAME
16374
16375 #undef  TARGET_BIG_SYM
16376 #define TARGET_BIG_SYM  powerpc_elf64_fbsd_vec
16377 #undef  TARGET_BIG_NAME
16378 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16379
16380 #undef  ELF_OSABI
16381 #define ELF_OSABI       ELFOSABI_FREEBSD
16382
16383 #undef  elf64_bed
16384 #define elf64_bed       elf64_powerpc_fbsd_bed
16385
16386 #include "elf64-target.h"