PR 20472, PowerPC64 ifunc confusion
[external/binutils.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2    Copyright (C) 1999-2016 Free Software Foundation, Inc.
3    Written by Linus Nordberg, Swox AB <info@swox.com>,
4    based on elf32-ppc.c by Ian Lance Taylor.
5    Largely rewritten by Alan Modra.
6
7    This file is part of BFD, the Binary File Descriptor library.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License along
20    with this program; if not, write to the Free Software Foundation, Inc.,
21    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
22
23
24 /* The 64-bit PowerPC ELF ABI may be found at
25    http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26    http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
27
28 #include "sysdep.h"
29 #include <stdarg.h>
30 #include "bfd.h"
31 #include "bfdlink.h"
32 #include "libbfd.h"
33 #include "elf-bfd.h"
34 #include "elf/ppc64.h"
35 #include "elf64-ppc.h"
36 #include "dwarf2.h"
37
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
55   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_vma opd_entry_value
57   (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
58
59 #define TARGET_LITTLE_SYM       powerpc_elf64_le_vec
60 #define TARGET_LITTLE_NAME      "elf64-powerpcle"
61 #define TARGET_BIG_SYM          powerpc_elf64_vec
62 #define TARGET_BIG_NAME         "elf64-powerpc"
63 #define ELF_ARCH                bfd_arch_powerpc
64 #define ELF_TARGET_ID           PPC64_ELF_DATA
65 #define ELF_MACHINE_CODE        EM_PPC64
66 #define ELF_MAXPAGESIZE         0x10000
67 #define ELF_COMMONPAGESIZE      0x10000
68 #define elf_info_to_howto       ppc64_elf_info_to_howto
69
70 #define elf_backend_want_got_sym 0
71 #define elf_backend_want_plt_sym 0
72 #define elf_backend_plt_alignment 3
73 #define elf_backend_plt_not_loaded 1
74 #define elf_backend_got_header_size 8
75 #define elf_backend_can_gc_sections 1
76 #define elf_backend_can_refcount 1
77 #define elf_backend_rela_normal 1
78 #define elf_backend_default_execstack 0
79
80 #define bfd_elf64_mkobject                    ppc64_elf_mkobject
81 #define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
82 #define bfd_elf64_bfd_reloc_name_lookup       ppc64_elf_reloc_name_lookup
83 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
84 #define bfd_elf64_bfd_print_private_bfd_data  ppc64_elf_print_private_bfd_data
85 #define bfd_elf64_new_section_hook            ppc64_elf_new_section_hook
86 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
87 #define bfd_elf64_get_synthetic_symtab        ppc64_elf_get_synthetic_symtab
88 #define bfd_elf64_bfd_link_just_syms          ppc64_elf_link_just_syms
89
90 #define elf_backend_object_p                  ppc64_elf_object_p
91 #define elf_backend_grok_prstatus             ppc64_elf_grok_prstatus
92 #define elf_backend_grok_psinfo               ppc64_elf_grok_psinfo
93 #define elf_backend_write_core_note           ppc64_elf_write_core_note
94 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
95 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
96 #define elf_backend_add_symbol_hook           ppc64_elf_add_symbol_hook
97 #define elf_backend_check_directives          ppc64_elf_before_check_relocs
98 #define elf_backend_notice_as_needed          ppc64_elf_notice_as_needed
99 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
100 #define elf_backend_check_relocs              ppc64_elf_check_relocs
101 #define elf_backend_gc_keep                   ppc64_elf_gc_keep
102 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
103 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
104 #define elf_backend_gc_sweep_hook             ppc64_elf_gc_sweep_hook
105 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
106 #define elf_backend_hide_symbol               ppc64_elf_hide_symbol
107 #define elf_backend_maybe_function_sym        ppc64_elf_maybe_function_sym
108 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
109 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
110 #define elf_backend_hash_symbol               ppc64_elf_hash_symbol
111 #define elf_backend_init_index_section        _bfd_elf_init_2_index_sections
112 #define elf_backend_action_discarded          ppc64_elf_action_discarded
113 #define elf_backend_relocate_section          ppc64_elf_relocate_section
114 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
115 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
116 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
117 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
118 #define elf_backend_special_sections          ppc64_elf_special_sections
119 #define elf_backend_merge_symbol_attribute    ppc64_elf_merge_symbol_attribute
120
121 /* The name of the dynamic interpreter.  This is put in the .interp
122    section.  */
123 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
124
125 /* The size in bytes of an entry in the procedure linkage table.  */
126 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
127
128 /* The initial size of the plt reserved for the dynamic linker.  */
129 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
130
131 /* Offsets to some stack save slots.  */
132 #define STK_LR 16
133 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
134 /* This one is dodgy.  ELFv2 does not have a linker word, so use the
135    CR save slot.  Used only by optimised __tls_get_addr call stub,
136    relying on __tls_get_addr_opt not saving CR..  */
137 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
138
139 /* TOC base pointers offset from start of TOC.  */
140 #define TOC_BASE_OFF    0x8000
141 /* TOC base alignment.  */
142 #define TOC_BASE_ALIGN  256
143
144 /* Offset of tp and dtp pointers from start of TLS block.  */
145 #define TP_OFFSET       0x7000
146 #define DTP_OFFSET      0x8000
147
148 /* .plt call stub instructions.  The normal stub is like this, but
149    sometimes the .plt entry crosses a 64k boundary and we need to
150    insert an addi to adjust r11.  */
151 #define STD_R2_0R1      0xf8410000      /* std   %r2,0+40(%r1)       */
152 #define ADDIS_R11_R2    0x3d620000      /* addis %r11,%r2,xxx@ha     */
153 #define LD_R12_0R11     0xe98b0000      /* ld    %r12,xxx+0@l(%r11)  */
154 #define MTCTR_R12       0x7d8903a6      /* mtctr %r12                */
155 #define LD_R2_0R11      0xe84b0000      /* ld    %r2,xxx+8@l(%r11)   */
156 #define LD_R11_0R11     0xe96b0000      /* ld    %r11,xxx+16@l(%r11) */
157 #define BCTR            0x4e800420      /* bctr                      */
158
159 #define ADDI_R11_R11    0x396b0000      /* addi %r11,%r11,off@l  */
160 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
161 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
162
163 #define XOR_R2_R12_R12  0x7d826278      /* xor   %r2,%r12,%r12   */
164 #define ADD_R11_R11_R2  0x7d6b1214      /* add   %r11,%r11,%r2   */
165 #define XOR_R11_R12_R12 0x7d8b6278      /* xor   %r11,%r12,%r12  */
166 #define ADD_R2_R2_R11   0x7c425a14      /* add   %r2,%r2,%r11    */
167 #define CMPLDI_R2_0     0x28220000      /* cmpldi %r2,0          */
168 #define BNECTR          0x4ca20420      /* bnectr+               */
169 #define BNECTR_P4       0x4ce20420      /* bnectr+               */
170
171 #define LD_R12_0R2      0xe9820000      /* ld    %r12,xxx+0(%r2) */
172 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
173 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
174
175 #define LD_R2_0R1       0xe8410000      /* ld    %r2,0(%r1)      */
176 #define LD_R2_0R12      0xe84c0000      /* ld    %r2,0(%r12)     */
177 #define ADD_R2_R2_R12   0x7c426214      /* add   %r2,%r2,%r12    */
178
179 #define LIS_R2          0x3c400000      /* lis %r2,xxx@ha         */
180 #define ADDIS_R2_R12    0x3c4c0000      /* addis %r2,%r12,xxx@ha  */
181 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha  */
182 #define ADDIS_R12_R12   0x3d8c0000      /* addis %r12,%r12,xxx@ha */
183 #define LD_R12_0R12     0xe98c0000      /* ld    %r12,xxx@l(%r12) */
184
185 /* glink call stub instructions.  We enter with the index in R0.  */
186 #define GLINK_CALL_STUB_SIZE (16*4)
187                                         /* 0:                           */
188                                         /*  .quad plt0-1f               */
189                                         /* __glink:                     */
190 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
191 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
192                                         /* 1:                           */
193 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
194                                         /*  ld %2,(0b-1b)(%11)          */
195 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
196 #define ADD_R11_R2_R11  0x7d625a14      /*  add %11,%2,%11              */
197                                         /*  ld %12,0(%11)               */
198                                         /*  ld %2,8(%11)                */
199                                         /*  mtctr %12                   */
200                                         /*  ld %11,16(%11)              */
201                                         /*  bctr                        */
202 #define MFLR_R0         0x7c0802a6      /*  mflr %r0                    */
203 #define MTLR_R0         0x7c0803a6      /*  mtlr %r0                    */
204 #define SUB_R12_R12_R11 0x7d8b6050      /*  subf %r12,%r11,%r12         */
205 #define ADDI_R0_R12     0x380c0000      /*  addi %r0,%r12,0             */
206 #define SRDI_R0_R0_2    0x7800f082      /*  rldicl %r0,%r0,62,2         */
207
208 /* Pad with this.  */
209 #define NOP             0x60000000
210
211 /* Some other nops.  */
212 #define CROR_151515     0x4def7b82
213 #define CROR_313131     0x4ffffb82
214
215 /* .glink entries for the first 32k functions are two instructions.  */
216 #define LI_R0_0         0x38000000      /* li    %r0,0          */
217 #define B_DOT           0x48000000      /* b     .              */
218
219 /* After that, we need two instructions to load the index, followed by
220    a branch.  */
221 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
222 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
223
224 /* Instructions used by the save and restore reg functions.  */
225 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
226 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
227 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
228 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
229 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
230 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
231 #define LI_R12_0        0x39800000      /* li    %r12,0         */
232 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
233 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
234 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
235 #define BLR             0x4e800020      /* blr                  */
236
237 /* Since .opd is an array of descriptors and each entry will end up
238    with identical R_PPC64_RELATIVE relocs, there is really no need to
239    propagate .opd relocs;  The dynamic linker should be taught to
240    relocate .opd without reloc entries.  */
241 #ifndef NO_OPD_RELOCS
242 #define NO_OPD_RELOCS 0
243 #endif
244
245 #ifndef ARRAY_SIZE
246 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
247 #endif
248
249 static inline int
250 abiversion (bfd *abfd)
251 {
252   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
253 }
254
255 static inline void
256 set_abiversion (bfd *abfd, int ver)
257 {
258   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
259   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
260 }
261 \f
262 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
263
264 /* Relocation HOWTO's.  */
265 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
266
267 static reloc_howto_type ppc64_elf_howto_raw[] = {
268   /* This reloc does nothing.  */
269   HOWTO (R_PPC64_NONE,          /* type */
270          0,                     /* rightshift */
271          3,                     /* size (0 = byte, 1 = short, 2 = long) */
272          0,                     /* bitsize */
273          FALSE,                 /* pc_relative */
274          0,                     /* bitpos */
275          complain_overflow_dont, /* complain_on_overflow */
276          bfd_elf_generic_reloc, /* special_function */
277          "R_PPC64_NONE",        /* name */
278          FALSE,                 /* partial_inplace */
279          0,                     /* src_mask */
280          0,                     /* dst_mask */
281          FALSE),                /* pcrel_offset */
282
283   /* A standard 32 bit relocation.  */
284   HOWTO (R_PPC64_ADDR32,        /* type */
285          0,                     /* rightshift */
286          2,                     /* size (0 = byte, 1 = short, 2 = long) */
287          32,                    /* bitsize */
288          FALSE,                 /* pc_relative */
289          0,                     /* bitpos */
290          complain_overflow_bitfield, /* complain_on_overflow */
291          bfd_elf_generic_reloc, /* special_function */
292          "R_PPC64_ADDR32",      /* name */
293          FALSE,                 /* partial_inplace */
294          0,                     /* src_mask */
295          0xffffffff,            /* dst_mask */
296          FALSE),                /* pcrel_offset */
297
298   /* An absolute 26 bit branch; the lower two bits must be zero.
299      FIXME: we don't check that, we just clear them.  */
300   HOWTO (R_PPC64_ADDR24,        /* type */
301          0,                     /* rightshift */
302          2,                     /* size (0 = byte, 1 = short, 2 = long) */
303          26,                    /* bitsize */
304          FALSE,                 /* pc_relative */
305          0,                     /* bitpos */
306          complain_overflow_bitfield, /* complain_on_overflow */
307          bfd_elf_generic_reloc, /* special_function */
308          "R_PPC64_ADDR24",      /* name */
309          FALSE,                 /* partial_inplace */
310          0,                     /* src_mask */
311          0x03fffffc,            /* dst_mask */
312          FALSE),                /* pcrel_offset */
313
314   /* A standard 16 bit relocation.  */
315   HOWTO (R_PPC64_ADDR16,        /* type */
316          0,                     /* rightshift */
317          1,                     /* size (0 = byte, 1 = short, 2 = long) */
318          16,                    /* bitsize */
319          FALSE,                 /* pc_relative */
320          0,                     /* bitpos */
321          complain_overflow_bitfield, /* complain_on_overflow */
322          bfd_elf_generic_reloc, /* special_function */
323          "R_PPC64_ADDR16",      /* name */
324          FALSE,                 /* partial_inplace */
325          0,                     /* src_mask */
326          0xffff,                /* dst_mask */
327          FALSE),                /* pcrel_offset */
328
329   /* A 16 bit relocation without overflow.  */
330   HOWTO (R_PPC64_ADDR16_LO,     /* type */
331          0,                     /* rightshift */
332          1,                     /* size (0 = byte, 1 = short, 2 = long) */
333          16,                    /* bitsize */
334          FALSE,                 /* pc_relative */
335          0,                     /* bitpos */
336          complain_overflow_dont,/* complain_on_overflow */
337          bfd_elf_generic_reloc, /* special_function */
338          "R_PPC64_ADDR16_LO",   /* name */
339          FALSE,                 /* partial_inplace */
340          0,                     /* src_mask */
341          0xffff,                /* dst_mask */
342          FALSE),                /* pcrel_offset */
343
344   /* Bits 16-31 of an address.  */
345   HOWTO (R_PPC64_ADDR16_HI,     /* type */
346          16,                    /* rightshift */
347          1,                     /* size (0 = byte, 1 = short, 2 = long) */
348          16,                    /* bitsize */
349          FALSE,                 /* pc_relative */
350          0,                     /* bitpos */
351          complain_overflow_signed, /* complain_on_overflow */
352          bfd_elf_generic_reloc, /* special_function */
353          "R_PPC64_ADDR16_HI",   /* name */
354          FALSE,                 /* partial_inplace */
355          0,                     /* src_mask */
356          0xffff,                /* dst_mask */
357          FALSE),                /* pcrel_offset */
358
359   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
360      bits, treated as a signed number, is negative.  */
361   HOWTO (R_PPC64_ADDR16_HA,     /* type */
362          16,                    /* rightshift */
363          1,                     /* size (0 = byte, 1 = short, 2 = long) */
364          16,                    /* bitsize */
365          FALSE,                 /* pc_relative */
366          0,                     /* bitpos */
367          complain_overflow_signed, /* complain_on_overflow */
368          ppc64_elf_ha_reloc,    /* special_function */
369          "R_PPC64_ADDR16_HA",   /* name */
370          FALSE,                 /* partial_inplace */
371          0,                     /* src_mask */
372          0xffff,                /* dst_mask */
373          FALSE),                /* pcrel_offset */
374
375   /* An absolute 16 bit branch; the lower two bits must be zero.
376      FIXME: we don't check that, we just clear them.  */
377   HOWTO (R_PPC64_ADDR14,        /* type */
378          0,                     /* rightshift */
379          2,                     /* size (0 = byte, 1 = short, 2 = long) */
380          16,                    /* bitsize */
381          FALSE,                 /* pc_relative */
382          0,                     /* bitpos */
383          complain_overflow_signed, /* complain_on_overflow */
384          ppc64_elf_branch_reloc, /* special_function */
385          "R_PPC64_ADDR14",      /* name */
386          FALSE,                 /* partial_inplace */
387          0,                     /* src_mask */
388          0x0000fffc,            /* dst_mask */
389          FALSE),                /* pcrel_offset */
390
391   /* An absolute 16 bit branch, for which bit 10 should be set to
392      indicate that the branch is expected to be taken.  The lower two
393      bits must be zero.  */
394   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
395          0,                     /* rightshift */
396          2,                     /* size (0 = byte, 1 = short, 2 = long) */
397          16,                    /* bitsize */
398          FALSE,                 /* pc_relative */
399          0,                     /* bitpos */
400          complain_overflow_signed, /* complain_on_overflow */
401          ppc64_elf_brtaken_reloc, /* special_function */
402          "R_PPC64_ADDR14_BRTAKEN",/* name */
403          FALSE,                 /* partial_inplace */
404          0,                     /* src_mask */
405          0x0000fffc,            /* dst_mask */
406          FALSE),                /* pcrel_offset */
407
408   /* An absolute 16 bit branch, for which bit 10 should be set to
409      indicate that the branch is not expected to be taken.  The lower
410      two bits must be zero.  */
411   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
412          0,                     /* rightshift */
413          2,                     /* size (0 = byte, 1 = short, 2 = long) */
414          16,                    /* bitsize */
415          FALSE,                 /* pc_relative */
416          0,                     /* bitpos */
417          complain_overflow_signed, /* complain_on_overflow */
418          ppc64_elf_brtaken_reloc, /* special_function */
419          "R_PPC64_ADDR14_BRNTAKEN",/* name */
420          FALSE,                 /* partial_inplace */
421          0,                     /* src_mask */
422          0x0000fffc,            /* dst_mask */
423          FALSE),                /* pcrel_offset */
424
425   /* A relative 26 bit branch; the lower two bits must be zero.  */
426   HOWTO (R_PPC64_REL24,         /* type */
427          0,                     /* rightshift */
428          2,                     /* size (0 = byte, 1 = short, 2 = long) */
429          26,                    /* bitsize */
430          TRUE,                  /* pc_relative */
431          0,                     /* bitpos */
432          complain_overflow_signed, /* complain_on_overflow */
433          ppc64_elf_branch_reloc, /* special_function */
434          "R_PPC64_REL24",       /* name */
435          FALSE,                 /* partial_inplace */
436          0,                     /* src_mask */
437          0x03fffffc,            /* dst_mask */
438          TRUE),                 /* pcrel_offset */
439
440   /* A relative 16 bit branch; the lower two bits must be zero.  */
441   HOWTO (R_PPC64_REL14,         /* type */
442          0,                     /* rightshift */
443          2,                     /* size (0 = byte, 1 = short, 2 = long) */
444          16,                    /* bitsize */
445          TRUE,                  /* pc_relative */
446          0,                     /* bitpos */
447          complain_overflow_signed, /* complain_on_overflow */
448          ppc64_elf_branch_reloc, /* special_function */
449          "R_PPC64_REL14",       /* name */
450          FALSE,                 /* partial_inplace */
451          0,                     /* src_mask */
452          0x0000fffc,            /* dst_mask */
453          TRUE),                 /* pcrel_offset */
454
455   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
456      the branch is expected to be taken.  The lower two bits must be
457      zero.  */
458   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
459          0,                     /* rightshift */
460          2,                     /* size (0 = byte, 1 = short, 2 = long) */
461          16,                    /* bitsize */
462          TRUE,                  /* pc_relative */
463          0,                     /* bitpos */
464          complain_overflow_signed, /* complain_on_overflow */
465          ppc64_elf_brtaken_reloc, /* special_function */
466          "R_PPC64_REL14_BRTAKEN", /* name */
467          FALSE,                 /* partial_inplace */
468          0,                     /* src_mask */
469          0x0000fffc,            /* dst_mask */
470          TRUE),                 /* pcrel_offset */
471
472   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
473      the branch is not expected to be taken.  The lower two bits must
474      be zero.  */
475   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
476          0,                     /* rightshift */
477          2,                     /* size (0 = byte, 1 = short, 2 = long) */
478          16,                    /* bitsize */
479          TRUE,                  /* pc_relative */
480          0,                     /* bitpos */
481          complain_overflow_signed, /* complain_on_overflow */
482          ppc64_elf_brtaken_reloc, /* special_function */
483          "R_PPC64_REL14_BRNTAKEN",/* name */
484          FALSE,                 /* partial_inplace */
485          0,                     /* src_mask */
486          0x0000fffc,            /* dst_mask */
487          TRUE),                 /* pcrel_offset */
488
489   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
490      symbol.  */
491   HOWTO (R_PPC64_GOT16,         /* type */
492          0,                     /* rightshift */
493          1,                     /* size (0 = byte, 1 = short, 2 = long) */
494          16,                    /* bitsize */
495          FALSE,                 /* pc_relative */
496          0,                     /* bitpos */
497          complain_overflow_signed, /* complain_on_overflow */
498          ppc64_elf_unhandled_reloc, /* special_function */
499          "R_PPC64_GOT16",       /* name */
500          FALSE,                 /* partial_inplace */
501          0,                     /* src_mask */
502          0xffff,                /* dst_mask */
503          FALSE),                /* pcrel_offset */
504
505   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
506      the symbol.  */
507   HOWTO (R_PPC64_GOT16_LO,      /* type */
508          0,                     /* rightshift */
509          1,                     /* size (0 = byte, 1 = short, 2 = long) */
510          16,                    /* bitsize */
511          FALSE,                 /* pc_relative */
512          0,                     /* bitpos */
513          complain_overflow_dont, /* complain_on_overflow */
514          ppc64_elf_unhandled_reloc, /* special_function */
515          "R_PPC64_GOT16_LO",    /* name */
516          FALSE,                 /* partial_inplace */
517          0,                     /* src_mask */
518          0xffff,                /* dst_mask */
519          FALSE),                /* pcrel_offset */
520
521   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
522      the symbol.  */
523   HOWTO (R_PPC64_GOT16_HI,      /* type */
524          16,                    /* rightshift */
525          1,                     /* size (0 = byte, 1 = short, 2 = long) */
526          16,                    /* bitsize */
527          FALSE,                 /* pc_relative */
528          0,                     /* bitpos */
529          complain_overflow_signed,/* complain_on_overflow */
530          ppc64_elf_unhandled_reloc, /* special_function */
531          "R_PPC64_GOT16_HI",    /* name */
532          FALSE,                 /* partial_inplace */
533          0,                     /* src_mask */
534          0xffff,                /* dst_mask */
535          FALSE),                /* pcrel_offset */
536
537   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
538      the symbol.  */
539   HOWTO (R_PPC64_GOT16_HA,      /* type */
540          16,                    /* rightshift */
541          1,                     /* size (0 = byte, 1 = short, 2 = long) */
542          16,                    /* bitsize */
543          FALSE,                 /* pc_relative */
544          0,                     /* bitpos */
545          complain_overflow_signed,/* complain_on_overflow */
546          ppc64_elf_unhandled_reloc, /* special_function */
547          "R_PPC64_GOT16_HA",    /* name */
548          FALSE,                 /* partial_inplace */
549          0,                     /* src_mask */
550          0xffff,                /* dst_mask */
551          FALSE),                /* pcrel_offset */
552
553   /* This is used only by the dynamic linker.  The symbol should exist
554      both in the object being run and in some shared library.  The
555      dynamic linker copies the data addressed by the symbol from the
556      shared library into the object, because the object being
557      run has to have the data at some particular address.  */
558   HOWTO (R_PPC64_COPY,          /* type */
559          0,                     /* rightshift */
560          0,                     /* this one is variable size */
561          0,                     /* bitsize */
562          FALSE,                 /* pc_relative */
563          0,                     /* bitpos */
564          complain_overflow_dont, /* complain_on_overflow */
565          ppc64_elf_unhandled_reloc, /* special_function */
566          "R_PPC64_COPY",        /* name */
567          FALSE,                 /* partial_inplace */
568          0,                     /* src_mask */
569          0,                     /* dst_mask */
570          FALSE),                /* pcrel_offset */
571
572   /* Like R_PPC64_ADDR64, but used when setting global offset table
573      entries.  */
574   HOWTO (R_PPC64_GLOB_DAT,      /* type */
575          0,                     /* rightshift */
576          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
577          64,                    /* bitsize */
578          FALSE,                 /* pc_relative */
579          0,                     /* bitpos */
580          complain_overflow_dont, /* complain_on_overflow */
581          ppc64_elf_unhandled_reloc,  /* special_function */
582          "R_PPC64_GLOB_DAT",    /* name */
583          FALSE,                 /* partial_inplace */
584          0,                     /* src_mask */
585          ONES (64),             /* dst_mask */
586          FALSE),                /* pcrel_offset */
587
588   /* Created by the link editor.  Marks a procedure linkage table
589      entry for a symbol.  */
590   HOWTO (R_PPC64_JMP_SLOT,      /* type */
591          0,                     /* rightshift */
592          0,                     /* size (0 = byte, 1 = short, 2 = long) */
593          0,                     /* bitsize */
594          FALSE,                 /* pc_relative */
595          0,                     /* bitpos */
596          complain_overflow_dont, /* complain_on_overflow */
597          ppc64_elf_unhandled_reloc, /* special_function */
598          "R_PPC64_JMP_SLOT",    /* name */
599          FALSE,                 /* partial_inplace */
600          0,                     /* src_mask */
601          0,                     /* dst_mask */
602          FALSE),                /* pcrel_offset */
603
604   /* Used only by the dynamic linker.  When the object is run, this
605      doubleword64 is set to the load address of the object, plus the
606      addend.  */
607   HOWTO (R_PPC64_RELATIVE,      /* type */
608          0,                     /* rightshift */
609          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
610          64,                    /* bitsize */
611          FALSE,                 /* pc_relative */
612          0,                     /* bitpos */
613          complain_overflow_dont, /* complain_on_overflow */
614          bfd_elf_generic_reloc, /* special_function */
615          "R_PPC64_RELATIVE",    /* name */
616          FALSE,                 /* partial_inplace */
617          0,                     /* src_mask */
618          ONES (64),             /* dst_mask */
619          FALSE),                /* pcrel_offset */
620
621   /* Like R_PPC64_ADDR32, but may be unaligned.  */
622   HOWTO (R_PPC64_UADDR32,       /* type */
623          0,                     /* rightshift */
624          2,                     /* size (0 = byte, 1 = short, 2 = long) */
625          32,                    /* bitsize */
626          FALSE,                 /* pc_relative */
627          0,                     /* bitpos */
628          complain_overflow_bitfield, /* complain_on_overflow */
629          bfd_elf_generic_reloc, /* special_function */
630          "R_PPC64_UADDR32",     /* name */
631          FALSE,                 /* partial_inplace */
632          0,                     /* src_mask */
633          0xffffffff,            /* dst_mask */
634          FALSE),                /* pcrel_offset */
635
636   /* Like R_PPC64_ADDR16, but may be unaligned.  */
637   HOWTO (R_PPC64_UADDR16,       /* type */
638          0,                     /* rightshift */
639          1,                     /* size (0 = byte, 1 = short, 2 = long) */
640          16,                    /* bitsize */
641          FALSE,                 /* pc_relative */
642          0,                     /* bitpos */
643          complain_overflow_bitfield, /* complain_on_overflow */
644          bfd_elf_generic_reloc, /* special_function */
645          "R_PPC64_UADDR16",     /* name */
646          FALSE,                 /* partial_inplace */
647          0,                     /* src_mask */
648          0xffff,                /* dst_mask */
649          FALSE),                /* pcrel_offset */
650
651   /* 32-bit PC relative.  */
652   HOWTO (R_PPC64_REL32,         /* type */
653          0,                     /* rightshift */
654          2,                     /* size (0 = byte, 1 = short, 2 = long) */
655          32,                    /* bitsize */
656          TRUE,                  /* pc_relative */
657          0,                     /* bitpos */
658          complain_overflow_signed, /* complain_on_overflow */
659          bfd_elf_generic_reloc, /* special_function */
660          "R_PPC64_REL32",       /* name */
661          FALSE,                 /* partial_inplace */
662          0,                     /* src_mask */
663          0xffffffff,            /* dst_mask */
664          TRUE),                 /* pcrel_offset */
665
666   /* 32-bit relocation to the symbol's procedure linkage table.  */
667   HOWTO (R_PPC64_PLT32,         /* type */
668          0,                     /* rightshift */
669          2,                     /* size (0 = byte, 1 = short, 2 = long) */
670          32,                    /* bitsize */
671          FALSE,                 /* pc_relative */
672          0,                     /* bitpos */
673          complain_overflow_bitfield, /* complain_on_overflow */
674          ppc64_elf_unhandled_reloc, /* special_function */
675          "R_PPC64_PLT32",       /* name */
676          FALSE,                 /* partial_inplace */
677          0,                     /* src_mask */
678          0xffffffff,            /* dst_mask */
679          FALSE),                /* pcrel_offset */
680
681   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
682      FIXME: R_PPC64_PLTREL32 not supported.  */
683   HOWTO (R_PPC64_PLTREL32,      /* type */
684          0,                     /* rightshift */
685          2,                     /* size (0 = byte, 1 = short, 2 = long) */
686          32,                    /* bitsize */
687          TRUE,                  /* pc_relative */
688          0,                     /* bitpos */
689          complain_overflow_signed, /* complain_on_overflow */
690          ppc64_elf_unhandled_reloc, /* special_function */
691          "R_PPC64_PLTREL32",    /* name */
692          FALSE,                 /* partial_inplace */
693          0,                     /* src_mask */
694          0xffffffff,            /* dst_mask */
695          TRUE),                 /* pcrel_offset */
696
697   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
698      the symbol.  */
699   HOWTO (R_PPC64_PLT16_LO,      /* type */
700          0,                     /* rightshift */
701          1,                     /* size (0 = byte, 1 = short, 2 = long) */
702          16,                    /* bitsize */
703          FALSE,                 /* pc_relative */
704          0,                     /* bitpos */
705          complain_overflow_dont, /* complain_on_overflow */
706          ppc64_elf_unhandled_reloc, /* special_function */
707          "R_PPC64_PLT16_LO",    /* name */
708          FALSE,                 /* partial_inplace */
709          0,                     /* src_mask */
710          0xffff,                /* dst_mask */
711          FALSE),                /* pcrel_offset */
712
713   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
714      the symbol.  */
715   HOWTO (R_PPC64_PLT16_HI,      /* type */
716          16,                    /* rightshift */
717          1,                     /* size (0 = byte, 1 = short, 2 = long) */
718          16,                    /* bitsize */
719          FALSE,                 /* pc_relative */
720          0,                     /* bitpos */
721          complain_overflow_signed, /* complain_on_overflow */
722          ppc64_elf_unhandled_reloc, /* special_function */
723          "R_PPC64_PLT16_HI",    /* name */
724          FALSE,                 /* partial_inplace */
725          0,                     /* src_mask */
726          0xffff,                /* dst_mask */
727          FALSE),                /* pcrel_offset */
728
729   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
730      the symbol.  */
731   HOWTO (R_PPC64_PLT16_HA,      /* type */
732          16,                    /* rightshift */
733          1,                     /* size (0 = byte, 1 = short, 2 = long) */
734          16,                    /* bitsize */
735          FALSE,                 /* pc_relative */
736          0,                     /* bitpos */
737          complain_overflow_signed, /* complain_on_overflow */
738          ppc64_elf_unhandled_reloc, /* special_function */
739          "R_PPC64_PLT16_HA",    /* name */
740          FALSE,                 /* partial_inplace */
741          0,                     /* src_mask */
742          0xffff,                /* dst_mask */
743          FALSE),                /* pcrel_offset */
744
745   /* 16-bit section relative relocation.  */
746   HOWTO (R_PPC64_SECTOFF,       /* type */
747          0,                     /* rightshift */
748          1,                     /* size (0 = byte, 1 = short, 2 = long) */
749          16,                    /* bitsize */
750          FALSE,                 /* pc_relative */
751          0,                     /* bitpos */
752          complain_overflow_signed, /* complain_on_overflow */
753          ppc64_elf_sectoff_reloc, /* special_function */
754          "R_PPC64_SECTOFF",     /* name */
755          FALSE,                 /* partial_inplace */
756          0,                     /* src_mask */
757          0xffff,                /* dst_mask */
758          FALSE),                /* pcrel_offset */
759
760   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
761   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
762          0,                     /* rightshift */
763          1,                     /* size (0 = byte, 1 = short, 2 = long) */
764          16,                    /* bitsize */
765          FALSE,                 /* pc_relative */
766          0,                     /* bitpos */
767          complain_overflow_dont, /* complain_on_overflow */
768          ppc64_elf_sectoff_reloc, /* special_function */
769          "R_PPC64_SECTOFF_LO",  /* name */
770          FALSE,                 /* partial_inplace */
771          0,                     /* src_mask */
772          0xffff,                /* dst_mask */
773          FALSE),                /* pcrel_offset */
774
775   /* 16-bit upper half section relative relocation.  */
776   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
777          16,                    /* rightshift */
778          1,                     /* size (0 = byte, 1 = short, 2 = long) */
779          16,                    /* bitsize */
780          FALSE,                 /* pc_relative */
781          0,                     /* bitpos */
782          complain_overflow_signed, /* complain_on_overflow */
783          ppc64_elf_sectoff_reloc, /* special_function */
784          "R_PPC64_SECTOFF_HI",  /* name */
785          FALSE,                 /* partial_inplace */
786          0,                     /* src_mask */
787          0xffff,                /* dst_mask */
788          FALSE),                /* pcrel_offset */
789
790   /* 16-bit upper half adjusted section relative relocation.  */
791   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
792          16,                    /* rightshift */
793          1,                     /* size (0 = byte, 1 = short, 2 = long) */
794          16,                    /* bitsize */
795          FALSE,                 /* pc_relative */
796          0,                     /* bitpos */
797          complain_overflow_signed, /* complain_on_overflow */
798          ppc64_elf_sectoff_ha_reloc, /* special_function */
799          "R_PPC64_SECTOFF_HA",  /* name */
800          FALSE,                 /* partial_inplace */
801          0,                     /* src_mask */
802          0xffff,                /* dst_mask */
803          FALSE),                /* pcrel_offset */
804
805   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
806   HOWTO (R_PPC64_REL30,         /* type */
807          2,                     /* rightshift */
808          2,                     /* size (0 = byte, 1 = short, 2 = long) */
809          30,                    /* bitsize */
810          TRUE,                  /* pc_relative */
811          0,                     /* bitpos */
812          complain_overflow_dont, /* complain_on_overflow */
813          bfd_elf_generic_reloc, /* special_function */
814          "R_PPC64_REL30",       /* name */
815          FALSE,                 /* partial_inplace */
816          0,                     /* src_mask */
817          0xfffffffc,            /* dst_mask */
818          TRUE),                 /* pcrel_offset */
819
820   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
821
822   /* A standard 64-bit relocation.  */
823   HOWTO (R_PPC64_ADDR64,        /* type */
824          0,                     /* rightshift */
825          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
826          64,                    /* bitsize */
827          FALSE,                 /* pc_relative */
828          0,                     /* bitpos */
829          complain_overflow_dont, /* complain_on_overflow */
830          bfd_elf_generic_reloc, /* special_function */
831          "R_PPC64_ADDR64",      /* name */
832          FALSE,                 /* partial_inplace */
833          0,                     /* src_mask */
834          ONES (64),             /* dst_mask */
835          FALSE),                /* pcrel_offset */
836
837   /* The bits 32-47 of an address.  */
838   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
839          32,                    /* rightshift */
840          1,                     /* size (0 = byte, 1 = short, 2 = long) */
841          16,                    /* bitsize */
842          FALSE,                 /* pc_relative */
843          0,                     /* bitpos */
844          complain_overflow_dont, /* complain_on_overflow */
845          bfd_elf_generic_reloc, /* special_function */
846          "R_PPC64_ADDR16_HIGHER", /* name */
847          FALSE,                 /* partial_inplace */
848          0,                     /* src_mask */
849          0xffff,                /* dst_mask */
850          FALSE),                /* pcrel_offset */
851
852   /* The bits 32-47 of an address, plus 1 if the contents of the low
853      16 bits, treated as a signed number, is negative.  */
854   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
855          32,                    /* rightshift */
856          1,                     /* size (0 = byte, 1 = short, 2 = long) */
857          16,                    /* bitsize */
858          FALSE,                 /* pc_relative */
859          0,                     /* bitpos */
860          complain_overflow_dont, /* complain_on_overflow */
861          ppc64_elf_ha_reloc,    /* special_function */
862          "R_PPC64_ADDR16_HIGHERA", /* name */
863          FALSE,                 /* partial_inplace */
864          0,                     /* src_mask */
865          0xffff,                /* dst_mask */
866          FALSE),                /* pcrel_offset */
867
868   /* The bits 48-63 of an address.  */
869   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
870          48,                    /* rightshift */
871          1,                     /* size (0 = byte, 1 = short, 2 = long) */
872          16,                    /* bitsize */
873          FALSE,                 /* pc_relative */
874          0,                     /* bitpos */
875          complain_overflow_dont, /* complain_on_overflow */
876          bfd_elf_generic_reloc, /* special_function */
877          "R_PPC64_ADDR16_HIGHEST", /* name */
878          FALSE,                 /* partial_inplace */
879          0,                     /* src_mask */
880          0xffff,                /* dst_mask */
881          FALSE),                /* pcrel_offset */
882
883   /* The bits 48-63 of an address, plus 1 if the contents of the low
884      16 bits, treated as a signed number, is negative.  */
885   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
886          48,                    /* rightshift */
887          1,                     /* size (0 = byte, 1 = short, 2 = long) */
888          16,                    /* bitsize */
889          FALSE,                 /* pc_relative */
890          0,                     /* bitpos */
891          complain_overflow_dont, /* complain_on_overflow */
892          ppc64_elf_ha_reloc,    /* special_function */
893          "R_PPC64_ADDR16_HIGHESTA", /* name */
894          FALSE,                 /* partial_inplace */
895          0,                     /* src_mask */
896          0xffff,                /* dst_mask */
897          FALSE),                /* pcrel_offset */
898
899   /* Like ADDR64, but may be unaligned.  */
900   HOWTO (R_PPC64_UADDR64,       /* type */
901          0,                     /* rightshift */
902          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
903          64,                    /* bitsize */
904          FALSE,                 /* pc_relative */
905          0,                     /* bitpos */
906          complain_overflow_dont, /* complain_on_overflow */
907          bfd_elf_generic_reloc, /* special_function */
908          "R_PPC64_UADDR64",     /* name */
909          FALSE,                 /* partial_inplace */
910          0,                     /* src_mask */
911          ONES (64),             /* dst_mask */
912          FALSE),                /* pcrel_offset */
913
914   /* 64-bit relative relocation.  */
915   HOWTO (R_PPC64_REL64,         /* type */
916          0,                     /* rightshift */
917          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
918          64,                    /* bitsize */
919          TRUE,                  /* pc_relative */
920          0,                     /* bitpos */
921          complain_overflow_dont, /* complain_on_overflow */
922          bfd_elf_generic_reloc, /* special_function */
923          "R_PPC64_REL64",       /* name */
924          FALSE,                 /* partial_inplace */
925          0,                     /* src_mask */
926          ONES (64),             /* dst_mask */
927          TRUE),                 /* pcrel_offset */
928
929   /* 64-bit relocation to the symbol's procedure linkage table.  */
930   HOWTO (R_PPC64_PLT64,         /* type */
931          0,                     /* rightshift */
932          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
933          64,                    /* bitsize */
934          FALSE,                 /* pc_relative */
935          0,                     /* bitpos */
936          complain_overflow_dont, /* complain_on_overflow */
937          ppc64_elf_unhandled_reloc, /* special_function */
938          "R_PPC64_PLT64",       /* name */
939          FALSE,                 /* partial_inplace */
940          0,                     /* src_mask */
941          ONES (64),             /* dst_mask */
942          FALSE),                /* pcrel_offset */
943
944   /* 64-bit PC relative relocation to the symbol's procedure linkage
945      table.  */
946   /* FIXME: R_PPC64_PLTREL64 not supported.  */
947   HOWTO (R_PPC64_PLTREL64,      /* type */
948          0,                     /* rightshift */
949          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
950          64,                    /* bitsize */
951          TRUE,                  /* pc_relative */
952          0,                     /* bitpos */
953          complain_overflow_dont, /* complain_on_overflow */
954          ppc64_elf_unhandled_reloc, /* special_function */
955          "R_PPC64_PLTREL64",    /* name */
956          FALSE,                 /* partial_inplace */
957          0,                     /* src_mask */
958          ONES (64),             /* dst_mask */
959          TRUE),                 /* pcrel_offset */
960
961   /* 16 bit TOC-relative relocation.  */
962
963   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
964   HOWTO (R_PPC64_TOC16,         /* type */
965          0,                     /* rightshift */
966          1,                     /* size (0 = byte, 1 = short, 2 = long) */
967          16,                    /* bitsize */
968          FALSE,                 /* pc_relative */
969          0,                     /* bitpos */
970          complain_overflow_signed, /* complain_on_overflow */
971          ppc64_elf_toc_reloc,   /* special_function */
972          "R_PPC64_TOC16",       /* name */
973          FALSE,                 /* partial_inplace */
974          0,                     /* src_mask */
975          0xffff,                /* dst_mask */
976          FALSE),                /* pcrel_offset */
977
978   /* 16 bit TOC-relative relocation without overflow.  */
979
980   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
981   HOWTO (R_PPC64_TOC16_LO,      /* type */
982          0,                     /* rightshift */
983          1,                     /* size (0 = byte, 1 = short, 2 = long) */
984          16,                    /* bitsize */
985          FALSE,                 /* pc_relative */
986          0,                     /* bitpos */
987          complain_overflow_dont, /* complain_on_overflow */
988          ppc64_elf_toc_reloc,   /* special_function */
989          "R_PPC64_TOC16_LO",    /* name */
990          FALSE,                 /* partial_inplace */
991          0,                     /* src_mask */
992          0xffff,                /* dst_mask */
993          FALSE),                /* pcrel_offset */
994
995   /* 16 bit TOC-relative relocation, high 16 bits.  */
996
997   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
998   HOWTO (R_PPC64_TOC16_HI,      /* type */
999          16,                    /* rightshift */
1000          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1001          16,                    /* bitsize */
1002          FALSE,                 /* pc_relative */
1003          0,                     /* bitpos */
1004          complain_overflow_signed, /* complain_on_overflow */
1005          ppc64_elf_toc_reloc,   /* special_function */
1006          "R_PPC64_TOC16_HI",    /* name */
1007          FALSE,                 /* partial_inplace */
1008          0,                     /* src_mask */
1009          0xffff,                /* dst_mask */
1010          FALSE),                /* pcrel_offset */
1011
1012   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1013      contents of the low 16 bits, treated as a signed number, is
1014      negative.  */
1015
1016   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
1017   HOWTO (R_PPC64_TOC16_HA,      /* type */
1018          16,                    /* rightshift */
1019          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1020          16,                    /* bitsize */
1021          FALSE,                 /* pc_relative */
1022          0,                     /* bitpos */
1023          complain_overflow_signed, /* complain_on_overflow */
1024          ppc64_elf_toc_ha_reloc, /* special_function */
1025          "R_PPC64_TOC16_HA",    /* name */
1026          FALSE,                 /* partial_inplace */
1027          0,                     /* src_mask */
1028          0xffff,                /* dst_mask */
1029          FALSE),                /* pcrel_offset */
1030
1031   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
1032
1033   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
1034   HOWTO (R_PPC64_TOC,           /* type */
1035          0,                     /* rightshift */
1036          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1037          64,                    /* bitsize */
1038          FALSE,                 /* pc_relative */
1039          0,                     /* bitpos */
1040          complain_overflow_dont, /* complain_on_overflow */
1041          ppc64_elf_toc64_reloc, /* special_function */
1042          "R_PPC64_TOC",         /* name */
1043          FALSE,                 /* partial_inplace */
1044          0,                     /* src_mask */
1045          ONES (64),             /* dst_mask */
1046          FALSE),                /* pcrel_offset */
1047
1048   /* Like R_PPC64_GOT16, but also informs the link editor that the
1049      value to relocate may (!) refer to a PLT entry which the link
1050      editor (a) may replace with the symbol value.  If the link editor
1051      is unable to fully resolve the symbol, it may (b) create a PLT
1052      entry and store the address to the new PLT entry in the GOT.
1053      This permits lazy resolution of function symbols at run time.
1054      The link editor may also skip all of this and just (c) emit a
1055      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
1056   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
1057     HOWTO (R_PPC64_PLTGOT16,    /* type */
1058          0,                     /* rightshift */
1059          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1060          16,                    /* bitsize */
1061          FALSE,                 /* pc_relative */
1062          0,                     /* bitpos */
1063          complain_overflow_signed, /* complain_on_overflow */
1064          ppc64_elf_unhandled_reloc, /* special_function */
1065          "R_PPC64_PLTGOT16",    /* name */
1066          FALSE,                 /* partial_inplace */
1067          0,                     /* src_mask */
1068          0xffff,                /* dst_mask */
1069          FALSE),                /* pcrel_offset */
1070
1071   /* Like R_PPC64_PLTGOT16, but without overflow.  */
1072   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1073   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1074          0,                     /* rightshift */
1075          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1076          16,                    /* bitsize */
1077          FALSE,                 /* pc_relative */
1078          0,                     /* bitpos */
1079          complain_overflow_dont, /* complain_on_overflow */
1080          ppc64_elf_unhandled_reloc, /* special_function */
1081          "R_PPC64_PLTGOT16_LO", /* name */
1082          FALSE,                 /* partial_inplace */
1083          0,                     /* src_mask */
1084          0xffff,                /* dst_mask */
1085          FALSE),                /* pcrel_offset */
1086
1087   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1088   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1089   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1090          16,                    /* rightshift */
1091          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1092          16,                    /* bitsize */
1093          FALSE,                 /* pc_relative */
1094          0,                     /* bitpos */
1095          complain_overflow_signed, /* complain_on_overflow */
1096          ppc64_elf_unhandled_reloc, /* special_function */
1097          "R_PPC64_PLTGOT16_HI", /* name */
1098          FALSE,                 /* partial_inplace */
1099          0,                     /* src_mask */
1100          0xffff,                /* dst_mask */
1101          FALSE),                /* pcrel_offset */
1102
1103   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1104      1 if the contents of the low 16 bits, treated as a signed number,
1105      is negative.  */
1106   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1107   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1108          16,                    /* rightshift */
1109          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1110          16,                    /* bitsize */
1111          FALSE,                 /* pc_relative */
1112          0,                     /* bitpos */
1113          complain_overflow_signed, /* complain_on_overflow */
1114          ppc64_elf_unhandled_reloc, /* special_function */
1115          "R_PPC64_PLTGOT16_HA", /* name */
1116          FALSE,                 /* partial_inplace */
1117          0,                     /* src_mask */
1118          0xffff,                /* dst_mask */
1119          FALSE),                /* pcrel_offset */
1120
1121   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1122   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1123          0,                     /* rightshift */
1124          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1125          16,                    /* bitsize */
1126          FALSE,                 /* pc_relative */
1127          0,                     /* bitpos */
1128          complain_overflow_signed, /* complain_on_overflow */
1129          bfd_elf_generic_reloc, /* special_function */
1130          "R_PPC64_ADDR16_DS",   /* name */
1131          FALSE,                 /* partial_inplace */
1132          0,                     /* src_mask */
1133          0xfffc,                /* dst_mask */
1134          FALSE),                /* pcrel_offset */
1135
1136   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1137   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1138          0,                     /* rightshift */
1139          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1140          16,                    /* bitsize */
1141          FALSE,                 /* pc_relative */
1142          0,                     /* bitpos */
1143          complain_overflow_dont,/* complain_on_overflow */
1144          bfd_elf_generic_reloc, /* special_function */
1145          "R_PPC64_ADDR16_LO_DS",/* name */
1146          FALSE,                 /* partial_inplace */
1147          0,                     /* src_mask */
1148          0xfffc,                /* dst_mask */
1149          FALSE),                /* pcrel_offset */
1150
1151   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1152   HOWTO (R_PPC64_GOT16_DS,      /* type */
1153          0,                     /* rightshift */
1154          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1155          16,                    /* bitsize */
1156          FALSE,                 /* pc_relative */
1157          0,                     /* bitpos */
1158          complain_overflow_signed, /* complain_on_overflow */
1159          ppc64_elf_unhandled_reloc, /* special_function */
1160          "R_PPC64_GOT16_DS",    /* name */
1161          FALSE,                 /* partial_inplace */
1162          0,                     /* src_mask */
1163          0xfffc,                /* dst_mask */
1164          FALSE),                /* pcrel_offset */
1165
1166   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1167   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1168          0,                     /* rightshift */
1169          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1170          16,                    /* bitsize */
1171          FALSE,                 /* pc_relative */
1172          0,                     /* bitpos */
1173          complain_overflow_dont, /* complain_on_overflow */
1174          ppc64_elf_unhandled_reloc, /* special_function */
1175          "R_PPC64_GOT16_LO_DS", /* name */
1176          FALSE,                 /* partial_inplace */
1177          0,                     /* src_mask */
1178          0xfffc,                /* dst_mask */
1179          FALSE),                /* pcrel_offset */
1180
1181   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1182   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1183          0,                     /* rightshift */
1184          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1185          16,                    /* bitsize */
1186          FALSE,                 /* pc_relative */
1187          0,                     /* bitpos */
1188          complain_overflow_dont, /* complain_on_overflow */
1189          ppc64_elf_unhandled_reloc, /* special_function */
1190          "R_PPC64_PLT16_LO_DS", /* name */
1191          FALSE,                 /* partial_inplace */
1192          0,                     /* src_mask */
1193          0xfffc,                /* dst_mask */
1194          FALSE),                /* pcrel_offset */
1195
1196   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1197   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1198          0,                     /* rightshift */
1199          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1200          16,                    /* bitsize */
1201          FALSE,                 /* pc_relative */
1202          0,                     /* bitpos */
1203          complain_overflow_signed, /* complain_on_overflow */
1204          ppc64_elf_sectoff_reloc, /* special_function */
1205          "R_PPC64_SECTOFF_DS",  /* name */
1206          FALSE,                 /* partial_inplace */
1207          0,                     /* src_mask */
1208          0xfffc,                /* dst_mask */
1209          FALSE),                /* pcrel_offset */
1210
1211   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1212   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1213          0,                     /* rightshift */
1214          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1215          16,                    /* bitsize */
1216          FALSE,                 /* pc_relative */
1217          0,                     /* bitpos */
1218          complain_overflow_dont, /* complain_on_overflow */
1219          ppc64_elf_sectoff_reloc, /* special_function */
1220          "R_PPC64_SECTOFF_LO_DS",/* name */
1221          FALSE,                 /* partial_inplace */
1222          0,                     /* src_mask */
1223          0xfffc,                /* dst_mask */
1224          FALSE),                /* pcrel_offset */
1225
1226   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1227   HOWTO (R_PPC64_TOC16_DS,      /* type */
1228          0,                     /* rightshift */
1229          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1230          16,                    /* bitsize */
1231          FALSE,                 /* pc_relative */
1232          0,                     /* bitpos */
1233          complain_overflow_signed, /* complain_on_overflow */
1234          ppc64_elf_toc_reloc,   /* special_function */
1235          "R_PPC64_TOC16_DS",    /* name */
1236          FALSE,                 /* partial_inplace */
1237          0,                     /* src_mask */
1238          0xfffc,                /* dst_mask */
1239          FALSE),                /* pcrel_offset */
1240
1241   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1242   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1243          0,                     /* rightshift */
1244          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1245          16,                    /* bitsize */
1246          FALSE,                 /* pc_relative */
1247          0,                     /* bitpos */
1248          complain_overflow_dont, /* complain_on_overflow */
1249          ppc64_elf_toc_reloc,   /* special_function */
1250          "R_PPC64_TOC16_LO_DS", /* name */
1251          FALSE,                 /* partial_inplace */
1252          0,                     /* src_mask */
1253          0xfffc,                /* dst_mask */
1254          FALSE),                /* pcrel_offset */
1255
1256   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1257   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1258   HOWTO (R_PPC64_PLTGOT16_DS,   /* type */
1259          0,                     /* rightshift */
1260          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1261          16,                    /* bitsize */
1262          FALSE,                 /* pc_relative */
1263          0,                     /* bitpos */
1264          complain_overflow_signed, /* complain_on_overflow */
1265          ppc64_elf_unhandled_reloc, /* special_function */
1266          "R_PPC64_PLTGOT16_DS", /* name */
1267          FALSE,                 /* partial_inplace */
1268          0,                     /* src_mask */
1269          0xfffc,                /* dst_mask */
1270          FALSE),                /* pcrel_offset */
1271
1272   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1273   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1274   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1275          0,                     /* rightshift */
1276          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1277          16,                    /* bitsize */
1278          FALSE,                 /* pc_relative */
1279          0,                     /* bitpos */
1280          complain_overflow_dont, /* complain_on_overflow */
1281          ppc64_elf_unhandled_reloc, /* special_function */
1282          "R_PPC64_PLTGOT16_LO_DS",/* name */
1283          FALSE,                 /* partial_inplace */
1284          0,                     /* src_mask */
1285          0xfffc,                /* dst_mask */
1286          FALSE),                /* pcrel_offset */
1287
1288   /* Marker relocs for TLS.  */
1289   HOWTO (R_PPC64_TLS,
1290          0,                     /* rightshift */
1291          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1292          32,                    /* bitsize */
1293          FALSE,                 /* pc_relative */
1294          0,                     /* bitpos */
1295          complain_overflow_dont, /* complain_on_overflow */
1296          bfd_elf_generic_reloc, /* special_function */
1297          "R_PPC64_TLS",         /* name */
1298          FALSE,                 /* partial_inplace */
1299          0,                     /* src_mask */
1300          0,                     /* dst_mask */
1301          FALSE),                /* pcrel_offset */
1302
1303   HOWTO (R_PPC64_TLSGD,
1304          0,                     /* rightshift */
1305          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1306          32,                    /* bitsize */
1307          FALSE,                 /* pc_relative */
1308          0,                     /* bitpos */
1309          complain_overflow_dont, /* complain_on_overflow */
1310          bfd_elf_generic_reloc, /* special_function */
1311          "R_PPC64_TLSGD",       /* name */
1312          FALSE,                 /* partial_inplace */
1313          0,                     /* src_mask */
1314          0,                     /* dst_mask */
1315          FALSE),                /* pcrel_offset */
1316
1317   HOWTO (R_PPC64_TLSLD,
1318          0,                     /* rightshift */
1319          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1320          32,                    /* bitsize */
1321          FALSE,                 /* pc_relative */
1322          0,                     /* bitpos */
1323          complain_overflow_dont, /* complain_on_overflow */
1324          bfd_elf_generic_reloc, /* special_function */
1325          "R_PPC64_TLSLD",       /* name */
1326          FALSE,                 /* partial_inplace */
1327          0,                     /* src_mask */
1328          0,                     /* dst_mask */
1329          FALSE),                /* pcrel_offset */
1330
1331   HOWTO (R_PPC64_TOCSAVE,
1332          0,                     /* rightshift */
1333          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1334          32,                    /* bitsize */
1335          FALSE,                 /* pc_relative */
1336          0,                     /* bitpos */
1337          complain_overflow_dont, /* complain_on_overflow */
1338          bfd_elf_generic_reloc, /* special_function */
1339          "R_PPC64_TOCSAVE",     /* name */
1340          FALSE,                 /* partial_inplace */
1341          0,                     /* src_mask */
1342          0,                     /* dst_mask */
1343          FALSE),                /* pcrel_offset */
1344
1345   /* Computes the load module index of the load module that contains the
1346      definition of its TLS sym.  */
1347   HOWTO (R_PPC64_DTPMOD64,
1348          0,                     /* rightshift */
1349          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1350          64,                    /* bitsize */
1351          FALSE,                 /* pc_relative */
1352          0,                     /* bitpos */
1353          complain_overflow_dont, /* complain_on_overflow */
1354          ppc64_elf_unhandled_reloc, /* special_function */
1355          "R_PPC64_DTPMOD64",    /* name */
1356          FALSE,                 /* partial_inplace */
1357          0,                     /* src_mask */
1358          ONES (64),             /* dst_mask */
1359          FALSE),                /* pcrel_offset */
1360
1361   /* Computes a dtv-relative displacement, the difference between the value
1362      of sym+add and the base address of the thread-local storage block that
1363      contains the definition of sym, minus 0x8000.  */
1364   HOWTO (R_PPC64_DTPREL64,
1365          0,                     /* rightshift */
1366          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1367          64,                    /* bitsize */
1368          FALSE,                 /* pc_relative */
1369          0,                     /* bitpos */
1370          complain_overflow_dont, /* complain_on_overflow */
1371          ppc64_elf_unhandled_reloc, /* special_function */
1372          "R_PPC64_DTPREL64",    /* name */
1373          FALSE,                 /* partial_inplace */
1374          0,                     /* src_mask */
1375          ONES (64),             /* dst_mask */
1376          FALSE),                /* pcrel_offset */
1377
1378   /* A 16 bit dtprel reloc.  */
1379   HOWTO (R_PPC64_DTPREL16,
1380          0,                     /* rightshift */
1381          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1382          16,                    /* bitsize */
1383          FALSE,                 /* pc_relative */
1384          0,                     /* bitpos */
1385          complain_overflow_signed, /* complain_on_overflow */
1386          ppc64_elf_unhandled_reloc, /* special_function */
1387          "R_PPC64_DTPREL16",    /* name */
1388          FALSE,                 /* partial_inplace */
1389          0,                     /* src_mask */
1390          0xffff,                /* dst_mask */
1391          FALSE),                /* pcrel_offset */
1392
1393   /* Like DTPREL16, but no overflow.  */
1394   HOWTO (R_PPC64_DTPREL16_LO,
1395          0,                     /* rightshift */
1396          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1397          16,                    /* bitsize */
1398          FALSE,                 /* pc_relative */
1399          0,                     /* bitpos */
1400          complain_overflow_dont, /* complain_on_overflow */
1401          ppc64_elf_unhandled_reloc, /* special_function */
1402          "R_PPC64_DTPREL16_LO", /* name */
1403          FALSE,                 /* partial_inplace */
1404          0,                     /* src_mask */
1405          0xffff,                /* dst_mask */
1406          FALSE),                /* pcrel_offset */
1407
1408   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1409   HOWTO (R_PPC64_DTPREL16_HI,
1410          16,                    /* rightshift */
1411          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1412          16,                    /* bitsize */
1413          FALSE,                 /* pc_relative */
1414          0,                     /* bitpos */
1415          complain_overflow_signed, /* complain_on_overflow */
1416          ppc64_elf_unhandled_reloc, /* special_function */
1417          "R_PPC64_DTPREL16_HI", /* name */
1418          FALSE,                 /* partial_inplace */
1419          0,                     /* src_mask */
1420          0xffff,                /* dst_mask */
1421          FALSE),                /* pcrel_offset */
1422
1423   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1424   HOWTO (R_PPC64_DTPREL16_HA,
1425          16,                    /* rightshift */
1426          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1427          16,                    /* bitsize */
1428          FALSE,                 /* pc_relative */
1429          0,                     /* bitpos */
1430          complain_overflow_signed, /* complain_on_overflow */
1431          ppc64_elf_unhandled_reloc, /* special_function */
1432          "R_PPC64_DTPREL16_HA", /* name */
1433          FALSE,                 /* partial_inplace */
1434          0,                     /* src_mask */
1435          0xffff,                /* dst_mask */
1436          FALSE),                /* pcrel_offset */
1437
1438   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1439   HOWTO (R_PPC64_DTPREL16_HIGHER,
1440          32,                    /* rightshift */
1441          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1442          16,                    /* bitsize */
1443          FALSE,                 /* pc_relative */
1444          0,                     /* bitpos */
1445          complain_overflow_dont, /* complain_on_overflow */
1446          ppc64_elf_unhandled_reloc, /* special_function */
1447          "R_PPC64_DTPREL16_HIGHER", /* name */
1448          FALSE,                 /* partial_inplace */
1449          0,                     /* src_mask */
1450          0xffff,                /* dst_mask */
1451          FALSE),                /* pcrel_offset */
1452
1453   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1454   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1455          32,                    /* rightshift */
1456          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1457          16,                    /* bitsize */
1458          FALSE,                 /* pc_relative */
1459          0,                     /* bitpos */
1460          complain_overflow_dont, /* complain_on_overflow */
1461          ppc64_elf_unhandled_reloc, /* special_function */
1462          "R_PPC64_DTPREL16_HIGHERA", /* name */
1463          FALSE,                 /* partial_inplace */
1464          0,                     /* src_mask */
1465          0xffff,                /* dst_mask */
1466          FALSE),                /* pcrel_offset */
1467
1468   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1469   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1470          48,                    /* rightshift */
1471          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1472          16,                    /* bitsize */
1473          FALSE,                 /* pc_relative */
1474          0,                     /* bitpos */
1475          complain_overflow_dont, /* complain_on_overflow */
1476          ppc64_elf_unhandled_reloc, /* special_function */
1477          "R_PPC64_DTPREL16_HIGHEST", /* name */
1478          FALSE,                 /* partial_inplace */
1479          0,                     /* src_mask */
1480          0xffff,                /* dst_mask */
1481          FALSE),                /* pcrel_offset */
1482
1483   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1484   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1485          48,                    /* rightshift */
1486          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1487          16,                    /* bitsize */
1488          FALSE,                 /* pc_relative */
1489          0,                     /* bitpos */
1490          complain_overflow_dont, /* complain_on_overflow */
1491          ppc64_elf_unhandled_reloc, /* special_function */
1492          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1493          FALSE,                 /* partial_inplace */
1494          0,                     /* src_mask */
1495          0xffff,                /* dst_mask */
1496          FALSE),                /* pcrel_offset */
1497
1498   /* Like DTPREL16, but for insns with a DS field.  */
1499   HOWTO (R_PPC64_DTPREL16_DS,
1500          0,                     /* rightshift */
1501          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1502          16,                    /* bitsize */
1503          FALSE,                 /* pc_relative */
1504          0,                     /* bitpos */
1505          complain_overflow_signed, /* complain_on_overflow */
1506          ppc64_elf_unhandled_reloc, /* special_function */
1507          "R_PPC64_DTPREL16_DS", /* name */
1508          FALSE,                 /* partial_inplace */
1509          0,                     /* src_mask */
1510          0xfffc,                /* dst_mask */
1511          FALSE),                /* pcrel_offset */
1512
1513   /* Like DTPREL16_DS, but no overflow.  */
1514   HOWTO (R_PPC64_DTPREL16_LO_DS,
1515          0,                     /* rightshift */
1516          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1517          16,                    /* bitsize */
1518          FALSE,                 /* pc_relative */
1519          0,                     /* bitpos */
1520          complain_overflow_dont, /* complain_on_overflow */
1521          ppc64_elf_unhandled_reloc, /* special_function */
1522          "R_PPC64_DTPREL16_LO_DS", /* name */
1523          FALSE,                 /* partial_inplace */
1524          0,                     /* src_mask */
1525          0xfffc,                /* dst_mask */
1526          FALSE),                /* pcrel_offset */
1527
1528   /* Computes a tp-relative displacement, the difference between the value of
1529      sym+add and the value of the thread pointer (r13).  */
1530   HOWTO (R_PPC64_TPREL64,
1531          0,                     /* rightshift */
1532          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1533          64,                    /* bitsize */
1534          FALSE,                 /* pc_relative */
1535          0,                     /* bitpos */
1536          complain_overflow_dont, /* complain_on_overflow */
1537          ppc64_elf_unhandled_reloc, /* special_function */
1538          "R_PPC64_TPREL64",     /* name */
1539          FALSE,                 /* partial_inplace */
1540          0,                     /* src_mask */
1541          ONES (64),             /* dst_mask */
1542          FALSE),                /* pcrel_offset */
1543
1544   /* A 16 bit tprel reloc.  */
1545   HOWTO (R_PPC64_TPREL16,
1546          0,                     /* rightshift */
1547          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1548          16,                    /* bitsize */
1549          FALSE,                 /* pc_relative */
1550          0,                     /* bitpos */
1551          complain_overflow_signed, /* complain_on_overflow */
1552          ppc64_elf_unhandled_reloc, /* special_function */
1553          "R_PPC64_TPREL16",     /* name */
1554          FALSE,                 /* partial_inplace */
1555          0,                     /* src_mask */
1556          0xffff,                /* dst_mask */
1557          FALSE),                /* pcrel_offset */
1558
1559   /* Like TPREL16, but no overflow.  */
1560   HOWTO (R_PPC64_TPREL16_LO,
1561          0,                     /* rightshift */
1562          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1563          16,                    /* bitsize */
1564          FALSE,                 /* pc_relative */
1565          0,                     /* bitpos */
1566          complain_overflow_dont, /* complain_on_overflow */
1567          ppc64_elf_unhandled_reloc, /* special_function */
1568          "R_PPC64_TPREL16_LO",  /* name */
1569          FALSE,                 /* partial_inplace */
1570          0,                     /* src_mask */
1571          0xffff,                /* dst_mask */
1572          FALSE),                /* pcrel_offset */
1573
1574   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1575   HOWTO (R_PPC64_TPREL16_HI,
1576          16,                    /* rightshift */
1577          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1578          16,                    /* bitsize */
1579          FALSE,                 /* pc_relative */
1580          0,                     /* bitpos */
1581          complain_overflow_signed, /* complain_on_overflow */
1582          ppc64_elf_unhandled_reloc, /* special_function */
1583          "R_PPC64_TPREL16_HI",  /* name */
1584          FALSE,                 /* partial_inplace */
1585          0,                     /* src_mask */
1586          0xffff,                /* dst_mask */
1587          FALSE),                /* pcrel_offset */
1588
1589   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1590   HOWTO (R_PPC64_TPREL16_HA,
1591          16,                    /* rightshift */
1592          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1593          16,                    /* bitsize */
1594          FALSE,                 /* pc_relative */
1595          0,                     /* bitpos */
1596          complain_overflow_signed, /* complain_on_overflow */
1597          ppc64_elf_unhandled_reloc, /* special_function */
1598          "R_PPC64_TPREL16_HA",  /* name */
1599          FALSE,                 /* partial_inplace */
1600          0,                     /* src_mask */
1601          0xffff,                /* dst_mask */
1602          FALSE),                /* pcrel_offset */
1603
1604   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1605   HOWTO (R_PPC64_TPREL16_HIGHER,
1606          32,                    /* rightshift */
1607          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1608          16,                    /* bitsize */
1609          FALSE,                 /* pc_relative */
1610          0,                     /* bitpos */
1611          complain_overflow_dont, /* complain_on_overflow */
1612          ppc64_elf_unhandled_reloc, /* special_function */
1613          "R_PPC64_TPREL16_HIGHER",      /* name */
1614          FALSE,                 /* partial_inplace */
1615          0,                     /* src_mask */
1616          0xffff,                /* dst_mask */
1617          FALSE),                /* pcrel_offset */
1618
1619   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1620   HOWTO (R_PPC64_TPREL16_HIGHERA,
1621          32,                    /* rightshift */
1622          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1623          16,                    /* bitsize */
1624          FALSE,                 /* pc_relative */
1625          0,                     /* bitpos */
1626          complain_overflow_dont, /* complain_on_overflow */
1627          ppc64_elf_unhandled_reloc, /* special_function */
1628          "R_PPC64_TPREL16_HIGHERA", /* name */
1629          FALSE,                 /* partial_inplace */
1630          0,                     /* src_mask */
1631          0xffff,                /* dst_mask */
1632          FALSE),                /* pcrel_offset */
1633
1634   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1635   HOWTO (R_PPC64_TPREL16_HIGHEST,
1636          48,                    /* rightshift */
1637          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1638          16,                    /* bitsize */
1639          FALSE,                 /* pc_relative */
1640          0,                     /* bitpos */
1641          complain_overflow_dont, /* complain_on_overflow */
1642          ppc64_elf_unhandled_reloc, /* special_function */
1643          "R_PPC64_TPREL16_HIGHEST", /* name */
1644          FALSE,                 /* partial_inplace */
1645          0,                     /* src_mask */
1646          0xffff,                /* dst_mask */
1647          FALSE),                /* pcrel_offset */
1648
1649   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1650   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1651          48,                    /* rightshift */
1652          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1653          16,                    /* bitsize */
1654          FALSE,                 /* pc_relative */
1655          0,                     /* bitpos */
1656          complain_overflow_dont, /* complain_on_overflow */
1657          ppc64_elf_unhandled_reloc, /* special_function */
1658          "R_PPC64_TPREL16_HIGHESTA", /* name */
1659          FALSE,                 /* partial_inplace */
1660          0,                     /* src_mask */
1661          0xffff,                /* dst_mask */
1662          FALSE),                /* pcrel_offset */
1663
1664   /* Like TPREL16, but for insns with a DS field.  */
1665   HOWTO (R_PPC64_TPREL16_DS,
1666          0,                     /* rightshift */
1667          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1668          16,                    /* bitsize */
1669          FALSE,                 /* pc_relative */
1670          0,                     /* bitpos */
1671          complain_overflow_signed, /* complain_on_overflow */
1672          ppc64_elf_unhandled_reloc, /* special_function */
1673          "R_PPC64_TPREL16_DS",  /* name */
1674          FALSE,                 /* partial_inplace */
1675          0,                     /* src_mask */
1676          0xfffc,                /* dst_mask */
1677          FALSE),                /* pcrel_offset */
1678
1679   /* Like TPREL16_DS, but no overflow.  */
1680   HOWTO (R_PPC64_TPREL16_LO_DS,
1681          0,                     /* rightshift */
1682          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1683          16,                    /* bitsize */
1684          FALSE,                 /* pc_relative */
1685          0,                     /* bitpos */
1686          complain_overflow_dont, /* complain_on_overflow */
1687          ppc64_elf_unhandled_reloc, /* special_function */
1688          "R_PPC64_TPREL16_LO_DS", /* name */
1689          FALSE,                 /* partial_inplace */
1690          0,                     /* src_mask */
1691          0xfffc,                /* dst_mask */
1692          FALSE),                /* pcrel_offset */
1693
1694   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1695      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1696      to the first entry relative to the TOC base (r2).  */
1697   HOWTO (R_PPC64_GOT_TLSGD16,
1698          0,                     /* rightshift */
1699          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1700          16,                    /* bitsize */
1701          FALSE,                 /* pc_relative */
1702          0,                     /* bitpos */
1703          complain_overflow_signed, /* complain_on_overflow */
1704          ppc64_elf_unhandled_reloc, /* special_function */
1705          "R_PPC64_GOT_TLSGD16", /* name */
1706          FALSE,                 /* partial_inplace */
1707          0,                     /* src_mask */
1708          0xffff,                /* dst_mask */
1709          FALSE),                /* pcrel_offset */
1710
1711   /* Like GOT_TLSGD16, but no overflow.  */
1712   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1713          0,                     /* rightshift */
1714          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1715          16,                    /* bitsize */
1716          FALSE,                 /* pc_relative */
1717          0,                     /* bitpos */
1718          complain_overflow_dont, /* complain_on_overflow */
1719          ppc64_elf_unhandled_reloc, /* special_function */
1720          "R_PPC64_GOT_TLSGD16_LO", /* name */
1721          FALSE,                 /* partial_inplace */
1722          0,                     /* src_mask */
1723          0xffff,                /* dst_mask */
1724          FALSE),                /* pcrel_offset */
1725
1726   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1727   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1728          16,                    /* rightshift */
1729          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1730          16,                    /* bitsize */
1731          FALSE,                 /* pc_relative */
1732          0,                     /* bitpos */
1733          complain_overflow_signed, /* complain_on_overflow */
1734          ppc64_elf_unhandled_reloc, /* special_function */
1735          "R_PPC64_GOT_TLSGD16_HI", /* name */
1736          FALSE,                 /* partial_inplace */
1737          0,                     /* src_mask */
1738          0xffff,                /* dst_mask */
1739          FALSE),                /* pcrel_offset */
1740
1741   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1742   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1743          16,                    /* rightshift */
1744          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1745          16,                    /* bitsize */
1746          FALSE,                 /* pc_relative */
1747          0,                     /* bitpos */
1748          complain_overflow_signed, /* complain_on_overflow */
1749          ppc64_elf_unhandled_reloc, /* special_function */
1750          "R_PPC64_GOT_TLSGD16_HA", /* name */
1751          FALSE,                 /* partial_inplace */
1752          0,                     /* src_mask */
1753          0xffff,                /* dst_mask */
1754          FALSE),                /* pcrel_offset */
1755
1756   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1757      with values (sym+add)@dtpmod and zero, and computes the offset to the
1758      first entry relative to the TOC base (r2).  */
1759   HOWTO (R_PPC64_GOT_TLSLD16,
1760          0,                     /* rightshift */
1761          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1762          16,                    /* bitsize */
1763          FALSE,                 /* pc_relative */
1764          0,                     /* bitpos */
1765          complain_overflow_signed, /* complain_on_overflow */
1766          ppc64_elf_unhandled_reloc, /* special_function */
1767          "R_PPC64_GOT_TLSLD16", /* name */
1768          FALSE,                 /* partial_inplace */
1769          0,                     /* src_mask */
1770          0xffff,                /* dst_mask */
1771          FALSE),                /* pcrel_offset */
1772
1773   /* Like GOT_TLSLD16, but no overflow.  */
1774   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1775          0,                     /* rightshift */
1776          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1777          16,                    /* bitsize */
1778          FALSE,                 /* pc_relative */
1779          0,                     /* bitpos */
1780          complain_overflow_dont, /* complain_on_overflow */
1781          ppc64_elf_unhandled_reloc, /* special_function */
1782          "R_PPC64_GOT_TLSLD16_LO", /* name */
1783          FALSE,                 /* partial_inplace */
1784          0,                     /* src_mask */
1785          0xffff,                /* dst_mask */
1786          FALSE),                /* pcrel_offset */
1787
1788   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1789   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1790          16,                    /* rightshift */
1791          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1792          16,                    /* bitsize */
1793          FALSE,                 /* pc_relative */
1794          0,                     /* bitpos */
1795          complain_overflow_signed, /* complain_on_overflow */
1796          ppc64_elf_unhandled_reloc, /* special_function */
1797          "R_PPC64_GOT_TLSLD16_HI", /* name */
1798          FALSE,                 /* partial_inplace */
1799          0,                     /* src_mask */
1800          0xffff,                /* dst_mask */
1801          FALSE),                /* pcrel_offset */
1802
1803   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1804   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1805          16,                    /* rightshift */
1806          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1807          16,                    /* bitsize */
1808          FALSE,                 /* pc_relative */
1809          0,                     /* bitpos */
1810          complain_overflow_signed, /* complain_on_overflow */
1811          ppc64_elf_unhandled_reloc, /* special_function */
1812          "R_PPC64_GOT_TLSLD16_HA", /* name */
1813          FALSE,                 /* partial_inplace */
1814          0,                     /* src_mask */
1815          0xffff,                /* dst_mask */
1816          FALSE),                /* pcrel_offset */
1817
1818   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1819      the offset to the entry relative to the TOC base (r2).  */
1820   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1821          0,                     /* rightshift */
1822          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1823          16,                    /* bitsize */
1824          FALSE,                 /* pc_relative */
1825          0,                     /* bitpos */
1826          complain_overflow_signed, /* complain_on_overflow */
1827          ppc64_elf_unhandled_reloc, /* special_function */
1828          "R_PPC64_GOT_DTPREL16_DS", /* name */
1829          FALSE,                 /* partial_inplace */
1830          0,                     /* src_mask */
1831          0xfffc,                /* dst_mask */
1832          FALSE),                /* pcrel_offset */
1833
1834   /* Like GOT_DTPREL16_DS, but no overflow.  */
1835   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1836          0,                     /* rightshift */
1837          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1838          16,                    /* bitsize */
1839          FALSE,                 /* pc_relative */
1840          0,                     /* bitpos */
1841          complain_overflow_dont, /* complain_on_overflow */
1842          ppc64_elf_unhandled_reloc, /* special_function */
1843          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1844          FALSE,                 /* partial_inplace */
1845          0,                     /* src_mask */
1846          0xfffc,                /* dst_mask */
1847          FALSE),                /* pcrel_offset */
1848
1849   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1850   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1851          16,                    /* rightshift */
1852          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1853          16,                    /* bitsize */
1854          FALSE,                 /* pc_relative */
1855          0,                     /* bitpos */
1856          complain_overflow_signed, /* complain_on_overflow */
1857          ppc64_elf_unhandled_reloc, /* special_function */
1858          "R_PPC64_GOT_DTPREL16_HI", /* name */
1859          FALSE,                 /* partial_inplace */
1860          0,                     /* src_mask */
1861          0xffff,                /* dst_mask */
1862          FALSE),                /* pcrel_offset */
1863
1864   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1865   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1866          16,                    /* rightshift */
1867          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1868          16,                    /* bitsize */
1869          FALSE,                 /* pc_relative */
1870          0,                     /* bitpos */
1871          complain_overflow_signed, /* complain_on_overflow */
1872          ppc64_elf_unhandled_reloc, /* special_function */
1873          "R_PPC64_GOT_DTPREL16_HA", /* name */
1874          FALSE,                 /* partial_inplace */
1875          0,                     /* src_mask */
1876          0xffff,                /* dst_mask */
1877          FALSE),                /* pcrel_offset */
1878
1879   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1880      offset to the entry relative to the TOC base (r2).  */
1881   HOWTO (R_PPC64_GOT_TPREL16_DS,
1882          0,                     /* rightshift */
1883          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1884          16,                    /* bitsize */
1885          FALSE,                 /* pc_relative */
1886          0,                     /* bitpos */
1887          complain_overflow_signed, /* complain_on_overflow */
1888          ppc64_elf_unhandled_reloc, /* special_function */
1889          "R_PPC64_GOT_TPREL16_DS", /* name */
1890          FALSE,                 /* partial_inplace */
1891          0,                     /* src_mask */
1892          0xfffc,                /* dst_mask */
1893          FALSE),                /* pcrel_offset */
1894
1895   /* Like GOT_TPREL16_DS, but no overflow.  */
1896   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1897          0,                     /* rightshift */
1898          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1899          16,                    /* bitsize */
1900          FALSE,                 /* pc_relative */
1901          0,                     /* bitpos */
1902          complain_overflow_dont, /* complain_on_overflow */
1903          ppc64_elf_unhandled_reloc, /* special_function */
1904          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1905          FALSE,                 /* partial_inplace */
1906          0,                     /* src_mask */
1907          0xfffc,                /* dst_mask */
1908          FALSE),                /* pcrel_offset */
1909
1910   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1911   HOWTO (R_PPC64_GOT_TPREL16_HI,
1912          16,                    /* rightshift */
1913          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1914          16,                    /* bitsize */
1915          FALSE,                 /* pc_relative */
1916          0,                     /* bitpos */
1917          complain_overflow_signed, /* complain_on_overflow */
1918          ppc64_elf_unhandled_reloc, /* special_function */
1919          "R_PPC64_GOT_TPREL16_HI", /* name */
1920          FALSE,                 /* partial_inplace */
1921          0,                     /* src_mask */
1922          0xffff,                /* dst_mask */
1923          FALSE),                /* pcrel_offset */
1924
1925   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1926   HOWTO (R_PPC64_GOT_TPREL16_HA,
1927          16,                    /* rightshift */
1928          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1929          16,                    /* bitsize */
1930          FALSE,                 /* pc_relative */
1931          0,                     /* bitpos */
1932          complain_overflow_signed, /* complain_on_overflow */
1933          ppc64_elf_unhandled_reloc, /* special_function */
1934          "R_PPC64_GOT_TPREL16_HA", /* name */
1935          FALSE,                 /* partial_inplace */
1936          0,                     /* src_mask */
1937          0xffff,                /* dst_mask */
1938          FALSE),                /* pcrel_offset */
1939
1940   HOWTO (R_PPC64_JMP_IREL,      /* type */
1941          0,                     /* rightshift */
1942          0,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1943          0,                     /* bitsize */
1944          FALSE,                 /* pc_relative */
1945          0,                     /* bitpos */
1946          complain_overflow_dont, /* complain_on_overflow */
1947          ppc64_elf_unhandled_reloc, /* special_function */
1948          "R_PPC64_JMP_IREL",    /* name */
1949          FALSE,                 /* partial_inplace */
1950          0,                     /* src_mask */
1951          0,                     /* dst_mask */
1952          FALSE),                /* pcrel_offset */
1953
1954   HOWTO (R_PPC64_IRELATIVE,     /* type */
1955          0,                     /* rightshift */
1956          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1957          64,                    /* bitsize */
1958          FALSE,                 /* pc_relative */
1959          0,                     /* bitpos */
1960          complain_overflow_dont, /* complain_on_overflow */
1961          bfd_elf_generic_reloc, /* special_function */
1962          "R_PPC64_IRELATIVE",   /* name */
1963          FALSE,                 /* partial_inplace */
1964          0,                     /* src_mask */
1965          ONES (64),             /* dst_mask */
1966          FALSE),                /* pcrel_offset */
1967
1968   /* A 16 bit relative relocation.  */
1969   HOWTO (R_PPC64_REL16,         /* type */
1970          0,                     /* rightshift */
1971          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1972          16,                    /* bitsize */
1973          TRUE,                  /* pc_relative */
1974          0,                     /* bitpos */
1975          complain_overflow_signed, /* complain_on_overflow */
1976          bfd_elf_generic_reloc, /* special_function */
1977          "R_PPC64_REL16",       /* name */
1978          FALSE,                 /* partial_inplace */
1979          0,                     /* src_mask */
1980          0xffff,                /* dst_mask */
1981          TRUE),                 /* pcrel_offset */
1982
1983   /* A 16 bit relative relocation without overflow.  */
1984   HOWTO (R_PPC64_REL16_LO,      /* type */
1985          0,                     /* rightshift */
1986          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1987          16,                    /* bitsize */
1988          TRUE,                  /* pc_relative */
1989          0,                     /* bitpos */
1990          complain_overflow_dont,/* complain_on_overflow */
1991          bfd_elf_generic_reloc, /* special_function */
1992          "R_PPC64_REL16_LO",    /* name */
1993          FALSE,                 /* partial_inplace */
1994          0,                     /* src_mask */
1995          0xffff,                /* dst_mask */
1996          TRUE),                 /* pcrel_offset */
1997
1998   /* The high order 16 bits of a relative address.  */
1999   HOWTO (R_PPC64_REL16_HI,      /* type */
2000          16,                    /* rightshift */
2001          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2002          16,                    /* bitsize */
2003          TRUE,                  /* pc_relative */
2004          0,                     /* bitpos */
2005          complain_overflow_signed, /* complain_on_overflow */
2006          bfd_elf_generic_reloc, /* special_function */
2007          "R_PPC64_REL16_HI",    /* name */
2008          FALSE,                 /* partial_inplace */
2009          0,                     /* src_mask */
2010          0xffff,                /* dst_mask */
2011          TRUE),                 /* pcrel_offset */
2012
2013   /* The high order 16 bits of a relative address, plus 1 if the contents of
2014      the low 16 bits, treated as a signed number, is negative.  */
2015   HOWTO (R_PPC64_REL16_HA,      /* type */
2016          16,                    /* rightshift */
2017          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2018          16,                    /* bitsize */
2019          TRUE,                  /* pc_relative */
2020          0,                     /* bitpos */
2021          complain_overflow_signed, /* complain_on_overflow */
2022          ppc64_elf_ha_reloc,    /* special_function */
2023          "R_PPC64_REL16_HA",    /* name */
2024          FALSE,                 /* partial_inplace */
2025          0,                     /* src_mask */
2026          0xffff,                /* dst_mask */
2027          TRUE),                 /* pcrel_offset */
2028
2029   /* Like R_PPC64_REL16_HA but for split field in addpcis.  */
2030   HOWTO (R_PPC64_REL16DX_HA,    /* type */
2031          16,                    /* rightshift */
2032          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2033          16,                    /* bitsize */
2034          TRUE,                  /* pc_relative */
2035          0,                     /* bitpos */
2036          complain_overflow_signed, /* complain_on_overflow */
2037          ppc64_elf_ha_reloc,    /* special_function */
2038          "R_PPC64_REL16DX_HA",  /* name */
2039          FALSE,                 /* partial_inplace */
2040          0,                     /* src_mask */
2041          0x1fffc1,              /* dst_mask */
2042          TRUE),                 /* pcrel_offset */
2043
2044   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
2045   HOWTO (R_PPC64_ADDR16_HIGH,   /* type */
2046          16,                    /* rightshift */
2047          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2048          16,                    /* bitsize */
2049          FALSE,                 /* pc_relative */
2050          0,                     /* bitpos */
2051          complain_overflow_dont, /* complain_on_overflow */
2052          bfd_elf_generic_reloc, /* special_function */
2053          "R_PPC64_ADDR16_HIGH", /* name */
2054          FALSE,                 /* partial_inplace */
2055          0,                     /* src_mask */
2056          0xffff,                /* dst_mask */
2057          FALSE),                /* pcrel_offset */
2058
2059   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
2060   HOWTO (R_PPC64_ADDR16_HIGHA,  /* type */
2061          16,                    /* rightshift */
2062          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2063          16,                    /* bitsize */
2064          FALSE,                 /* pc_relative */
2065          0,                     /* bitpos */
2066          complain_overflow_dont, /* complain_on_overflow */
2067          ppc64_elf_ha_reloc,    /* special_function */
2068          "R_PPC64_ADDR16_HIGHA",        /* name */
2069          FALSE,                 /* partial_inplace */
2070          0,                     /* src_mask */
2071          0xffff,                /* dst_mask */
2072          FALSE),                /* pcrel_offset */
2073
2074   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
2075   HOWTO (R_PPC64_DTPREL16_HIGH,
2076          16,                    /* rightshift */
2077          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2078          16,                    /* bitsize */
2079          FALSE,                 /* pc_relative */
2080          0,                     /* bitpos */
2081          complain_overflow_dont, /* complain_on_overflow */
2082          ppc64_elf_unhandled_reloc, /* special_function */
2083          "R_PPC64_DTPREL16_HIGH", /* name */
2084          FALSE,                 /* partial_inplace */
2085          0,                     /* src_mask */
2086          0xffff,                /* dst_mask */
2087          FALSE),                /* pcrel_offset */
2088
2089   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
2090   HOWTO (R_PPC64_DTPREL16_HIGHA,
2091          16,                    /* rightshift */
2092          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2093          16,                    /* bitsize */
2094          FALSE,                 /* pc_relative */
2095          0,                     /* bitpos */
2096          complain_overflow_dont, /* complain_on_overflow */
2097          ppc64_elf_unhandled_reloc, /* special_function */
2098          "R_PPC64_DTPREL16_HIGHA", /* name */
2099          FALSE,                 /* partial_inplace */
2100          0,                     /* src_mask */
2101          0xffff,                /* dst_mask */
2102          FALSE),                /* pcrel_offset */
2103
2104   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
2105   HOWTO (R_PPC64_TPREL16_HIGH,
2106          16,                    /* rightshift */
2107          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2108          16,                    /* bitsize */
2109          FALSE,                 /* pc_relative */
2110          0,                     /* bitpos */
2111          complain_overflow_dont, /* complain_on_overflow */
2112          ppc64_elf_unhandled_reloc, /* special_function */
2113          "R_PPC64_TPREL16_HIGH",        /* name */
2114          FALSE,                 /* partial_inplace */
2115          0,                     /* src_mask */
2116          0xffff,                /* dst_mask */
2117          FALSE),                /* pcrel_offset */
2118
2119   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
2120   HOWTO (R_PPC64_TPREL16_HIGHA,
2121          16,                    /* rightshift */
2122          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2123          16,                    /* bitsize */
2124          FALSE,                 /* pc_relative */
2125          0,                     /* bitpos */
2126          complain_overflow_dont, /* complain_on_overflow */
2127          ppc64_elf_unhandled_reloc, /* special_function */
2128          "R_PPC64_TPREL16_HIGHA",       /* name */
2129          FALSE,                 /* partial_inplace */
2130          0,                     /* src_mask */
2131          0xffff,                /* dst_mask */
2132          FALSE),                /* pcrel_offset */
2133
2134   /* Marker reloc on ELFv2 large-model function entry.  */
2135   HOWTO (R_PPC64_ENTRY,
2136          0,                     /* rightshift */
2137          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2138          32,                    /* bitsize */
2139          FALSE,                 /* pc_relative */
2140          0,                     /* bitpos */
2141          complain_overflow_dont, /* complain_on_overflow */
2142          bfd_elf_generic_reloc, /* special_function */
2143          "R_PPC64_ENTRY",       /* name */
2144          FALSE,                 /* partial_inplace */
2145          0,                     /* src_mask */
2146          0,                     /* dst_mask */
2147          FALSE),                /* pcrel_offset */
2148
2149   /* Like ADDR64, but use local entry point of function.  */
2150   HOWTO (R_PPC64_ADDR64_LOCAL,  /* type */
2151          0,                     /* rightshift */
2152          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2153          64,                    /* bitsize */
2154          FALSE,                 /* pc_relative */
2155          0,                     /* bitpos */
2156          complain_overflow_dont, /* complain_on_overflow */
2157          bfd_elf_generic_reloc, /* special_function */
2158          "R_PPC64_ADDR64_LOCAL", /* name */
2159          FALSE,                 /* partial_inplace */
2160          0,                     /* src_mask */
2161          ONES (64),             /* dst_mask */
2162          FALSE),                /* pcrel_offset */
2163
2164   /* GNU extension to record C++ vtable hierarchy.  */
2165   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2166          0,                     /* rightshift */
2167          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2168          0,                     /* bitsize */
2169          FALSE,                 /* pc_relative */
2170          0,                     /* bitpos */
2171          complain_overflow_dont, /* complain_on_overflow */
2172          NULL,                  /* special_function */
2173          "R_PPC64_GNU_VTINHERIT", /* name */
2174          FALSE,                 /* partial_inplace */
2175          0,                     /* src_mask */
2176          0,                     /* dst_mask */
2177          FALSE),                /* pcrel_offset */
2178
2179   /* GNU extension to record C++ vtable member usage.  */
2180   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
2181          0,                     /* rightshift */
2182          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2183          0,                     /* bitsize */
2184          FALSE,                 /* pc_relative */
2185          0,                     /* bitpos */
2186          complain_overflow_dont, /* complain_on_overflow */
2187          NULL,                  /* special_function */
2188          "R_PPC64_GNU_VTENTRY", /* name */
2189          FALSE,                 /* partial_inplace */
2190          0,                     /* src_mask */
2191          0,                     /* dst_mask */
2192          FALSE),                /* pcrel_offset */
2193 };
2194
2195 \f
2196 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2197    be done.  */
2198
2199 static void
2200 ppc_howto_init (void)
2201 {
2202   unsigned int i, type;
2203
2204   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2205     {
2206       type = ppc64_elf_howto_raw[i].type;
2207       BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
2208       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2209     }
2210 }
2211
2212 static reloc_howto_type *
2213 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2214                              bfd_reloc_code_real_type code)
2215 {
2216   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2217
2218   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2219     /* Initialize howto table if needed.  */
2220     ppc_howto_init ();
2221
2222   switch (code)
2223     {
2224     default:
2225       return NULL;
2226
2227     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
2228       break;
2229     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
2230       break;
2231     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
2232       break;
2233     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
2234       break;
2235     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
2236       break;
2237     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
2238       break;
2239     case BFD_RELOC_PPC64_ADDR16_HIGH:           r = R_PPC64_ADDR16_HIGH;
2240       break;
2241     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
2242       break;
2243     case BFD_RELOC_PPC64_ADDR16_HIGHA:          r = R_PPC64_ADDR16_HIGHA;
2244       break;
2245     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
2246       break;
2247     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
2248       break;
2249     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
2250       break;
2251     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
2252       break;
2253     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
2254       break;
2255     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
2256       break;
2257     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
2258       break;
2259     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
2260       break;
2261     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
2262       break;
2263     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
2264       break;
2265     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
2266       break;
2267     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
2268       break;
2269     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
2270       break;
2271     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
2272       break;
2273     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
2274       break;
2275     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
2276       break;
2277     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
2278       break;
2279     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
2280       break;
2281     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
2282       break;
2283     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
2284       break;
2285     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
2286       break;
2287     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
2288       break;
2289     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
2290       break;
2291     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
2292       break;
2293     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
2294       break;
2295     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
2296       break;
2297     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
2298       break;
2299     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
2300       break;
2301     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
2302       break;
2303     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
2304       break;
2305     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
2306       break;
2307     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
2308       break;
2309     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
2310       break;
2311     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
2312       break;
2313     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
2314       break;
2315     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
2316       break;
2317     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
2318       break;
2319     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
2320       break;
2321     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
2322       break;
2323     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
2324       break;
2325     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
2326       break;
2327     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
2328       break;
2329     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
2330       break;
2331     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
2332       break;
2333     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
2334       break;
2335     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
2336       break;
2337     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
2338       break;
2339     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2340       break;
2341     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2342       break;
2343     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2344       break;
2345     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2346       break;
2347     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2348       break;
2349     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2350       break;
2351     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
2352       break;
2353     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
2354       break;
2355     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2356       break;
2357     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2358       break;
2359     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2360       break;
2361     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2362       break;
2363     case BFD_RELOC_PPC64_TPREL16_HIGH:          r = R_PPC64_TPREL16_HIGH;
2364       break;
2365     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2366       break;
2367     case BFD_RELOC_PPC64_TPREL16_HIGHA:         r = R_PPC64_TPREL16_HIGHA;
2368       break;
2369     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2370       break;
2371     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2372       break;
2373     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2374       break;
2375     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2376       break;
2377     case BFD_RELOC_PPC64_DTPREL16_HIGH:         r = R_PPC64_DTPREL16_HIGH;
2378       break;
2379     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2380       break;
2381     case BFD_RELOC_PPC64_DTPREL16_HIGHA:        r = R_PPC64_DTPREL16_HIGHA;
2382       break;
2383     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2384       break;
2385     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2386       break;
2387     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2388       break;
2389     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2390       break;
2391     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2392       break;
2393     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2394       break;
2395     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2396       break;
2397     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2398       break;
2399     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2400       break;
2401     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2402       break;
2403     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2404       break;
2405     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2406       break;
2407     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2408       break;
2409     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2410       break;
2411     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2412       break;
2413     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2414       break;
2415     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2416       break;
2417     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2418       break;
2419     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2420       break;
2421     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2422       break;
2423     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2424       break;
2425     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2426       break;
2427     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2428       break;
2429     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2430       break;
2431     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2432       break;
2433     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2434       break;
2435     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2436       break;
2437     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2438       break;
2439     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2440       break;
2441     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
2442       break;
2443     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
2444       break;
2445     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
2446       break;
2447     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
2448       break;
2449     case BFD_RELOC_PPC_REL16DX_HA:              r = R_PPC64_REL16DX_HA;
2450       break;
2451     case BFD_RELOC_PPC64_ENTRY:                 r = R_PPC64_ENTRY;
2452       break;
2453     case BFD_RELOC_PPC64_ADDR64_LOCAL:          r = R_PPC64_ADDR64_LOCAL;
2454       break;
2455     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2456       break;
2457     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2458       break;
2459     }
2460
2461   return ppc64_elf_howto_table[r];
2462 };
2463
2464 static reloc_howto_type *
2465 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2466                              const char *r_name)
2467 {
2468   unsigned int i;
2469
2470   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2471     if (ppc64_elf_howto_raw[i].name != NULL
2472         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2473       return &ppc64_elf_howto_raw[i];
2474
2475   return NULL;
2476 }
2477
2478 /* Set the howto pointer for a PowerPC ELF reloc.  */
2479
2480 static void
2481 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2482                          Elf_Internal_Rela *dst)
2483 {
2484   unsigned int type;
2485
2486   /* Initialize howto table if needed.  */
2487   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2488     ppc_howto_init ();
2489
2490   type = ELF64_R_TYPE (dst->r_info);
2491   if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
2492     {
2493       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2494                              abfd, (int) type);
2495       type = R_PPC64_NONE;
2496     }
2497   cache_ptr->howto = ppc64_elf_howto_table[type];
2498 }
2499
2500 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2501
2502 static bfd_reloc_status_type
2503 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2504                     void *data, asection *input_section,
2505                     bfd *output_bfd, char **error_message)
2506 {
2507   enum elf_ppc64_reloc_type r_type;
2508   long insn;
2509   bfd_size_type octets;
2510   bfd_vma value;
2511
2512   /* If this is a relocatable link (output_bfd test tells us), just
2513      call the generic function.  Any adjustment will be done at final
2514      link time.  */
2515   if (output_bfd != NULL)
2516     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2517                                   input_section, output_bfd, error_message);
2518
2519   /* Adjust the addend for sign extension of the low 16 bits.
2520      We won't actually be using the low 16 bits, so trashing them
2521      doesn't matter.  */
2522   reloc_entry->addend += 0x8000;
2523   r_type = reloc_entry->howto->type;
2524   if (r_type != R_PPC64_REL16DX_HA)
2525     return bfd_reloc_continue;
2526
2527   value = 0;
2528   if (!bfd_is_com_section (symbol->section))
2529     value = symbol->value;
2530   value += (reloc_entry->addend
2531             + symbol->section->output_offset
2532             + symbol->section->output_section->vma);
2533   value -= (reloc_entry->address
2534             + input_section->output_offset
2535             + input_section->output_section->vma);
2536   value = (bfd_signed_vma) value >> 16;
2537
2538   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2539   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2540   insn &= ~0x1fffc1;
2541   insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2542   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2543   if (value + 0x8000 > 0xffff)
2544     return bfd_reloc_overflow;
2545   return bfd_reloc_ok;
2546 }
2547
2548 static bfd_reloc_status_type
2549 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2550                         void *data, asection *input_section,
2551                         bfd *output_bfd, char **error_message)
2552 {
2553   if (output_bfd != NULL)
2554     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2555                                   input_section, output_bfd, error_message);
2556
2557   if (strcmp (symbol->section->name, ".opd") == 0
2558       && (symbol->section->owner->flags & DYNAMIC) == 0)
2559     {
2560       bfd_vma dest = opd_entry_value (symbol->section,
2561                                       symbol->value + reloc_entry->addend,
2562                                       NULL, NULL, FALSE);
2563       if (dest != (bfd_vma) -1)
2564         reloc_entry->addend = dest - (symbol->value
2565                                       + symbol->section->output_section->vma
2566                                       + symbol->section->output_offset);
2567     }
2568   else
2569     {
2570       elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2571
2572       if (symbol->section->owner != abfd
2573           && symbol->section->owner != NULL
2574           && abiversion (symbol->section->owner) >= 2)
2575         {
2576           unsigned int i;
2577
2578           for (i = 0; i < symbol->section->owner->symcount; ++i)
2579             {
2580               asymbol *symdef = symbol->section->owner->outsymbols[i];
2581
2582               if (strcmp (symdef->name, symbol->name) == 0)
2583                 {
2584                   elfsym = (elf_symbol_type *) symdef;
2585                   break;
2586                 }
2587             }
2588         }
2589       reloc_entry->addend
2590         += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2591     }
2592   return bfd_reloc_continue;
2593 }
2594
2595 static bfd_reloc_status_type
2596 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2597                          void *data, asection *input_section,
2598                          bfd *output_bfd, char **error_message)
2599 {
2600   long insn;
2601   enum elf_ppc64_reloc_type r_type;
2602   bfd_size_type octets;
2603   /* Assume 'at' branch hints.  */
2604   bfd_boolean is_isa_v2 = TRUE;
2605
2606   /* If this is a relocatable link (output_bfd test tells us), just
2607      call the generic function.  Any adjustment will be done at final
2608      link time.  */
2609   if (output_bfd != NULL)
2610     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2611                                   input_section, output_bfd, error_message);
2612
2613   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2614   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2615   insn &= ~(0x01 << 21);
2616   r_type = reloc_entry->howto->type;
2617   if (r_type == R_PPC64_ADDR14_BRTAKEN
2618       || r_type == R_PPC64_REL14_BRTAKEN)
2619     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2620
2621   if (is_isa_v2)
2622     {
2623       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2624          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2625          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2626       if ((insn & (0x14 << 21)) == (0x04 << 21))
2627         insn |= 0x02 << 21;
2628       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2629         insn |= 0x08 << 21;
2630       else
2631         goto out;
2632     }
2633   else
2634     {
2635       bfd_vma target = 0;
2636       bfd_vma from;
2637
2638       if (!bfd_is_com_section (symbol->section))
2639         target = symbol->value;
2640       target += symbol->section->output_section->vma;
2641       target += symbol->section->output_offset;
2642       target += reloc_entry->addend;
2643
2644       from = (reloc_entry->address
2645               + input_section->output_offset
2646               + input_section->output_section->vma);
2647
2648       /* Invert 'y' bit if not the default.  */
2649       if ((bfd_signed_vma) (target - from) < 0)
2650         insn ^= 0x01 << 21;
2651     }
2652   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2653  out:
2654   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2655                                  input_section, output_bfd, error_message);
2656 }
2657
2658 static bfd_reloc_status_type
2659 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2660                          void *data, asection *input_section,
2661                          bfd *output_bfd, char **error_message)
2662 {
2663   /* If this is a relocatable link (output_bfd test tells us), just
2664      call the generic function.  Any adjustment will be done at final
2665      link time.  */
2666   if (output_bfd != NULL)
2667     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2668                                   input_section, output_bfd, error_message);
2669
2670   /* Subtract the symbol section base address.  */
2671   reloc_entry->addend -= symbol->section->output_section->vma;
2672   return bfd_reloc_continue;
2673 }
2674
2675 static bfd_reloc_status_type
2676 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2677                             void *data, asection *input_section,
2678                             bfd *output_bfd, char **error_message)
2679 {
2680   /* If this is a relocatable link (output_bfd test tells us), just
2681      call the generic function.  Any adjustment will be done at final
2682      link time.  */
2683   if (output_bfd != NULL)
2684     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2685                                   input_section, output_bfd, error_message);
2686
2687   /* Subtract the symbol section base address.  */
2688   reloc_entry->addend -= symbol->section->output_section->vma;
2689
2690   /* Adjust the addend for sign extension of the low 16 bits.  */
2691   reloc_entry->addend += 0x8000;
2692   return bfd_reloc_continue;
2693 }
2694
2695 static bfd_reloc_status_type
2696 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2697                      void *data, asection *input_section,
2698                      bfd *output_bfd, char **error_message)
2699 {
2700   bfd_vma TOCstart;
2701
2702   /* If this is a relocatable link (output_bfd test tells us), just
2703      call the generic function.  Any adjustment will be done at final
2704      link time.  */
2705   if (output_bfd != NULL)
2706     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2707                                   input_section, output_bfd, error_message);
2708
2709   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2710   if (TOCstart == 0)
2711     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2712
2713   /* Subtract the TOC base address.  */
2714   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2715   return bfd_reloc_continue;
2716 }
2717
2718 static bfd_reloc_status_type
2719 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2720                         void *data, asection *input_section,
2721                         bfd *output_bfd, char **error_message)
2722 {
2723   bfd_vma TOCstart;
2724
2725   /* If this is a relocatable link (output_bfd test tells us), just
2726      call the generic function.  Any adjustment will be done at final
2727      link time.  */
2728   if (output_bfd != NULL)
2729     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2730                                   input_section, output_bfd, error_message);
2731
2732   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2733   if (TOCstart == 0)
2734     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2735
2736   /* Subtract the TOC base address.  */
2737   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2738
2739   /* Adjust the addend for sign extension of the low 16 bits.  */
2740   reloc_entry->addend += 0x8000;
2741   return bfd_reloc_continue;
2742 }
2743
2744 static bfd_reloc_status_type
2745 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2746                        void *data, asection *input_section,
2747                        bfd *output_bfd, char **error_message)
2748 {
2749   bfd_vma TOCstart;
2750   bfd_size_type octets;
2751
2752   /* If this is a relocatable link (output_bfd test tells us), just
2753      call the generic function.  Any adjustment will be done at final
2754      link time.  */
2755   if (output_bfd != NULL)
2756     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2757                                   input_section, output_bfd, error_message);
2758
2759   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2760   if (TOCstart == 0)
2761     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2762
2763   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2764   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2765   return bfd_reloc_ok;
2766 }
2767
2768 static bfd_reloc_status_type
2769 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2770                            void *data, asection *input_section,
2771                            bfd *output_bfd, char **error_message)
2772 {
2773   /* If this is a relocatable link (output_bfd test tells us), just
2774      call the generic function.  Any adjustment will be done at final
2775      link time.  */
2776   if (output_bfd != NULL)
2777     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2778                                   input_section, output_bfd, error_message);
2779
2780   if (error_message != NULL)
2781     {
2782       static char buf[60];
2783       sprintf (buf, "generic linker can't handle %s",
2784                reloc_entry->howto->name);
2785       *error_message = buf;
2786     }
2787   return bfd_reloc_dangerous;
2788 }
2789
2790 /* Track GOT entries needed for a given symbol.  We might need more
2791    than one got entry per symbol.  */
2792 struct got_entry
2793 {
2794   struct got_entry *next;
2795
2796   /* The symbol addend that we'll be placing in the GOT.  */
2797   bfd_vma addend;
2798
2799   /* Unlike other ELF targets, we use separate GOT entries for the same
2800      symbol referenced from different input files.  This is to support
2801      automatic multiple TOC/GOT sections, where the TOC base can vary
2802      from one input file to another.  After partitioning into TOC groups
2803      we merge entries within the group.
2804
2805      Point to the BFD owning this GOT entry.  */
2806   bfd *owner;
2807
2808   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2809      TLS_TPREL or TLS_DTPREL for tls entries.  */
2810   unsigned char tls_type;
2811
2812   /* Non-zero if got.ent points to real entry.  */
2813   unsigned char is_indirect;
2814
2815   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2816   union
2817     {
2818       bfd_signed_vma refcount;
2819       bfd_vma offset;
2820       struct got_entry *ent;
2821     } got;
2822 };
2823
2824 /* The same for PLT.  */
2825 struct plt_entry
2826 {
2827   struct plt_entry *next;
2828
2829   bfd_vma addend;
2830
2831   union
2832     {
2833       bfd_signed_vma refcount;
2834       bfd_vma offset;
2835     } plt;
2836 };
2837
2838 struct ppc64_elf_obj_tdata
2839 {
2840   struct elf_obj_tdata elf;
2841
2842   /* Shortcuts to dynamic linker sections.  */
2843   asection *got;
2844   asection *relgot;
2845
2846   /* Used during garbage collection.  We attach global symbols defined
2847      on removed .opd entries to this section so that the sym is removed.  */
2848   asection *deleted_section;
2849
2850   /* TLS local dynamic got entry handling.  Support for multiple GOT
2851      sections means we potentially need one of these for each input bfd.  */
2852   struct got_entry tlsld_got;
2853
2854   union {
2855     /* A copy of relocs before they are modified for --emit-relocs.  */
2856     Elf_Internal_Rela *relocs;
2857
2858     /* Section contents.  */
2859     bfd_byte *contents;
2860   } opd;
2861
2862   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2863      the reloc to be in the range -32768 to 32767.  */
2864   unsigned int has_small_toc_reloc : 1;
2865
2866   /* Set if toc/got ha relocs detected not using r2, or lo reloc
2867      instruction not one we handle.  */
2868   unsigned int unexpected_toc_insn : 1;
2869 };
2870
2871 #define ppc64_elf_tdata(bfd) \
2872   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2873
2874 #define ppc64_tlsld_got(bfd) \
2875   (&ppc64_elf_tdata (bfd)->tlsld_got)
2876
2877 #define is_ppc64_elf(bfd) \
2878   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2879    && elf_object_id (bfd) == PPC64_ELF_DATA)
2880
2881 /* Override the generic function because we store some extras.  */
2882
2883 static bfd_boolean
2884 ppc64_elf_mkobject (bfd *abfd)
2885 {
2886   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2887                                   PPC64_ELF_DATA);
2888 }
2889
2890 /* Fix bad default arch selected for a 64 bit input bfd when the
2891    default is 32 bit.  Also select arch based on apuinfo.  */
2892
2893 static bfd_boolean
2894 ppc64_elf_object_p (bfd *abfd)
2895 {
2896   if (!abfd->arch_info->the_default)
2897     return TRUE;
2898
2899   if (abfd->arch_info->bits_per_word == 32)
2900     {
2901       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2902
2903       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2904         {
2905           /* Relies on arch after 32 bit default being 64 bit default.  */
2906           abfd->arch_info = abfd->arch_info->next;
2907           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2908         }
2909     }
2910   return _bfd_elf_ppc_set_arch (abfd);
2911 }
2912
2913 /* Support for core dump NOTE sections.  */
2914
2915 static bfd_boolean
2916 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2917 {
2918   size_t offset, size;
2919
2920   if (note->descsz != 504)
2921     return FALSE;
2922
2923   /* pr_cursig */
2924   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2925
2926   /* pr_pid */
2927   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2928
2929   /* pr_reg */
2930   offset = 112;
2931   size = 384;
2932
2933   /* Make a ".reg/999" section.  */
2934   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2935                                           size, note->descpos + offset);
2936 }
2937
2938 static bfd_boolean
2939 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2940 {
2941   if (note->descsz != 136)
2942     return FALSE;
2943
2944   elf_tdata (abfd)->core->pid
2945     = bfd_get_32 (abfd, note->descdata + 24);
2946   elf_tdata (abfd)->core->program
2947     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2948   elf_tdata (abfd)->core->command
2949     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2950
2951   return TRUE;
2952 }
2953
2954 static char *
2955 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2956                            ...)
2957 {
2958   switch (note_type)
2959     {
2960     default:
2961       return NULL;
2962
2963     case NT_PRPSINFO:
2964       {
2965         char data[136];
2966         va_list ap;
2967
2968         va_start (ap, note_type);
2969         memset (data, 0, sizeof (data));
2970         strncpy (data + 40, va_arg (ap, const char *), 16);
2971         strncpy (data + 56, va_arg (ap, const char *), 80);
2972         va_end (ap);
2973         return elfcore_write_note (abfd, buf, bufsiz,
2974                                    "CORE", note_type, data, sizeof (data));
2975       }
2976
2977     case NT_PRSTATUS:
2978       {
2979         char data[504];
2980         va_list ap;
2981         long pid;
2982         int cursig;
2983         const void *greg;
2984
2985         va_start (ap, note_type);
2986         memset (data, 0, 112);
2987         pid = va_arg (ap, long);
2988         bfd_put_32 (abfd, pid, data + 32);
2989         cursig = va_arg (ap, int);
2990         bfd_put_16 (abfd, cursig, data + 12);
2991         greg = va_arg (ap, const void *);
2992         memcpy (data + 112, greg, 384);
2993         memset (data + 496, 0, 8);
2994         va_end (ap);
2995         return elfcore_write_note (abfd, buf, bufsiz,
2996                                    "CORE", note_type, data, sizeof (data));
2997       }
2998     }
2999 }
3000
3001 /* Add extra PPC sections.  */
3002
3003 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3004 {
3005   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
3006   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3007   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3008   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3009   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3010   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3011   { NULL,                     0,  0, 0,            0 }
3012 };
3013
3014 enum _ppc64_sec_type {
3015   sec_normal = 0,
3016   sec_opd = 1,
3017   sec_toc = 2
3018 };
3019
3020 struct _ppc64_elf_section_data
3021 {
3022   struct bfd_elf_section_data elf;
3023
3024   union
3025   {
3026     /* An array with one entry for each opd function descriptor,
3027        and some spares since opd entries may be either 16 or 24 bytes.  */
3028 #define OPD_NDX(OFF) ((OFF) >> 4)
3029     struct _opd_sec_data
3030     {
3031       /* Points to the function code section for local opd entries.  */
3032       asection **func_sec;
3033
3034       /* After editing .opd, adjust references to opd local syms.  */
3035       long *adjust;
3036     } opd;
3037
3038     /* An array for toc sections, indexed by offset/8.  */
3039     struct _toc_sec_data
3040     {
3041       /* Specifies the relocation symbol index used at a given toc offset.  */
3042       unsigned *symndx;
3043
3044       /* And the relocation addend.  */
3045       bfd_vma *add;
3046     } toc;
3047   } u;
3048
3049   enum _ppc64_sec_type sec_type:2;
3050
3051   /* Flag set when small branches are detected.  Used to
3052      select suitable defaults for the stub group size.  */
3053   unsigned int has_14bit_branch:1;
3054 };
3055
3056 #define ppc64_elf_section_data(sec) \
3057   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3058
3059 static bfd_boolean
3060 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3061 {
3062   if (!sec->used_by_bfd)
3063     {
3064       struct _ppc64_elf_section_data *sdata;
3065       bfd_size_type amt = sizeof (*sdata);
3066
3067       sdata = bfd_zalloc (abfd, amt);
3068       if (sdata == NULL)
3069         return FALSE;
3070       sec->used_by_bfd = sdata;
3071     }
3072
3073   return _bfd_elf_new_section_hook (abfd, sec);
3074 }
3075
3076 static struct _opd_sec_data *
3077 get_opd_info (asection * sec)
3078 {
3079   if (sec != NULL
3080       && ppc64_elf_section_data (sec) != NULL
3081       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3082     return &ppc64_elf_section_data (sec)->u.opd;
3083   return NULL;
3084 }
3085 \f
3086 /* Parameters for the qsort hook.  */
3087 static bfd_boolean synthetic_relocatable;
3088
3089 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
3090
3091 static int
3092 compare_symbols (const void *ap, const void *bp)
3093 {
3094   const asymbol *a = * (const asymbol **) ap;
3095   const asymbol *b = * (const asymbol **) bp;
3096
3097   /* Section symbols first.  */
3098   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3099     return -1;
3100   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3101     return 1;
3102
3103   /* then .opd symbols.  */
3104   if (strcmp (a->section->name, ".opd") == 0
3105       && strcmp (b->section->name, ".opd") != 0)
3106     return -1;
3107   if (strcmp (a->section->name, ".opd") != 0
3108       && strcmp (b->section->name, ".opd") == 0)
3109     return 1;
3110
3111   /* then other code symbols.  */
3112   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3113       == (SEC_CODE | SEC_ALLOC)
3114       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3115          != (SEC_CODE | SEC_ALLOC))
3116     return -1;
3117
3118   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3119       != (SEC_CODE | SEC_ALLOC)
3120       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3121          == (SEC_CODE | SEC_ALLOC))
3122     return 1;
3123
3124   if (synthetic_relocatable)
3125     {
3126       if (a->section->id < b->section->id)
3127         return -1;
3128
3129       if (a->section->id > b->section->id)
3130         return 1;
3131     }
3132
3133   if (a->value + a->section->vma < b->value + b->section->vma)
3134     return -1;
3135
3136   if (a->value + a->section->vma > b->value + b->section->vma)
3137     return 1;
3138
3139   /* For syms with the same value, prefer strong dynamic global function
3140      syms over other syms.  */
3141   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3142     return -1;
3143
3144   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3145     return 1;
3146
3147   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3148     return -1;
3149
3150   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3151     return 1;
3152
3153   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3154     return -1;
3155
3156   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3157     return 1;
3158
3159   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3160     return -1;
3161
3162   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3163     return 1;
3164
3165   return 0;
3166 }
3167
3168 /* Search SYMS for a symbol of the given VALUE.  */
3169
3170 static asymbol *
3171 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3172 {
3173   long mid;
3174
3175   if (id == (unsigned) -1)
3176     {
3177       while (lo < hi)
3178         {
3179           mid = (lo + hi) >> 1;
3180           if (syms[mid]->value + syms[mid]->section->vma < value)
3181             lo = mid + 1;
3182           else if (syms[mid]->value + syms[mid]->section->vma > value)
3183             hi = mid;
3184           else
3185             return syms[mid];
3186         }
3187     }
3188   else
3189     {
3190       while (lo < hi)
3191         {
3192           mid = (lo + hi) >> 1;
3193           if (syms[mid]->section->id < id)
3194             lo = mid + 1;
3195           else if (syms[mid]->section->id > id)
3196             hi = mid;
3197           else if (syms[mid]->value < value)
3198             lo = mid + 1;
3199           else if (syms[mid]->value > value)
3200             hi = mid;
3201           else
3202             return syms[mid];
3203         }
3204     }
3205   return NULL;
3206 }
3207
3208 static bfd_boolean
3209 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3210 {
3211   bfd_vma vma = *(bfd_vma *) ptr;
3212   return ((section->flags & SEC_ALLOC) != 0
3213           && section->vma <= vma
3214           && vma < section->vma + section->size);
3215 }
3216
3217 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3218    entry syms.  Also generate @plt symbols for the glink branch table.
3219    Returns count of synthetic symbols in RET or -1 on error.  */
3220
3221 static long
3222 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3223                                 long static_count, asymbol **static_syms,
3224                                 long dyn_count, asymbol **dyn_syms,
3225                                 asymbol **ret)
3226 {
3227   asymbol *s;
3228   long i;
3229   long count;
3230   char *names;
3231   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3232   asection *opd = NULL;
3233   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3234   asymbol **syms;
3235   int abi = abiversion (abfd);
3236
3237   *ret = NULL;
3238
3239   if (abi < 2)
3240     {
3241       opd = bfd_get_section_by_name (abfd, ".opd");
3242       if (opd == NULL && abi == 1)
3243         return 0;
3244     }
3245
3246   symcount = static_count;
3247   if (!relocatable)
3248     symcount += dyn_count;
3249   if (symcount == 0)
3250     return 0;
3251
3252   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3253   if (syms == NULL)
3254     return -1;
3255
3256   if (!relocatable && static_count != 0 && dyn_count != 0)
3257     {
3258       /* Use both symbol tables.  */
3259       memcpy (syms, static_syms, static_count * sizeof (*syms));
3260       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3261     }
3262   else if (!relocatable && static_count == 0)
3263     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3264   else
3265     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3266
3267   synthetic_relocatable = relocatable;
3268   qsort (syms, symcount, sizeof (*syms), compare_symbols);
3269
3270   if (!relocatable && symcount > 1)
3271     {
3272       long j;
3273       /* Trim duplicate syms, since we may have merged the normal and
3274          dynamic symbols.  Actually, we only care about syms that have
3275          different values, so trim any with the same value.  */
3276       for (i = 1, j = 1; i < symcount; ++i)
3277         if (syms[i - 1]->value + syms[i - 1]->section->vma
3278             != syms[i]->value + syms[i]->section->vma)
3279           syms[j++] = syms[i];
3280       symcount = j;
3281     }
3282
3283   i = 0;
3284   if (strcmp (syms[i]->section->name, ".opd") == 0)
3285     ++i;
3286   codesecsym = i;
3287
3288   for (; i < symcount; ++i)
3289     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3290          != (SEC_CODE | SEC_ALLOC))
3291         || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3292       break;
3293   codesecsymend = i;
3294
3295   for (; i < symcount; ++i)
3296     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3297       break;
3298   secsymend = i;
3299
3300   for (; i < symcount; ++i)
3301     if (strcmp (syms[i]->section->name, ".opd") != 0)
3302       break;
3303   opdsymend = i;
3304
3305   for (; i < symcount; ++i)
3306     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3307         != (SEC_CODE | SEC_ALLOC))
3308       break;
3309   symcount = i;
3310
3311   count = 0;
3312
3313   if (relocatable)
3314     {
3315       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3316       arelent *r;
3317       size_t size;
3318       long relcount;
3319
3320       if (opdsymend == secsymend)
3321         goto done;
3322
3323       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3324       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3325       if (relcount == 0)
3326         goto done;
3327
3328       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3329         {
3330           count = -1;
3331           goto done;
3332         }
3333
3334       size = 0;
3335       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3336         {
3337           asymbol *sym;
3338
3339           while (r < opd->relocation + relcount
3340                  && r->address < syms[i]->value + opd->vma)
3341             ++r;
3342
3343           if (r == opd->relocation + relcount)
3344             break;
3345
3346           if (r->address != syms[i]->value + opd->vma)
3347             continue;
3348
3349           if (r->howto->type != R_PPC64_ADDR64)
3350             continue;
3351
3352           sym = *r->sym_ptr_ptr;
3353           if (!sym_exists_at (syms, opdsymend, symcount,
3354                               sym->section->id, sym->value + r->addend))
3355             {
3356               ++count;
3357               size += sizeof (asymbol);
3358               size += strlen (syms[i]->name) + 2;
3359             }
3360         }
3361
3362       if (size == 0)
3363         goto done;
3364       s = *ret = bfd_malloc (size);
3365       if (s == NULL)
3366         {
3367           count = -1;
3368           goto done;
3369         }
3370
3371       names = (char *) (s + count);
3372
3373       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3374         {
3375           asymbol *sym;
3376
3377           while (r < opd->relocation + relcount
3378                  && r->address < syms[i]->value + opd->vma)
3379             ++r;
3380
3381           if (r == opd->relocation + relcount)
3382             break;
3383
3384           if (r->address != syms[i]->value + opd->vma)
3385             continue;
3386
3387           if (r->howto->type != R_PPC64_ADDR64)
3388             continue;
3389
3390           sym = *r->sym_ptr_ptr;
3391           if (!sym_exists_at (syms, opdsymend, symcount,
3392                               sym->section->id, sym->value + r->addend))
3393             {
3394               size_t len;
3395
3396               *s = *syms[i];
3397               s->flags |= BSF_SYNTHETIC;
3398               s->section = sym->section;
3399               s->value = sym->value + r->addend;
3400               s->name = names;
3401               *names++ = '.';
3402               len = strlen (syms[i]->name);
3403               memcpy (names, syms[i]->name, len + 1);
3404               names += len + 1;
3405               /* Have udata.p point back to the original symbol this
3406                  synthetic symbol was derived from.  */
3407               s->udata.p = syms[i];
3408               s++;
3409             }
3410         }
3411     }
3412   else
3413     {
3414       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3415       bfd_byte *contents = NULL;
3416       size_t size;
3417       long plt_count = 0;
3418       bfd_vma glink_vma = 0, resolv_vma = 0;
3419       asection *dynamic, *glink = NULL, *relplt = NULL;
3420       arelent *p;
3421
3422       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3423         {
3424         free_contents_and_exit_err:
3425           count = -1;
3426         free_contents_and_exit:
3427           if (contents)
3428             free (contents);
3429           goto done;
3430         }
3431
3432       size = 0;
3433       for (i = secsymend; i < opdsymend; ++i)
3434         {
3435           bfd_vma ent;
3436
3437           /* Ignore bogus symbols.  */
3438           if (syms[i]->value > opd->size - 8)
3439             continue;
3440
3441           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3442           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3443             {
3444               ++count;
3445               size += sizeof (asymbol);
3446               size += strlen (syms[i]->name) + 2;
3447             }
3448         }
3449
3450       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
3451       if (dyn_count != 0
3452           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3453         {
3454           bfd_byte *dynbuf, *extdyn, *extdynend;
3455           size_t extdynsize;
3456           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3457
3458           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3459             goto free_contents_and_exit_err;
3460
3461           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3462           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3463
3464           extdyn = dynbuf;
3465           extdynend = extdyn + dynamic->size;
3466           for (; extdyn < extdynend; extdyn += extdynsize)
3467             {
3468               Elf_Internal_Dyn dyn;
3469               (*swap_dyn_in) (abfd, extdyn, &dyn);
3470
3471               if (dyn.d_tag == DT_NULL)
3472                 break;
3473
3474               if (dyn.d_tag == DT_PPC64_GLINK)
3475                 {
3476                   /* The first glink stub starts at offset 32; see
3477                      comment in ppc64_elf_finish_dynamic_sections. */
3478                   glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
3479                   /* The .glink section usually does not survive the final
3480                      link; search for the section (usually .text) where the
3481                      glink stubs now reside.  */
3482                   glink = bfd_sections_find_if (abfd, section_covers_vma,
3483                                                 &glink_vma);
3484                   break;
3485                 }
3486             }
3487
3488           free (dynbuf);
3489         }
3490
3491       if (glink != NULL)
3492         {
3493           /* Determine __glink trampoline by reading the relative branch
3494              from the first glink stub.  */
3495           bfd_byte buf[4];
3496           unsigned int off = 0;
3497
3498           while (bfd_get_section_contents (abfd, glink, buf,
3499                                            glink_vma + off - glink->vma, 4))
3500             {
3501               unsigned int insn = bfd_get_32 (abfd, buf);
3502               insn ^= B_DOT;
3503               if ((insn & ~0x3fffffc) == 0)
3504                 {
3505                   resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3506                   break;
3507                 }
3508               off += 4;
3509               if (off > 4)
3510                 break;
3511             }
3512
3513           if (resolv_vma)
3514             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3515
3516           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3517           if (relplt != NULL)
3518             {
3519               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3520               if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3521                 goto free_contents_and_exit_err;
3522
3523               plt_count = relplt->size / sizeof (Elf64_External_Rela);
3524               size += plt_count * sizeof (asymbol);
3525
3526               p = relplt->relocation;
3527               for (i = 0; i < plt_count; i++, p++)
3528                 {
3529                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3530                   if (p->addend != 0)
3531                     size += sizeof ("+0x") - 1 + 16;
3532                 }
3533             }
3534         }
3535
3536       if (size == 0)
3537         goto free_contents_and_exit;
3538       s = *ret = bfd_malloc (size);
3539       if (s == NULL)
3540         goto free_contents_and_exit_err;
3541
3542       names = (char *) (s + count + plt_count + (resolv_vma != 0));
3543
3544       for (i = secsymend; i < opdsymend; ++i)
3545         {
3546           bfd_vma ent;
3547
3548           if (syms[i]->value > opd->size - 8)
3549             continue;
3550
3551           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3552           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3553             {
3554               long lo, hi;
3555               size_t len;
3556               asection *sec = abfd->sections;
3557
3558               *s = *syms[i];
3559               lo = codesecsym;
3560               hi = codesecsymend;
3561               while (lo < hi)
3562                 {
3563                   long mid = (lo + hi) >> 1;
3564                   if (syms[mid]->section->vma < ent)
3565                     lo = mid + 1;
3566                   else if (syms[mid]->section->vma > ent)
3567                     hi = mid;
3568                   else
3569                     {
3570                       sec = syms[mid]->section;
3571                       break;
3572                     }
3573                 }
3574
3575               if (lo >= hi && lo > codesecsym)
3576                 sec = syms[lo - 1]->section;
3577
3578               for (; sec != NULL; sec = sec->next)
3579                 {
3580                   if (sec->vma > ent)
3581                     break;
3582                   /* SEC_LOAD may not be set if SEC is from a separate debug
3583                      info file.  */
3584                   if ((sec->flags & SEC_ALLOC) == 0)
3585                     break;
3586                   if ((sec->flags & SEC_CODE) != 0)
3587                     s->section = sec;
3588                 }
3589               s->flags |= BSF_SYNTHETIC;
3590               s->value = ent - s->section->vma;
3591               s->name = names;
3592               *names++ = '.';
3593               len = strlen (syms[i]->name);
3594               memcpy (names, syms[i]->name, len + 1);
3595               names += len + 1;
3596               /* Have udata.p point back to the original symbol this
3597                  synthetic symbol was derived from.  */
3598               s->udata.p = syms[i];
3599               s++;
3600             }
3601         }
3602       free (contents);
3603
3604       if (glink != NULL && relplt != NULL)
3605         {
3606           if (resolv_vma)
3607             {
3608               /* Add a symbol for the main glink trampoline.  */
3609               memset (s, 0, sizeof *s);
3610               s->the_bfd = abfd;
3611               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3612               s->section = glink;
3613               s->value = resolv_vma - glink->vma;
3614               s->name = names;
3615               memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3616               names += sizeof ("__glink_PLTresolve");
3617               s++;
3618               count++;
3619             }
3620
3621           /* FIXME: It would be very much nicer to put sym@plt on the
3622              stub rather than on the glink branch table entry.  The
3623              objdump disassembler would then use a sensible symbol
3624              name on plt calls.  The difficulty in doing so is
3625              a) finding the stubs, and,
3626              b) matching stubs against plt entries, and,
3627              c) there can be multiple stubs for a given plt entry.
3628
3629              Solving (a) could be done by code scanning, but older
3630              ppc64 binaries used different stubs to current code.
3631              (b) is the tricky one since you need to known the toc
3632              pointer for at least one function that uses a pic stub to
3633              be able to calculate the plt address referenced.
3634              (c) means gdb would need to set multiple breakpoints (or
3635              find the glink branch itself) when setting breakpoints
3636              for pending shared library loads.  */
3637           p = relplt->relocation;
3638           for (i = 0; i < plt_count; i++, p++)
3639             {
3640               size_t len;
3641
3642               *s = **p->sym_ptr_ptr;
3643               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
3644                  we are defining a symbol, ensure one of them is set.  */
3645               if ((s->flags & BSF_LOCAL) == 0)
3646                 s->flags |= BSF_GLOBAL;
3647               s->flags |= BSF_SYNTHETIC;
3648               s->section = glink;
3649               s->value = glink_vma - glink->vma;
3650               s->name = names;
3651               s->udata.p = NULL;
3652               len = strlen ((*p->sym_ptr_ptr)->name);
3653               memcpy (names, (*p->sym_ptr_ptr)->name, len);
3654               names += len;
3655               if (p->addend != 0)
3656                 {
3657                   memcpy (names, "+0x", sizeof ("+0x") - 1);
3658                   names += sizeof ("+0x") - 1;
3659                   bfd_sprintf_vma (abfd, names, p->addend);
3660                   names += strlen (names);
3661                 }
3662               memcpy (names, "@plt", sizeof ("@plt"));
3663               names += sizeof ("@plt");
3664               s++;
3665               if (abi < 2)
3666                 {
3667                   glink_vma += 8;
3668                   if (i >= 0x8000)
3669                     glink_vma += 4;
3670                 }
3671               else
3672                 glink_vma += 4;
3673             }
3674           count += plt_count;
3675         }
3676     }
3677
3678  done:
3679   free (syms);
3680   return count;
3681 }
3682 \f
3683 /* The following functions are specific to the ELF linker, while
3684    functions above are used generally.  Those named ppc64_elf_* are
3685    called by the main ELF linker code.  They appear in this file more
3686    or less in the order in which they are called.  eg.
3687    ppc64_elf_check_relocs is called early in the link process,
3688    ppc64_elf_finish_dynamic_sections is one of the last functions
3689    called.
3690
3691    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3692    functions have both a function code symbol and a function descriptor
3693    symbol.  A call to foo in a relocatable object file looks like:
3694
3695    .            .text
3696    .    x:
3697    .            bl      .foo
3698    .            nop
3699
3700    The function definition in another object file might be:
3701
3702    .            .section .opd
3703    .    foo:    .quad   .foo
3704    .            .quad   .TOC.@tocbase
3705    .            .quad   0
3706    .
3707    .            .text
3708    .    .foo:   blr
3709
3710    When the linker resolves the call during a static link, the branch
3711    unsurprisingly just goes to .foo and the .opd information is unused.
3712    If the function definition is in a shared library, things are a little
3713    different:  The call goes via a plt call stub, the opd information gets
3714    copied to the plt, and the linker patches the nop.
3715
3716    .    x:
3717    .            bl      .foo_stub
3718    .            ld      2,40(1)
3719    .
3720    .
3721    .    .foo_stub:
3722    .            std     2,40(1)                 # in practice, the call stub
3723    .            addis   11,2,Lfoo@toc@ha        # is slightly optimized, but
3724    .            addi    11,11,Lfoo@toc@l        # this is the general idea
3725    .            ld      12,0(11)
3726    .            ld      2,8(11)
3727    .            mtctr   12
3728    .            ld      11,16(11)
3729    .            bctr
3730    .
3731    .            .section .plt
3732    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
3733
3734    The "reloc ()" notation is supposed to indicate that the linker emits
3735    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3736    copying.
3737
3738    What are the difficulties here?  Well, firstly, the relocations
3739    examined by the linker in check_relocs are against the function code
3740    sym .foo, while the dynamic relocation in the plt is emitted against
3741    the function descriptor symbol, foo.  Somewhere along the line, we need
3742    to carefully copy dynamic link information from one symbol to the other.
3743    Secondly, the generic part of the elf linker will make .foo a dynamic
3744    symbol as is normal for most other backends.  We need foo dynamic
3745    instead, at least for an application final link.  However, when
3746    creating a shared library containing foo, we need to have both symbols
3747    dynamic so that references to .foo are satisfied during the early
3748    stages of linking.  Otherwise the linker might decide to pull in a
3749    definition from some other object, eg. a static library.
3750
3751    Update: As of August 2004, we support a new convention.  Function
3752    calls may use the function descriptor symbol, ie. "bl foo".  This
3753    behaves exactly as "bl .foo".  */
3754
3755 /* Of those relocs that might be copied as dynamic relocs, this function
3756    selects those that must be copied when linking a shared library,
3757    even when the symbol is local.  */
3758
3759 static int
3760 must_be_dyn_reloc (struct bfd_link_info *info,
3761                    enum elf_ppc64_reloc_type r_type)
3762 {
3763   switch (r_type)
3764     {
3765     default:
3766       return 1;
3767
3768     case R_PPC64_REL32:
3769     case R_PPC64_REL64:
3770     case R_PPC64_REL30:
3771       return 0;
3772
3773     case R_PPC64_TPREL16:
3774     case R_PPC64_TPREL16_LO:
3775     case R_PPC64_TPREL16_HI:
3776     case R_PPC64_TPREL16_HA:
3777     case R_PPC64_TPREL16_DS:
3778     case R_PPC64_TPREL16_LO_DS:
3779     case R_PPC64_TPREL16_HIGH:
3780     case R_PPC64_TPREL16_HIGHA:
3781     case R_PPC64_TPREL16_HIGHER:
3782     case R_PPC64_TPREL16_HIGHERA:
3783     case R_PPC64_TPREL16_HIGHEST:
3784     case R_PPC64_TPREL16_HIGHESTA:
3785     case R_PPC64_TPREL64:
3786       return !bfd_link_executable (info);
3787     }
3788 }
3789
3790 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3791    copying dynamic variables from a shared lib into an app's dynbss
3792    section, and instead use a dynamic relocation to point into the
3793    shared lib.  With code that gcc generates, it's vital that this be
3794    enabled;  In the PowerPC64 ABI, the address of a function is actually
3795    the address of a function descriptor, which resides in the .opd
3796    section.  gcc uses the descriptor directly rather than going via the
3797    GOT as some other ABI's do, which means that initialized function
3798    pointers must reference the descriptor.  Thus, a function pointer
3799    initialized to the address of a function in a shared library will
3800    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3801    redefines the function descriptor symbol to point to the copy.  This
3802    presents a problem as a plt entry for that function is also
3803    initialized from the function descriptor symbol and the copy reloc
3804    may not be initialized first.  */
3805 #define ELIMINATE_COPY_RELOCS 1
3806
3807 /* Section name for stubs is the associated section name plus this
3808    string.  */
3809 #define STUB_SUFFIX ".stub"
3810
3811 /* Linker stubs.
3812    ppc_stub_long_branch:
3813    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3814    destination, but a 24 bit branch in a stub section will reach.
3815    .    b       dest
3816
3817    ppc_stub_plt_branch:
3818    Similar to the above, but a 24 bit branch in the stub section won't
3819    reach its destination.
3820    .    addis   %r11,%r2,xxx@toc@ha
3821    .    ld      %r12,xxx@toc@l(%r11)
3822    .    mtctr   %r12
3823    .    bctr
3824
3825    ppc_stub_plt_call:
3826    Used to call a function in a shared library.  If it so happens that
3827    the plt entry referenced crosses a 64k boundary, then an extra
3828    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3829    .    std     %r2,40(%r1)
3830    .    addis   %r11,%r2,xxx@toc@ha
3831    .    ld      %r12,xxx+0@toc@l(%r11)
3832    .    mtctr   %r12
3833    .    ld      %r2,xxx+8@toc@l(%r11)
3834    .    ld      %r11,xxx+16@toc@l(%r11)
3835    .    bctr
3836
3837    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3838    code to adjust the value and save r2 to support multiple toc sections.
3839    A ppc_stub_long_branch with an r2 offset looks like:
3840    .    std     %r2,40(%r1)
3841    .    addis   %r2,%r2,off@ha
3842    .    addi    %r2,%r2,off@l
3843    .    b       dest
3844
3845    A ppc_stub_plt_branch with an r2 offset looks like:
3846    .    std     %r2,40(%r1)
3847    .    addis   %r11,%r2,xxx@toc@ha
3848    .    ld      %r12,xxx@toc@l(%r11)
3849    .    addis   %r2,%r2,off@ha
3850    .    addi    %r2,%r2,off@l
3851    .    mtctr   %r12
3852    .    bctr
3853
3854    In cases where the "addis" instruction would add zero, the "addis" is
3855    omitted and following instructions modified slightly in some cases.
3856 */
3857
3858 enum ppc_stub_type {
3859   ppc_stub_none,
3860   ppc_stub_long_branch,
3861   ppc_stub_long_branch_r2off,
3862   ppc_stub_plt_branch,
3863   ppc_stub_plt_branch_r2off,
3864   ppc_stub_plt_call,
3865   ppc_stub_plt_call_r2save,
3866   ppc_stub_global_entry,
3867   ppc_stub_save_res
3868 };
3869
3870 /* Information on stub grouping.  */
3871 struct map_stub
3872 {
3873   /* The stub section.  */
3874   asection *stub_sec;
3875   /* This is the section to which stubs in the group will be attached.  */
3876   asection *link_sec;
3877   /* Next group.  */
3878   struct map_stub *next;
3879   /* Whether to emit a copy of register save/restore functions in this
3880      group.  */
3881   int needs_save_res;
3882 };
3883
3884 struct ppc_stub_hash_entry {
3885
3886   /* Base hash table entry structure.  */
3887   struct bfd_hash_entry root;
3888
3889   enum ppc_stub_type stub_type;
3890
3891   /* Group information.  */
3892   struct map_stub *group;
3893
3894   /* Offset within stub_sec of the beginning of this stub.  */
3895   bfd_vma stub_offset;
3896
3897   /* Given the symbol's value and its section we can determine its final
3898      value when building the stubs (so the stub knows where to jump.  */
3899   bfd_vma target_value;
3900   asection *target_section;
3901
3902   /* The symbol table entry, if any, that this was derived from.  */
3903   struct ppc_link_hash_entry *h;
3904   struct plt_entry *plt_ent;
3905
3906   /* Symbol st_other.  */
3907   unsigned char other;
3908 };
3909
3910 struct ppc_branch_hash_entry {
3911
3912   /* Base hash table entry structure.  */
3913   struct bfd_hash_entry root;
3914
3915   /* Offset within branch lookup table.  */
3916   unsigned int offset;
3917
3918   /* Generation marker.  */
3919   unsigned int iter;
3920 };
3921
3922 /* Used to track dynamic relocations for local symbols.  */
3923 struct ppc_dyn_relocs
3924 {
3925   struct ppc_dyn_relocs *next;
3926
3927   /* The input section of the reloc.  */
3928   asection *sec;
3929
3930   /* Total number of relocs copied for the input section.  */
3931   unsigned int count : 31;
3932
3933   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
3934   unsigned int ifunc : 1;
3935 };
3936
3937 struct ppc_link_hash_entry
3938 {
3939   struct elf_link_hash_entry elf;
3940
3941   union {
3942     /* A pointer to the most recently used stub hash entry against this
3943        symbol.  */
3944     struct ppc_stub_hash_entry *stub_cache;
3945
3946     /* A pointer to the next symbol starting with a '.'  */
3947     struct ppc_link_hash_entry *next_dot_sym;
3948   } u;
3949
3950   /* Track dynamic relocs copied for this symbol.  */
3951   struct elf_dyn_relocs *dyn_relocs;
3952
3953   /* Link between function code and descriptor symbols.  */
3954   struct ppc_link_hash_entry *oh;
3955
3956   /* Flag function code and descriptor symbols.  */
3957   unsigned int is_func:1;
3958   unsigned int is_func_descriptor:1;
3959   unsigned int fake:1;
3960
3961   /* Whether global opd/toc sym has been adjusted or not.
3962      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3963      should be set for all globals defined in any opd/toc section.  */
3964   unsigned int adjust_done:1;
3965
3966   /* Set if we twiddled this symbol to weak at some stage.  */
3967   unsigned int was_undefined:1;
3968
3969   /* Set if this is an out-of-line register save/restore function,
3970      with non-standard calling convention.  */
3971   unsigned int save_res:1;
3972
3973   /* Contexts in which symbol is used in the GOT (or TOC).
3974      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3975      corresponding relocs are encountered during check_relocs.
3976      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3977      indicate the corresponding GOT entry type is not needed.
3978      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3979      a TPREL one.  We use a separate flag rather than setting TPREL
3980      just for convenience in distinguishing the two cases.  */
3981 #define TLS_GD           1      /* GD reloc. */
3982 #define TLS_LD           2      /* LD reloc. */
3983 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
3984 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
3985 #define TLS_TLS         16      /* Any TLS reloc.  */
3986 #define TLS_EXPLICIT    32      /* Marks TOC section TLS relocs. */
3987 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
3988 #define PLT_IFUNC      128      /* STT_GNU_IFUNC.  */
3989   unsigned char tls_mask;
3990 };
3991
3992 /* ppc64 ELF linker hash table.  */
3993
3994 struct ppc_link_hash_table
3995 {
3996   struct elf_link_hash_table elf;
3997
3998   /* The stub hash table.  */
3999   struct bfd_hash_table stub_hash_table;
4000
4001   /* Another hash table for plt_branch stubs.  */
4002   struct bfd_hash_table branch_hash_table;
4003
4004   /* Hash table for function prologue tocsave.  */
4005   htab_t tocsave_htab;
4006
4007   /* Various options and other info passed from the linker.  */
4008   struct ppc64_elf_params *params;
4009
4010   /* The size of sec_info below.  */
4011   unsigned int sec_info_arr_size;
4012
4013   /* Per-section array of extra section info.  Done this way rather
4014      than as part of ppc64_elf_section_data so we have the info for
4015      non-ppc64 sections.  */
4016   struct
4017   {
4018     /* Along with elf_gp, specifies the TOC pointer used by this section.  */
4019     bfd_vma toc_off;
4020
4021     union
4022     {
4023       /* The section group that this section belongs to.  */
4024       struct map_stub *group;
4025       /* A temp section list pointer.  */
4026       asection *list;
4027     } u;
4028   } *sec_info;
4029
4030   /* Linked list of groups.  */
4031   struct map_stub *group;
4032
4033   /* Temp used when calculating TOC pointers.  */
4034   bfd_vma toc_curr;
4035   bfd *toc_bfd;
4036   asection *toc_first_sec;
4037
4038   /* Used when adding symbols.  */
4039   struct ppc_link_hash_entry *dot_syms;
4040
4041   /* Shortcuts to get to dynamic linker sections.  */
4042   asection *dynbss;
4043   asection *relbss;
4044   asection *glink;
4045   asection *sfpr;
4046   asection *brlt;
4047   asection *relbrlt;
4048   asection *glink_eh_frame;
4049
4050   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
4051   struct ppc_link_hash_entry *tls_get_addr;
4052   struct ppc_link_hash_entry *tls_get_addr_fd;
4053
4054   /* The size of reliplt used by got entry relocs.  */
4055   bfd_size_type got_reli_size;
4056
4057   /* Statistics.  */
4058   unsigned long stub_count[ppc_stub_global_entry];
4059
4060   /* Number of stubs against global syms.  */
4061   unsigned long stub_globals;
4062
4063   /* Set if we're linking code with function descriptors.  */
4064   unsigned int opd_abi:1;
4065
4066   /* Support for multiple toc sections.  */
4067   unsigned int do_multi_toc:1;
4068   unsigned int multi_toc_needed:1;
4069   unsigned int second_toc_pass:1;
4070   unsigned int do_toc_opt:1;
4071
4072   /* Set on error.  */
4073   unsigned int stub_error:1;
4074
4075   /* Temp used by ppc64_elf_before_check_relocs.  */
4076   unsigned int twiddled_syms:1;
4077
4078   /* Incremented every time we size stubs.  */
4079   unsigned int stub_iteration;
4080
4081   /* Small local sym cache.  */
4082   struct sym_cache sym_cache;
4083 };
4084
4085 /* Rename some of the generic section flags to better document how they
4086    are used here.  */
4087
4088 /* Nonzero if this section has TLS related relocations.  */
4089 #define has_tls_reloc sec_flg0
4090
4091 /* Nonzero if this section has a call to __tls_get_addr.  */
4092 #define has_tls_get_addr_call sec_flg1
4093
4094 /* Nonzero if this section has any toc or got relocs.  */
4095 #define has_toc_reloc sec_flg2
4096
4097 /* Nonzero if this section has a call to another section that uses
4098    the toc or got.  */
4099 #define makes_toc_func_call sec_flg3
4100
4101 /* Recursion protection when determining above flag.  */
4102 #define call_check_in_progress sec_flg4
4103 #define call_check_done sec_flg5
4104
4105 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
4106
4107 #define ppc_hash_table(p) \
4108   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4109   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4110
4111 #define ppc_stub_hash_lookup(table, string, create, copy) \
4112   ((struct ppc_stub_hash_entry *) \
4113    bfd_hash_lookup ((table), (string), (create), (copy)))
4114
4115 #define ppc_branch_hash_lookup(table, string, create, copy) \
4116   ((struct ppc_branch_hash_entry *) \
4117    bfd_hash_lookup ((table), (string), (create), (copy)))
4118
4119 /* Create an entry in the stub hash table.  */
4120
4121 static struct bfd_hash_entry *
4122 stub_hash_newfunc (struct bfd_hash_entry *entry,
4123                    struct bfd_hash_table *table,
4124                    const char *string)
4125 {
4126   /* Allocate the structure if it has not already been allocated by a
4127      subclass.  */
4128   if (entry == NULL)
4129     {
4130       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4131       if (entry == NULL)
4132         return entry;
4133     }
4134
4135   /* Call the allocation method of the superclass.  */
4136   entry = bfd_hash_newfunc (entry, table, string);
4137   if (entry != NULL)
4138     {
4139       struct ppc_stub_hash_entry *eh;
4140
4141       /* Initialize the local fields.  */
4142       eh = (struct ppc_stub_hash_entry *) entry;
4143       eh->stub_type = ppc_stub_none;
4144       eh->group = NULL;
4145       eh->stub_offset = 0;
4146       eh->target_value = 0;
4147       eh->target_section = NULL;
4148       eh->h = NULL;
4149       eh->plt_ent = NULL;
4150       eh->other = 0;
4151     }
4152
4153   return entry;
4154 }
4155
4156 /* Create an entry in the branch hash table.  */
4157
4158 static struct bfd_hash_entry *
4159 branch_hash_newfunc (struct bfd_hash_entry *entry,
4160                      struct bfd_hash_table *table,
4161                      const char *string)
4162 {
4163   /* Allocate the structure if it has not already been allocated by a
4164      subclass.  */
4165   if (entry == NULL)
4166     {
4167       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4168       if (entry == NULL)
4169         return entry;
4170     }
4171
4172   /* Call the allocation method of the superclass.  */
4173   entry = bfd_hash_newfunc (entry, table, string);
4174   if (entry != NULL)
4175     {
4176       struct ppc_branch_hash_entry *eh;
4177
4178       /* Initialize the local fields.  */
4179       eh = (struct ppc_branch_hash_entry *) entry;
4180       eh->offset = 0;
4181       eh->iter = 0;
4182     }
4183
4184   return entry;
4185 }
4186
4187 /* Create an entry in a ppc64 ELF linker hash table.  */
4188
4189 static struct bfd_hash_entry *
4190 link_hash_newfunc (struct bfd_hash_entry *entry,
4191                    struct bfd_hash_table *table,
4192                    const char *string)
4193 {
4194   /* Allocate the structure if it has not already been allocated by a
4195      subclass.  */
4196   if (entry == NULL)
4197     {
4198       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4199       if (entry == NULL)
4200         return entry;
4201     }
4202
4203   /* Call the allocation method of the superclass.  */
4204   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4205   if (entry != NULL)
4206     {
4207       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4208
4209       memset (&eh->u.stub_cache, 0,
4210               (sizeof (struct ppc_link_hash_entry)
4211                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4212
4213       /* When making function calls, old ABI code references function entry
4214          points (dot symbols), while new ABI code references the function
4215          descriptor symbol.  We need to make any combination of reference and
4216          definition work together, without breaking archive linking.
4217
4218          For a defined function "foo" and an undefined call to "bar":
4219          An old object defines "foo" and ".foo", references ".bar" (possibly
4220          "bar" too).
4221          A new object defines "foo" and references "bar".
4222
4223          A new object thus has no problem with its undefined symbols being
4224          satisfied by definitions in an old object.  On the other hand, the
4225          old object won't have ".bar" satisfied by a new object.
4226
4227          Keep a list of newly added dot-symbols.  */
4228
4229       if (string[0] == '.')
4230         {
4231           struct ppc_link_hash_table *htab;
4232
4233           htab = (struct ppc_link_hash_table *) table;
4234           eh->u.next_dot_sym = htab->dot_syms;
4235           htab->dot_syms = eh;
4236         }
4237     }
4238
4239   return entry;
4240 }
4241
4242 struct tocsave_entry {
4243   asection *sec;
4244   bfd_vma offset;
4245 };
4246
4247 static hashval_t
4248 tocsave_htab_hash (const void *p)
4249 {
4250   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4251   return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4252 }
4253
4254 static int
4255 tocsave_htab_eq (const void *p1, const void *p2)
4256 {
4257   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4258   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4259   return e1->sec == e2->sec && e1->offset == e2->offset;
4260 }
4261
4262 /* Destroy a ppc64 ELF linker hash table.  */
4263
4264 static void
4265 ppc64_elf_link_hash_table_free (bfd *obfd)
4266 {
4267   struct ppc_link_hash_table *htab;
4268
4269   htab = (struct ppc_link_hash_table *) obfd->link.hash;
4270   if (htab->tocsave_htab)
4271     htab_delete (htab->tocsave_htab);
4272   bfd_hash_table_free (&htab->branch_hash_table);
4273   bfd_hash_table_free (&htab->stub_hash_table);
4274   _bfd_elf_link_hash_table_free (obfd);
4275 }
4276
4277 /* Create a ppc64 ELF linker hash table.  */
4278
4279 static struct bfd_link_hash_table *
4280 ppc64_elf_link_hash_table_create (bfd *abfd)
4281 {
4282   struct ppc_link_hash_table *htab;
4283   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4284
4285   htab = bfd_zmalloc (amt);
4286   if (htab == NULL)
4287     return NULL;
4288
4289   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4290                                       sizeof (struct ppc_link_hash_entry),
4291                                       PPC64_ELF_DATA))
4292     {
4293       free (htab);
4294       return NULL;
4295     }
4296
4297   /* Init the stub hash table too.  */
4298   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4299                             sizeof (struct ppc_stub_hash_entry)))
4300     {
4301       _bfd_elf_link_hash_table_free (abfd);
4302       return NULL;
4303     }
4304
4305   /* And the branch hash table.  */
4306   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4307                             sizeof (struct ppc_branch_hash_entry)))
4308     {
4309       bfd_hash_table_free (&htab->stub_hash_table);
4310       _bfd_elf_link_hash_table_free (abfd);
4311       return NULL;
4312     }
4313
4314   htab->tocsave_htab = htab_try_create (1024,
4315                                         tocsave_htab_hash,
4316                                         tocsave_htab_eq,
4317                                         NULL);
4318   if (htab->tocsave_htab == NULL)
4319     {
4320       ppc64_elf_link_hash_table_free (abfd);
4321       return NULL;
4322     }
4323   htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4324
4325   /* Initializing two fields of the union is just cosmetic.  We really
4326      only care about glist, but when compiled on a 32-bit host the
4327      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
4328      debugger inspection of these fields look nicer.  */
4329   htab->elf.init_got_refcount.refcount = 0;
4330   htab->elf.init_got_refcount.glist = NULL;
4331   htab->elf.init_plt_refcount.refcount = 0;
4332   htab->elf.init_plt_refcount.glist = NULL;
4333   htab->elf.init_got_offset.offset = 0;
4334   htab->elf.init_got_offset.glist = NULL;
4335   htab->elf.init_plt_offset.offset = 0;
4336   htab->elf.init_plt_offset.glist = NULL;
4337
4338   return &htab->elf.root;
4339 }
4340
4341 /* Create sections for linker generated code.  */
4342
4343 static bfd_boolean
4344 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4345 {
4346   struct ppc_link_hash_table *htab;
4347   flagword flags;
4348
4349   htab = ppc_hash_table (info);
4350
4351   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4352            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4353   if (htab->params->save_restore_funcs)
4354     {
4355       /* Create .sfpr for code to save and restore fp regs.  */
4356       htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4357                                                        flags);
4358       if (htab->sfpr == NULL
4359           || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4360         return FALSE;
4361     }
4362
4363   if (bfd_link_relocatable (info))
4364     return TRUE;
4365
4366   /* Create .glink for lazy dynamic linking support.  */
4367   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4368                                                     flags);
4369   if (htab->glink == NULL
4370       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4371     return FALSE;
4372
4373   if (!info->no_ld_generated_unwind_info)
4374     {
4375       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4376                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4377       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4378                                                                  ".eh_frame",
4379                                                                  flags);
4380       if (htab->glink_eh_frame == NULL
4381           || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4382         return FALSE;
4383     }
4384
4385   flags = SEC_ALLOC | SEC_LINKER_CREATED;
4386   htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4387   if (htab->elf.iplt == NULL
4388       || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4389     return FALSE;
4390
4391   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4392            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4393   htab->elf.irelplt
4394     = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4395   if (htab->elf.irelplt == NULL
4396       || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4397     return FALSE;
4398
4399   /* Create branch lookup table for plt_branch stubs.  */
4400   flags = (SEC_ALLOC | SEC_LOAD
4401            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4402   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4403                                                    flags);
4404   if (htab->brlt == NULL
4405       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4406     return FALSE;
4407
4408   if (!bfd_link_pic (info))
4409     return TRUE;
4410
4411   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4412            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4413   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4414                                                       ".rela.branch_lt",
4415                                                       flags);
4416   if (htab->relbrlt == NULL
4417       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4418     return FALSE;
4419
4420   return TRUE;
4421 }
4422
4423 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
4424
4425 bfd_boolean
4426 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4427                          struct ppc64_elf_params *params)
4428 {
4429   struct ppc_link_hash_table *htab;
4430
4431   elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4432
4433 /* Always hook our dynamic sections into the first bfd, which is the
4434    linker created stub bfd.  This ensures that the GOT header is at
4435    the start of the output TOC section.  */
4436   htab = ppc_hash_table (info);
4437   htab->elf.dynobj = params->stub_bfd;
4438   htab->params = params;
4439
4440   return create_linkage_sections (htab->elf.dynobj, info);
4441 }
4442
4443 /* Build a name for an entry in the stub hash table.  */
4444
4445 static char *
4446 ppc_stub_name (const asection *input_section,
4447                const asection *sym_sec,
4448                const struct ppc_link_hash_entry *h,
4449                const Elf_Internal_Rela *rel)
4450 {
4451   char *stub_name;
4452   ssize_t len;
4453
4454   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4455      offsets from a sym as a branch target?  In fact, we could
4456      probably assume the addend is always zero.  */
4457   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4458
4459   if (h)
4460     {
4461       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4462       stub_name = bfd_malloc (len);
4463       if (stub_name == NULL)
4464         return stub_name;
4465
4466       len = sprintf (stub_name, "%08x.%s+%x",
4467                      input_section->id & 0xffffffff,
4468                      h->elf.root.root.string,
4469                      (int) rel->r_addend & 0xffffffff);
4470     }
4471   else
4472     {
4473       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4474       stub_name = bfd_malloc (len);
4475       if (stub_name == NULL)
4476         return stub_name;
4477
4478       len = sprintf (stub_name, "%08x.%x:%x+%x",
4479                      input_section->id & 0xffffffff,
4480                      sym_sec->id & 0xffffffff,
4481                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4482                      (int) rel->r_addend & 0xffffffff);
4483     }
4484   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4485     stub_name[len - 2] = 0;
4486   return stub_name;
4487 }
4488
4489 /* Look up an entry in the stub hash.  Stub entries are cached because
4490    creating the stub name takes a bit of time.  */
4491
4492 static struct ppc_stub_hash_entry *
4493 ppc_get_stub_entry (const asection *input_section,
4494                     const asection *sym_sec,
4495                     struct ppc_link_hash_entry *h,
4496                     const Elf_Internal_Rela *rel,
4497                     struct ppc_link_hash_table *htab)
4498 {
4499   struct ppc_stub_hash_entry *stub_entry;
4500   struct map_stub *group;
4501
4502   /* If this input section is part of a group of sections sharing one
4503      stub section, then use the id of the first section in the group.
4504      Stub names need to include a section id, as there may well be
4505      more than one stub used to reach say, printf, and we need to
4506      distinguish between them.  */
4507   group = htab->sec_info[input_section->id].u.group;
4508   if (group == NULL)
4509     return NULL;
4510
4511   if (h != NULL && h->u.stub_cache != NULL
4512       && h->u.stub_cache->h == h
4513       && h->u.stub_cache->group == group)
4514     {
4515       stub_entry = h->u.stub_cache;
4516     }
4517   else
4518     {
4519       char *stub_name;
4520
4521       stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4522       if (stub_name == NULL)
4523         return NULL;
4524
4525       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4526                                          stub_name, FALSE, FALSE);
4527       if (h != NULL)
4528         h->u.stub_cache = stub_entry;
4529
4530       free (stub_name);
4531     }
4532
4533   return stub_entry;
4534 }
4535
4536 /* Add a new stub entry to the stub hash.  Not all fields of the new
4537    stub entry are initialised.  */
4538
4539 static struct ppc_stub_hash_entry *
4540 ppc_add_stub (const char *stub_name,
4541               asection *section,
4542               struct bfd_link_info *info)
4543 {
4544   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4545   struct map_stub *group;
4546   asection *link_sec;
4547   asection *stub_sec;
4548   struct ppc_stub_hash_entry *stub_entry;
4549
4550   group = htab->sec_info[section->id].u.group;
4551   link_sec = group->link_sec;
4552   stub_sec = group->stub_sec;
4553   if (stub_sec == NULL)
4554     {
4555       size_t namelen;
4556       bfd_size_type len;
4557       char *s_name;
4558
4559       namelen = strlen (link_sec->name);
4560       len = namelen + sizeof (STUB_SUFFIX);
4561       s_name = bfd_alloc (htab->params->stub_bfd, len);
4562       if (s_name == NULL)
4563         return NULL;
4564
4565       memcpy (s_name, link_sec->name, namelen);
4566       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4567       stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4568       if (stub_sec == NULL)
4569         return NULL;
4570       group->stub_sec = stub_sec;
4571     }
4572
4573   /* Enter this entry into the linker stub hash table.  */
4574   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4575                                      TRUE, FALSE);
4576   if (stub_entry == NULL)
4577     {
4578       info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4579                               section->owner, stub_name);
4580       return NULL;
4581     }
4582
4583   stub_entry->group = group;
4584   stub_entry->stub_offset = 0;
4585   return stub_entry;
4586 }
4587
4588 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4589    not already done.  */
4590
4591 static bfd_boolean
4592 create_got_section (bfd *abfd, struct bfd_link_info *info)
4593 {
4594   asection *got, *relgot;
4595   flagword flags;
4596   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4597
4598   if (!is_ppc64_elf (abfd))
4599     return FALSE;
4600   if (htab == NULL)
4601     return FALSE;
4602
4603   if (!htab->elf.sgot
4604       && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4605     return FALSE;
4606
4607   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4608            | SEC_LINKER_CREATED);
4609
4610   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4611   if (!got
4612       || !bfd_set_section_alignment (abfd, got, 3))
4613     return FALSE;
4614
4615   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4616                                                flags | SEC_READONLY);
4617   if (!relgot
4618       || ! bfd_set_section_alignment (abfd, relgot, 3))
4619     return FALSE;
4620
4621   ppc64_elf_tdata (abfd)->got = got;
4622   ppc64_elf_tdata (abfd)->relgot = relgot;
4623   return TRUE;
4624 }
4625
4626 /* Create the dynamic sections, and set up shortcuts.  */
4627
4628 static bfd_boolean
4629 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4630 {
4631   struct ppc_link_hash_table *htab;
4632
4633   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4634     return FALSE;
4635
4636   htab = ppc_hash_table (info);
4637   if (htab == NULL)
4638     return FALSE;
4639
4640   htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
4641   if (!bfd_link_pic (info))
4642     htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
4643
4644   if (!htab->elf.sgot || !htab->elf.splt || !htab->elf.srelplt || !htab->dynbss
4645       || (!bfd_link_pic (info) && !htab->relbss))
4646     abort ();
4647
4648   return TRUE;
4649 }
4650
4651 /* Follow indirect and warning symbol links.  */
4652
4653 static inline struct bfd_link_hash_entry *
4654 follow_link (struct bfd_link_hash_entry *h)
4655 {
4656   while (h->type == bfd_link_hash_indirect
4657          || h->type == bfd_link_hash_warning)
4658     h = h->u.i.link;
4659   return h;
4660 }
4661
4662 static inline struct elf_link_hash_entry *
4663 elf_follow_link (struct elf_link_hash_entry *h)
4664 {
4665   return (struct elf_link_hash_entry *) follow_link (&h->root);
4666 }
4667
4668 static inline struct ppc_link_hash_entry *
4669 ppc_follow_link (struct ppc_link_hash_entry *h)
4670 {
4671   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4672 }
4673
4674 /* Merge PLT info on FROM with that on TO.  */
4675
4676 static void
4677 move_plt_plist (struct ppc_link_hash_entry *from,
4678                 struct ppc_link_hash_entry *to)
4679 {
4680   if (from->elf.plt.plist != NULL)
4681     {
4682       if (to->elf.plt.plist != NULL)
4683         {
4684           struct plt_entry **entp;
4685           struct plt_entry *ent;
4686
4687           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4688             {
4689               struct plt_entry *dent;
4690
4691               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4692                 if (dent->addend == ent->addend)
4693                   {
4694                     dent->plt.refcount += ent->plt.refcount;
4695                     *entp = ent->next;
4696                     break;
4697                   }
4698               if (dent == NULL)
4699                 entp = &ent->next;
4700             }
4701           *entp = to->elf.plt.plist;
4702         }
4703
4704       to->elf.plt.plist = from->elf.plt.plist;
4705       from->elf.plt.plist = NULL;
4706     }
4707 }
4708
4709 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
4710
4711 static void
4712 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4713                                 struct elf_link_hash_entry *dir,
4714                                 struct elf_link_hash_entry *ind)
4715 {
4716   struct ppc_link_hash_entry *edir, *eind;
4717
4718   edir = (struct ppc_link_hash_entry *) dir;
4719   eind = (struct ppc_link_hash_entry *) ind;
4720
4721   edir->is_func |= eind->is_func;
4722   edir->is_func_descriptor |= eind->is_func_descriptor;
4723   edir->tls_mask |= eind->tls_mask;
4724   if (eind->oh != NULL)
4725     edir->oh = ppc_follow_link (eind->oh);
4726
4727   /* If called to transfer flags for a weakdef during processing
4728      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4729      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
4730   if (!(ELIMINATE_COPY_RELOCS
4731         && eind->elf.root.type != bfd_link_hash_indirect
4732         && edir->elf.dynamic_adjusted))
4733     edir->elf.non_got_ref |= eind->elf.non_got_ref;
4734
4735   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4736   edir->elf.ref_regular |= eind->elf.ref_regular;
4737   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4738   edir->elf.needs_plt |= eind->elf.needs_plt;
4739   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4740
4741   /* Copy over any dynamic relocs we may have on the indirect sym.  */
4742   if (eind->dyn_relocs != NULL)
4743     {
4744       if (edir->dyn_relocs != NULL)
4745         {
4746           struct elf_dyn_relocs **pp;
4747           struct elf_dyn_relocs *p;
4748
4749           /* Add reloc counts against the indirect sym to the direct sym
4750              list.  Merge any entries against the same section.  */
4751           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4752             {
4753               struct elf_dyn_relocs *q;
4754
4755               for (q = edir->dyn_relocs; q != NULL; q = q->next)
4756                 if (q->sec == p->sec)
4757                   {
4758                     q->pc_count += p->pc_count;
4759                     q->count += p->count;
4760                     *pp = p->next;
4761                     break;
4762                   }
4763               if (q == NULL)
4764                 pp = &p->next;
4765             }
4766           *pp = edir->dyn_relocs;
4767         }
4768
4769       edir->dyn_relocs = eind->dyn_relocs;
4770       eind->dyn_relocs = NULL;
4771     }
4772
4773   /* If we were called to copy over info for a weak sym, that's all.
4774      You might think dyn_relocs need not be copied over;  After all,
4775      both syms will be dynamic or both non-dynamic so we're just
4776      moving reloc accounting around.  However, ELIMINATE_COPY_RELOCS
4777      code in ppc64_elf_adjust_dynamic_symbol needs to check for
4778      dyn_relocs in read-only sections, and it does so on what is the
4779      DIR sym here.  */
4780   if (eind->elf.root.type != bfd_link_hash_indirect)
4781     return;
4782
4783   /* Copy over got entries that we may have already seen to the
4784      symbol which just became indirect.  */
4785   if (eind->elf.got.glist != NULL)
4786     {
4787       if (edir->elf.got.glist != NULL)
4788         {
4789           struct got_entry **entp;
4790           struct got_entry *ent;
4791
4792           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4793             {
4794               struct got_entry *dent;
4795
4796               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4797                 if (dent->addend == ent->addend
4798                     && dent->owner == ent->owner
4799                     && dent->tls_type == ent->tls_type)
4800                   {
4801                     dent->got.refcount += ent->got.refcount;
4802                     *entp = ent->next;
4803                     break;
4804                   }
4805               if (dent == NULL)
4806                 entp = &ent->next;
4807             }
4808           *entp = edir->elf.got.glist;
4809         }
4810
4811       edir->elf.got.glist = eind->elf.got.glist;
4812       eind->elf.got.glist = NULL;
4813     }
4814
4815   /* And plt entries.  */
4816   move_plt_plist (eind, edir);
4817
4818   if (eind->elf.dynindx != -1)
4819     {
4820       if (edir->elf.dynindx != -1)
4821         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4822                                 edir->elf.dynstr_index);
4823       edir->elf.dynindx = eind->elf.dynindx;
4824       edir->elf.dynstr_index = eind->elf.dynstr_index;
4825       eind->elf.dynindx = -1;
4826       eind->elf.dynstr_index = 0;
4827     }
4828 }
4829
4830 /* Find the function descriptor hash entry from the given function code
4831    hash entry FH.  Link the entries via their OH fields.  */
4832
4833 static struct ppc_link_hash_entry *
4834 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4835 {
4836   struct ppc_link_hash_entry *fdh = fh->oh;
4837
4838   if (fdh == NULL)
4839     {
4840       const char *fd_name = fh->elf.root.root.string + 1;
4841
4842       fdh = (struct ppc_link_hash_entry *)
4843         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4844       if (fdh == NULL)
4845         return fdh;
4846
4847       fdh->is_func_descriptor = 1;
4848       fdh->oh = fh;
4849       fh->is_func = 1;
4850       fh->oh = fdh;
4851     }
4852
4853   return ppc_follow_link (fdh);
4854 }
4855
4856 /* Make a fake function descriptor sym for the code sym FH.  */
4857
4858 static struct ppc_link_hash_entry *
4859 make_fdh (struct bfd_link_info *info,
4860           struct ppc_link_hash_entry *fh)
4861 {
4862   bfd *abfd;
4863   asymbol *newsym;
4864   struct bfd_link_hash_entry *bh;
4865   struct ppc_link_hash_entry *fdh;
4866
4867   abfd = fh->elf.root.u.undef.abfd;
4868   newsym = bfd_make_empty_symbol (abfd);
4869   newsym->name = fh->elf.root.root.string + 1;
4870   newsym->section = bfd_und_section_ptr;
4871   newsym->value = 0;
4872   newsym->flags = BSF_WEAK;
4873
4874   bh = NULL;
4875   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4876                                          newsym->flags, newsym->section,
4877                                          newsym->value, NULL, FALSE, FALSE,
4878                                          &bh))
4879     return NULL;
4880
4881   fdh = (struct ppc_link_hash_entry *) bh;
4882   fdh->elf.non_elf = 0;
4883   fdh->fake = 1;
4884   fdh->is_func_descriptor = 1;
4885   fdh->oh = fh;
4886   fh->is_func = 1;
4887   fh->oh = fdh;
4888   return fdh;
4889 }
4890
4891 /* Fix function descriptor symbols defined in .opd sections to be
4892    function type.  */
4893
4894 static bfd_boolean
4895 ppc64_elf_add_symbol_hook (bfd *ibfd,
4896                            struct bfd_link_info *info,
4897                            Elf_Internal_Sym *isym,
4898                            const char **name,
4899                            flagword *flags ATTRIBUTE_UNUSED,
4900                            asection **sec,
4901                            bfd_vma *value)
4902 {
4903   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4904       && (ibfd->flags & DYNAMIC) == 0
4905       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4906     elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
4907
4908   if (*sec != NULL
4909       && strcmp ((*sec)->name, ".opd") == 0)
4910     {
4911       asection *code_sec;
4912
4913       if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4914             || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4915         isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4916
4917       /* If the symbol is a function defined in .opd, and the function
4918          code is in a discarded group, let it appear to be undefined.  */
4919       if (!bfd_link_relocatable (info)
4920           && (*sec)->reloc_count != 0
4921           && opd_entry_value (*sec, *value, &code_sec, NULL,
4922                               FALSE) != (bfd_vma) -1
4923           && discarded_section (code_sec))
4924         {
4925           *sec = bfd_und_section_ptr;
4926           isym->st_shndx = SHN_UNDEF;
4927         }
4928     }
4929   else if (*sec != NULL
4930            && strcmp ((*sec)->name, ".toc") == 0
4931            && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4932     {
4933       struct ppc_link_hash_table *htab = ppc_hash_table (info);
4934       if (htab != NULL)
4935         htab->params->object_in_toc = 1;
4936     }
4937
4938   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4939     {
4940       if (abiversion (ibfd) == 0)
4941         set_abiversion (ibfd, 2);
4942       else if (abiversion (ibfd) == 1)
4943         {
4944           info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4945                                     " for ABI version 1\n"), name);
4946           bfd_set_error (bfd_error_bad_value);
4947           return FALSE;
4948         }
4949     }
4950
4951   return TRUE;
4952 }
4953
4954 /* Merge non-visibility st_other attributes: local entry point.  */
4955
4956 static void
4957 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4958                                   const Elf_Internal_Sym *isym,
4959                                   bfd_boolean definition,
4960                                   bfd_boolean dynamic)
4961 {
4962   if (definition && !dynamic)
4963     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4964                 | ELF_ST_VISIBILITY (h->other));
4965 }
4966
4967 /* This function makes an old ABI object reference to ".bar" cause the
4968    inclusion of a new ABI object archive that defines "bar".
4969    NAME is a symbol defined in an archive.  Return a symbol in the hash
4970    table that might be satisfied by the archive symbols.  */
4971
4972 static struct elf_link_hash_entry *
4973 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4974                                  struct bfd_link_info *info,
4975                                  const char *name)
4976 {
4977   struct elf_link_hash_entry *h;
4978   char *dot_name;
4979   size_t len;
4980
4981   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4982   if (h != NULL
4983       /* Don't return this sym if it is a fake function descriptor
4984          created by add_symbol_adjust.  */
4985       && !(h->root.type == bfd_link_hash_undefweak
4986            && ((struct ppc_link_hash_entry *) h)->fake))
4987     return h;
4988
4989   if (name[0] == '.')
4990     return h;
4991
4992   len = strlen (name);
4993   dot_name = bfd_alloc (abfd, len + 2);
4994   if (dot_name == NULL)
4995     return (struct elf_link_hash_entry *) 0 - 1;
4996   dot_name[0] = '.';
4997   memcpy (dot_name + 1, name, len + 1);
4998   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4999   bfd_release (abfd, dot_name);
5000   return h;
5001 }
5002
5003 /* This function satisfies all old ABI object references to ".bar" if a
5004    new ABI object defines "bar".  Well, at least, undefined dot symbols
5005    are made weak.  This stops later archive searches from including an
5006    object if we already have a function descriptor definition.  It also
5007    prevents the linker complaining about undefined symbols.
5008    We also check and correct mismatched symbol visibility here.  The
5009    most restrictive visibility of the function descriptor and the
5010    function entry symbol is used.  */
5011
5012 static bfd_boolean
5013 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5014 {
5015   struct ppc_link_hash_table *htab;
5016   struct ppc_link_hash_entry *fdh;
5017
5018   if (eh->elf.root.type == bfd_link_hash_indirect)
5019     return TRUE;
5020
5021   if (eh->elf.root.type == bfd_link_hash_warning)
5022     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5023
5024   if (eh->elf.root.root.string[0] != '.')
5025     abort ();
5026
5027   htab = ppc_hash_table (info);
5028   if (htab == NULL)
5029     return FALSE;
5030
5031   fdh = lookup_fdh (eh, htab);
5032   if (fdh == NULL)
5033     {
5034       if (!bfd_link_relocatable (info)
5035           && (eh->elf.root.type == bfd_link_hash_undefined
5036               || eh->elf.root.type == bfd_link_hash_undefweak)
5037           && eh->elf.ref_regular)
5038         {
5039           /* Make an undefweak function descriptor sym, which is enough to
5040              pull in an --as-needed shared lib, but won't cause link
5041              errors.  Archives are handled elsewhere.  */
5042           fdh = make_fdh (info, eh);
5043           if (fdh == NULL)
5044             return FALSE;
5045           fdh->elf.ref_regular = 1;
5046         }
5047     }
5048   else
5049     {
5050       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5051       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5052       if (entry_vis < descr_vis)
5053         fdh->elf.other += entry_vis - descr_vis;
5054       else if (entry_vis > descr_vis)
5055         eh->elf.other += descr_vis - entry_vis;
5056
5057       if ((fdh->elf.root.type == bfd_link_hash_defined
5058            || fdh->elf.root.type == bfd_link_hash_defweak)
5059           && eh->elf.root.type == bfd_link_hash_undefined)
5060         {
5061           eh->elf.root.type = bfd_link_hash_undefweak;
5062           eh->was_undefined = 1;
5063           htab->twiddled_syms = 1;
5064         }
5065     }
5066
5067   return TRUE;
5068 }
5069
5070 /* Set up opd section info and abiversion for IBFD, and process list
5071    of dot-symbols we made in link_hash_newfunc.  */
5072
5073 static bfd_boolean
5074 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5075 {
5076   struct ppc_link_hash_table *htab;
5077   struct ppc_link_hash_entry **p, *eh;
5078   asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5079
5080   if (opd != NULL && opd->size != 0)
5081     {
5082       if (abiversion (ibfd) == 0)
5083         set_abiversion (ibfd, 1);
5084       else if (abiversion (ibfd) >= 2)
5085         {
5086           info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5087                                     " version %d\n"),
5088                                   ibfd, abiversion (ibfd));
5089           bfd_set_error (bfd_error_bad_value);
5090           return FALSE;
5091         }
5092
5093       if ((ibfd->flags & DYNAMIC) == 0
5094           && (opd->flags & SEC_RELOC) != 0
5095           && opd->reloc_count != 0
5096           && !bfd_is_abs_section (opd->output_section))
5097         {
5098           /* Garbage collection needs some extra help with .opd sections.
5099              We don't want to necessarily keep everything referenced by
5100              relocs in .opd, as that would keep all functions.  Instead,
5101              if we reference an .opd symbol (a function descriptor), we
5102              want to keep the function code symbol's section.  This is
5103              easy for global symbols, but for local syms we need to keep
5104              information about the associated function section.  */
5105           bfd_size_type amt;
5106           asection **opd_sym_map;
5107
5108           amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5109           opd_sym_map = bfd_zalloc (ibfd, amt);
5110           if (opd_sym_map == NULL)
5111             return FALSE;
5112           ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5113           BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5114           ppc64_elf_section_data (opd)->sec_type = sec_opd;
5115         }
5116     }
5117
5118   if (!is_ppc64_elf (info->output_bfd))
5119     return TRUE;
5120   htab = ppc_hash_table (info);
5121   if (htab == NULL)
5122     return FALSE;
5123
5124   /* For input files without an explicit abiversion in e_flags
5125      we should have flagged any with symbol st_other bits set
5126      as ELFv1 and above flagged those with .opd as ELFv2.
5127      Set the output abiversion if not yet set, and for any input
5128      still ambiguous, take its abiversion from the output.
5129      Differences in ABI are reported later.  */
5130   if (abiversion (info->output_bfd) == 0)
5131     set_abiversion (info->output_bfd, abiversion (ibfd));
5132   else if (abiversion (ibfd) == 0)
5133     set_abiversion (ibfd, abiversion (info->output_bfd));
5134
5135   p = &htab->dot_syms;
5136   while ((eh = *p) != NULL)
5137     {
5138       *p = NULL;
5139       if (&eh->elf == htab->elf.hgot)
5140         ;
5141       else if (htab->elf.hgot == NULL
5142                && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5143         htab->elf.hgot = &eh->elf;
5144       else if (!add_symbol_adjust (eh, info))
5145         return FALSE;
5146       p = &eh->u.next_dot_sym;
5147     }
5148
5149   /* Clear the list for non-ppc64 input files.  */
5150   p = &htab->dot_syms;
5151   while ((eh = *p) != NULL)
5152     {
5153       *p = NULL;
5154       p = &eh->u.next_dot_sym;
5155     }
5156
5157   /* We need to fix the undefs list for any syms we have twiddled to
5158      undefweak.  */
5159   if (htab->twiddled_syms)
5160     {
5161       bfd_link_repair_undef_list (&htab->elf.root);
5162       htab->twiddled_syms = 0;
5163     }
5164   return TRUE;
5165 }
5166
5167 /* Undo hash table changes when an --as-needed input file is determined
5168    not to be needed.  */
5169
5170 static bfd_boolean
5171 ppc64_elf_notice_as_needed (bfd *ibfd,
5172                             struct bfd_link_info *info,
5173                             enum notice_asneeded_action act)
5174 {
5175   if (act == notice_not_needed)
5176     {
5177       struct ppc_link_hash_table *htab = ppc_hash_table (info);
5178
5179       if (htab == NULL)
5180         return FALSE;
5181
5182       htab->dot_syms = NULL;
5183     }
5184   return _bfd_elf_notice_as_needed (ibfd, info, act);
5185 }
5186
5187 /* If --just-symbols against a final linked binary, then assume we need
5188    toc adjusting stubs when calling functions defined there.  */
5189
5190 static void
5191 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5192 {
5193   if ((sec->flags & SEC_CODE) != 0
5194       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5195       && is_ppc64_elf (sec->owner))
5196     {
5197       if (abiversion (sec->owner) >= 2
5198           || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5199         sec->has_toc_reloc = 1;
5200     }
5201   _bfd_elf_link_just_syms (sec, info);
5202 }
5203
5204 static struct plt_entry **
5205 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5206                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5207 {
5208   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5209   struct plt_entry **local_plt;
5210   unsigned char *local_got_tls_masks;
5211
5212   if (local_got_ents == NULL)
5213     {
5214       bfd_size_type size = symtab_hdr->sh_info;
5215
5216       size *= (sizeof (*local_got_ents)
5217                + sizeof (*local_plt)
5218                + sizeof (*local_got_tls_masks));
5219       local_got_ents = bfd_zalloc (abfd, size);
5220       if (local_got_ents == NULL)
5221         return NULL;
5222       elf_local_got_ents (abfd) = local_got_ents;
5223     }
5224
5225   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5226     {
5227       struct got_entry *ent;
5228
5229       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5230         if (ent->addend == r_addend
5231             && ent->owner == abfd
5232             && ent->tls_type == tls_type)
5233           break;
5234       if (ent == NULL)
5235         {
5236           bfd_size_type amt = sizeof (*ent);
5237           ent = bfd_alloc (abfd, amt);
5238           if (ent == NULL)
5239             return FALSE;
5240           ent->next = local_got_ents[r_symndx];
5241           ent->addend = r_addend;
5242           ent->owner = abfd;
5243           ent->tls_type = tls_type;
5244           ent->is_indirect = FALSE;
5245           ent->got.refcount = 0;
5246           local_got_ents[r_symndx] = ent;
5247         }
5248       ent->got.refcount += 1;
5249     }
5250
5251   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5252   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5253   local_got_tls_masks[r_symndx] |= tls_type;
5254
5255   return local_plt + r_symndx;
5256 }
5257
5258 static bfd_boolean
5259 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5260 {
5261   struct plt_entry *ent;
5262
5263   for (ent = *plist; ent != NULL; ent = ent->next)
5264     if (ent->addend == addend)
5265       break;
5266   if (ent == NULL)
5267     {
5268       bfd_size_type amt = sizeof (*ent);
5269       ent = bfd_alloc (abfd, amt);
5270       if (ent == NULL)
5271         return FALSE;
5272       ent->next = *plist;
5273       ent->addend = addend;
5274       ent->plt.refcount = 0;
5275       *plist = ent;
5276     }
5277   ent->plt.refcount += 1;
5278   return TRUE;
5279 }
5280
5281 static bfd_boolean
5282 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5283 {
5284   return (r_type == R_PPC64_REL24
5285           || r_type == R_PPC64_REL14
5286           || r_type == R_PPC64_REL14_BRTAKEN
5287           || r_type == R_PPC64_REL14_BRNTAKEN
5288           || r_type == R_PPC64_ADDR24
5289           || r_type == R_PPC64_ADDR14
5290           || r_type == R_PPC64_ADDR14_BRTAKEN
5291           || r_type == R_PPC64_ADDR14_BRNTAKEN);
5292 }
5293
5294 /* Look through the relocs for a section during the first phase, and
5295    calculate needed space in the global offset table, procedure
5296    linkage table, and dynamic reloc sections.  */
5297
5298 static bfd_boolean
5299 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5300                         asection *sec, const Elf_Internal_Rela *relocs)
5301 {
5302   struct ppc_link_hash_table *htab;
5303   Elf_Internal_Shdr *symtab_hdr;
5304   struct elf_link_hash_entry **sym_hashes;
5305   const Elf_Internal_Rela *rel;
5306   const Elf_Internal_Rela *rel_end;
5307   asection *sreloc;
5308   asection **opd_sym_map;
5309   struct elf_link_hash_entry *tga, *dottga;
5310
5311   if (bfd_link_relocatable (info))
5312     return TRUE;
5313
5314   /* Don't do anything special with non-loaded, non-alloced sections.
5315      In particular, any relocs in such sections should not affect GOT
5316      and PLT reference counting (ie. we don't allow them to create GOT
5317      or PLT entries), there's no possibility or desire to optimize TLS
5318      relocs, and there's not much point in propagating relocs to shared
5319      libs that the dynamic linker won't relocate.  */
5320   if ((sec->flags & SEC_ALLOC) == 0)
5321     return TRUE;
5322
5323   BFD_ASSERT (is_ppc64_elf (abfd));
5324
5325   htab = ppc_hash_table (info);
5326   if (htab == NULL)
5327     return FALSE;
5328
5329   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5330                               FALSE, FALSE, TRUE);
5331   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5332                                  FALSE, FALSE, TRUE);
5333   symtab_hdr = &elf_symtab_hdr (abfd);
5334   sym_hashes = elf_sym_hashes (abfd);
5335   sreloc = NULL;
5336   opd_sym_map = NULL;
5337   if (ppc64_elf_section_data (sec) != NULL
5338       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5339     opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5340
5341   rel_end = relocs + sec->reloc_count;
5342   for (rel = relocs; rel < rel_end; rel++)
5343     {
5344       unsigned long r_symndx;
5345       struct elf_link_hash_entry *h;
5346       enum elf_ppc64_reloc_type r_type;
5347       int tls_type;
5348       struct _ppc64_elf_section_data *ppc64_sec;
5349       struct plt_entry **ifunc, **plt_list;
5350
5351       r_symndx = ELF64_R_SYM (rel->r_info);
5352       if (r_symndx < symtab_hdr->sh_info)
5353         h = NULL;
5354       else
5355         {
5356           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5357           h = elf_follow_link (h);
5358
5359           /* PR15323, ref flags aren't set for references in the same
5360              object.  */
5361           h->root.non_ir_ref = 1;
5362
5363           if (h == htab->elf.hgot)
5364             sec->has_toc_reloc = 1;
5365         }
5366
5367       tls_type = 0;
5368       ifunc = NULL;
5369       if (h != NULL)
5370         {
5371           if (h->type == STT_GNU_IFUNC)
5372             {
5373               h->needs_plt = 1;
5374               ifunc = &h->plt.plist;
5375             }
5376         }
5377       else
5378         {
5379           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5380                                                           abfd, r_symndx);
5381           if (isym == NULL)
5382             return FALSE;
5383
5384           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5385             {
5386               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5387                                              rel->r_addend, PLT_IFUNC);
5388               if (ifunc == NULL)
5389                 return FALSE;
5390             }
5391         }
5392
5393       r_type = ELF64_R_TYPE (rel->r_info);
5394       switch (r_type)
5395         {
5396         case R_PPC64_TLSGD:
5397         case R_PPC64_TLSLD:
5398           /* These special tls relocs tie a call to __tls_get_addr with
5399              its parameter symbol.  */
5400           break;
5401
5402         case R_PPC64_GOT_TLSLD16:
5403         case R_PPC64_GOT_TLSLD16_LO:
5404         case R_PPC64_GOT_TLSLD16_HI:
5405         case R_PPC64_GOT_TLSLD16_HA:
5406           tls_type = TLS_TLS | TLS_LD;
5407           goto dogottls;
5408
5409         case R_PPC64_GOT_TLSGD16:
5410         case R_PPC64_GOT_TLSGD16_LO:
5411         case R_PPC64_GOT_TLSGD16_HI:
5412         case R_PPC64_GOT_TLSGD16_HA:
5413           tls_type = TLS_TLS | TLS_GD;
5414           goto dogottls;
5415
5416         case R_PPC64_GOT_TPREL16_DS:
5417         case R_PPC64_GOT_TPREL16_LO_DS:
5418         case R_PPC64_GOT_TPREL16_HI:
5419         case R_PPC64_GOT_TPREL16_HA:
5420           if (bfd_link_pic (info))
5421             info->flags |= DF_STATIC_TLS;
5422           tls_type = TLS_TLS | TLS_TPREL;
5423           goto dogottls;
5424
5425         case R_PPC64_GOT_DTPREL16_DS:
5426         case R_PPC64_GOT_DTPREL16_LO_DS:
5427         case R_PPC64_GOT_DTPREL16_HI:
5428         case R_PPC64_GOT_DTPREL16_HA:
5429           tls_type = TLS_TLS | TLS_DTPREL;
5430         dogottls:
5431           sec->has_tls_reloc = 1;
5432           /* Fall thru */
5433
5434         case R_PPC64_GOT16:
5435         case R_PPC64_GOT16_DS:
5436         case R_PPC64_GOT16_HA:
5437         case R_PPC64_GOT16_HI:
5438         case R_PPC64_GOT16_LO:
5439         case R_PPC64_GOT16_LO_DS:
5440           /* This symbol requires a global offset table entry.  */
5441           sec->has_toc_reloc = 1;
5442           if (r_type == R_PPC64_GOT_TLSLD16
5443               || r_type == R_PPC64_GOT_TLSGD16
5444               || r_type == R_PPC64_GOT_TPREL16_DS
5445               || r_type == R_PPC64_GOT_DTPREL16_DS
5446               || r_type == R_PPC64_GOT16
5447               || r_type == R_PPC64_GOT16_DS)
5448             {
5449               htab->do_multi_toc = 1;
5450               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5451             }
5452
5453           if (ppc64_elf_tdata (abfd)->got == NULL
5454               && !create_got_section (abfd, info))
5455             return FALSE;
5456
5457           if (h != NULL)
5458             {
5459               struct ppc_link_hash_entry *eh;
5460               struct got_entry *ent;
5461
5462               eh = (struct ppc_link_hash_entry *) h;
5463               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5464                 if (ent->addend == rel->r_addend
5465                     && ent->owner == abfd
5466                     && ent->tls_type == tls_type)
5467                   break;
5468               if (ent == NULL)
5469                 {
5470                   bfd_size_type amt = sizeof (*ent);
5471                   ent = bfd_alloc (abfd, amt);
5472                   if (ent == NULL)
5473                     return FALSE;
5474                   ent->next = eh->elf.got.glist;
5475                   ent->addend = rel->r_addend;
5476                   ent->owner = abfd;
5477                   ent->tls_type = tls_type;
5478                   ent->is_indirect = FALSE;
5479                   ent->got.refcount = 0;
5480                   eh->elf.got.glist = ent;
5481                 }
5482               ent->got.refcount += 1;
5483               eh->tls_mask |= tls_type;
5484             }
5485           else
5486             /* This is a global offset table entry for a local symbol.  */
5487             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5488                                         rel->r_addend, tls_type))
5489               return FALSE;
5490
5491           /* We may also need a plt entry if the symbol turns out to be
5492              an ifunc.  */
5493           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5494             {
5495               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5496                 return FALSE;
5497             }
5498           break;
5499
5500         case R_PPC64_PLT16_HA:
5501         case R_PPC64_PLT16_HI:
5502         case R_PPC64_PLT16_LO:
5503         case R_PPC64_PLT32:
5504         case R_PPC64_PLT64:
5505           /* This symbol requires a procedure linkage table entry.  */
5506           plt_list = ifunc;
5507           if (h != NULL)
5508             {
5509               h->needs_plt = 1;
5510               if (h->root.root.string[0] == '.'
5511                   && h->root.root.string[1] != '\0')
5512                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5513               plt_list = &h->plt.plist;
5514             }
5515           if (plt_list == NULL)
5516             {
5517               /* It does not make sense to have a procedure linkage
5518                  table entry for a non-ifunc local symbol.  */
5519               info->callbacks->einfo
5520                 (_("%P: %H: %s reloc against local symbol\n"),
5521                  abfd, sec, rel->r_offset,
5522                  ppc64_elf_howto_table[r_type]->name);
5523               bfd_set_error (bfd_error_bad_value);
5524               return FALSE;
5525             }
5526           if (!update_plt_info (abfd, plt_list, rel->r_addend))
5527             return FALSE;
5528           break;
5529
5530           /* The following relocations don't need to propagate the
5531              relocation if linking a shared object since they are
5532              section relative.  */
5533         case R_PPC64_SECTOFF:
5534         case R_PPC64_SECTOFF_LO:
5535         case R_PPC64_SECTOFF_HI:
5536         case R_PPC64_SECTOFF_HA:
5537         case R_PPC64_SECTOFF_DS:
5538         case R_PPC64_SECTOFF_LO_DS:
5539         case R_PPC64_DTPREL16:
5540         case R_PPC64_DTPREL16_LO:
5541         case R_PPC64_DTPREL16_HI:
5542         case R_PPC64_DTPREL16_HA:
5543         case R_PPC64_DTPREL16_DS:
5544         case R_PPC64_DTPREL16_LO_DS:
5545         case R_PPC64_DTPREL16_HIGH:
5546         case R_PPC64_DTPREL16_HIGHA:
5547         case R_PPC64_DTPREL16_HIGHER:
5548         case R_PPC64_DTPREL16_HIGHERA:
5549         case R_PPC64_DTPREL16_HIGHEST:
5550         case R_PPC64_DTPREL16_HIGHESTA:
5551           break;
5552
5553           /* Nor do these.  */
5554         case R_PPC64_REL16:
5555         case R_PPC64_REL16_LO:
5556         case R_PPC64_REL16_HI:
5557         case R_PPC64_REL16_HA:
5558         case R_PPC64_REL16DX_HA:
5559           break;
5560
5561           /* Not supported as a dynamic relocation.  */
5562         case R_PPC64_ADDR64_LOCAL:
5563           if (bfd_link_pic (info))
5564             {
5565               if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5566                 ppc_howto_init ();
5567               info->callbacks->einfo (_("%P: %H: %s reloc unsupported "
5568                                         "in shared libraries and PIEs.\n"),
5569                                       abfd, sec, rel->r_offset,
5570                                       ppc64_elf_howto_table[r_type]->name);
5571               bfd_set_error (bfd_error_bad_value);
5572               return FALSE;
5573             }
5574           break;
5575
5576         case R_PPC64_TOC16:
5577         case R_PPC64_TOC16_DS:
5578           htab->do_multi_toc = 1;
5579           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5580         case R_PPC64_TOC16_LO:
5581         case R_PPC64_TOC16_HI:
5582         case R_PPC64_TOC16_HA:
5583         case R_PPC64_TOC16_LO_DS:
5584           sec->has_toc_reloc = 1;
5585           break;
5586
5587           /* Marker reloc.  */
5588         case R_PPC64_ENTRY:
5589           break;
5590
5591           /* This relocation describes the C++ object vtable hierarchy.
5592              Reconstruct it for later use during GC.  */
5593         case R_PPC64_GNU_VTINHERIT:
5594           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5595             return FALSE;
5596           break;
5597
5598           /* This relocation describes which C++ vtable entries are actually
5599              used.  Record for later use during GC.  */
5600         case R_PPC64_GNU_VTENTRY:
5601           BFD_ASSERT (h != NULL);
5602           if (h != NULL
5603               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5604             return FALSE;
5605           break;
5606
5607         case R_PPC64_REL14:
5608         case R_PPC64_REL14_BRTAKEN:
5609         case R_PPC64_REL14_BRNTAKEN:
5610           {
5611             asection *dest = NULL;
5612
5613             /* Heuristic: If jumping outside our section, chances are
5614                we are going to need a stub.  */
5615             if (h != NULL)
5616               {
5617                 /* If the sym is weak it may be overridden later, so
5618                    don't assume we know where a weak sym lives.  */
5619                 if (h->root.type == bfd_link_hash_defined)
5620                   dest = h->root.u.def.section;
5621               }
5622             else
5623               {
5624                 Elf_Internal_Sym *isym;
5625
5626                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5627                                               abfd, r_symndx);
5628                 if (isym == NULL)
5629                   return FALSE;
5630
5631                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5632               }
5633
5634             if (dest != sec)
5635               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5636           }
5637           /* Fall through.  */
5638
5639         case R_PPC64_REL24:
5640           plt_list = ifunc;
5641           if (h != NULL)
5642             {
5643               h->needs_plt = 1;
5644               if (h->root.root.string[0] == '.'
5645                   && h->root.root.string[1] != '\0')
5646                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5647
5648               if (h == tga || h == dottga)
5649                 {
5650                   sec->has_tls_reloc = 1;
5651                   if (rel != relocs
5652                       && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5653                           || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5654                     /* We have a new-style __tls_get_addr call with
5655                        a marker reloc.  */
5656                     ;
5657                   else
5658                     /* Mark this section as having an old-style call.  */
5659                     sec->has_tls_get_addr_call = 1;
5660                 }
5661               plt_list = &h->plt.plist;
5662             }
5663
5664           /* We may need a .plt entry if the function this reloc
5665              refers to is in a shared lib.  */
5666           if (plt_list
5667               && !update_plt_info (abfd, plt_list, rel->r_addend))
5668             return FALSE;
5669           break;
5670
5671         case R_PPC64_ADDR14:
5672         case R_PPC64_ADDR14_BRNTAKEN:
5673         case R_PPC64_ADDR14_BRTAKEN:
5674         case R_PPC64_ADDR24:
5675           goto dodyn;
5676
5677         case R_PPC64_TPREL64:
5678           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5679           if (bfd_link_pic (info))
5680             info->flags |= DF_STATIC_TLS;
5681           goto dotlstoc;
5682
5683         case R_PPC64_DTPMOD64:
5684           if (rel + 1 < rel_end
5685               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5686               && rel[1].r_offset == rel->r_offset + 8)
5687             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5688           else
5689             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5690           goto dotlstoc;
5691
5692         case R_PPC64_DTPREL64:
5693           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5694           if (rel != relocs
5695               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5696               && rel[-1].r_offset == rel->r_offset - 8)
5697             /* This is the second reloc of a dtpmod, dtprel pair.
5698                Don't mark with TLS_DTPREL.  */
5699             goto dodyn;
5700
5701         dotlstoc:
5702           sec->has_tls_reloc = 1;
5703           if (h != NULL)
5704             {
5705               struct ppc_link_hash_entry *eh;
5706               eh = (struct ppc_link_hash_entry *) h;
5707               eh->tls_mask |= tls_type;
5708             }
5709           else
5710             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5711                                         rel->r_addend, tls_type))
5712               return FALSE;
5713
5714           ppc64_sec = ppc64_elf_section_data (sec);
5715           if (ppc64_sec->sec_type != sec_toc)
5716             {
5717               bfd_size_type amt;
5718
5719               /* One extra to simplify get_tls_mask.  */
5720               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5721               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5722               if (ppc64_sec->u.toc.symndx == NULL)
5723                 return FALSE;
5724               amt = sec->size * sizeof (bfd_vma) / 8;
5725               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5726               if (ppc64_sec->u.toc.add == NULL)
5727                 return FALSE;
5728               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5729               ppc64_sec->sec_type = sec_toc;
5730             }
5731           BFD_ASSERT (rel->r_offset % 8 == 0);
5732           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5733           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5734
5735           /* Mark the second slot of a GD or LD entry.
5736              -1 to indicate GD and -2 to indicate LD.  */
5737           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5738             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5739           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5740             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5741           goto dodyn;
5742
5743         case R_PPC64_TPREL16:
5744         case R_PPC64_TPREL16_LO:
5745         case R_PPC64_TPREL16_HI:
5746         case R_PPC64_TPREL16_HA:
5747         case R_PPC64_TPREL16_DS:
5748         case R_PPC64_TPREL16_LO_DS:
5749         case R_PPC64_TPREL16_HIGH:
5750         case R_PPC64_TPREL16_HIGHA:
5751         case R_PPC64_TPREL16_HIGHER:
5752         case R_PPC64_TPREL16_HIGHERA:
5753         case R_PPC64_TPREL16_HIGHEST:
5754         case R_PPC64_TPREL16_HIGHESTA:
5755           if (bfd_link_pic (info))
5756             {
5757               info->flags |= DF_STATIC_TLS;
5758               goto dodyn;
5759             }
5760           break;
5761
5762         case R_PPC64_ADDR64:
5763           if (opd_sym_map != NULL
5764               && rel + 1 < rel_end
5765               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5766             {
5767               if (h != NULL)
5768                 {
5769                   if (h->root.root.string[0] == '.'
5770                       && h->root.root.string[1] != 0
5771                       && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5772                     ;
5773                   else
5774                     ((struct ppc_link_hash_entry *) h)->is_func = 1;
5775                 }
5776               else
5777                 {
5778                   asection *s;
5779                   Elf_Internal_Sym *isym;
5780
5781                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5782                                                 abfd, r_symndx);
5783                   if (isym == NULL)
5784                     return FALSE;
5785
5786                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5787                   if (s != NULL && s != sec)
5788                     opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5789                 }
5790             }
5791           /* Fall through.  */
5792
5793         case R_PPC64_ADDR16:
5794         case R_PPC64_ADDR16_DS:
5795         case R_PPC64_ADDR16_HA:
5796         case R_PPC64_ADDR16_HI:
5797         case R_PPC64_ADDR16_HIGH:
5798         case R_PPC64_ADDR16_HIGHA:
5799         case R_PPC64_ADDR16_HIGHER:
5800         case R_PPC64_ADDR16_HIGHERA:
5801         case R_PPC64_ADDR16_HIGHEST:
5802         case R_PPC64_ADDR16_HIGHESTA:
5803         case R_PPC64_ADDR16_LO:
5804         case R_PPC64_ADDR16_LO_DS:
5805           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5806               && rel->r_addend == 0)
5807             {
5808               /* We may need a .plt entry if this reloc refers to a
5809                  function in a shared lib.  */
5810               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5811                 return FALSE;
5812               h->pointer_equality_needed = 1;
5813             }
5814           /* Fall through.  */
5815
5816         case R_PPC64_REL30:
5817         case R_PPC64_REL32:
5818         case R_PPC64_REL64:
5819         case R_PPC64_ADDR32:
5820         case R_PPC64_UADDR16:
5821         case R_PPC64_UADDR32:
5822         case R_PPC64_UADDR64:
5823         case R_PPC64_TOC:
5824           if (h != NULL && !bfd_link_pic (info))
5825             /* We may need a copy reloc.  */
5826             h->non_got_ref = 1;
5827
5828           /* Don't propagate .opd relocs.  */
5829           if (NO_OPD_RELOCS && opd_sym_map != NULL)
5830             break;
5831
5832           /* If we are creating a shared library, and this is a reloc
5833              against a global symbol, or a non PC relative reloc
5834              against a local symbol, then we need to copy the reloc
5835              into the shared library.  However, if we are linking with
5836              -Bsymbolic, we do not need to copy a reloc against a
5837              global symbol which is defined in an object we are
5838              including in the link (i.e., DEF_REGULAR is set).  At
5839              this point we have not seen all the input files, so it is
5840              possible that DEF_REGULAR is not set now but will be set
5841              later (it is never cleared).  In case of a weak definition,
5842              DEF_REGULAR may be cleared later by a strong definition in
5843              a shared library.  We account for that possibility below by
5844              storing information in the dyn_relocs field of the hash
5845              table entry.  A similar situation occurs when creating
5846              shared libraries and symbol visibility changes render the
5847              symbol local.
5848
5849              If on the other hand, we are creating an executable, we
5850              may need to keep relocations for symbols satisfied by a
5851              dynamic library if we manage to avoid copy relocs for the
5852              symbol.  */
5853         dodyn:
5854           if ((bfd_link_pic (info)
5855                && (must_be_dyn_reloc (info, r_type)
5856                    || (h != NULL
5857                        && (!SYMBOLIC_BIND (info, h)
5858                            || h->root.type == bfd_link_hash_defweak
5859                            || !h->def_regular))))
5860               || (ELIMINATE_COPY_RELOCS
5861                   && !bfd_link_pic (info)
5862                   && h != NULL
5863                   && (h->root.type == bfd_link_hash_defweak
5864                       || !h->def_regular))
5865               || (!bfd_link_pic (info)
5866                   && ifunc != NULL))
5867             {
5868               /* We must copy these reloc types into the output file.
5869                  Create a reloc section in dynobj and make room for
5870                  this reloc.  */
5871               if (sreloc == NULL)
5872                 {
5873                   sreloc = _bfd_elf_make_dynamic_reloc_section
5874                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5875
5876                   if (sreloc == NULL)
5877                     return FALSE;
5878                 }
5879
5880               /* If this is a global symbol, we count the number of
5881                  relocations we need for this symbol.  */
5882               if (h != NULL)
5883                 {
5884                   struct elf_dyn_relocs *p;
5885                   struct elf_dyn_relocs **head;
5886
5887                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5888                   p = *head;
5889                   if (p == NULL || p->sec != sec)
5890                     {
5891                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5892                       if (p == NULL)
5893                         return FALSE;
5894                       p->next = *head;
5895                       *head = p;
5896                       p->sec = sec;
5897                       p->count = 0;
5898                       p->pc_count = 0;
5899                     }
5900                   p->count += 1;
5901                   if (!must_be_dyn_reloc (info, r_type))
5902                     p->pc_count += 1;
5903                 }
5904               else
5905                 {
5906                   /* Track dynamic relocs needed for local syms too.
5907                      We really need local syms available to do this
5908                      easily.  Oh well.  */
5909                   struct ppc_dyn_relocs *p;
5910                   struct ppc_dyn_relocs **head;
5911                   bfd_boolean is_ifunc;
5912                   asection *s;
5913                   void *vpp;
5914                   Elf_Internal_Sym *isym;
5915
5916                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5917                                                 abfd, r_symndx);
5918                   if (isym == NULL)
5919                     return FALSE;
5920
5921                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5922                   if (s == NULL)
5923                     s = sec;
5924
5925                   vpp = &elf_section_data (s)->local_dynrel;
5926                   head = (struct ppc_dyn_relocs **) vpp;
5927                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5928                   p = *head;
5929                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5930                     p = p->next;
5931                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5932                     {
5933                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5934                       if (p == NULL)
5935                         return FALSE;
5936                       p->next = *head;
5937                       *head = p;
5938                       p->sec = sec;
5939                       p->ifunc = is_ifunc;
5940                       p->count = 0;
5941                     }
5942                   p->count += 1;
5943                 }
5944             }
5945           break;
5946
5947         default:
5948           break;
5949         }
5950     }
5951
5952   return TRUE;
5953 }
5954
5955 /* Merge backend specific data from an object file to the output
5956    object file when linking.  */
5957
5958 static bfd_boolean
5959 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5960 {
5961   unsigned long iflags, oflags;
5962
5963   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5964     return TRUE;
5965
5966   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5967     return TRUE;
5968
5969   if (!_bfd_generic_verify_endian_match (ibfd, obfd))
5970     return FALSE;
5971
5972   iflags = elf_elfheader (ibfd)->e_flags;
5973   oflags = elf_elfheader (obfd)->e_flags;
5974
5975   if (iflags & ~EF_PPC64_ABI)
5976     {
5977       (*_bfd_error_handler)
5978         (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
5979       bfd_set_error (bfd_error_bad_value);
5980       return FALSE;
5981     }
5982   else if (iflags != oflags && iflags != 0)
5983     {
5984       (*_bfd_error_handler)
5985         (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
5986          ibfd, iflags, oflags);
5987       bfd_set_error (bfd_error_bad_value);
5988       return FALSE;
5989     }
5990
5991   /* Merge Tag_compatibility attributes and any common GNU ones.  */
5992   _bfd_elf_merge_object_attributes (ibfd, obfd);
5993
5994   return TRUE;
5995 }
5996
5997 static bfd_boolean
5998 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5999 {
6000   /* Print normal ELF private data.  */
6001   _bfd_elf_print_private_bfd_data (abfd, ptr);
6002
6003   if (elf_elfheader (abfd)->e_flags != 0)
6004     {
6005       FILE *file = ptr;
6006
6007       /* xgettext:c-format */
6008       fprintf (file, _("private flags = 0x%lx:"),
6009                elf_elfheader (abfd)->e_flags);
6010
6011       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6012         fprintf (file, _(" [abiv%ld]"),
6013                  elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6014       fputc ('\n', file);
6015     }
6016
6017   return TRUE;
6018 }
6019
6020 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
6021    of the code entry point, and its section, which must be in the same
6022    object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
6023
6024 static bfd_vma
6025 opd_entry_value (asection *opd_sec,
6026                  bfd_vma offset,
6027                  asection **code_sec,
6028                  bfd_vma *code_off,
6029                  bfd_boolean in_code_sec)
6030 {
6031   bfd *opd_bfd = opd_sec->owner;
6032   Elf_Internal_Rela *relocs;
6033   Elf_Internal_Rela *lo, *hi, *look;
6034   bfd_vma val;
6035
6036   /* No relocs implies we are linking a --just-symbols object, or looking
6037      at a final linked executable with addr2line or somesuch.  */
6038   if (opd_sec->reloc_count == 0)
6039     {
6040       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6041
6042       if (contents == NULL)
6043         {
6044           if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6045             return (bfd_vma) -1;
6046           ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6047         }
6048
6049       /* PR 17512: file: 64b9dfbb.  */
6050       if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6051         return (bfd_vma) -1;
6052
6053       val = bfd_get_64 (opd_bfd, contents + offset);
6054       if (code_sec != NULL)
6055         {
6056           asection *sec, *likely = NULL;
6057
6058           if (in_code_sec)
6059             {
6060               sec = *code_sec;
6061               if (sec->vma <= val
6062                   && val < sec->vma + sec->size)
6063                 likely = sec;
6064               else
6065                 val = -1;
6066             }
6067           else
6068             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6069               if (sec->vma <= val
6070                   && (sec->flags & SEC_LOAD) != 0
6071                   && (sec->flags & SEC_ALLOC) != 0)
6072                 likely = sec;
6073           if (likely != NULL)
6074             {
6075               *code_sec = likely;
6076               if (code_off != NULL)
6077                 *code_off = val - likely->vma;
6078             }
6079         }
6080       return val;
6081     }
6082
6083   BFD_ASSERT (is_ppc64_elf (opd_bfd));
6084
6085   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6086   if (relocs == NULL)
6087     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6088   /* PR 17512: file: df8e1fd6.  */
6089   if (relocs == NULL)
6090     return (bfd_vma) -1;
6091
6092   /* Go find the opd reloc at the sym address.  */
6093   lo = relocs;
6094   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6095   val = (bfd_vma) -1;
6096   while (lo < hi)
6097     {
6098       look = lo + (hi - lo) / 2;
6099       if (look->r_offset < offset)
6100         lo = look + 1;
6101       else if (look->r_offset > offset)
6102         hi = look;
6103       else
6104         {
6105           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6106
6107           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6108               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6109             {
6110               unsigned long symndx = ELF64_R_SYM (look->r_info);
6111               asection *sec = NULL;
6112
6113               if (symndx >= symtab_hdr->sh_info
6114                   && elf_sym_hashes (opd_bfd) != NULL)
6115                 {
6116                   struct elf_link_hash_entry **sym_hashes;
6117                   struct elf_link_hash_entry *rh;
6118
6119                   sym_hashes = elf_sym_hashes (opd_bfd);
6120                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
6121                   if (rh != NULL)
6122                     {
6123                       rh = elf_follow_link (rh);
6124                       if (rh->root.type != bfd_link_hash_defined
6125                           && rh->root.type != bfd_link_hash_defweak)
6126                         break;
6127                       if (rh->root.u.def.section->owner == opd_bfd)
6128                         {
6129                           val = rh->root.u.def.value;
6130                           sec = rh->root.u.def.section;
6131                         }
6132                     }
6133                 }
6134
6135               if (sec == NULL)
6136                 {
6137                   Elf_Internal_Sym *sym;
6138
6139                   if (symndx < symtab_hdr->sh_info)
6140                     {
6141                       sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6142                       if (sym == NULL)
6143                         {
6144                           size_t symcnt = symtab_hdr->sh_info;
6145                           sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6146                                                       symcnt, 0,
6147                                                       NULL, NULL, NULL);
6148                           if (sym == NULL)
6149                             break;
6150                           symtab_hdr->contents = (bfd_byte *) sym;
6151                         }
6152                       sym += symndx;
6153                     }
6154                   else
6155                     {
6156                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6157                                                   1, symndx,
6158                                                   NULL, NULL, NULL);
6159                       if (sym == NULL)
6160                         break;
6161                     }
6162                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6163                   if (sec == NULL)
6164                     break;
6165                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6166                   val = sym->st_value;
6167                 }
6168
6169               val += look->r_addend;
6170               if (code_off != NULL)
6171                 *code_off = val;
6172               if (code_sec != NULL)
6173                 {
6174                   if (in_code_sec && *code_sec != sec)
6175                     return -1;
6176                   else
6177                     *code_sec = sec;
6178                 }
6179               if (sec->output_section != NULL)
6180                 val += sec->output_section->vma + sec->output_offset;
6181             }
6182           break;
6183         }
6184     }
6185
6186   return val;
6187 }
6188
6189 /* If the ELF symbol SYM might be a function in SEC, return the
6190    function size and set *CODE_OFF to the function's entry point,
6191    otherwise return zero.  */
6192
6193 static bfd_size_type
6194 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6195                               bfd_vma *code_off)
6196 {
6197   bfd_size_type size;
6198
6199   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6200                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6201     return 0;
6202
6203   size = 0;
6204   if (!(sym->flags & BSF_SYNTHETIC))
6205     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6206
6207   if (strcmp (sym->section->name, ".opd") == 0)
6208     {
6209       struct _opd_sec_data *opd = get_opd_info (sym->section);
6210       bfd_vma symval = sym->value;
6211
6212       if (opd != NULL
6213           && opd->adjust != NULL
6214           && elf_section_data (sym->section)->relocs != NULL)
6215         {
6216           /* opd_entry_value will use cached relocs that have been
6217              adjusted, but with raw symbols.  That means both local
6218              and global symbols need adjusting.  */
6219           long adjust = opd->adjust[OPD_NDX (symval)];
6220           if (adjust == -1)
6221             return 0;
6222           symval += adjust;
6223         }
6224
6225       if (opd_entry_value (sym->section, symval,
6226                            &sec, code_off, TRUE) == (bfd_vma) -1)
6227         return 0;
6228       /* An old ABI binary with dot-syms has a size of 24 on the .opd
6229          symbol.  This size has nothing to do with the code size of the
6230          function, which is what we're supposed to return, but the
6231          code size isn't available without looking up the dot-sym.
6232          However, doing that would be a waste of time particularly
6233          since elf_find_function will look at the dot-sym anyway.
6234          Now, elf_find_function will keep the largest size of any
6235          function sym found at the code address of interest, so return
6236          1 here to avoid it incorrectly caching a larger function size
6237          for a small function.  This does mean we return the wrong
6238          size for a new-ABI function of size 24, but all that does is
6239          disable caching for such functions.  */
6240       if (size == 24)
6241         size = 1;
6242     }
6243   else
6244     {
6245       if (sym->section != sec)
6246         return 0;
6247       *code_off = sym->value;
6248     }
6249   if (size == 0)
6250     size = 1;
6251   return size;
6252 }
6253
6254 /* Return true if symbol is defined in a regular object file.  */
6255
6256 static bfd_boolean
6257 is_static_defined (struct elf_link_hash_entry *h)
6258 {
6259   return ((h->root.type == bfd_link_hash_defined
6260            || h->root.type == bfd_link_hash_defweak)
6261           && h->root.u.def.section != NULL
6262           && h->root.u.def.section->output_section != NULL);
6263 }
6264
6265 /* If FDH is a function descriptor symbol, return the associated code
6266    entry symbol if it is defined.  Return NULL otherwise.  */
6267
6268 static struct ppc_link_hash_entry *
6269 defined_code_entry (struct ppc_link_hash_entry *fdh)
6270 {
6271   if (fdh->is_func_descriptor)
6272     {
6273       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6274       if (fh->elf.root.type == bfd_link_hash_defined
6275           || fh->elf.root.type == bfd_link_hash_defweak)
6276         return fh;
6277     }
6278   return NULL;
6279 }
6280
6281 /* If FH is a function code entry symbol, return the associated
6282    function descriptor symbol if it is defined.  Return NULL otherwise.  */
6283
6284 static struct ppc_link_hash_entry *
6285 defined_func_desc (struct ppc_link_hash_entry *fh)
6286 {
6287   if (fh->oh != NULL
6288       && fh->oh->is_func_descriptor)
6289     {
6290       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6291       if (fdh->elf.root.type == bfd_link_hash_defined
6292           || fdh->elf.root.type == bfd_link_hash_defweak)
6293         return fdh;
6294     }
6295   return NULL;
6296 }
6297
6298 /* Mark all our entry sym sections, both opd and code section.  */
6299
6300 static void
6301 ppc64_elf_gc_keep (struct bfd_link_info *info)
6302 {
6303   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6304   struct bfd_sym_chain *sym;
6305
6306   if (htab == NULL)
6307     return;
6308
6309   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6310     {
6311       struct ppc_link_hash_entry *eh, *fh;
6312       asection *sec;
6313
6314       eh = (struct ppc_link_hash_entry *)
6315         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6316       if (eh == NULL)
6317         continue;
6318       if (eh->elf.root.type != bfd_link_hash_defined
6319           && eh->elf.root.type != bfd_link_hash_defweak)
6320         continue;
6321
6322       fh = defined_code_entry (eh);
6323       if (fh != NULL)
6324         {
6325           sec = fh->elf.root.u.def.section;
6326           sec->flags |= SEC_KEEP;
6327         }
6328       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6329                && opd_entry_value (eh->elf.root.u.def.section,
6330                                    eh->elf.root.u.def.value,
6331                                    &sec, NULL, FALSE) != (bfd_vma) -1)
6332         sec->flags |= SEC_KEEP;
6333
6334       sec = eh->elf.root.u.def.section;
6335       sec->flags |= SEC_KEEP;
6336     }
6337 }
6338
6339 /* Mark sections containing dynamically referenced symbols.  When
6340    building shared libraries, we must assume that any visible symbol is
6341    referenced.  */
6342
6343 static bfd_boolean
6344 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6345 {
6346   struct bfd_link_info *info = (struct bfd_link_info *) inf;
6347   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6348   struct ppc_link_hash_entry *fdh;
6349   struct bfd_elf_dynamic_list *d = info->dynamic_list;
6350
6351   /* Dynamic linking info is on the func descriptor sym.  */
6352   fdh = defined_func_desc (eh);
6353   if (fdh != NULL)
6354     eh = fdh;
6355
6356   if ((eh->elf.root.type == bfd_link_hash_defined
6357        || eh->elf.root.type == bfd_link_hash_defweak)
6358       && (eh->elf.ref_dynamic
6359           || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6360               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6361               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6362               && (!bfd_link_executable (info)
6363                   || info->export_dynamic
6364                   || (eh->elf.dynamic
6365                       && d != NULL
6366                       && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6367               && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6368                   || !bfd_hide_sym_by_version (info->version_info,
6369                                                eh->elf.root.root.string)))))
6370     {
6371       asection *code_sec;
6372       struct ppc_link_hash_entry *fh;
6373
6374       eh->elf.root.u.def.section->flags |= SEC_KEEP;
6375
6376       /* Function descriptor syms cause the associated
6377          function code sym section to be marked.  */
6378       fh = defined_code_entry (eh);
6379       if (fh != NULL)
6380         {
6381           code_sec = fh->elf.root.u.def.section;
6382           code_sec->flags |= SEC_KEEP;
6383         }
6384       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6385                && opd_entry_value (eh->elf.root.u.def.section,
6386                                    eh->elf.root.u.def.value,
6387                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
6388         code_sec->flags |= SEC_KEEP;
6389     }
6390
6391   return TRUE;
6392 }
6393
6394 /* Return the section that should be marked against GC for a given
6395    relocation.  */
6396
6397 static asection *
6398 ppc64_elf_gc_mark_hook (asection *sec,
6399                         struct bfd_link_info *info,
6400                         Elf_Internal_Rela *rel,
6401                         struct elf_link_hash_entry *h,
6402                         Elf_Internal_Sym *sym)
6403 {
6404   asection *rsec;
6405
6406   /* Syms return NULL if we're marking .opd, so we avoid marking all
6407      function sections, as all functions are referenced in .opd.  */
6408   rsec = NULL;
6409   if (get_opd_info (sec) != NULL)
6410     return rsec;
6411
6412   if (h != NULL)
6413     {
6414       enum elf_ppc64_reloc_type r_type;
6415       struct ppc_link_hash_entry *eh, *fh, *fdh;
6416
6417       r_type = ELF64_R_TYPE (rel->r_info);
6418       switch (r_type)
6419         {
6420         case R_PPC64_GNU_VTINHERIT:
6421         case R_PPC64_GNU_VTENTRY:
6422           break;
6423
6424         default:
6425           switch (h->root.type)
6426             {
6427             case bfd_link_hash_defined:
6428             case bfd_link_hash_defweak:
6429               eh = (struct ppc_link_hash_entry *) h;
6430               fdh = defined_func_desc (eh);
6431               if (fdh != NULL)
6432                 eh = fdh;
6433
6434               /* Function descriptor syms cause the associated
6435                  function code sym section to be marked.  */
6436               fh = defined_code_entry (eh);
6437               if (fh != NULL)
6438                 {
6439                   /* They also mark their opd section.  */
6440                   eh->elf.root.u.def.section->gc_mark = 1;
6441
6442                   rsec = fh->elf.root.u.def.section;
6443                 }
6444               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6445                        && opd_entry_value (eh->elf.root.u.def.section,
6446                                            eh->elf.root.u.def.value,
6447                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
6448                 eh->elf.root.u.def.section->gc_mark = 1;
6449               else
6450                 rsec = h->root.u.def.section;
6451               break;
6452
6453             case bfd_link_hash_common:
6454               rsec = h->root.u.c.p->section;
6455               break;
6456
6457             default:
6458               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6459             }
6460         }
6461     }
6462   else
6463     {
6464       struct _opd_sec_data *opd;
6465
6466       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6467       opd = get_opd_info (rsec);
6468       if (opd != NULL && opd->func_sec != NULL)
6469         {
6470           rsec->gc_mark = 1;
6471
6472           rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6473         }
6474     }
6475
6476   return rsec;
6477 }
6478
6479 /* Update the .got, .plt. and dynamic reloc reference counts for the
6480    section being removed.  */
6481
6482 static bfd_boolean
6483 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6484                          asection *sec, const Elf_Internal_Rela *relocs)
6485 {
6486   struct ppc_link_hash_table *htab;
6487   Elf_Internal_Shdr *symtab_hdr;
6488   struct elf_link_hash_entry **sym_hashes;
6489   struct got_entry **local_got_ents;
6490   const Elf_Internal_Rela *rel, *relend;
6491
6492   if (bfd_link_relocatable (info))
6493     return TRUE;
6494
6495   if ((sec->flags & SEC_ALLOC) == 0)
6496     return TRUE;
6497
6498   elf_section_data (sec)->local_dynrel = NULL;
6499
6500   htab = ppc_hash_table (info);
6501   if (htab == NULL)
6502     return FALSE;
6503
6504   symtab_hdr = &elf_symtab_hdr (abfd);
6505   sym_hashes = elf_sym_hashes (abfd);
6506   local_got_ents = elf_local_got_ents (abfd);
6507
6508   relend = relocs + sec->reloc_count;
6509   for (rel = relocs; rel < relend; rel++)
6510     {
6511       unsigned long r_symndx;
6512       enum elf_ppc64_reloc_type r_type;
6513       struct elf_link_hash_entry *h = NULL;
6514       struct plt_entry **plt_list;
6515       unsigned char tls_type = 0;
6516
6517       r_symndx = ELF64_R_SYM (rel->r_info);
6518       r_type = ELF64_R_TYPE (rel->r_info);
6519       if (r_symndx >= symtab_hdr->sh_info)
6520         {
6521           struct ppc_link_hash_entry *eh;
6522           struct elf_dyn_relocs **pp;
6523           struct elf_dyn_relocs *p;
6524
6525           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6526           h = elf_follow_link (h);
6527           eh = (struct ppc_link_hash_entry *) h;
6528
6529           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6530             if (p->sec == sec)
6531               {
6532                 /* Everything must go for SEC.  */
6533                 *pp = p->next;
6534                 break;
6535               }
6536         }
6537
6538       switch (r_type)
6539         {
6540         case R_PPC64_GOT_TLSLD16:
6541         case R_PPC64_GOT_TLSLD16_LO:
6542         case R_PPC64_GOT_TLSLD16_HI:
6543         case R_PPC64_GOT_TLSLD16_HA:
6544           tls_type = TLS_TLS | TLS_LD;
6545           goto dogot;
6546
6547         case R_PPC64_GOT_TLSGD16:
6548         case R_PPC64_GOT_TLSGD16_LO:
6549         case R_PPC64_GOT_TLSGD16_HI:
6550         case R_PPC64_GOT_TLSGD16_HA:
6551           tls_type = TLS_TLS | TLS_GD;
6552           goto dogot;
6553
6554         case R_PPC64_GOT_TPREL16_DS:
6555         case R_PPC64_GOT_TPREL16_LO_DS:
6556         case R_PPC64_GOT_TPREL16_HI:
6557         case R_PPC64_GOT_TPREL16_HA:
6558           tls_type = TLS_TLS | TLS_TPREL;
6559           goto dogot;
6560
6561         case R_PPC64_GOT_DTPREL16_DS:
6562         case R_PPC64_GOT_DTPREL16_LO_DS:
6563         case R_PPC64_GOT_DTPREL16_HI:
6564         case R_PPC64_GOT_DTPREL16_HA:
6565           tls_type = TLS_TLS | TLS_DTPREL;
6566           goto dogot;
6567
6568         case R_PPC64_GOT16:
6569         case R_PPC64_GOT16_DS:
6570         case R_PPC64_GOT16_HA:
6571         case R_PPC64_GOT16_HI:
6572         case R_PPC64_GOT16_LO:
6573         case R_PPC64_GOT16_LO_DS:
6574         dogot:
6575           {
6576             struct got_entry *ent;
6577
6578             if (h != NULL)
6579               ent = h->got.glist;
6580             else
6581               ent = local_got_ents[r_symndx];
6582
6583             for (; ent != NULL; ent = ent->next)
6584               if (ent->addend == rel->r_addend
6585                   && ent->owner == abfd
6586                   && ent->tls_type == tls_type)
6587                 break;
6588             if (ent == NULL)
6589               abort ();
6590             if (ent->got.refcount > 0)
6591               ent->got.refcount -= 1;
6592           }
6593           break;
6594
6595         case R_PPC64_PLT16_HA:
6596         case R_PPC64_PLT16_HI:
6597         case R_PPC64_PLT16_LO:
6598         case R_PPC64_PLT32:
6599         case R_PPC64_PLT64:
6600         case R_PPC64_REL14:
6601         case R_PPC64_REL14_BRNTAKEN:
6602         case R_PPC64_REL14_BRTAKEN:
6603         case R_PPC64_REL24:
6604           plt_list = NULL;
6605           if (h != NULL)
6606             plt_list = &h->plt.plist;
6607           else if (local_got_ents != NULL)
6608             {
6609               struct plt_entry **local_plt = (struct plt_entry **)
6610                 (local_got_ents + symtab_hdr->sh_info);
6611               unsigned char *local_got_tls_masks = (unsigned char *)
6612                 (local_plt + symtab_hdr->sh_info);
6613               if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6614                 plt_list = local_plt + r_symndx;
6615             }
6616           if (plt_list)
6617             {
6618               struct plt_entry *ent;
6619
6620               for (ent = *plt_list; ent != NULL; ent = ent->next)
6621                 if (ent->addend == rel->r_addend)
6622                   break;
6623               if (ent != NULL && ent->plt.refcount > 0)
6624                 ent->plt.refcount -= 1;
6625             }
6626           break;
6627
6628         default:
6629           break;
6630         }
6631     }
6632   return TRUE;
6633 }
6634
6635 /* The maximum size of .sfpr.  */
6636 #define SFPR_MAX (218*4)
6637
6638 struct sfpr_def_parms
6639 {
6640   const char name[12];
6641   unsigned char lo, hi;
6642   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6643   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6644 };
6645
6646 /* Auto-generate _save*, _rest* functions in .sfpr.
6647    If STUB_SEC is non-null, define alias symbols in STUB_SEC
6648    instead.  */
6649
6650 static bfd_boolean
6651 sfpr_define (struct bfd_link_info *info,
6652              const struct sfpr_def_parms *parm,
6653              asection *stub_sec)
6654 {
6655   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6656   unsigned int i;
6657   size_t len = strlen (parm->name);
6658   bfd_boolean writing = FALSE;
6659   char sym[16];
6660
6661   if (htab == NULL)
6662     return FALSE;
6663
6664   memcpy (sym, parm->name, len);
6665   sym[len + 2] = 0;
6666
6667   for (i = parm->lo; i <= parm->hi; i++)
6668     {
6669       struct ppc_link_hash_entry *h;
6670
6671       sym[len + 0] = i / 10 + '0';
6672       sym[len + 1] = i % 10 + '0';
6673       h = (struct ppc_link_hash_entry *)
6674         elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6675       if (stub_sec != NULL)
6676         {
6677           if (h != NULL
6678               && h->elf.root.type == bfd_link_hash_defined
6679               && h->elf.root.u.def.section == htab->sfpr)
6680             {
6681               struct elf_link_hash_entry *s;
6682               char buf[32];
6683               sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6684               s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6685               if (s == NULL)
6686                 return FALSE;
6687               if (s->root.type == bfd_link_hash_new
6688                   || (s->root.type = bfd_link_hash_defined
6689                       && s->root.u.def.section == stub_sec))
6690                 {
6691                   s->root.type = bfd_link_hash_defined;
6692                   s->root.u.def.section = stub_sec;
6693                   s->root.u.def.value = (stub_sec->size
6694                                          + h->elf.root.u.def.value);
6695                   s->ref_regular = 1;
6696                   s->def_regular = 1;
6697                   s->ref_regular_nonweak = 1;
6698                   s->forced_local = 1;
6699                   s->non_elf = 0;
6700                   s->root.linker_def = 1;
6701                 }
6702             }
6703           continue;
6704         }
6705       if (h != NULL)
6706         {
6707           h->save_res = 1;
6708           if (!h->elf.def_regular)
6709             {
6710               h->elf.root.type = bfd_link_hash_defined;
6711               h->elf.root.u.def.section = htab->sfpr;
6712               h->elf.root.u.def.value = htab->sfpr->size;
6713               h->elf.type = STT_FUNC;
6714               h->elf.def_regular = 1;
6715               h->elf.non_elf = 0;
6716               _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6717               writing = TRUE;
6718               if (htab->sfpr->contents == NULL)
6719                 {
6720                   htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6721                   if (htab->sfpr->contents == NULL)
6722                     return FALSE;
6723                 }
6724             }
6725         }
6726       if (writing)
6727         {
6728           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6729           if (i != parm->hi)
6730             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6731           else
6732             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6733           htab->sfpr->size = p - htab->sfpr->contents;
6734         }
6735     }
6736
6737   return TRUE;
6738 }
6739
6740 static bfd_byte *
6741 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6742 {
6743   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6744   return p + 4;
6745 }
6746
6747 static bfd_byte *
6748 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6749 {
6750   p = savegpr0 (abfd, p, r);
6751   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6752   p = p + 4;
6753   bfd_put_32 (abfd, BLR, p);
6754   return p + 4;
6755 }
6756
6757 static bfd_byte *
6758 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6759 {
6760   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6761   return p + 4;
6762 }
6763
6764 static bfd_byte *
6765 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6766 {
6767   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6768   p = p + 4;
6769   p = restgpr0 (abfd, p, r);
6770   bfd_put_32 (abfd, MTLR_R0, p);
6771   p = p + 4;
6772   if (r == 29)
6773     {
6774       p = restgpr0 (abfd, p, 30);
6775       p = restgpr0 (abfd, p, 31);
6776     }
6777   bfd_put_32 (abfd, BLR, p);
6778   return p + 4;
6779 }
6780
6781 static bfd_byte *
6782 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6783 {
6784   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6785   return p + 4;
6786 }
6787
6788 static bfd_byte *
6789 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6790 {
6791   p = savegpr1 (abfd, p, r);
6792   bfd_put_32 (abfd, BLR, p);
6793   return p + 4;
6794 }
6795
6796 static bfd_byte *
6797 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6798 {
6799   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6800   return p + 4;
6801 }
6802
6803 static bfd_byte *
6804 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6805 {
6806   p = restgpr1 (abfd, p, r);
6807   bfd_put_32 (abfd, BLR, p);
6808   return p + 4;
6809 }
6810
6811 static bfd_byte *
6812 savefpr (bfd *abfd, bfd_byte *p, int r)
6813 {
6814   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6815   return p + 4;
6816 }
6817
6818 static bfd_byte *
6819 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6820 {
6821   p = savefpr (abfd, p, r);
6822   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6823   p = p + 4;
6824   bfd_put_32 (abfd, BLR, p);
6825   return p + 4;
6826 }
6827
6828 static bfd_byte *
6829 restfpr (bfd *abfd, bfd_byte *p, int r)
6830 {
6831   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6832   return p + 4;
6833 }
6834
6835 static bfd_byte *
6836 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6837 {
6838   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6839   p = p + 4;
6840   p = restfpr (abfd, p, r);
6841   bfd_put_32 (abfd, MTLR_R0, p);
6842   p = p + 4;
6843   if (r == 29)
6844     {
6845       p = restfpr (abfd, p, 30);
6846       p = restfpr (abfd, p, 31);
6847     }
6848   bfd_put_32 (abfd, BLR, p);
6849   return p + 4;
6850 }
6851
6852 static bfd_byte *
6853 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6854 {
6855   p = savefpr (abfd, p, r);
6856   bfd_put_32 (abfd, BLR, p);
6857   return p + 4;
6858 }
6859
6860 static bfd_byte *
6861 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6862 {
6863   p = restfpr (abfd, p, r);
6864   bfd_put_32 (abfd, BLR, p);
6865   return p + 4;
6866 }
6867
6868 static bfd_byte *
6869 savevr (bfd *abfd, bfd_byte *p, int r)
6870 {
6871   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6872   p = p + 4;
6873   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6874   return p + 4;
6875 }
6876
6877 static bfd_byte *
6878 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6879 {
6880   p = savevr (abfd, p, r);
6881   bfd_put_32 (abfd, BLR, p);
6882   return p + 4;
6883 }
6884
6885 static bfd_byte *
6886 restvr (bfd *abfd, bfd_byte *p, int r)
6887 {
6888   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6889   p = p + 4;
6890   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6891   return p + 4;
6892 }
6893
6894 static bfd_byte *
6895 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6896 {
6897   p = restvr (abfd, p, r);
6898   bfd_put_32 (abfd, BLR, p);
6899   return p + 4;
6900 }
6901
6902 /* Called via elf_link_hash_traverse to transfer dynamic linking
6903    information on function code symbol entries to their corresponding
6904    function descriptor symbol entries.  */
6905
6906 static bfd_boolean
6907 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6908 {
6909   struct bfd_link_info *info;
6910   struct ppc_link_hash_table *htab;
6911   struct plt_entry *ent;
6912   struct ppc_link_hash_entry *fh;
6913   struct ppc_link_hash_entry *fdh;
6914   bfd_boolean force_local;
6915
6916   fh = (struct ppc_link_hash_entry *) h;
6917   if (fh->elf.root.type == bfd_link_hash_indirect)
6918     return TRUE;
6919
6920   info = inf;
6921   htab = ppc_hash_table (info);
6922   if (htab == NULL)
6923     return FALSE;
6924
6925   /* Resolve undefined references to dot-symbols as the value
6926      in the function descriptor, if we have one in a regular object.
6927      This is to satisfy cases like ".quad .foo".  Calls to functions
6928      in dynamic objects are handled elsewhere.  */
6929   if (fh->elf.root.type == bfd_link_hash_undefweak
6930       && fh->was_undefined
6931       && (fdh = defined_func_desc (fh)) != NULL
6932       && get_opd_info (fdh->elf.root.u.def.section) != NULL
6933       && opd_entry_value (fdh->elf.root.u.def.section,
6934                           fdh->elf.root.u.def.value,
6935                           &fh->elf.root.u.def.section,
6936                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6937     {
6938       fh->elf.root.type = fdh->elf.root.type;
6939       fh->elf.forced_local = 1;
6940       fh->elf.def_regular = fdh->elf.def_regular;
6941       fh->elf.def_dynamic = fdh->elf.def_dynamic;
6942     }
6943
6944   /* If this is a function code symbol, transfer dynamic linking
6945      information to the function descriptor symbol.  */
6946   if (!fh->is_func)
6947     return TRUE;
6948
6949   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6950     if (ent->plt.refcount > 0)
6951       break;
6952   if (ent == NULL
6953       || fh->elf.root.root.string[0] != '.'
6954       || fh->elf.root.root.string[1] == '\0')
6955     return TRUE;
6956
6957   /* Find the corresponding function descriptor symbol.  Create it
6958      as undefined if necessary.  */
6959
6960   fdh = lookup_fdh (fh, htab);
6961   if (fdh == NULL
6962       && !bfd_link_executable (info)
6963       && (fh->elf.root.type == bfd_link_hash_undefined
6964           || fh->elf.root.type == bfd_link_hash_undefweak))
6965     {
6966       fdh = make_fdh (info, fh);
6967       if (fdh == NULL)
6968         return FALSE;
6969     }
6970
6971   /* Fake function descriptors are made undefweak.  If the function
6972      code symbol is strong undefined, make the fake sym the same.
6973      If the function code symbol is defined, then force the fake
6974      descriptor local;  We can't support overriding of symbols in a
6975      shared library on a fake descriptor.  */
6976
6977   if (fdh != NULL
6978       && fdh->fake
6979       && fdh->elf.root.type == bfd_link_hash_undefweak)
6980     {
6981       if (fh->elf.root.type == bfd_link_hash_undefined)
6982         {
6983           fdh->elf.root.type = bfd_link_hash_undefined;
6984           bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6985         }
6986       else if (fh->elf.root.type == bfd_link_hash_defined
6987                || fh->elf.root.type == bfd_link_hash_defweak)
6988         {
6989           _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6990         }
6991     }
6992
6993   if (fdh != NULL
6994       && !fdh->elf.forced_local
6995       && (!bfd_link_executable (info)
6996           || fdh->elf.def_dynamic
6997           || fdh->elf.ref_dynamic
6998           || (fdh->elf.root.type == bfd_link_hash_undefweak
6999               && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
7000     {
7001       if (fdh->elf.dynindx == -1)
7002         if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7003           return FALSE;
7004       fdh->elf.ref_regular |= fh->elf.ref_regular;
7005       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7006       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7007       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
7008       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
7009         {
7010           move_plt_plist (fh, fdh);
7011           fdh->elf.needs_plt = 1;
7012         }
7013       fdh->is_func_descriptor = 1;
7014       fdh->oh = fh;
7015       fh->oh = fdh;
7016     }
7017
7018   /* Now that the info is on the function descriptor, clear the
7019      function code sym info.  Any function code syms for which we
7020      don't have a definition in a regular file, we force local.
7021      This prevents a shared library from exporting syms that have
7022      been imported from another library.  Function code syms that
7023      are really in the library we must leave global to prevent the
7024      linker dragging in a definition from a static library.  */
7025   force_local = (!fh->elf.def_regular
7026                  || fdh == NULL
7027                  || !fdh->elf.def_regular
7028                  || fdh->elf.forced_local);
7029   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7030
7031   return TRUE;
7032 }
7033
7034 static const struct sfpr_def_parms save_res_funcs[] =
7035   {
7036     { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7037     { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7038     { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7039     { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7040     { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7041     { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7042     { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7043     { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7044     { "._savef", 14, 31, savefpr, savefpr1_tail },
7045     { "._restf", 14, 31, restfpr, restfpr1_tail },
7046     { "_savevr_", 20, 31, savevr, savevr_tail },
7047     { "_restvr_", 20, 31, restvr, restvr_tail }
7048   };
7049
7050 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
7051    this hook to a) provide some gcc support functions, and b) transfer
7052    dynamic linking information gathered so far on function code symbol
7053    entries, to their corresponding function descriptor symbol entries.  */
7054
7055 static bfd_boolean
7056 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7057                             struct bfd_link_info *info)
7058 {
7059   struct ppc_link_hash_table *htab;
7060
7061   htab = ppc_hash_table (info);
7062   if (htab == NULL)
7063     return FALSE;
7064
7065   /* Provide any missing _save* and _rest* functions.  */
7066   if (htab->sfpr != NULL)
7067     {
7068       unsigned int i;
7069
7070       htab->sfpr->size = 0;
7071       for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7072         if (!sfpr_define (info, &save_res_funcs[i], NULL))
7073           return FALSE;
7074       if (htab->sfpr->size == 0)
7075         htab->sfpr->flags |= SEC_EXCLUDE;
7076     }
7077
7078   if (bfd_link_relocatable (info))
7079     return TRUE;
7080
7081   if (htab->elf.hgot != NULL)
7082     {
7083       _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7084       /* Make .TOC. defined so as to prevent it being made dynamic.
7085          The wrong value here is fixed later in ppc64_elf_set_toc.  */
7086       if (!htab->elf.hgot->def_regular
7087           || htab->elf.hgot->root.type != bfd_link_hash_defined)
7088         {
7089           htab->elf.hgot->root.type = bfd_link_hash_defined;
7090           htab->elf.hgot->root.u.def.value = 0;
7091           htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7092           htab->elf.hgot->def_regular = 1;
7093           htab->elf.hgot->root.linker_def = 1;
7094         }
7095       htab->elf.hgot->type = STT_OBJECT;
7096       htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7097                                | STV_HIDDEN);
7098     }
7099
7100   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7101
7102   return TRUE;
7103 }
7104
7105 /* Return true if we have dynamic relocs against H that apply to
7106    read-only sections.  */
7107
7108 static bfd_boolean
7109 readonly_dynrelocs (struct elf_link_hash_entry *h)
7110 {
7111   struct ppc_link_hash_entry *eh;
7112   struct elf_dyn_relocs *p;
7113
7114   eh = (struct ppc_link_hash_entry *) h;
7115   for (p = eh->dyn_relocs; p != NULL; p = p->next)
7116     {
7117       asection *s = p->sec->output_section;
7118
7119       if (s != NULL && (s->flags & SEC_READONLY) != 0)
7120         return TRUE;
7121     }
7122   return FALSE;
7123 }
7124
7125
7126 /* Return true if a global entry stub will be created for H.  Valid
7127    for ELFv2 before plt entries have been allocated.  */
7128
7129 static bfd_boolean
7130 global_entry_stub (struct elf_link_hash_entry *h)
7131 {
7132   struct plt_entry *pent;
7133
7134   if (!h->pointer_equality_needed
7135       || h->def_regular)
7136     return FALSE;
7137
7138   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7139     if (pent->plt.refcount > 0
7140         && pent->addend == 0)
7141       return TRUE;
7142
7143   return FALSE;
7144 }
7145
7146 /* Adjust a symbol defined by a dynamic object and referenced by a
7147    regular object.  The current definition is in some section of the
7148    dynamic object, but we're not including those sections.  We have to
7149    change the definition to something the rest of the link can
7150    understand.  */
7151
7152 static bfd_boolean
7153 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7154                                  struct elf_link_hash_entry *h)
7155 {
7156   struct ppc_link_hash_table *htab;
7157   asection *s;
7158
7159   htab = ppc_hash_table (info);
7160   if (htab == NULL)
7161     return FALSE;
7162
7163   /* Deal with function syms.  */
7164   if (h->type == STT_FUNC
7165       || h->type == STT_GNU_IFUNC
7166       || h->needs_plt)
7167     {
7168       /* Clear procedure linkage table information for any symbol that
7169          won't need a .plt entry.  */
7170       struct plt_entry *ent;
7171       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7172         if (ent->plt.refcount > 0)
7173           break;
7174       if (ent == NULL
7175           || (h->type != STT_GNU_IFUNC
7176               && (SYMBOL_CALLS_LOCAL (info, h)
7177                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7178                       && h->root.type == bfd_link_hash_undefweak)))
7179           || ((struct ppc_link_hash_entry *) h)->save_res)
7180         {
7181           h->plt.plist = NULL;
7182           h->needs_plt = 0;
7183           h->pointer_equality_needed = 0;
7184         }
7185       else if (abiversion (info->output_bfd) >= 2)
7186         {
7187           /* Taking a function's address in a read/write section
7188              doesn't require us to define the function symbol in the
7189              executable on a global entry stub.  A dynamic reloc can
7190              be used instead.  The reason we prefer a few more dynamic
7191              relocs is that calling via a global entry stub costs a
7192              few more instructions, and pointer_equality_needed causes
7193              extra work in ld.so when resolving these symbols.  */
7194           if (global_entry_stub (h)
7195               && !readonly_dynrelocs (h))
7196             {
7197               h->pointer_equality_needed = 0;
7198               /* After adjust_dynamic_symbol, non_got_ref set in
7199                  the non-pic case means that dyn_relocs for this
7200                  symbol should be discarded.  */
7201               h->non_got_ref = 0;
7202             }
7203
7204           /* If making a plt entry, then we don't need copy relocs.  */
7205           return TRUE;
7206         }
7207     }
7208   else
7209     h->plt.plist = NULL;
7210
7211   /* If this is a weak symbol, and there is a real definition, the
7212      processor independent code will have arranged for us to see the
7213      real definition first, and we can just use the same value.  */
7214   if (h->u.weakdef != NULL)
7215     {
7216       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7217                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
7218       h->root.u.def.section = h->u.weakdef->root.u.def.section;
7219       h->root.u.def.value = h->u.weakdef->root.u.def.value;
7220       if (ELIMINATE_COPY_RELOCS)
7221         h->non_got_ref = h->u.weakdef->non_got_ref;
7222       return TRUE;
7223     }
7224
7225   /* If we are creating a shared library, we must presume that the
7226      only references to the symbol are via the global offset table.
7227      For such cases we need not do anything here; the relocations will
7228      be handled correctly by relocate_section.  */
7229   if (bfd_link_pic (info))
7230     return TRUE;
7231
7232   /* If there are no references to this symbol that do not use the
7233      GOT, we don't need to generate a copy reloc.  */
7234   if (!h->non_got_ref)
7235     return TRUE;
7236
7237   /* Don't generate a copy reloc for symbols defined in the executable.  */
7238   if (!h->def_dynamic || !h->ref_regular || h->def_regular
7239
7240       /* If -z nocopyreloc was given, don't generate them either.  */
7241       || info->nocopyreloc
7242
7243       /* If we didn't find any dynamic relocs in read-only sections, then
7244          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
7245       || (ELIMINATE_COPY_RELOCS && !readonly_dynrelocs (h))
7246
7247       /* Protected variables do not work with .dynbss.  The copy in
7248          .dynbss won't be used by the shared library with the protected
7249          definition for the variable.  Text relocations are preferable
7250          to an incorrect program.  */
7251       || h->protected_def)
7252     {
7253       h->non_got_ref = 0;
7254       return TRUE;
7255     }
7256
7257   if (h->plt.plist != NULL)
7258     {
7259       /* We should never get here, but unfortunately there are versions
7260          of gcc out there that improperly (for this ABI) put initialized
7261          function pointers, vtable refs and suchlike in read-only
7262          sections.  Allow them to proceed, but warn that this might
7263          break at runtime.  */
7264       info->callbacks->einfo
7265         (_("%P: copy reloc against `%T' requires lazy plt linking; "
7266            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7267          h->root.root.string);
7268     }
7269
7270   /* This is a reference to a symbol defined by a dynamic object which
7271      is not a function.  */
7272
7273   /* We must allocate the symbol in our .dynbss section, which will
7274      become part of the .bss section of the executable.  There will be
7275      an entry for this symbol in the .dynsym section.  The dynamic
7276      object will contain position independent code, so all references
7277      from the dynamic object to this symbol will go through the global
7278      offset table.  The dynamic linker will use the .dynsym entry to
7279      determine the address it must put in the global offset table, so
7280      both the dynamic object and the regular object will refer to the
7281      same memory location for the variable.  */
7282
7283   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7284      to copy the initial value out of the dynamic object and into the
7285      runtime process image.  We need to remember the offset into the
7286      .rela.bss section we are going to use.  */
7287   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7288     {
7289       htab->relbss->size += sizeof (Elf64_External_Rela);
7290       h->needs_copy = 1;
7291     }
7292
7293   s = htab->dynbss;
7294
7295   return _bfd_elf_adjust_dynamic_copy (info, h, s);
7296 }
7297
7298 /* If given a function descriptor symbol, hide both the function code
7299    sym and the descriptor.  */
7300 static void
7301 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7302                        struct elf_link_hash_entry *h,
7303                        bfd_boolean force_local)
7304 {
7305   struct ppc_link_hash_entry *eh;
7306   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7307
7308   eh = (struct ppc_link_hash_entry *) h;
7309   if (eh->is_func_descriptor)
7310     {
7311       struct ppc_link_hash_entry *fh = eh->oh;
7312
7313       if (fh == NULL)
7314         {
7315           const char *p, *q;
7316           struct ppc_link_hash_table *htab;
7317           char save;
7318
7319           /* We aren't supposed to use alloca in BFD because on
7320              systems which do not have alloca the version in libiberty
7321              calls xmalloc, which might cause the program to crash
7322              when it runs out of memory.  This function doesn't have a
7323              return status, so there's no way to gracefully return an
7324              error.  So cheat.  We know that string[-1] can be safely
7325              accessed;  It's either a string in an ELF string table,
7326              or allocated in an objalloc structure.  */
7327
7328           p = eh->elf.root.root.string - 1;
7329           save = *p;
7330           *(char *) p = '.';
7331           htab = ppc_hash_table (info);
7332           if (htab == NULL)
7333             return;
7334
7335           fh = (struct ppc_link_hash_entry *)
7336             elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7337           *(char *) p = save;
7338
7339           /* Unfortunately, if it so happens that the string we were
7340              looking for was allocated immediately before this string,
7341              then we overwrote the string terminator.  That's the only
7342              reason the lookup should fail.  */
7343           if (fh == NULL)
7344             {
7345               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7346               while (q >= eh->elf.root.root.string && *q == *p)
7347                 --q, --p;
7348               if (q < eh->elf.root.root.string && *p == '.')
7349                 fh = (struct ppc_link_hash_entry *)
7350                   elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7351             }
7352           if (fh != NULL)
7353             {
7354               eh->oh = fh;
7355               fh->oh = eh;
7356             }
7357         }
7358       if (fh != NULL)
7359         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7360     }
7361 }
7362
7363 static bfd_boolean
7364 get_sym_h (struct elf_link_hash_entry **hp,
7365            Elf_Internal_Sym **symp,
7366            asection **symsecp,
7367            unsigned char **tls_maskp,
7368            Elf_Internal_Sym **locsymsp,
7369            unsigned long r_symndx,
7370            bfd *ibfd)
7371 {
7372   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7373
7374   if (r_symndx >= symtab_hdr->sh_info)
7375     {
7376       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7377       struct elf_link_hash_entry *h;
7378
7379       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7380       h = elf_follow_link (h);
7381
7382       if (hp != NULL)
7383         *hp = h;
7384
7385       if (symp != NULL)
7386         *symp = NULL;
7387
7388       if (symsecp != NULL)
7389         {
7390           asection *symsec = NULL;
7391           if (h->root.type == bfd_link_hash_defined
7392               || h->root.type == bfd_link_hash_defweak)
7393             symsec = h->root.u.def.section;
7394           *symsecp = symsec;
7395         }
7396
7397       if (tls_maskp != NULL)
7398         {
7399           struct ppc_link_hash_entry *eh;
7400
7401           eh = (struct ppc_link_hash_entry *) h;
7402           *tls_maskp = &eh->tls_mask;
7403         }
7404     }
7405   else
7406     {
7407       Elf_Internal_Sym *sym;
7408       Elf_Internal_Sym *locsyms = *locsymsp;
7409
7410       if (locsyms == NULL)
7411         {
7412           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7413           if (locsyms == NULL)
7414             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7415                                             symtab_hdr->sh_info,
7416                                             0, NULL, NULL, NULL);
7417           if (locsyms == NULL)
7418             return FALSE;
7419           *locsymsp = locsyms;
7420         }
7421       sym = locsyms + r_symndx;
7422
7423       if (hp != NULL)
7424         *hp = NULL;
7425
7426       if (symp != NULL)
7427         *symp = sym;
7428
7429       if (symsecp != NULL)
7430         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7431
7432       if (tls_maskp != NULL)
7433         {
7434           struct got_entry **lgot_ents;
7435           unsigned char *tls_mask;
7436
7437           tls_mask = NULL;
7438           lgot_ents = elf_local_got_ents (ibfd);
7439           if (lgot_ents != NULL)
7440             {
7441               struct plt_entry **local_plt = (struct plt_entry **)
7442                 (lgot_ents + symtab_hdr->sh_info);
7443               unsigned char *lgot_masks = (unsigned char *)
7444                 (local_plt + symtab_hdr->sh_info);
7445               tls_mask = &lgot_masks[r_symndx];
7446             }
7447           *tls_maskp = tls_mask;
7448         }
7449     }
7450   return TRUE;
7451 }
7452
7453 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
7454    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7455    type suitable for optimization, and 1 otherwise.  */
7456
7457 static int
7458 get_tls_mask (unsigned char **tls_maskp,
7459               unsigned long *toc_symndx,
7460               bfd_vma *toc_addend,
7461               Elf_Internal_Sym **locsymsp,
7462               const Elf_Internal_Rela *rel,
7463               bfd *ibfd)
7464 {
7465   unsigned long r_symndx;
7466   int next_r;
7467   struct elf_link_hash_entry *h;
7468   Elf_Internal_Sym *sym;
7469   asection *sec;
7470   bfd_vma off;
7471
7472   r_symndx = ELF64_R_SYM (rel->r_info);
7473   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7474     return 0;
7475
7476   if ((*tls_maskp != NULL && **tls_maskp != 0)
7477       || sec == NULL
7478       || ppc64_elf_section_data (sec) == NULL
7479       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7480     return 1;
7481
7482   /* Look inside a TOC section too.  */
7483   if (h != NULL)
7484     {
7485       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7486       off = h->root.u.def.value;
7487     }
7488   else
7489     off = sym->st_value;
7490   off += rel->r_addend;
7491   BFD_ASSERT (off % 8 == 0);
7492   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7493   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7494   if (toc_symndx != NULL)
7495     *toc_symndx = r_symndx;
7496   if (toc_addend != NULL)
7497     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7498   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7499     return 0;
7500   if ((h == NULL || is_static_defined (h))
7501       && (next_r == -1 || next_r == -2))
7502     return 1 - next_r;
7503   return 1;
7504 }
7505
7506 /* Find (or create) an entry in the tocsave hash table.  */
7507
7508 static struct tocsave_entry *
7509 tocsave_find (struct ppc_link_hash_table *htab,
7510               enum insert_option insert,
7511               Elf_Internal_Sym **local_syms,
7512               const Elf_Internal_Rela *irela,
7513               bfd *ibfd)
7514 {
7515   unsigned long r_indx;
7516   struct elf_link_hash_entry *h;
7517   Elf_Internal_Sym *sym;
7518   struct tocsave_entry ent, *p;
7519   hashval_t hash;
7520   struct tocsave_entry **slot;
7521
7522   r_indx = ELF64_R_SYM (irela->r_info);
7523   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7524     return NULL;
7525   if (ent.sec == NULL || ent.sec->output_section == NULL)
7526     {
7527       (*_bfd_error_handler)
7528         (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7529       return NULL;
7530     }
7531
7532   if (h != NULL)
7533     ent.offset = h->root.u.def.value;
7534   else
7535     ent.offset = sym->st_value;
7536   ent.offset += irela->r_addend;
7537
7538   hash = tocsave_htab_hash (&ent);
7539   slot = ((struct tocsave_entry **)
7540           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7541   if (slot == NULL)
7542     return NULL;
7543
7544   if (*slot == NULL)
7545     {
7546       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7547       if (p == NULL)
7548         return NULL;
7549       *p = ent;
7550       *slot = p;
7551     }
7552   return *slot;
7553 }
7554
7555 /* Adjust all global syms defined in opd sections.  In gcc generated
7556    code for the old ABI, these will already have been done.  */
7557
7558 static bfd_boolean
7559 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7560 {
7561   struct ppc_link_hash_entry *eh;
7562   asection *sym_sec;
7563   struct _opd_sec_data *opd;
7564
7565   if (h->root.type == bfd_link_hash_indirect)
7566     return TRUE;
7567
7568   if (h->root.type != bfd_link_hash_defined
7569       && h->root.type != bfd_link_hash_defweak)
7570     return TRUE;
7571
7572   eh = (struct ppc_link_hash_entry *) h;
7573   if (eh->adjust_done)
7574     return TRUE;
7575
7576   sym_sec = eh->elf.root.u.def.section;
7577   opd = get_opd_info (sym_sec);
7578   if (opd != NULL && opd->adjust != NULL)
7579     {
7580       long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7581       if (adjust == -1)
7582         {
7583           /* This entry has been deleted.  */
7584           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7585           if (dsec == NULL)
7586             {
7587               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7588                 if (discarded_section (dsec))
7589                   {
7590                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7591                     break;
7592                   }
7593             }
7594           eh->elf.root.u.def.value = 0;
7595           eh->elf.root.u.def.section = dsec;
7596         }
7597       else
7598         eh->elf.root.u.def.value += adjust;
7599       eh->adjust_done = 1;
7600     }
7601   return TRUE;
7602 }
7603
7604 /* Handles decrementing dynamic reloc counts for the reloc specified by
7605    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
7606    have already been determined.  */
7607
7608 static bfd_boolean
7609 dec_dynrel_count (bfd_vma r_info,
7610                   asection *sec,
7611                   struct bfd_link_info *info,
7612                   Elf_Internal_Sym **local_syms,
7613                   struct elf_link_hash_entry *h,
7614                   Elf_Internal_Sym *sym)
7615 {
7616   enum elf_ppc64_reloc_type r_type;
7617   asection *sym_sec = NULL;
7618
7619   /* Can this reloc be dynamic?  This switch, and later tests here
7620      should be kept in sync with the code in check_relocs.  */
7621   r_type = ELF64_R_TYPE (r_info);
7622   switch (r_type)
7623     {
7624     default:
7625       return TRUE;
7626
7627     case R_PPC64_TPREL16:
7628     case R_PPC64_TPREL16_LO:
7629     case R_PPC64_TPREL16_HI:
7630     case R_PPC64_TPREL16_HA:
7631     case R_PPC64_TPREL16_DS:
7632     case R_PPC64_TPREL16_LO_DS:
7633     case R_PPC64_TPREL16_HIGH:
7634     case R_PPC64_TPREL16_HIGHA:
7635     case R_PPC64_TPREL16_HIGHER:
7636     case R_PPC64_TPREL16_HIGHERA:
7637     case R_PPC64_TPREL16_HIGHEST:
7638     case R_PPC64_TPREL16_HIGHESTA:
7639       if (!bfd_link_pic (info))
7640         return TRUE;
7641
7642     case R_PPC64_TPREL64:
7643     case R_PPC64_DTPMOD64:
7644     case R_PPC64_DTPREL64:
7645     case R_PPC64_ADDR64:
7646     case R_PPC64_REL30:
7647     case R_PPC64_REL32:
7648     case R_PPC64_REL64:
7649     case R_PPC64_ADDR14:
7650     case R_PPC64_ADDR14_BRNTAKEN:
7651     case R_PPC64_ADDR14_BRTAKEN:
7652     case R_PPC64_ADDR16:
7653     case R_PPC64_ADDR16_DS:
7654     case R_PPC64_ADDR16_HA:
7655     case R_PPC64_ADDR16_HI:
7656     case R_PPC64_ADDR16_HIGH:
7657     case R_PPC64_ADDR16_HIGHA:
7658     case R_PPC64_ADDR16_HIGHER:
7659     case R_PPC64_ADDR16_HIGHERA:
7660     case R_PPC64_ADDR16_HIGHEST:
7661     case R_PPC64_ADDR16_HIGHESTA:
7662     case R_PPC64_ADDR16_LO:
7663     case R_PPC64_ADDR16_LO_DS:
7664     case R_PPC64_ADDR24:
7665     case R_PPC64_ADDR32:
7666     case R_PPC64_UADDR16:
7667     case R_PPC64_UADDR32:
7668     case R_PPC64_UADDR64:
7669     case R_PPC64_TOC:
7670       break;
7671     }
7672
7673   if (local_syms != NULL)
7674     {
7675       unsigned long r_symndx;
7676       bfd *ibfd = sec->owner;
7677
7678       r_symndx = ELF64_R_SYM (r_info);
7679       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7680         return FALSE;
7681     }
7682
7683   if ((bfd_link_pic (info)
7684        && (must_be_dyn_reloc (info, r_type)
7685            || (h != NULL
7686                && (!SYMBOLIC_BIND (info, h)
7687                    || h->root.type == bfd_link_hash_defweak
7688                    || !h->def_regular))))
7689       || (ELIMINATE_COPY_RELOCS
7690           && !bfd_link_pic (info)
7691           && h != NULL
7692           && (h->root.type == bfd_link_hash_defweak
7693               || !h->def_regular)))
7694     ;
7695   else
7696     return TRUE;
7697
7698   if (h != NULL)
7699     {
7700       struct elf_dyn_relocs *p;
7701       struct elf_dyn_relocs **pp;
7702       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7703
7704       /* elf_gc_sweep may have already removed all dyn relocs associated
7705          with local syms for a given section.  Also, symbol flags are
7706          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
7707          report a dynreloc miscount.  */
7708       if (*pp == NULL && info->gc_sections)
7709         return TRUE;
7710
7711       while ((p = *pp) != NULL)
7712         {
7713           if (p->sec == sec)
7714             {
7715               if (!must_be_dyn_reloc (info, r_type))
7716                 p->pc_count -= 1;
7717               p->count -= 1;
7718               if (p->count == 0)
7719                 *pp = p->next;
7720               return TRUE;
7721             }
7722           pp = &p->next;
7723         }
7724     }
7725   else
7726     {
7727       struct ppc_dyn_relocs *p;
7728       struct ppc_dyn_relocs **pp;
7729       void *vpp;
7730       bfd_boolean is_ifunc;
7731
7732       if (local_syms == NULL)
7733         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7734       if (sym_sec == NULL)
7735         sym_sec = sec;
7736
7737       vpp = &elf_section_data (sym_sec)->local_dynrel;
7738       pp = (struct ppc_dyn_relocs **) vpp;
7739
7740       if (*pp == NULL && info->gc_sections)
7741         return TRUE;
7742
7743       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7744       while ((p = *pp) != NULL)
7745         {
7746           if (p->sec == sec && p->ifunc == is_ifunc)
7747             {
7748               p->count -= 1;
7749               if (p->count == 0)
7750                 *pp = p->next;
7751               return TRUE;
7752             }
7753           pp = &p->next;
7754         }
7755     }
7756
7757   info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7758                           sec->owner, sec);
7759   bfd_set_error (bfd_error_bad_value);
7760   return FALSE;
7761 }
7762
7763 /* Remove unused Official Procedure Descriptor entries.  Currently we
7764    only remove those associated with functions in discarded link-once
7765    sections, or weakly defined functions that have been overridden.  It
7766    would be possible to remove many more entries for statically linked
7767    applications.  */
7768
7769 bfd_boolean
7770 ppc64_elf_edit_opd (struct bfd_link_info *info)
7771 {
7772   bfd *ibfd;
7773   bfd_boolean some_edited = FALSE;
7774   asection *need_pad = NULL;
7775   struct ppc_link_hash_table *htab;
7776
7777   htab = ppc_hash_table (info);
7778   if (htab == NULL)
7779     return FALSE;
7780
7781   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7782     {
7783       asection *sec;
7784       Elf_Internal_Rela *relstart, *rel, *relend;
7785       Elf_Internal_Shdr *symtab_hdr;
7786       Elf_Internal_Sym *local_syms;
7787       struct _opd_sec_data *opd;
7788       bfd_boolean need_edit, add_aux_fields, broken;
7789       bfd_size_type cnt_16b = 0;
7790
7791       if (!is_ppc64_elf (ibfd))
7792         continue;
7793
7794       sec = bfd_get_section_by_name (ibfd, ".opd");
7795       if (sec == NULL || sec->size == 0)
7796         continue;
7797
7798       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7799         continue;
7800
7801       if (sec->output_section == bfd_abs_section_ptr)
7802         continue;
7803
7804       /* Look through the section relocs.  */
7805       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7806         continue;
7807
7808       local_syms = NULL;
7809       symtab_hdr = &elf_symtab_hdr (ibfd);
7810
7811       /* Read the relocations.  */
7812       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7813                                             info->keep_memory);
7814       if (relstart == NULL)
7815         return FALSE;
7816
7817       /* First run through the relocs to check they are sane, and to
7818          determine whether we need to edit this opd section.  */
7819       need_edit = FALSE;
7820       broken = FALSE;
7821       need_pad = sec;
7822       relend = relstart + sec->reloc_count;
7823       for (rel = relstart; rel < relend; )
7824         {
7825           enum elf_ppc64_reloc_type r_type;
7826           unsigned long r_symndx;
7827           asection *sym_sec;
7828           struct elf_link_hash_entry *h;
7829           Elf_Internal_Sym *sym;
7830           bfd_vma offset;
7831
7832           /* .opd contains an array of 16 or 24 byte entries.  We're
7833              only interested in the reloc pointing to a function entry
7834              point.  */
7835           offset = rel->r_offset;
7836           if (rel + 1 == relend
7837               || rel[1].r_offset != offset + 8)
7838             {
7839               /* If someone messes with .opd alignment then after a
7840                  "ld -r" we might have padding in the middle of .opd.
7841                  Also, there's nothing to prevent someone putting
7842                  something silly in .opd with the assembler.  No .opd
7843                  optimization for them!  */
7844             broken_opd:
7845               (*_bfd_error_handler)
7846                 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7847               broken = TRUE;
7848               break;
7849             }
7850
7851           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7852               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7853             {
7854               (*_bfd_error_handler)
7855                 (_("%B: unexpected reloc type %u in .opd section"),
7856                  ibfd, r_type);
7857               broken = TRUE;
7858               break;
7859             }
7860
7861           r_symndx = ELF64_R_SYM (rel->r_info);
7862           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7863                           r_symndx, ibfd))
7864             goto error_ret;
7865
7866           if (sym_sec == NULL || sym_sec->owner == NULL)
7867             {
7868               const char *sym_name;
7869               if (h != NULL)
7870                 sym_name = h->root.root.string;
7871               else
7872                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7873                                              sym_sec);
7874
7875               (*_bfd_error_handler)
7876                 (_("%B: undefined sym `%s' in .opd section"),
7877                  ibfd, sym_name);
7878               broken = TRUE;
7879               break;
7880             }
7881
7882           /* opd entries are always for functions defined in the
7883              current input bfd.  If the symbol isn't defined in the
7884              input bfd, then we won't be using the function in this
7885              bfd;  It must be defined in a linkonce section in another
7886              bfd, or is weak.  It's also possible that we are
7887              discarding the function due to a linker script /DISCARD/,
7888              which we test for via the output_section.  */
7889           if (sym_sec->owner != ibfd
7890               || sym_sec->output_section == bfd_abs_section_ptr)
7891             need_edit = TRUE;
7892
7893           rel += 2;
7894           if (rel + 1 == relend
7895               || (rel + 2 < relend
7896                   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7897             ++rel;
7898
7899           if (rel == relend)
7900             {
7901               if (sec->size == offset + 24)
7902                 {
7903                   need_pad = NULL;
7904                   break;
7905                 }
7906               if (sec->size == offset + 16)
7907                 {
7908                   cnt_16b++;
7909                   break;
7910                 }
7911               goto broken_opd;
7912             }
7913           else if (rel + 1 < relend
7914                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7915                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7916             {
7917               if (rel[0].r_offset == offset + 16)
7918                 cnt_16b++;
7919               else if (rel[0].r_offset != offset + 24)
7920                 goto broken_opd;
7921             }
7922           else
7923             goto broken_opd;
7924         }
7925
7926       add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7927
7928       if (!broken && (need_edit || add_aux_fields))
7929         {
7930           Elf_Internal_Rela *write_rel;
7931           Elf_Internal_Shdr *rel_hdr;
7932           bfd_byte *rptr, *wptr;
7933           bfd_byte *new_contents;
7934           bfd_size_type amt;
7935
7936           new_contents = NULL;
7937           amt = OPD_NDX (sec->size) * sizeof (long);
7938           opd = &ppc64_elf_section_data (sec)->u.opd;
7939           opd->adjust = bfd_zalloc (sec->owner, amt);
7940           if (opd->adjust == NULL)
7941             return FALSE;
7942           ppc64_elf_section_data (sec)->sec_type = sec_opd;
7943
7944           /* This seems a waste of time as input .opd sections are all
7945              zeros as generated by gcc, but I suppose there's no reason
7946              this will always be so.  We might start putting something in
7947              the third word of .opd entries.  */
7948           if ((sec->flags & SEC_IN_MEMORY) == 0)
7949             {
7950               bfd_byte *loc;
7951               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7952                 {
7953                   if (loc != NULL)
7954                     free (loc);
7955                 error_ret:
7956                   if (local_syms != NULL
7957                       && symtab_hdr->contents != (unsigned char *) local_syms)
7958                     free (local_syms);
7959                   if (elf_section_data (sec)->relocs != relstart)
7960                     free (relstart);
7961                   return FALSE;
7962                 }
7963               sec->contents = loc;
7964               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7965             }
7966
7967           elf_section_data (sec)->relocs = relstart;
7968
7969           new_contents = sec->contents;
7970           if (add_aux_fields)
7971             {
7972               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7973               if (new_contents == NULL)
7974                 return FALSE;
7975               need_pad = NULL;
7976             }
7977           wptr = new_contents;
7978           rptr = sec->contents;
7979           write_rel = relstart;
7980           for (rel = relstart; rel < relend; )
7981             {
7982               unsigned long r_symndx;
7983               asection *sym_sec;
7984               struct elf_link_hash_entry *h;
7985               struct ppc_link_hash_entry *fdh = NULL;
7986               Elf_Internal_Sym *sym;
7987               long opd_ent_size;
7988               Elf_Internal_Rela *next_rel;
7989               bfd_boolean skip;
7990
7991               r_symndx = ELF64_R_SYM (rel->r_info);
7992               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7993                               r_symndx, ibfd))
7994                 goto error_ret;
7995
7996               next_rel = rel + 2;
7997               if (next_rel + 1 == relend
7998                   || (next_rel + 2 < relend
7999                       && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8000                 ++next_rel;
8001
8002               /* See if the .opd entry is full 24 byte or
8003                  16 byte (with fd_aux entry overlapped with next
8004                  fd_func).  */
8005               opd_ent_size = 24;
8006               if (next_rel == relend)
8007                 {
8008                   if (sec->size == rel->r_offset + 16)
8009                     opd_ent_size = 16;
8010                 }
8011               else if (next_rel->r_offset == rel->r_offset + 16)
8012                 opd_ent_size = 16;
8013
8014               if (h != NULL
8015                   && h->root.root.string[0] == '.')
8016                 {
8017                   fdh = lookup_fdh ((struct ppc_link_hash_entry *) h, htab);
8018                   if (fdh != NULL
8019                       && fdh->elf.root.type != bfd_link_hash_defined
8020                       && fdh->elf.root.type != bfd_link_hash_defweak)
8021                     fdh = NULL;
8022                 }
8023
8024               skip = (sym_sec->owner != ibfd
8025                       || sym_sec->output_section == bfd_abs_section_ptr);
8026               if (skip)
8027                 {
8028                   if (fdh != NULL && sym_sec->owner == ibfd)
8029                     {
8030                       /* Arrange for the function descriptor sym
8031                          to be dropped.  */
8032                       fdh->elf.root.u.def.value = 0;
8033                       fdh->elf.root.u.def.section = sym_sec;
8034                     }
8035                   opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8036
8037                   if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8038                     rel = next_rel;
8039                   else
8040                     while (1)
8041                       {
8042                         if (!dec_dynrel_count (rel->r_info, sec, info,
8043                                                NULL, h, sym))
8044                           goto error_ret;
8045
8046                         if (++rel == next_rel)
8047                           break;
8048
8049                         r_symndx = ELF64_R_SYM (rel->r_info);
8050                         if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8051                                         r_symndx, ibfd))
8052                           goto error_ret;
8053                       }
8054                 }
8055               else
8056                 {
8057                   /* We'll be keeping this opd entry.  */
8058                   long adjust;
8059
8060                   if (fdh != NULL)
8061                     {
8062                       /* Redefine the function descriptor symbol to
8063                          this location in the opd section.  It is
8064                          necessary to update the value here rather
8065                          than using an array of adjustments as we do
8066                          for local symbols, because various places
8067                          in the generic ELF code use the value
8068                          stored in u.def.value.  */
8069                       fdh->elf.root.u.def.value = wptr - new_contents;
8070                       fdh->adjust_done = 1;
8071                     }
8072
8073                   /* Local syms are a bit tricky.  We could
8074                      tweak them as they can be cached, but
8075                      we'd need to look through the local syms
8076                      for the function descriptor sym which we
8077                      don't have at the moment.  So keep an
8078                      array of adjustments.  */
8079                   adjust = (wptr - new_contents) - (rptr - sec->contents);
8080                   opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8081
8082                   if (wptr != rptr)
8083                     memcpy (wptr, rptr, opd_ent_size);
8084                   wptr += opd_ent_size;
8085                   if (add_aux_fields && opd_ent_size == 16)
8086                     {
8087                       memset (wptr, '\0', 8);
8088                       wptr += 8;
8089                     }
8090
8091                   /* We need to adjust any reloc offsets to point to the
8092                      new opd entries.  */
8093                   for ( ; rel != next_rel; ++rel)
8094                     {
8095                       rel->r_offset += adjust;
8096                       if (write_rel != rel)
8097                         memcpy (write_rel, rel, sizeof (*rel));
8098                       ++write_rel;
8099                     }
8100                 }
8101
8102               rptr += opd_ent_size;
8103             }
8104
8105           sec->size = wptr - new_contents;
8106           sec->reloc_count = write_rel - relstart;
8107           if (add_aux_fields)
8108             {
8109               free (sec->contents);
8110               sec->contents = new_contents;
8111             }
8112
8113           /* Fudge the header size too, as this is used later in
8114              elf_bfd_final_link if we are emitting relocs.  */
8115           rel_hdr = _bfd_elf_single_rel_hdr (sec);
8116           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8117           some_edited = TRUE;
8118         }
8119       else if (elf_section_data (sec)->relocs != relstart)
8120         free (relstart);
8121
8122       if (local_syms != NULL
8123           && symtab_hdr->contents != (unsigned char *) local_syms)
8124         {
8125           if (!info->keep_memory)
8126             free (local_syms);
8127           else
8128             symtab_hdr->contents = (unsigned char *) local_syms;
8129         }
8130     }
8131
8132   if (some_edited)
8133     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8134
8135   /* If we are doing a final link and the last .opd entry is just 16 byte
8136      long, add a 8 byte padding after it.  */
8137   if (need_pad != NULL && !bfd_link_relocatable (info))
8138     {
8139       bfd_byte *p;
8140
8141       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8142         {
8143           BFD_ASSERT (need_pad->size > 0);
8144
8145           p = bfd_malloc (need_pad->size + 8);
8146           if (p == NULL)
8147             return FALSE;
8148
8149           if (! bfd_get_section_contents (need_pad->owner, need_pad,
8150                                           p, 0, need_pad->size))
8151             return FALSE;
8152
8153           need_pad->contents = p;
8154           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8155         }
8156       else
8157         {
8158           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8159           if (p == NULL)
8160             return FALSE;
8161
8162           need_pad->contents = p;
8163         }
8164
8165       memset (need_pad->contents + need_pad->size, 0, 8);
8166       need_pad->size += 8;
8167     }
8168
8169   return TRUE;
8170 }
8171
8172 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
8173
8174 asection *
8175 ppc64_elf_tls_setup (struct bfd_link_info *info)
8176 {
8177   struct ppc_link_hash_table *htab;
8178
8179   htab = ppc_hash_table (info);
8180   if (htab == NULL)
8181     return NULL;
8182
8183   if (abiversion (info->output_bfd) == 1)
8184     htab->opd_abi = 1;
8185
8186   if (htab->params->no_multi_toc)
8187     htab->do_multi_toc = 0;
8188   else if (!htab->do_multi_toc)
8189     htab->params->no_multi_toc = 1;
8190
8191   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8192                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8193                                               FALSE, FALSE, TRUE));
8194   /* Move dynamic linking info to the function descriptor sym.  */
8195   if (htab->tls_get_addr != NULL)
8196     func_desc_adjust (&htab->tls_get_addr->elf, info);
8197   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8198                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8199                                                  FALSE, FALSE, TRUE));
8200   if (htab->params->tls_get_addr_opt)
8201     {
8202       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8203
8204       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8205                                   FALSE, FALSE, TRUE);
8206       if (opt != NULL)
8207         func_desc_adjust (opt, info);
8208       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8209                                      FALSE, FALSE, TRUE);
8210       if (opt_fd != NULL
8211           && (opt_fd->root.type == bfd_link_hash_defined
8212               || opt_fd->root.type == bfd_link_hash_defweak))
8213         {
8214           /* If glibc supports an optimized __tls_get_addr call stub,
8215              signalled by the presence of __tls_get_addr_opt, and we'll
8216              be calling __tls_get_addr via a plt call stub, then
8217              make __tls_get_addr point to __tls_get_addr_opt.  */
8218           tga_fd = &htab->tls_get_addr_fd->elf;
8219           if (htab->elf.dynamic_sections_created
8220               && tga_fd != NULL
8221               && (tga_fd->type == STT_FUNC
8222                   || tga_fd->needs_plt)
8223               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8224                    || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
8225                        && tga_fd->root.type == bfd_link_hash_undefweak)))
8226             {
8227               struct plt_entry *ent;
8228
8229               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8230                 if (ent->plt.refcount > 0)
8231                   break;
8232               if (ent != NULL)
8233                 {
8234                   tga_fd->root.type = bfd_link_hash_indirect;
8235                   tga_fd->root.u.i.link = &opt_fd->root;
8236                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8237                   opt_fd->forced_local = 0;
8238                   if (opt_fd->dynindx != -1)
8239                     {
8240                       /* Use __tls_get_addr_opt in dynamic relocations.  */
8241                       opt_fd->dynindx = -1;
8242                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8243                                               opt_fd->dynstr_index);
8244                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8245                         return NULL;
8246                     }
8247                   htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8248                   tga = &htab->tls_get_addr->elf;
8249                   if (opt != NULL && tga != NULL)
8250                     {
8251                       tga->root.type = bfd_link_hash_indirect;
8252                       tga->root.u.i.link = &opt->root;
8253                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
8254                       opt->forced_local = 0;
8255                       _bfd_elf_link_hash_hide_symbol (info, opt,
8256                                                       tga->forced_local);
8257                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8258                     }
8259                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8260                   htab->tls_get_addr_fd->is_func_descriptor = 1;
8261                   if (htab->tls_get_addr != NULL)
8262                     {
8263                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8264                       htab->tls_get_addr->is_func = 1;
8265                     }
8266                 }
8267             }
8268         }
8269       else if (htab->params->tls_get_addr_opt < 0)
8270         htab->params->tls_get_addr_opt = 0;
8271     }
8272   return _bfd_elf_tls_setup (info->output_bfd, info);
8273 }
8274
8275 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8276    HASH1 or HASH2.  */
8277
8278 static bfd_boolean
8279 branch_reloc_hash_match (const bfd *ibfd,
8280                          const Elf_Internal_Rela *rel,
8281                          const struct ppc_link_hash_entry *hash1,
8282                          const struct ppc_link_hash_entry *hash2)
8283 {
8284   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8285   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8286   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8287
8288   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8289     {
8290       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8291       struct elf_link_hash_entry *h;
8292
8293       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8294       h = elf_follow_link (h);
8295       if (h == &hash1->elf || h == &hash2->elf)
8296         return TRUE;
8297     }
8298   return FALSE;
8299 }
8300
8301 /* Run through all the TLS relocs looking for optimization
8302    opportunities.  The linker has been hacked (see ppc64elf.em) to do
8303    a preliminary section layout so that we know the TLS segment
8304    offsets.  We can't optimize earlier because some optimizations need
8305    to know the tp offset, and we need to optimize before allocating
8306    dynamic relocations.  */
8307
8308 bfd_boolean
8309 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8310 {
8311   bfd *ibfd;
8312   asection *sec;
8313   struct ppc_link_hash_table *htab;
8314   unsigned char *toc_ref;
8315   int pass;
8316
8317   if (!bfd_link_executable (info))
8318     return TRUE;
8319
8320   htab = ppc_hash_table (info);
8321   if (htab == NULL)
8322     return FALSE;
8323
8324   /* Make two passes over the relocs.  On the first pass, mark toc
8325      entries involved with tls relocs, and check that tls relocs
8326      involved in setting up a tls_get_addr call are indeed followed by
8327      such a call.  If they are not, we can't do any tls optimization.
8328      On the second pass twiddle tls_mask flags to notify
8329      relocate_section that optimization can be done, and adjust got
8330      and plt refcounts.  */
8331   toc_ref = NULL;
8332   for (pass = 0; pass < 2; ++pass)
8333     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8334       {
8335         Elf_Internal_Sym *locsyms = NULL;
8336         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8337
8338         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8339           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8340             {
8341               Elf_Internal_Rela *relstart, *rel, *relend;
8342               bfd_boolean found_tls_get_addr_arg = 0;
8343
8344               /* Read the relocations.  */
8345               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8346                                                     info->keep_memory);
8347               if (relstart == NULL)
8348                 {
8349                   free (toc_ref);
8350                   return FALSE;
8351                 }
8352
8353               relend = relstart + sec->reloc_count;
8354               for (rel = relstart; rel < relend; rel++)
8355                 {
8356                   enum elf_ppc64_reloc_type r_type;
8357                   unsigned long r_symndx;
8358                   struct elf_link_hash_entry *h;
8359                   Elf_Internal_Sym *sym;
8360                   asection *sym_sec;
8361                   unsigned char *tls_mask;
8362                   unsigned char tls_set, tls_clear, tls_type = 0;
8363                   bfd_vma value;
8364                   bfd_boolean ok_tprel, is_local;
8365                   long toc_ref_index = 0;
8366                   int expecting_tls_get_addr = 0;
8367                   bfd_boolean ret = FALSE;
8368
8369                   r_symndx = ELF64_R_SYM (rel->r_info);
8370                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8371                                   r_symndx, ibfd))
8372                     {
8373                     err_free_rel:
8374                       if (elf_section_data (sec)->relocs != relstart)
8375                         free (relstart);
8376                       if (toc_ref != NULL)
8377                         free (toc_ref);
8378                       if (locsyms != NULL
8379                           && (elf_symtab_hdr (ibfd).contents
8380                               != (unsigned char *) locsyms))
8381                         free (locsyms);
8382                       return ret;
8383                     }
8384
8385                   if (h != NULL)
8386                     {
8387                       if (h->root.type == bfd_link_hash_defined
8388                           || h->root.type == bfd_link_hash_defweak)
8389                         value = h->root.u.def.value;
8390                       else if (h->root.type == bfd_link_hash_undefweak)
8391                         value = 0;
8392                       else
8393                         {
8394                           found_tls_get_addr_arg = 0;
8395                           continue;
8396                         }
8397                     }
8398                   else
8399                     /* Symbols referenced by TLS relocs must be of type
8400                        STT_TLS.  So no need for .opd local sym adjust.  */
8401                     value = sym->st_value;
8402
8403                   ok_tprel = FALSE;
8404                   is_local = FALSE;
8405                   if (h == NULL
8406                       || !h->def_dynamic)
8407                     {
8408                       is_local = TRUE;
8409                       if (h != NULL
8410                           && h->root.type == bfd_link_hash_undefweak)
8411                         ok_tprel = TRUE;
8412                       else if (sym_sec != NULL
8413                                && sym_sec->output_section != NULL)
8414                         {
8415                           value += sym_sec->output_offset;
8416                           value += sym_sec->output_section->vma;
8417                           value -= htab->elf.tls_sec->vma;
8418                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8419                                       < (bfd_vma) 1 << 32);
8420                         }
8421                     }
8422
8423                   r_type = ELF64_R_TYPE (rel->r_info);
8424                   /* If this section has old-style __tls_get_addr calls
8425                      without marker relocs, then check that each
8426                      __tls_get_addr call reloc is preceded by a reloc
8427                      that conceivably belongs to the __tls_get_addr arg
8428                      setup insn.  If we don't find matching arg setup
8429                      relocs, don't do any tls optimization.  */
8430                   if (pass == 0
8431                       && sec->has_tls_get_addr_call
8432                       && h != NULL
8433                       && (h == &htab->tls_get_addr->elf
8434                           || h == &htab->tls_get_addr_fd->elf)
8435                       && !found_tls_get_addr_arg
8436                       && is_branch_reloc (r_type))
8437                     {
8438                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8439                                                 "TLS optimization disabled\n"),
8440                                               ibfd, sec, rel->r_offset);
8441                       ret = TRUE;
8442                       goto err_free_rel;
8443                     }
8444
8445                   found_tls_get_addr_arg = 0;
8446                   switch (r_type)
8447                     {
8448                     case R_PPC64_GOT_TLSLD16:
8449                     case R_PPC64_GOT_TLSLD16_LO:
8450                       expecting_tls_get_addr = 1;
8451                       found_tls_get_addr_arg = 1;
8452                       /* Fall thru */
8453
8454                     case R_PPC64_GOT_TLSLD16_HI:
8455                     case R_PPC64_GOT_TLSLD16_HA:
8456                       /* These relocs should never be against a symbol
8457                          defined in a shared lib.  Leave them alone if
8458                          that turns out to be the case.  */
8459                       if (!is_local)
8460                         continue;
8461
8462                       /* LD -> LE */
8463                       tls_set = 0;
8464                       tls_clear = TLS_LD;
8465                       tls_type = TLS_TLS | TLS_LD;
8466                       break;
8467
8468                     case R_PPC64_GOT_TLSGD16:
8469                     case R_PPC64_GOT_TLSGD16_LO:
8470                       expecting_tls_get_addr = 1;
8471                       found_tls_get_addr_arg = 1;
8472                       /* Fall thru */
8473
8474                     case R_PPC64_GOT_TLSGD16_HI:
8475                     case R_PPC64_GOT_TLSGD16_HA:
8476                       if (ok_tprel)
8477                         /* GD -> LE */
8478                         tls_set = 0;
8479                       else
8480                         /* GD -> IE */
8481                         tls_set = TLS_TLS | TLS_TPRELGD;
8482                       tls_clear = TLS_GD;
8483                       tls_type = TLS_TLS | TLS_GD;
8484                       break;
8485
8486                     case R_PPC64_GOT_TPREL16_DS:
8487                     case R_PPC64_GOT_TPREL16_LO_DS:
8488                     case R_PPC64_GOT_TPREL16_HI:
8489                     case R_PPC64_GOT_TPREL16_HA:
8490                       if (ok_tprel)
8491                         {
8492                           /* IE -> LE */
8493                           tls_set = 0;
8494                           tls_clear = TLS_TPREL;
8495                           tls_type = TLS_TLS | TLS_TPREL;
8496                           break;
8497                         }
8498                       continue;
8499
8500                     case R_PPC64_TLSGD:
8501                     case R_PPC64_TLSLD:
8502                       found_tls_get_addr_arg = 1;
8503                       /* Fall thru */
8504
8505                     case R_PPC64_TLS:
8506                     case R_PPC64_TOC16:
8507                     case R_PPC64_TOC16_LO:
8508                       if (sym_sec == NULL || sym_sec != toc)
8509                         continue;
8510
8511                       /* Mark this toc entry as referenced by a TLS
8512                          code sequence.  We can do that now in the
8513                          case of R_PPC64_TLS, and after checking for
8514                          tls_get_addr for the TOC16 relocs.  */
8515                       if (toc_ref == NULL)
8516                         toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8517                       if (toc_ref == NULL)
8518                         goto err_free_rel;
8519
8520                       if (h != NULL)
8521                         value = h->root.u.def.value;
8522                       else
8523                         value = sym->st_value;
8524                       value += rel->r_addend;
8525                       if (value % 8 != 0)
8526                         continue;
8527                       BFD_ASSERT (value < toc->size
8528                                   && toc->output_offset % 8 == 0);
8529                       toc_ref_index = (value + toc->output_offset) / 8;
8530                       if (r_type == R_PPC64_TLS
8531                           || r_type == R_PPC64_TLSGD
8532                           || r_type == R_PPC64_TLSLD)
8533                         {
8534                           toc_ref[toc_ref_index] = 1;
8535                           continue;
8536                         }
8537
8538                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
8539                         continue;
8540
8541                       tls_set = 0;
8542                       tls_clear = 0;
8543                       expecting_tls_get_addr = 2;
8544                       break;
8545
8546                     case R_PPC64_TPREL64:
8547                       if (pass == 0
8548                           || sec != toc
8549                           || toc_ref == NULL
8550                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8551                         continue;
8552                       if (ok_tprel)
8553                         {
8554                           /* IE -> LE */
8555                           tls_set = TLS_EXPLICIT;
8556                           tls_clear = TLS_TPREL;
8557                           break;
8558                         }
8559                       continue;
8560
8561                     case R_PPC64_DTPMOD64:
8562                       if (pass == 0
8563                           || sec != toc
8564                           || toc_ref == NULL
8565                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8566                         continue;
8567                       if (rel + 1 < relend
8568                           && (rel[1].r_info
8569                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8570                           && rel[1].r_offset == rel->r_offset + 8)
8571                         {
8572                           if (ok_tprel)
8573                             /* GD -> LE */
8574                             tls_set = TLS_EXPLICIT | TLS_GD;
8575                           else
8576                             /* GD -> IE */
8577                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8578                           tls_clear = TLS_GD;
8579                         }
8580                       else
8581                         {
8582                           if (!is_local)
8583                             continue;
8584
8585                           /* LD -> LE */
8586                           tls_set = TLS_EXPLICIT;
8587                           tls_clear = TLS_LD;
8588                         }
8589                       break;
8590
8591                     default:
8592                       continue;
8593                     }
8594
8595                   if (pass == 0)
8596                     {
8597                       if (!expecting_tls_get_addr
8598                           || !sec->has_tls_get_addr_call)
8599                         continue;
8600
8601                       if (rel + 1 < relend
8602                           && branch_reloc_hash_match (ibfd, rel + 1,
8603                                                       htab->tls_get_addr,
8604                                                       htab->tls_get_addr_fd))
8605                         {
8606                           if (expecting_tls_get_addr == 2)
8607                             {
8608                               /* Check for toc tls entries.  */
8609                               unsigned char *toc_tls;
8610                               int retval;
8611
8612                               retval = get_tls_mask (&toc_tls, NULL, NULL,
8613                                                      &locsyms,
8614                                                      rel, ibfd);
8615                               if (retval == 0)
8616                                 goto err_free_rel;
8617                               if (toc_tls != NULL)
8618                                 {
8619                                   if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8620                                     found_tls_get_addr_arg = 1;
8621                                   if (retval > 1)
8622                                     toc_ref[toc_ref_index] = 1;
8623                                 }
8624                             }
8625                           continue;
8626                         }
8627
8628                       if (expecting_tls_get_addr != 1)
8629                         continue;
8630
8631                       /* Uh oh, we didn't find the expected call.  We
8632                          could just mark this symbol to exclude it
8633                          from tls optimization but it's safer to skip
8634                          the entire optimization.  */
8635                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8636                                                 "TLS optimization disabled\n"),
8637                                               ibfd, sec, rel->r_offset);
8638                       ret = TRUE;
8639                       goto err_free_rel;
8640                     }
8641
8642                   if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8643                     {
8644                       struct plt_entry *ent;
8645                       for (ent = htab->tls_get_addr->elf.plt.plist;
8646                            ent != NULL;
8647                            ent = ent->next)
8648                         if (ent->addend == 0)
8649                           {
8650                             if (ent->plt.refcount > 0)
8651                               {
8652                                 ent->plt.refcount -= 1;
8653                                 expecting_tls_get_addr = 0;
8654                               }
8655                             break;
8656                           }
8657                     }
8658
8659                   if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8660                     {
8661                       struct plt_entry *ent;
8662                       for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8663                            ent != NULL;
8664                            ent = ent->next)
8665                         if (ent->addend == 0)
8666                           {
8667                             if (ent->plt.refcount > 0)
8668                               ent->plt.refcount -= 1;
8669                             break;
8670                           }
8671                     }
8672
8673                   if (tls_clear == 0)
8674                     continue;
8675
8676                   if ((tls_set & TLS_EXPLICIT) == 0)
8677                     {
8678                       struct got_entry *ent;
8679
8680                       /* Adjust got entry for this reloc.  */
8681                       if (h != NULL)
8682                         ent = h->got.glist;
8683                       else
8684                         ent = elf_local_got_ents (ibfd)[r_symndx];
8685
8686                       for (; ent != NULL; ent = ent->next)
8687                         if (ent->addend == rel->r_addend
8688                             && ent->owner == ibfd
8689                             && ent->tls_type == tls_type)
8690                           break;
8691                       if (ent == NULL)
8692                         abort ();
8693
8694                       if (tls_set == 0)
8695                         {
8696                           /* We managed to get rid of a got entry.  */
8697                           if (ent->got.refcount > 0)
8698                             ent->got.refcount -= 1;
8699                         }
8700                     }
8701                   else
8702                     {
8703                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
8704                          we'll lose one or two dyn relocs.  */
8705                       if (!dec_dynrel_count (rel->r_info, sec, info,
8706                                              NULL, h, sym))
8707                         return FALSE;
8708
8709                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
8710                         {
8711                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8712                                                  NULL, h, sym))
8713                             return FALSE;
8714                         }
8715                     }
8716
8717                   *tls_mask |= tls_set;
8718                   *tls_mask &= ~tls_clear;
8719                 }
8720
8721               if (elf_section_data (sec)->relocs != relstart)
8722                 free (relstart);
8723             }
8724
8725         if (locsyms != NULL
8726             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8727           {
8728             if (!info->keep_memory)
8729               free (locsyms);
8730             else
8731               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8732           }
8733       }
8734
8735   if (toc_ref != NULL)
8736     free (toc_ref);
8737   return TRUE;
8738 }
8739
8740 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8741    the values of any global symbols in a toc section that has been
8742    edited.  Globals in toc sections should be a rarity, so this function
8743    sets a flag if any are found in toc sections other than the one just
8744    edited, so that futher hash table traversals can be avoided.  */
8745
8746 struct adjust_toc_info
8747 {
8748   asection *toc;
8749   unsigned long *skip;
8750   bfd_boolean global_toc_syms;
8751 };
8752
8753 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8754
8755 static bfd_boolean
8756 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8757 {
8758   struct ppc_link_hash_entry *eh;
8759   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8760   unsigned long i;
8761
8762   if (h->root.type != bfd_link_hash_defined
8763       && h->root.type != bfd_link_hash_defweak)
8764     return TRUE;
8765
8766   eh = (struct ppc_link_hash_entry *) h;
8767   if (eh->adjust_done)
8768     return TRUE;
8769
8770   if (eh->elf.root.u.def.section == toc_inf->toc)
8771     {
8772       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8773         i = toc_inf->toc->rawsize >> 3;
8774       else
8775         i = eh->elf.root.u.def.value >> 3;
8776
8777       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8778         {
8779           (*_bfd_error_handler)
8780             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8781           do
8782             ++i;
8783           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8784           eh->elf.root.u.def.value = (bfd_vma) i << 3;
8785         }
8786
8787       eh->elf.root.u.def.value -= toc_inf->skip[i];
8788       eh->adjust_done = 1;
8789     }
8790   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8791     toc_inf->global_toc_syms = TRUE;
8792
8793   return TRUE;
8794 }
8795
8796 /* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc.  */
8797
8798 static bfd_boolean
8799 ok_lo_toc_insn (unsigned int insn)
8800 {
8801   return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8802           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8803           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8804           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8805           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8806           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8807           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8808           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8809           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8810           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8811           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8812           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8813           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8814           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8815           || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8816               && (insn & 3) != 1)
8817           || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8818               && ((insn & 3) == 0 || (insn & 3) == 3))
8819           || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8820 }
8821
8822 /* Examine all relocs referencing .toc sections in order to remove
8823    unused .toc entries.  */
8824
8825 bfd_boolean
8826 ppc64_elf_edit_toc (struct bfd_link_info *info)
8827 {
8828   bfd *ibfd;
8829   struct adjust_toc_info toc_inf;
8830   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8831
8832   htab->do_toc_opt = 1;
8833   toc_inf.global_toc_syms = TRUE;
8834   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8835     {
8836       asection *toc, *sec;
8837       Elf_Internal_Shdr *symtab_hdr;
8838       Elf_Internal_Sym *local_syms;
8839       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8840       unsigned long *skip, *drop;
8841       unsigned char *used;
8842       unsigned char *keep, last, some_unused;
8843
8844       if (!is_ppc64_elf (ibfd))
8845         continue;
8846
8847       toc = bfd_get_section_by_name (ibfd, ".toc");
8848       if (toc == NULL
8849           || toc->size == 0
8850           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8851           || discarded_section (toc))
8852         continue;
8853
8854       toc_relocs = NULL;
8855       local_syms = NULL;
8856       symtab_hdr = &elf_symtab_hdr (ibfd);
8857
8858       /* Look at sections dropped from the final link.  */
8859       skip = NULL;
8860       relstart = NULL;
8861       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8862         {
8863           if (sec->reloc_count == 0
8864               || !discarded_section (sec)
8865               || get_opd_info (sec)
8866               || (sec->flags & SEC_ALLOC) == 0
8867               || (sec->flags & SEC_DEBUGGING) != 0)
8868             continue;
8869
8870           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8871           if (relstart == NULL)
8872             goto error_ret;
8873
8874           /* Run through the relocs to see which toc entries might be
8875              unused.  */
8876           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8877             {
8878               enum elf_ppc64_reloc_type r_type;
8879               unsigned long r_symndx;
8880               asection *sym_sec;
8881               struct elf_link_hash_entry *h;
8882               Elf_Internal_Sym *sym;
8883               bfd_vma val;
8884
8885               r_type = ELF64_R_TYPE (rel->r_info);
8886               switch (r_type)
8887                 {
8888                 default:
8889                   continue;
8890
8891                 case R_PPC64_TOC16:
8892                 case R_PPC64_TOC16_LO:
8893                 case R_PPC64_TOC16_HI:
8894                 case R_PPC64_TOC16_HA:
8895                 case R_PPC64_TOC16_DS:
8896                 case R_PPC64_TOC16_LO_DS:
8897                   break;
8898                 }
8899
8900               r_symndx = ELF64_R_SYM (rel->r_info);
8901               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8902                               r_symndx, ibfd))
8903                 goto error_ret;
8904
8905               if (sym_sec != toc)
8906                 continue;
8907
8908               if (h != NULL)
8909                 val = h->root.u.def.value;
8910               else
8911                 val = sym->st_value;
8912               val += rel->r_addend;
8913
8914               if (val >= toc->size)
8915                 continue;
8916
8917               /* Anything in the toc ought to be aligned to 8 bytes.
8918                  If not, don't mark as unused.  */
8919               if (val & 7)
8920                 continue;
8921
8922               if (skip == NULL)
8923                 {
8924                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8925                   if (skip == NULL)
8926                     goto error_ret;
8927                 }
8928
8929               skip[val >> 3] = ref_from_discarded;
8930             }
8931
8932           if (elf_section_data (sec)->relocs != relstart)
8933             free (relstart);
8934         }
8935
8936       /* For largetoc loads of address constants, we can convert
8937          .  addis rx,2,addr@got@ha
8938          .  ld ry,addr@got@l(rx)
8939          to
8940          .  addis rx,2,addr@toc@ha
8941          .  addi ry,rx,addr@toc@l
8942          when addr is within 2G of the toc pointer.  This then means
8943          that the word storing "addr" in the toc is no longer needed.  */
8944
8945       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8946           && toc->output_section->rawsize < (bfd_vma) 1 << 31
8947           && toc->reloc_count != 0)
8948         {
8949           /* Read toc relocs.  */
8950           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8951                                                   info->keep_memory);
8952           if (toc_relocs == NULL)
8953             goto error_ret;
8954
8955           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8956             {
8957               enum elf_ppc64_reloc_type r_type;
8958               unsigned long r_symndx;
8959               asection *sym_sec;
8960               struct elf_link_hash_entry *h;
8961               Elf_Internal_Sym *sym;
8962               bfd_vma val, addr;
8963
8964               r_type = ELF64_R_TYPE (rel->r_info);
8965               if (r_type != R_PPC64_ADDR64)
8966                 continue;
8967
8968               r_symndx = ELF64_R_SYM (rel->r_info);
8969               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8970                               r_symndx, ibfd))
8971                 goto error_ret;
8972
8973               if (sym_sec == NULL
8974                   || sym_sec->output_section == NULL
8975                   || discarded_section (sym_sec))
8976                 continue;
8977
8978               if (!SYMBOL_REFERENCES_LOCAL (info, h))
8979                 continue;
8980
8981               if (h != NULL)
8982                 {
8983                   if (h->type == STT_GNU_IFUNC)
8984                     continue;
8985                   val = h->root.u.def.value;
8986                 }
8987               else
8988                 {
8989                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8990                     continue;
8991                   val = sym->st_value;
8992                 }
8993               val += rel->r_addend;
8994               val += sym_sec->output_section->vma + sym_sec->output_offset;
8995
8996               /* We don't yet know the exact toc pointer value, but we
8997                  know it will be somewhere in the toc section.  Don't
8998                  optimize if the difference from any possible toc
8999                  pointer is outside [ff..f80008000, 7fff7fff].  */
9000               addr = toc->output_section->vma + TOC_BASE_OFF;
9001               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9002                 continue;
9003
9004               addr = toc->output_section->vma + toc->output_section->rawsize;
9005               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9006                 continue;
9007
9008               if (skip == NULL)
9009                 {
9010                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9011                   if (skip == NULL)
9012                     goto error_ret;
9013                 }
9014
9015               skip[rel->r_offset >> 3]
9016                 |= can_optimize | ((rel - toc_relocs) << 2);
9017             }
9018         }
9019
9020       if (skip == NULL)
9021         continue;
9022
9023       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9024       if (used == NULL)
9025         {
9026         error_ret:
9027           if (local_syms != NULL
9028               && symtab_hdr->contents != (unsigned char *) local_syms)
9029             free (local_syms);
9030           if (sec != NULL
9031               && relstart != NULL
9032               && elf_section_data (sec)->relocs != relstart)
9033             free (relstart);
9034           if (toc_relocs != NULL
9035               && elf_section_data (toc)->relocs != toc_relocs)
9036             free (toc_relocs);
9037           if (skip != NULL)
9038             free (skip);
9039           return FALSE;
9040         }
9041
9042       /* Now check all kept sections that might reference the toc.
9043          Check the toc itself last.  */
9044       for (sec = (ibfd->sections == toc && toc->next ? toc->next
9045                   : ibfd->sections);
9046            sec != NULL;
9047            sec = (sec == toc ? NULL
9048                   : sec->next == NULL ? toc
9049                   : sec->next == toc && toc->next ? toc->next
9050                   : sec->next))
9051         {
9052           int repeat;
9053
9054           if (sec->reloc_count == 0
9055               || discarded_section (sec)
9056               || get_opd_info (sec)
9057               || (sec->flags & SEC_ALLOC) == 0
9058               || (sec->flags & SEC_DEBUGGING) != 0)
9059             continue;
9060
9061           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9062                                                 info->keep_memory);
9063           if (relstart == NULL)
9064             {
9065               free (used);
9066               goto error_ret;
9067             }
9068
9069           /* Mark toc entries referenced as used.  */
9070           do
9071             {
9072               repeat = 0;
9073               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9074                 {
9075                   enum elf_ppc64_reloc_type r_type;
9076                   unsigned long r_symndx;
9077                   asection *sym_sec;
9078                   struct elf_link_hash_entry *h;
9079                   Elf_Internal_Sym *sym;
9080                   bfd_vma val;
9081                   enum {no_check, check_lo, check_ha} insn_check;
9082
9083                   r_type = ELF64_R_TYPE (rel->r_info);
9084                   switch (r_type)
9085                     {
9086                     default:
9087                       insn_check = no_check;
9088                       break;
9089
9090                     case R_PPC64_GOT_TLSLD16_HA:
9091                     case R_PPC64_GOT_TLSGD16_HA:
9092                     case R_PPC64_GOT_TPREL16_HA:
9093                     case R_PPC64_GOT_DTPREL16_HA:
9094                     case R_PPC64_GOT16_HA:
9095                     case R_PPC64_TOC16_HA:
9096                       insn_check = check_ha;
9097                       break;
9098
9099                     case R_PPC64_GOT_TLSLD16_LO:
9100                     case R_PPC64_GOT_TLSGD16_LO:
9101                     case R_PPC64_GOT_TPREL16_LO_DS:
9102                     case R_PPC64_GOT_DTPREL16_LO_DS:
9103                     case R_PPC64_GOT16_LO:
9104                     case R_PPC64_GOT16_LO_DS:
9105                     case R_PPC64_TOC16_LO:
9106                     case R_PPC64_TOC16_LO_DS:
9107                       insn_check = check_lo;
9108                       break;
9109                     }
9110
9111                   if (insn_check != no_check)
9112                     {
9113                       bfd_vma off = rel->r_offset & ~3;
9114                       unsigned char buf[4];
9115                       unsigned int insn;
9116
9117                       if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9118                         {
9119                           free (used);
9120                           goto error_ret;
9121                         }
9122                       insn = bfd_get_32 (ibfd, buf);
9123                       if (insn_check == check_lo
9124                           ? !ok_lo_toc_insn (insn)
9125                           : ((insn & ((0x3f << 26) | 0x1f << 16))
9126                              != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9127                         {
9128                           char str[12];
9129
9130                           ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9131                           sprintf (str, "%#08x", insn);
9132                           info->callbacks->einfo
9133                             (_("%P: %H: toc optimization is not supported for"
9134                                " %s instruction.\n"),
9135                              ibfd, sec, rel->r_offset & ~3, str);
9136                         }
9137                     }
9138
9139                   switch (r_type)
9140                     {
9141                     case R_PPC64_TOC16:
9142                     case R_PPC64_TOC16_LO:
9143                     case R_PPC64_TOC16_HI:
9144                     case R_PPC64_TOC16_HA:
9145                     case R_PPC64_TOC16_DS:
9146                     case R_PPC64_TOC16_LO_DS:
9147                       /* In case we're taking addresses of toc entries.  */
9148                     case R_PPC64_ADDR64:
9149                       break;
9150
9151                     default:
9152                       continue;
9153                     }
9154
9155                   r_symndx = ELF64_R_SYM (rel->r_info);
9156                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9157                                   r_symndx, ibfd))
9158                     {
9159                       free (used);
9160                       goto error_ret;
9161                     }
9162
9163                   if (sym_sec != toc)
9164                     continue;
9165
9166                   if (h != NULL)
9167                     val = h->root.u.def.value;
9168                   else
9169                     val = sym->st_value;
9170                   val += rel->r_addend;
9171
9172                   if (val >= toc->size)
9173                     continue;
9174
9175                   if ((skip[val >> 3] & can_optimize) != 0)
9176                     {
9177                       bfd_vma off;
9178                       unsigned char opc;
9179
9180                       switch (r_type)
9181                         {
9182                         case R_PPC64_TOC16_HA:
9183                           break;
9184
9185                         case R_PPC64_TOC16_LO_DS:
9186                           off = rel->r_offset;
9187                           off += (bfd_big_endian (ibfd) ? -2 : 3);
9188                           if (!bfd_get_section_contents (ibfd, sec, &opc,
9189                                                          off, 1))
9190                             {
9191                               free (used);
9192                               goto error_ret;
9193                             }
9194                           if ((opc & (0x3f << 2)) == (58u << 2))
9195                             break;
9196                           /* Fall thru */
9197
9198                         default:
9199                           /* Wrong sort of reloc, or not a ld.  We may
9200                              as well clear ref_from_discarded too.  */
9201                           skip[val >> 3] = 0;
9202                         }
9203                     }
9204
9205                   if (sec != toc)
9206                     used[val >> 3] = 1;
9207                   /* For the toc section, we only mark as used if this
9208                      entry itself isn't unused.  */
9209                   else if ((used[rel->r_offset >> 3]
9210                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9211                            && !used[val >> 3])
9212                     {
9213                       /* Do all the relocs again, to catch reference
9214                          chains.  */
9215                       repeat = 1;
9216                       used[val >> 3] = 1;
9217                     }
9218                 }
9219             }
9220           while (repeat);
9221
9222           if (elf_section_data (sec)->relocs != relstart)
9223             free (relstart);
9224         }
9225
9226       /* Merge the used and skip arrays.  Assume that TOC
9227          doublewords not appearing as either used or unused belong
9228          to to an entry more than one doubleword in size.  */
9229       for (drop = skip, keep = used, last = 0, some_unused = 0;
9230            drop < skip + (toc->size + 7) / 8;
9231            ++drop, ++keep)
9232         {
9233           if (*keep)
9234             {
9235               *drop &= ~ref_from_discarded;
9236               if ((*drop & can_optimize) != 0)
9237                 some_unused = 1;
9238               last = 0;
9239             }
9240           else if ((*drop & ref_from_discarded) != 0)
9241             {
9242               some_unused = 1;
9243               last = ref_from_discarded;
9244             }
9245           else
9246             *drop = last;
9247         }
9248
9249       free (used);
9250
9251       if (some_unused)
9252         {
9253           bfd_byte *contents, *src;
9254           unsigned long off;
9255           Elf_Internal_Sym *sym;
9256           bfd_boolean local_toc_syms = FALSE;
9257
9258           /* Shuffle the toc contents, and at the same time convert the
9259              skip array from booleans into offsets.  */
9260           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9261             goto error_ret;
9262
9263           elf_section_data (toc)->this_hdr.contents = contents;
9264
9265           for (src = contents, off = 0, drop = skip;
9266                src < contents + toc->size;
9267                src += 8, ++drop)
9268             {
9269               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9270                 off += 8;
9271               else if (off != 0)
9272                 {
9273                   *drop = off;
9274                   memcpy (src - off, src, 8);
9275                 }
9276             }
9277           *drop = off;
9278           toc->rawsize = toc->size;
9279           toc->size = src - contents - off;
9280
9281           /* Adjust addends for relocs against the toc section sym,
9282              and optimize any accesses we can.  */
9283           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9284             {
9285               if (sec->reloc_count == 0
9286                   || discarded_section (sec))
9287                 continue;
9288
9289               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9290                                                     info->keep_memory);
9291               if (relstart == NULL)
9292                 goto error_ret;
9293
9294               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9295                 {
9296                   enum elf_ppc64_reloc_type r_type;
9297                   unsigned long r_symndx;
9298                   asection *sym_sec;
9299                   struct elf_link_hash_entry *h;
9300                   bfd_vma val;
9301
9302                   r_type = ELF64_R_TYPE (rel->r_info);
9303                   switch (r_type)
9304                     {
9305                     default:
9306                       continue;
9307
9308                     case R_PPC64_TOC16:
9309                     case R_PPC64_TOC16_LO:
9310                     case R_PPC64_TOC16_HI:
9311                     case R_PPC64_TOC16_HA:
9312                     case R_PPC64_TOC16_DS:
9313                     case R_PPC64_TOC16_LO_DS:
9314                     case R_PPC64_ADDR64:
9315                       break;
9316                     }
9317
9318                   r_symndx = ELF64_R_SYM (rel->r_info);
9319                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9320                                   r_symndx, ibfd))
9321                     goto error_ret;
9322
9323                   if (sym_sec != toc)
9324                     continue;
9325
9326                   if (h != NULL)
9327                     val = h->root.u.def.value;
9328                   else
9329                     {
9330                       val = sym->st_value;
9331                       if (val != 0)
9332                         local_toc_syms = TRUE;
9333                     }
9334
9335                   val += rel->r_addend;
9336
9337                   if (val > toc->rawsize)
9338                     val = toc->rawsize;
9339                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
9340                     continue;
9341                   else if ((skip[val >> 3] & can_optimize) != 0)
9342                     {
9343                       Elf_Internal_Rela *tocrel
9344                         = toc_relocs + (skip[val >> 3] >> 2);
9345                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9346
9347                       switch (r_type)
9348                         {
9349                         case R_PPC64_TOC16_HA:
9350                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9351                           break;
9352
9353                         case R_PPC64_TOC16_LO_DS:
9354                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9355                           break;
9356
9357                         default:
9358                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9359                             ppc_howto_init ();
9360                           info->callbacks->einfo
9361                             (_("%P: %H: %s references "
9362                                "optimized away TOC entry\n"),
9363                              ibfd, sec, rel->r_offset,
9364                              ppc64_elf_howto_table[r_type]->name);
9365                           bfd_set_error (bfd_error_bad_value);
9366                           goto error_ret;
9367                         }
9368                       rel->r_addend = tocrel->r_addend;
9369                       elf_section_data (sec)->relocs = relstart;
9370                       continue;
9371                     }
9372
9373                   if (h != NULL || sym->st_value != 0)
9374                     continue;
9375
9376                   rel->r_addend -= skip[val >> 3];
9377                   elf_section_data (sec)->relocs = relstart;
9378                 }
9379
9380               if (elf_section_data (sec)->relocs != relstart)
9381                 free (relstart);
9382             }
9383
9384           /* We shouldn't have local or global symbols defined in the TOC,
9385              but handle them anyway.  */
9386           if (local_syms != NULL)
9387             for (sym = local_syms;
9388                  sym < local_syms + symtab_hdr->sh_info;
9389                  ++sym)
9390               if (sym->st_value != 0
9391                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9392                 {
9393                   unsigned long i;
9394
9395                   if (sym->st_value > toc->rawsize)
9396                     i = toc->rawsize >> 3;
9397                   else
9398                     i = sym->st_value >> 3;
9399
9400                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9401                     {
9402                       if (local_toc_syms)
9403                         (*_bfd_error_handler)
9404                           (_("%s defined on removed toc entry"),
9405                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9406                       do
9407                         ++i;
9408                       while ((skip[i] & (ref_from_discarded | can_optimize)));
9409                       sym->st_value = (bfd_vma) i << 3;
9410                     }
9411
9412                   sym->st_value -= skip[i];
9413                   symtab_hdr->contents = (unsigned char *) local_syms;
9414                 }
9415
9416           /* Adjust any global syms defined in this toc input section.  */
9417           if (toc_inf.global_toc_syms)
9418             {
9419               toc_inf.toc = toc;
9420               toc_inf.skip = skip;
9421               toc_inf.global_toc_syms = FALSE;
9422               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9423                                       &toc_inf);
9424             }
9425
9426           if (toc->reloc_count != 0)
9427             {
9428               Elf_Internal_Shdr *rel_hdr;
9429               Elf_Internal_Rela *wrel;
9430               bfd_size_type sz;
9431
9432               /* Remove unused toc relocs, and adjust those we keep.  */
9433               if (toc_relocs == NULL)
9434                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9435                                                         info->keep_memory);
9436               if (toc_relocs == NULL)
9437                 goto error_ret;
9438
9439               wrel = toc_relocs;
9440               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9441                 if ((skip[rel->r_offset >> 3]
9442                      & (ref_from_discarded | can_optimize)) == 0)
9443                   {
9444                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9445                     wrel->r_info = rel->r_info;
9446                     wrel->r_addend = rel->r_addend;
9447                     ++wrel;
9448                   }
9449                 else if (!dec_dynrel_count (rel->r_info, toc, info,
9450                                             &local_syms, NULL, NULL))
9451                   goto error_ret;
9452
9453               elf_section_data (toc)->relocs = toc_relocs;
9454               toc->reloc_count = wrel - toc_relocs;
9455               rel_hdr = _bfd_elf_single_rel_hdr (toc);
9456               sz = rel_hdr->sh_entsize;
9457               rel_hdr->sh_size = toc->reloc_count * sz;
9458             }
9459         }
9460       else if (toc_relocs != NULL
9461                && elf_section_data (toc)->relocs != toc_relocs)
9462         free (toc_relocs);
9463
9464       if (local_syms != NULL
9465           && symtab_hdr->contents != (unsigned char *) local_syms)
9466         {
9467           if (!info->keep_memory)
9468             free (local_syms);
9469           else
9470             symtab_hdr->contents = (unsigned char *) local_syms;
9471         }
9472       free (skip);
9473     }
9474
9475   return TRUE;
9476 }
9477
9478 /* Return true iff input section I references the TOC using
9479    instructions limited to +/-32k offsets.  */
9480
9481 bfd_boolean
9482 ppc64_elf_has_small_toc_reloc (asection *i)
9483 {
9484   return (is_ppc64_elf (i->owner)
9485           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9486 }
9487
9488 /* Allocate space for one GOT entry.  */
9489
9490 static void
9491 allocate_got (struct elf_link_hash_entry *h,
9492               struct bfd_link_info *info,
9493               struct got_entry *gent)
9494 {
9495   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9496   bfd_boolean dyn;
9497   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9498   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9499                  ? 16 : 8);
9500   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9501                   ? 2 : 1) * sizeof (Elf64_External_Rela);
9502   asection *got = ppc64_elf_tdata (gent->owner)->got;
9503
9504   gent->got.offset = got->size;
9505   got->size += entsize;
9506
9507   dyn = htab->elf.dynamic_sections_created;
9508   if (h->type == STT_GNU_IFUNC)
9509     {
9510       htab->elf.irelplt->size += rentsize;
9511       htab->got_reli_size += rentsize;
9512     }
9513   else if ((bfd_link_pic (info)
9514             || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9515            && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9516                || h->root.type != bfd_link_hash_undefweak))
9517     {
9518       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9519       relgot->size += rentsize;
9520     }
9521 }
9522
9523 /* This function merges got entries in the same toc group.  */
9524
9525 static void
9526 merge_got_entries (struct got_entry **pent)
9527 {
9528   struct got_entry *ent, *ent2;
9529
9530   for (ent = *pent; ent != NULL; ent = ent->next)
9531     if (!ent->is_indirect)
9532       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9533         if (!ent2->is_indirect
9534             && ent2->addend == ent->addend
9535             && ent2->tls_type == ent->tls_type
9536             && elf_gp (ent2->owner) == elf_gp (ent->owner))
9537           {
9538             ent2->is_indirect = TRUE;
9539             ent2->got.ent = ent;
9540           }
9541 }
9542
9543 /* Allocate space in .plt, .got and associated reloc sections for
9544    dynamic relocs.  */
9545
9546 static bfd_boolean
9547 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9548 {
9549   struct bfd_link_info *info;
9550   struct ppc_link_hash_table *htab;
9551   asection *s;
9552   struct ppc_link_hash_entry *eh;
9553   struct got_entry **pgent, *gent;
9554
9555   if (h->root.type == bfd_link_hash_indirect)
9556     return TRUE;
9557
9558   info = (struct bfd_link_info *) inf;
9559   htab = ppc_hash_table (info);
9560   if (htab == NULL)
9561     return FALSE;
9562
9563   eh = (struct ppc_link_hash_entry *) h;
9564   /* Run through the TLS GD got entries first if we're changing them
9565      to TPREL.  */
9566   if ((eh->tls_mask & TLS_TPRELGD) != 0)
9567     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9568       if (gent->got.refcount > 0
9569           && (gent->tls_type & TLS_GD) != 0)
9570         {
9571           /* This was a GD entry that has been converted to TPREL.  If
9572              there happens to be a TPREL entry we can use that one.  */
9573           struct got_entry *ent;
9574           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9575             if (ent->got.refcount > 0
9576                 && (ent->tls_type & TLS_TPREL) != 0
9577                 && ent->addend == gent->addend
9578                 && ent->owner == gent->owner)
9579               {
9580                 gent->got.refcount = 0;
9581                 break;
9582               }
9583
9584           /* If not, then we'll be using our own TPREL entry.  */
9585           if (gent->got.refcount != 0)
9586             gent->tls_type = TLS_TLS | TLS_TPREL;
9587         }
9588
9589   /* Remove any list entry that won't generate a word in the GOT before
9590      we call merge_got_entries.  Otherwise we risk merging to empty
9591      entries.  */
9592   pgent = &h->got.glist;
9593   while ((gent = *pgent) != NULL)
9594     if (gent->got.refcount > 0)
9595       {
9596         if ((gent->tls_type & TLS_LD) != 0
9597             && !h->def_dynamic)
9598           {
9599             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9600             *pgent = gent->next;
9601           }
9602         else
9603           pgent = &gent->next;
9604       }
9605     else
9606       *pgent = gent->next;
9607
9608   if (!htab->do_multi_toc)
9609     merge_got_entries (&h->got.glist);
9610
9611   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9612     if (!gent->is_indirect)
9613       {
9614         /* Make sure this symbol is output as a dynamic symbol.
9615            Undefined weak syms won't yet be marked as dynamic,
9616            nor will all TLS symbols.  */
9617         if (h->dynindx == -1
9618             && !h->forced_local
9619             && h->type != STT_GNU_IFUNC
9620             && htab->elf.dynamic_sections_created)
9621           {
9622             if (! bfd_elf_link_record_dynamic_symbol (info, h))
9623               return FALSE;
9624           }
9625
9626         if (!is_ppc64_elf (gent->owner))
9627           abort ();
9628
9629         allocate_got (h, info, gent);
9630       }
9631
9632   if (!htab->elf.dynamic_sections_created
9633       && h->type != STT_GNU_IFUNC)
9634     eh->dyn_relocs = NULL;
9635
9636   if (eh->dyn_relocs != NULL)
9637     {
9638       struct elf_dyn_relocs *p, **pp;
9639
9640       /* In the shared -Bsymbolic case, discard space allocated for
9641          dynamic pc-relative relocs against symbols which turn out to
9642          be defined in regular objects.  For the normal shared case,
9643          discard space for relocs that have become local due to symbol
9644          visibility changes.  */
9645
9646       if (bfd_link_pic (info))
9647         {
9648           /* Relocs that use pc_count are those that appear on a call
9649              insn, or certain REL relocs (see must_be_dyn_reloc) that
9650              can be generated via assembly.  We want calls to
9651              protected symbols to resolve directly to the function
9652              rather than going via the plt.  If people want function
9653              pointer comparisons to work as expected then they should
9654              avoid writing weird assembly.  */
9655           if (SYMBOL_CALLS_LOCAL (info, h))
9656             {
9657               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9658                 {
9659                   p->count -= p->pc_count;
9660                   p->pc_count = 0;
9661                   if (p->count == 0)
9662                     *pp = p->next;
9663                   else
9664                     pp = &p->next;
9665                 }
9666             }
9667
9668           /* Also discard relocs on undefined weak syms with
9669              non-default visibility.  */
9670           if (eh->dyn_relocs != NULL
9671               && h->root.type == bfd_link_hash_undefweak)
9672             {
9673               if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9674                 eh->dyn_relocs = NULL;
9675
9676               /* Make sure this symbol is output as a dynamic symbol.
9677                  Undefined weak syms won't yet be marked as dynamic.  */
9678               else if (h->dynindx == -1
9679                        && !h->forced_local)
9680                 {
9681                   if (! bfd_elf_link_record_dynamic_symbol (info, h))
9682                     return FALSE;
9683                 }
9684             }
9685         }
9686       else if (h->type == STT_GNU_IFUNC)
9687         {
9688           /* A plt entry is always created when making direct calls to
9689              an ifunc, even when building a static executable, but
9690              that doesn't cover all cases.  We may have only an ifunc
9691              initialised function pointer for a given ifunc symbol.
9692
9693              For ELFv2, dynamic relocations are not required when
9694              generating a global entry PLT stub.  */
9695           if (abiversion (info->output_bfd) >= 2)
9696             {
9697               if (global_entry_stub (h))
9698                 eh->dyn_relocs = NULL;
9699             }
9700
9701           /* For ELFv1 we have function descriptors.  Descriptors need
9702              to be treated like PLT entries and thus have dynamic
9703              relocations.  One exception is when the function
9704              descriptor is copied into .dynbss (which should only
9705              happen with ancient versions of gcc).  */
9706           else if (h->needs_copy)
9707             eh->dyn_relocs = NULL;
9708         }
9709       else if (ELIMINATE_COPY_RELOCS)
9710         {
9711           /* For the non-pic case, discard space for relocs against
9712              symbols which turn out to need copy relocs or are not
9713              dynamic.  */
9714
9715           /* First make sure this symbol is output as a dynamic symbol.
9716              Undefined weak syms won't yet be marked as dynamic.  */
9717           if (h->root.type == bfd_link_hash_undefweak
9718               && !h->non_got_ref
9719               && !h->def_regular
9720               && h->dynindx == -1
9721               && !h->forced_local
9722               && !bfd_elf_link_record_dynamic_symbol (info, h))
9723             return FALSE;
9724
9725           if (h->non_got_ref
9726               || h->def_regular
9727               || h->dynindx == -1)
9728             eh->dyn_relocs = NULL;
9729         }
9730
9731       /* Finally, allocate space.  */
9732       for (p = eh->dyn_relocs; p != NULL; p = p->next)
9733         {
9734           asection *sreloc = elf_section_data (p->sec)->sreloc;
9735           if (eh->elf.type == STT_GNU_IFUNC)
9736             sreloc = htab->elf.irelplt;
9737           sreloc->size += p->count * sizeof (Elf64_External_Rela);
9738         }
9739     }
9740
9741   if ((htab->elf.dynamic_sections_created
9742        && h->dynindx != -1
9743        && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
9744       || h->type == STT_GNU_IFUNC)
9745     {
9746       struct plt_entry *pent;
9747       bfd_boolean doneone = FALSE;
9748       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9749         if (pent->plt.refcount > 0)
9750           {
9751             if (!htab->elf.dynamic_sections_created
9752                 || h->dynindx == -1)
9753               {
9754                 s = htab->elf.iplt;
9755                 pent->plt.offset = s->size;
9756                 s->size += PLT_ENTRY_SIZE (htab);
9757                 s = htab->elf.irelplt;
9758               }
9759             else
9760               {
9761                 /* If this is the first .plt entry, make room for the special
9762                    first entry.  */
9763                 s = htab->elf.splt;
9764                 if (s->size == 0)
9765                   s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9766
9767                 pent->plt.offset = s->size;
9768
9769                 /* Make room for this entry.  */
9770                 s->size += PLT_ENTRY_SIZE (htab);
9771
9772                 /* Make room for the .glink code.  */
9773                 s = htab->glink;
9774                 if (s->size == 0)
9775                   s->size += GLINK_CALL_STUB_SIZE;
9776                 if (htab->opd_abi)
9777                   {
9778                     /* We need bigger stubs past index 32767.  */
9779                     if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9780                       s->size += 4;
9781                     s->size += 2*4;
9782                   }
9783                 else
9784                   s->size += 4;
9785
9786                 /* We also need to make an entry in the .rela.plt section.  */
9787                 s = htab->elf.srelplt;
9788               }
9789             s->size += sizeof (Elf64_External_Rela);
9790             doneone = TRUE;
9791           }
9792         else
9793           pent->plt.offset = (bfd_vma) -1;
9794       if (!doneone)
9795         {
9796           h->plt.plist = NULL;
9797           h->needs_plt = 0;
9798         }
9799     }
9800   else
9801     {
9802       h->plt.plist = NULL;
9803       h->needs_plt = 0;
9804     }
9805
9806   return TRUE;
9807 }
9808
9809 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9810    to set up space for global entry stubs.  These are put in glink,
9811    after the branch table.  */
9812
9813 static bfd_boolean
9814 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9815 {
9816   struct bfd_link_info *info;
9817   struct ppc_link_hash_table *htab;
9818   struct plt_entry *pent;
9819   asection *s;
9820
9821   if (h->root.type == bfd_link_hash_indirect)
9822     return TRUE;
9823
9824   if (!h->pointer_equality_needed)
9825     return TRUE;
9826
9827   if (h->def_regular)
9828     return TRUE;
9829
9830   info = inf;
9831   htab = ppc_hash_table (info);
9832   if (htab == NULL)
9833     return FALSE;
9834
9835   s = htab->glink;
9836   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9837     if (pent->plt.offset != (bfd_vma) -1
9838         && pent->addend == 0)
9839       {
9840         /* For ELFv2, if this symbol is not defined in a regular file
9841            and we are not generating a shared library or pie, then we
9842            need to define the symbol in the executable on a call stub.
9843            This is to avoid text relocations.  */
9844         s->size = (s->size + 15) & -16;
9845         h->root.type = bfd_link_hash_defined;
9846         h->root.u.def.section = s;
9847         h->root.u.def.value = s->size;
9848         s->size += 16;
9849         break;
9850       }
9851   return TRUE;
9852 }
9853
9854 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9855    read-only sections.  */
9856
9857 static bfd_boolean
9858 maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
9859 {
9860   if (h->root.type == bfd_link_hash_indirect)
9861     return TRUE;
9862
9863   if (readonly_dynrelocs (h))
9864     {
9865       ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
9866
9867       /* Not an error, just cut short the traversal.  */
9868       return FALSE;
9869     }
9870   return TRUE;
9871 }
9872
9873 /* Set the sizes of the dynamic sections.  */
9874
9875 static bfd_boolean
9876 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9877                                  struct bfd_link_info *info)
9878 {
9879   struct ppc_link_hash_table *htab;
9880   bfd *dynobj;
9881   asection *s;
9882   bfd_boolean relocs;
9883   bfd *ibfd;
9884   struct got_entry *first_tlsld;
9885
9886   htab = ppc_hash_table (info);
9887   if (htab == NULL)
9888     return FALSE;
9889
9890   dynobj = htab->elf.dynobj;
9891   if (dynobj == NULL)
9892     abort ();
9893
9894   if (htab->elf.dynamic_sections_created)
9895     {
9896       /* Set the contents of the .interp section to the interpreter.  */
9897       if (bfd_link_executable (info) && !info->nointerp)
9898         {
9899           s = bfd_get_linker_section (dynobj, ".interp");
9900           if (s == NULL)
9901             abort ();
9902           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9903           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9904         }
9905     }
9906
9907   /* Set up .got offsets for local syms, and space for local dynamic
9908      relocs.  */
9909   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9910     {
9911       struct got_entry **lgot_ents;
9912       struct got_entry **end_lgot_ents;
9913       struct plt_entry **local_plt;
9914       struct plt_entry **end_local_plt;
9915       unsigned char *lgot_masks;
9916       bfd_size_type locsymcount;
9917       Elf_Internal_Shdr *symtab_hdr;
9918
9919       if (!is_ppc64_elf (ibfd))
9920         continue;
9921
9922       for (s = ibfd->sections; s != NULL; s = s->next)
9923         {
9924           struct ppc_dyn_relocs *p;
9925
9926           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9927             {
9928               if (!bfd_is_abs_section (p->sec)
9929                   && bfd_is_abs_section (p->sec->output_section))
9930                 {
9931                   /* Input section has been discarded, either because
9932                      it is a copy of a linkonce section or due to
9933                      linker script /DISCARD/, so we'll be discarding
9934                      the relocs too.  */
9935                 }
9936               else if (p->count != 0)
9937                 {
9938                   asection *srel = elf_section_data (p->sec)->sreloc;
9939                   if (p->ifunc)
9940                     srel = htab->elf.irelplt;
9941                   srel->size += p->count * sizeof (Elf64_External_Rela);
9942                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9943                     info->flags |= DF_TEXTREL;
9944                 }
9945             }
9946         }
9947
9948       lgot_ents = elf_local_got_ents (ibfd);
9949       if (!lgot_ents)
9950         continue;
9951
9952       symtab_hdr = &elf_symtab_hdr (ibfd);
9953       locsymcount = symtab_hdr->sh_info;
9954       end_lgot_ents = lgot_ents + locsymcount;
9955       local_plt = (struct plt_entry **) end_lgot_ents;
9956       end_local_plt = local_plt + locsymcount;
9957       lgot_masks = (unsigned char *) end_local_plt;
9958       s = ppc64_elf_tdata (ibfd)->got;
9959       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9960         {
9961           struct got_entry **pent, *ent;
9962
9963           pent = lgot_ents;
9964           while ((ent = *pent) != NULL)
9965             if (ent->got.refcount > 0)
9966               {
9967                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9968                   {
9969                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
9970                     *pent = ent->next;
9971                   }
9972                 else
9973                   {
9974                     unsigned int ent_size = 8;
9975                     unsigned int rel_size = sizeof (Elf64_External_Rela);
9976
9977                     ent->got.offset = s->size;
9978                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9979                       {
9980                         ent_size *= 2;
9981                         rel_size *= 2;
9982                       }
9983                     s->size += ent_size;
9984                     if ((*lgot_masks & PLT_IFUNC) != 0)
9985                       {
9986                         htab->elf.irelplt->size += rel_size;
9987                         htab->got_reli_size += rel_size;
9988                       }
9989                     else if (bfd_link_pic (info))
9990                       {
9991                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9992                         srel->size += rel_size;
9993                       }
9994                     pent = &ent->next;
9995                   }
9996               }
9997             else
9998               *pent = ent->next;
9999         }
10000
10001       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
10002       for (; local_plt < end_local_plt; ++local_plt)
10003         {
10004           struct plt_entry *ent;
10005
10006           for (ent = *local_plt; ent != NULL; ent = ent->next)
10007             if (ent->plt.refcount > 0)
10008               {
10009                 s = htab->elf.iplt;
10010                 ent->plt.offset = s->size;
10011                 s->size += PLT_ENTRY_SIZE (htab);
10012
10013                 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10014               }
10015             else
10016               ent->plt.offset = (bfd_vma) -1;
10017         }
10018     }
10019
10020   /* Allocate global sym .plt and .got entries, and space for global
10021      sym dynamic relocs.  */
10022   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10023   /* Stash the end of glink branch table.  */
10024   if (htab->glink != NULL)
10025     htab->glink->rawsize = htab->glink->size;
10026
10027   if (!htab->opd_abi && !bfd_link_pic (info))
10028     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10029
10030   first_tlsld = NULL;
10031   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10032     {
10033       struct got_entry *ent;
10034
10035       if (!is_ppc64_elf (ibfd))
10036         continue;
10037
10038       ent = ppc64_tlsld_got (ibfd);
10039       if (ent->got.refcount > 0)
10040         {
10041           if (!htab->do_multi_toc && first_tlsld != NULL)
10042             {
10043               ent->is_indirect = TRUE;
10044               ent->got.ent = first_tlsld;
10045             }
10046           else
10047             {
10048               if (first_tlsld == NULL)
10049                 first_tlsld = ent;
10050               s = ppc64_elf_tdata (ibfd)->got;
10051               ent->got.offset = s->size;
10052               ent->owner = ibfd;
10053               s->size += 16;
10054               if (bfd_link_pic (info))
10055                 {
10056                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10057                   srel->size += sizeof (Elf64_External_Rela);
10058                 }
10059             }
10060         }
10061       else
10062         ent->got.offset = (bfd_vma) -1;
10063     }
10064
10065   /* We now have determined the sizes of the various dynamic sections.
10066      Allocate memory for them.  */
10067   relocs = FALSE;
10068   for (s = dynobj->sections; s != NULL; s = s->next)
10069     {
10070       if ((s->flags & SEC_LINKER_CREATED) == 0)
10071         continue;
10072
10073       if (s == htab->brlt || s == htab->relbrlt)
10074         /* These haven't been allocated yet;  don't strip.  */
10075         continue;
10076       else if (s == htab->elf.sgot
10077                || s == htab->elf.splt
10078                || s == htab->elf.iplt
10079                || s == htab->glink
10080                || s == htab->dynbss)
10081         {
10082           /* Strip this section if we don't need it; see the
10083              comment below.  */
10084         }
10085       else if (s == htab->glink_eh_frame)
10086         {
10087           if (!bfd_is_abs_section (s->output_section))
10088             /* Not sized yet.  */
10089             continue;
10090         }
10091       else if (CONST_STRNEQ (s->name, ".rela"))
10092         {
10093           if (s->size != 0)
10094             {
10095               if (s != htab->elf.srelplt)
10096                 relocs = TRUE;
10097
10098               /* We use the reloc_count field as a counter if we need
10099                  to copy relocs into the output file.  */
10100               s->reloc_count = 0;
10101             }
10102         }
10103       else
10104         {
10105           /* It's not one of our sections, so don't allocate space.  */
10106           continue;
10107         }
10108
10109       if (s->size == 0)
10110         {
10111           /* If we don't need this section, strip it from the
10112              output file.  This is mostly to handle .rela.bss and
10113              .rela.plt.  We must create both sections in
10114              create_dynamic_sections, because they must be created
10115              before the linker maps input sections to output
10116              sections.  The linker does that before
10117              adjust_dynamic_symbol is called, and it is that
10118              function which decides whether anything needs to go
10119              into these sections.  */
10120           s->flags |= SEC_EXCLUDE;
10121           continue;
10122         }
10123
10124       if ((s->flags & SEC_HAS_CONTENTS) == 0)
10125         continue;
10126
10127       /* Allocate memory for the section contents.  We use bfd_zalloc
10128          here in case unused entries are not reclaimed before the
10129          section's contents are written out.  This should not happen,
10130          but this way if it does we get a R_PPC64_NONE reloc in .rela
10131          sections instead of garbage.
10132          We also rely on the section contents being zero when writing
10133          the GOT.  */
10134       s->contents = bfd_zalloc (dynobj, s->size);
10135       if (s->contents == NULL)
10136         return FALSE;
10137     }
10138
10139   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10140     {
10141       if (!is_ppc64_elf (ibfd))
10142         continue;
10143
10144       s = ppc64_elf_tdata (ibfd)->got;
10145       if (s != NULL && s != htab->elf.sgot)
10146         {
10147           if (s->size == 0)
10148             s->flags |= SEC_EXCLUDE;
10149           else
10150             {
10151               s->contents = bfd_zalloc (ibfd, s->size);
10152               if (s->contents == NULL)
10153                 return FALSE;
10154             }
10155         }
10156       s = ppc64_elf_tdata (ibfd)->relgot;
10157       if (s != NULL)
10158         {
10159           if (s->size == 0)
10160             s->flags |= SEC_EXCLUDE;
10161           else
10162             {
10163               s->contents = bfd_zalloc (ibfd, s->size);
10164               if (s->contents == NULL)
10165                 return FALSE;
10166               relocs = TRUE;
10167               s->reloc_count = 0;
10168             }
10169         }
10170     }
10171
10172   if (htab->elf.dynamic_sections_created)
10173     {
10174       bfd_boolean tls_opt;
10175
10176       /* Add some entries to the .dynamic section.  We fill in the
10177          values later, in ppc64_elf_finish_dynamic_sections, but we
10178          must add the entries now so that we get the correct size for
10179          the .dynamic section.  The DT_DEBUG entry is filled in by the
10180          dynamic linker and used by the debugger.  */
10181 #define add_dynamic_entry(TAG, VAL) \
10182   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10183
10184       if (bfd_link_executable (info))
10185         {
10186           if (!add_dynamic_entry (DT_DEBUG, 0))
10187             return FALSE;
10188         }
10189
10190       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10191         {
10192           if (!add_dynamic_entry (DT_PLTGOT, 0)
10193               || !add_dynamic_entry (DT_PLTRELSZ, 0)
10194               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10195               || !add_dynamic_entry (DT_JMPREL, 0)
10196               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10197             return FALSE;
10198         }
10199
10200       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10201         {
10202           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10203               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10204             return FALSE;
10205         }
10206
10207       tls_opt = (htab->params->tls_get_addr_opt
10208                  && htab->tls_get_addr_fd != NULL
10209                  && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10210       if (tls_opt || !htab->opd_abi)
10211         {
10212           if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10213             return FALSE;
10214         }
10215
10216       if (relocs)
10217         {
10218           if (!add_dynamic_entry (DT_RELA, 0)
10219               || !add_dynamic_entry (DT_RELASZ, 0)
10220               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10221             return FALSE;
10222
10223           /* If any dynamic relocs apply to a read-only section,
10224              then we need a DT_TEXTREL entry.  */
10225           if ((info->flags & DF_TEXTREL) == 0)
10226             elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10227
10228           if ((info->flags & DF_TEXTREL) != 0)
10229             {
10230               if (!add_dynamic_entry (DT_TEXTREL, 0))
10231                 return FALSE;
10232             }
10233         }
10234     }
10235 #undef add_dynamic_entry
10236
10237   return TRUE;
10238 }
10239
10240 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
10241
10242 static bfd_boolean
10243 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10244 {
10245   if (h->plt.plist != NULL
10246       && !h->def_regular
10247       && !h->pointer_equality_needed)
10248     return FALSE;
10249
10250   return _bfd_elf_hash_symbol (h);
10251 }
10252
10253 /* Determine the type of stub needed, if any, for a call.  */
10254
10255 static inline enum ppc_stub_type
10256 ppc_type_of_stub (asection *input_sec,
10257                   const Elf_Internal_Rela *rel,
10258                   struct ppc_link_hash_entry **hash,
10259                   struct plt_entry **plt_ent,
10260                   bfd_vma destination,
10261                   unsigned long local_off)
10262 {
10263   struct ppc_link_hash_entry *h = *hash;
10264   bfd_vma location;
10265   bfd_vma branch_offset;
10266   bfd_vma max_branch_offset;
10267   enum elf_ppc64_reloc_type r_type;
10268
10269   if (h != NULL)
10270     {
10271       struct plt_entry *ent;
10272       struct ppc_link_hash_entry *fdh = h;
10273       if (h->oh != NULL
10274           && h->oh->is_func_descriptor)
10275         {
10276           fdh = ppc_follow_link (h->oh);
10277           *hash = fdh;
10278         }
10279
10280       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10281         if (ent->addend == rel->r_addend
10282             && ent->plt.offset != (bfd_vma) -1)
10283           {
10284             *plt_ent = ent;
10285             return ppc_stub_plt_call;
10286           }
10287
10288       /* Here, we know we don't have a plt entry.  If we don't have a
10289          either a defined function descriptor or a defined entry symbol
10290          in a regular object file, then it is pointless trying to make
10291          any other type of stub.  */
10292       if (!is_static_defined (&fdh->elf)
10293           && !is_static_defined (&h->elf))
10294         return ppc_stub_none;
10295     }
10296   else if (elf_local_got_ents (input_sec->owner) != NULL)
10297     {
10298       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10299       struct plt_entry **local_plt = (struct plt_entry **)
10300         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10301       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10302
10303       if (local_plt[r_symndx] != NULL)
10304         {
10305           struct plt_entry *ent;
10306
10307           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10308             if (ent->addend == rel->r_addend
10309                 && ent->plt.offset != (bfd_vma) -1)
10310               {
10311                 *plt_ent = ent;
10312                 return ppc_stub_plt_call;
10313               }
10314         }
10315     }
10316
10317   /* Determine where the call point is.  */
10318   location = (input_sec->output_offset
10319               + input_sec->output_section->vma
10320               + rel->r_offset);
10321
10322   branch_offset = destination - location;
10323   r_type = ELF64_R_TYPE (rel->r_info);
10324
10325   /* Determine if a long branch stub is needed.  */
10326   max_branch_offset = 1 << 25;
10327   if (r_type != R_PPC64_REL24)
10328     max_branch_offset = 1 << 15;
10329
10330   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10331     /* We need a stub.  Figure out whether a long_branch or plt_branch
10332        is needed later.  */
10333     return ppc_stub_long_branch;
10334
10335   return ppc_stub_none;
10336 }
10337
10338 /* With power7 weakly ordered memory model, it is possible for ld.so
10339    to update a plt entry in one thread and have another thread see a
10340    stale zero toc entry.  To avoid this we need some sort of acquire
10341    barrier in the call stub.  One solution is to make the load of the
10342    toc word seem to appear to depend on the load of the function entry
10343    word.  Another solution is to test for r2 being zero, and branch to
10344    the appropriate glink entry if so.
10345
10346    .    fake dep barrier        compare
10347    .    ld 12,xxx(2)            ld 12,xxx(2)
10348    .    mtctr 12                mtctr 12
10349    .    xor 11,12,12            ld 2,xxx+8(2)
10350    .    add 2,2,11              cmpldi 2,0
10351    .    ld 2,xxx+8(2)           bnectr+
10352    .    bctr                    b <glink_entry>
10353
10354    The solution involving the compare turns out to be faster, so
10355    that's what we use unless the branch won't reach.  */
10356
10357 #define ALWAYS_USE_FAKE_DEP 0
10358 #define ALWAYS_EMIT_R2SAVE 0
10359
10360 #define PPC_LO(v) ((v) & 0xffff)
10361 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10362 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10363
10364 static inline unsigned int
10365 plt_stub_size (struct ppc_link_hash_table *htab,
10366                struct ppc_stub_hash_entry *stub_entry,
10367                bfd_vma off)
10368 {
10369   unsigned size = 12;
10370
10371   if (ALWAYS_EMIT_R2SAVE
10372       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10373     size += 4;
10374   if (PPC_HA (off) != 0)
10375     size += 4;
10376   if (htab->opd_abi)
10377     {
10378       size += 4;
10379       if (htab->params->plt_static_chain)
10380         size += 4;
10381       if (htab->params->plt_thread_safe
10382           && htab->elf.dynamic_sections_created
10383           && stub_entry->h != NULL
10384           && stub_entry->h->elf.dynindx != -1)
10385         size += 8;
10386       if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10387         size += 4;
10388     }
10389   if (stub_entry->h != NULL
10390       && (stub_entry->h == htab->tls_get_addr_fd
10391           || stub_entry->h == htab->tls_get_addr)
10392       && htab->params->tls_get_addr_opt)
10393     size += 13 * 4;
10394   return size;
10395 }
10396
10397 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10398    then return the padding needed to do so.  */
10399 static inline unsigned int
10400 plt_stub_pad (struct ppc_link_hash_table *htab,
10401               struct ppc_stub_hash_entry *stub_entry,
10402               bfd_vma plt_off)
10403 {
10404   int stub_align = 1 << htab->params->plt_stub_align;
10405   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10406   bfd_vma stub_off = stub_entry->group->stub_sec->size;
10407
10408   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10409       > ((stub_size - 1) & -stub_align))
10410     return stub_align - (stub_off & (stub_align - 1));
10411   return 0;
10412 }
10413
10414 /* Build a .plt call stub.  */
10415
10416 static inline bfd_byte *
10417 build_plt_stub (struct ppc_link_hash_table *htab,
10418                 struct ppc_stub_hash_entry *stub_entry,
10419                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10420 {
10421   bfd *obfd = htab->params->stub_bfd;
10422   bfd_boolean plt_load_toc = htab->opd_abi;
10423   bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10424   bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10425                                  && htab->elf.dynamic_sections_created
10426                                  && stub_entry->h != NULL
10427                                  && stub_entry->h->elf.dynindx != -1);
10428   bfd_boolean use_fake_dep = plt_thread_safe;
10429   bfd_vma cmp_branch_off = 0;
10430
10431   if (!ALWAYS_USE_FAKE_DEP
10432       && plt_load_toc
10433       && plt_thread_safe
10434       && !((stub_entry->h == htab->tls_get_addr_fd
10435             || stub_entry->h == htab->tls_get_addr)
10436            && htab->params->tls_get_addr_opt))
10437     {
10438       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10439       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10440                           / PLT_ENTRY_SIZE (htab));
10441       bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10442       bfd_vma to, from;
10443
10444       if (pltindex > 32768)
10445         glinkoff += (pltindex - 32768) * 4;
10446       to = (glinkoff
10447             + htab->glink->output_offset
10448             + htab->glink->output_section->vma);
10449       from = (p - stub_entry->group->stub_sec->contents
10450               + 4 * (ALWAYS_EMIT_R2SAVE
10451                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10452               + 4 * (PPC_HA (offset) != 0)
10453               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10454                      != PPC_HA (offset))
10455               + 4 * (plt_static_chain != 0)
10456               + 20
10457               + stub_entry->group->stub_sec->output_offset
10458               + stub_entry->group->stub_sec->output_section->vma);
10459       cmp_branch_off = to - from;
10460       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10461     }
10462
10463   if (PPC_HA (offset) != 0)
10464     {
10465       if (r != NULL)
10466         {
10467           if (ALWAYS_EMIT_R2SAVE
10468               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10469             r[0].r_offset += 4;
10470           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10471           r[1].r_offset = r[0].r_offset + 4;
10472           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10473           r[1].r_addend = r[0].r_addend;
10474           if (plt_load_toc)
10475             {
10476               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10477                 {
10478                   r[2].r_offset = r[1].r_offset + 4;
10479                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10480                   r[2].r_addend = r[0].r_addend;
10481                 }
10482               else
10483                 {
10484                   r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10485                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10486                   r[2].r_addend = r[0].r_addend + 8;
10487                   if (plt_static_chain)
10488                     {
10489                       r[3].r_offset = r[2].r_offset + 4;
10490                       r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10491                       r[3].r_addend = r[0].r_addend + 16;
10492                     }
10493                 }
10494             }
10495         }
10496       if (ALWAYS_EMIT_R2SAVE
10497           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10498         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10499       if (plt_load_toc)
10500         {
10501           bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10502           bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),  p += 4;
10503         }
10504       else
10505         {
10506           bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10507           bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),  p += 4;
10508         }
10509       if (plt_load_toc
10510           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10511         {
10512           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10513           offset = 0;
10514         }
10515       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10516       if (plt_load_toc)
10517         {
10518           if (use_fake_dep)
10519             {
10520               bfd_put_32 (obfd, XOR_R2_R12_R12, p),             p += 4;
10521               bfd_put_32 (obfd, ADD_R11_R11_R2, p),             p += 4;
10522             }
10523           bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10524           if (plt_static_chain)
10525             bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10526         }
10527     }
10528   else
10529     {
10530       if (r != NULL)
10531         {
10532           if (ALWAYS_EMIT_R2SAVE
10533               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10534             r[0].r_offset += 4;
10535           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10536           if (plt_load_toc)
10537             {
10538               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10539                 {
10540                   r[1].r_offset = r[0].r_offset + 4;
10541                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10542                   r[1].r_addend = r[0].r_addend;
10543                 }
10544               else
10545                 {
10546                   r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10547                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10548                   r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10549                   if (plt_static_chain)
10550                     {
10551                       r[2].r_offset = r[1].r_offset + 4;
10552                       r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10553                       r[2].r_addend = r[0].r_addend + 8;
10554                     }
10555                 }
10556             }
10557         }
10558       if (ALWAYS_EMIT_R2SAVE
10559           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10560         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10561       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
10562       if (plt_load_toc
10563           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10564         {
10565           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
10566           offset = 0;
10567         }
10568       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10569       if (plt_load_toc)
10570         {
10571           if (use_fake_dep)
10572             {
10573               bfd_put_32 (obfd, XOR_R11_R12_R12, p),            p += 4;
10574               bfd_put_32 (obfd, ADD_R2_R2_R11, p),              p += 4;
10575             }
10576           if (plt_static_chain)
10577             bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10578           bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10579         }
10580     }
10581   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10582     {
10583       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
10584       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
10585       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10586     }
10587   else
10588     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
10589   return p;
10590 }
10591
10592 /* Build a special .plt call stub for __tls_get_addr.  */
10593
10594 #define LD_R11_0R3      0xe9630000
10595 #define LD_R12_0R3      0xe9830000
10596 #define MR_R0_R3        0x7c601b78
10597 #define CMPDI_R11_0     0x2c2b0000
10598 #define ADD_R3_R12_R13  0x7c6c6a14
10599 #define BEQLR           0x4d820020
10600 #define MR_R3_R0        0x7c030378
10601 #define STD_R11_0R1     0xf9610000
10602 #define BCTRL           0x4e800421
10603 #define LD_R11_0R1      0xe9610000
10604 #define MTLR_R11        0x7d6803a6
10605
10606 static inline bfd_byte *
10607 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10608                          struct ppc_stub_hash_entry *stub_entry,
10609                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10610 {
10611   bfd *obfd = htab->params->stub_bfd;
10612
10613   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
10614   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
10615   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
10616   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
10617   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
10618   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
10619   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
10620   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
10621   bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10622
10623   if (r != NULL)
10624     r[0].r_offset += 9 * 4;
10625   p = build_plt_stub (htab, stub_entry, p, offset, r);
10626   bfd_put_32 (obfd, BCTRL, p - 4);
10627
10628   bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p),     p += 4;
10629   bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10630   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
10631   bfd_put_32 (obfd, BLR, p),                    p += 4;
10632
10633   return p;
10634 }
10635
10636 static Elf_Internal_Rela *
10637 get_relocs (asection *sec, int count)
10638 {
10639   Elf_Internal_Rela *relocs;
10640   struct bfd_elf_section_data *elfsec_data;
10641
10642   elfsec_data = elf_section_data (sec);
10643   relocs = elfsec_data->relocs;
10644   if (relocs == NULL)
10645     {
10646       bfd_size_type relsize;
10647       relsize = sec->reloc_count * sizeof (*relocs);
10648       relocs = bfd_alloc (sec->owner, relsize);
10649       if (relocs == NULL)
10650         return NULL;
10651       elfsec_data->relocs = relocs;
10652       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10653                                           sizeof (Elf_Internal_Shdr));
10654       if (elfsec_data->rela.hdr == NULL)
10655         return NULL;
10656       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10657                                         * sizeof (Elf64_External_Rela));
10658       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10659       sec->reloc_count = 0;
10660     }
10661   relocs += sec->reloc_count;
10662   sec->reloc_count += count;
10663   return relocs;
10664 }
10665
10666 static bfd_vma
10667 get_r2off (struct bfd_link_info *info,
10668            struct ppc_stub_hash_entry *stub_entry)
10669 {
10670   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10671   bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10672
10673   if (r2off == 0)
10674     {
10675       /* Support linking -R objects.  Get the toc pointer from the
10676          opd entry.  */
10677       char buf[8];
10678       if (!htab->opd_abi)
10679         return r2off;
10680       asection *opd = stub_entry->h->elf.root.u.def.section;
10681       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10682
10683       if (strcmp (opd->name, ".opd") != 0
10684           || opd->reloc_count != 0)
10685         {
10686           info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10687                                   stub_entry->h->elf.root.root.string);
10688           bfd_set_error (bfd_error_bad_value);
10689           return (bfd_vma) -1;
10690         }
10691       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10692         return (bfd_vma) -1;
10693       r2off = bfd_get_64 (opd->owner, buf);
10694       r2off -= elf_gp (info->output_bfd);
10695     }
10696   r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10697   return r2off;
10698 }
10699
10700 static bfd_boolean
10701 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10702 {
10703   struct ppc_stub_hash_entry *stub_entry;
10704   struct ppc_branch_hash_entry *br_entry;
10705   struct bfd_link_info *info;
10706   struct ppc_link_hash_table *htab;
10707   bfd_byte *loc;
10708   bfd_byte *p;
10709   bfd_vma dest, off;
10710   int size;
10711   Elf_Internal_Rela *r;
10712   asection *plt;
10713
10714   /* Massage our args to the form they really have.  */
10715   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10716   info = in_arg;
10717
10718   htab = ppc_hash_table (info);
10719   if (htab == NULL)
10720     return FALSE;
10721
10722   /* Make a note of the offset within the stubs for this entry.  */
10723   stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10724   loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10725
10726   htab->stub_count[stub_entry->stub_type - 1] += 1;
10727   switch (stub_entry->stub_type)
10728     {
10729     case ppc_stub_long_branch:
10730     case ppc_stub_long_branch_r2off:
10731       /* Branches are relative.  This is where we are going to.  */
10732       dest = (stub_entry->target_value
10733               + stub_entry->target_section->output_offset
10734               + stub_entry->target_section->output_section->vma);
10735       dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10736       off = dest;
10737
10738       /* And this is where we are coming from.  */
10739       off -= (stub_entry->stub_offset
10740               + stub_entry->group->stub_sec->output_offset
10741               + stub_entry->group->stub_sec->output_section->vma);
10742
10743       size = 4;
10744       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10745         {
10746           bfd_vma r2off = get_r2off (info, stub_entry);
10747
10748           if (r2off == (bfd_vma) -1)
10749             {
10750               htab->stub_error = TRUE;
10751               return FALSE;
10752             }
10753           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10754           loc += 4;
10755           size = 8;
10756           if (PPC_HA (r2off) != 0)
10757             {
10758               bfd_put_32 (htab->params->stub_bfd,
10759                           ADDIS_R2_R2 | PPC_HA (r2off), loc);
10760               loc += 4;
10761               size += 4;
10762             }
10763           if (PPC_LO (r2off) != 0)
10764             {
10765               bfd_put_32 (htab->params->stub_bfd,
10766                           ADDI_R2_R2 | PPC_LO (r2off), loc);
10767               loc += 4;
10768               size += 4;
10769             }
10770           off -= size - 4;
10771         }
10772       bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
10773
10774       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10775         {
10776           info->callbacks->einfo
10777             (_("%P: long branch stub `%s' offset overflow\n"),
10778              stub_entry->root.string);
10779           htab->stub_error = TRUE;
10780           return FALSE;
10781         }
10782
10783       if (info->emitrelocations)
10784         {
10785           r = get_relocs (stub_entry->group->stub_sec, 1);
10786           if (r == NULL)
10787             return FALSE;
10788           r->r_offset = loc - stub_entry->group->stub_sec->contents;
10789           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10790           r->r_addend = dest;
10791           if (stub_entry->h != NULL)
10792             {
10793               struct elf_link_hash_entry **hashes;
10794               unsigned long symndx;
10795               struct ppc_link_hash_entry *h;
10796
10797               hashes = elf_sym_hashes (htab->params->stub_bfd);
10798               if (hashes == NULL)
10799                 {
10800                   bfd_size_type hsize;
10801
10802                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10803                   hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10804                   if (hashes == NULL)
10805                     return FALSE;
10806                   elf_sym_hashes (htab->params->stub_bfd) = hashes;
10807                   htab->stub_globals = 1;
10808                 }
10809               symndx = htab->stub_globals++;
10810               h = stub_entry->h;
10811               hashes[symndx] = &h->elf;
10812               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10813               if (h->oh != NULL && h->oh->is_func)
10814                 h = ppc_follow_link (h->oh);
10815               if (h->elf.root.u.def.section != stub_entry->target_section)
10816                 /* H is an opd symbol.  The addend must be zero.  */
10817                 r->r_addend = 0;
10818               else
10819                 {
10820                   off = (h->elf.root.u.def.value
10821                          + h->elf.root.u.def.section->output_offset
10822                          + h->elf.root.u.def.section->output_section->vma);
10823                   r->r_addend -= off;
10824                 }
10825             }
10826         }
10827       break;
10828
10829     case ppc_stub_plt_branch:
10830     case ppc_stub_plt_branch_r2off:
10831       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10832                                          stub_entry->root.string + 9,
10833                                          FALSE, FALSE);
10834       if (br_entry == NULL)
10835         {
10836           info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10837                                   stub_entry->root.string);
10838           htab->stub_error = TRUE;
10839           return FALSE;
10840         }
10841
10842       dest = (stub_entry->target_value
10843               + stub_entry->target_section->output_offset
10844               + stub_entry->target_section->output_section->vma);
10845       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10846         dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10847
10848       bfd_put_64 (htab->brlt->owner, dest,
10849                   htab->brlt->contents + br_entry->offset);
10850
10851       if (br_entry->iter == htab->stub_iteration)
10852         {
10853           br_entry->iter = 0;
10854
10855           if (htab->relbrlt != NULL)
10856             {
10857               /* Create a reloc for the branch lookup table entry.  */
10858               Elf_Internal_Rela rela;
10859               bfd_byte *rl;
10860
10861               rela.r_offset = (br_entry->offset
10862                                + htab->brlt->output_offset
10863                                + htab->brlt->output_section->vma);
10864               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10865               rela.r_addend = dest;
10866
10867               rl = htab->relbrlt->contents;
10868               rl += (htab->relbrlt->reloc_count++
10869                      * sizeof (Elf64_External_Rela));
10870               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10871             }
10872           else if (info->emitrelocations)
10873             {
10874               r = get_relocs (htab->brlt, 1);
10875               if (r == NULL)
10876                 return FALSE;
10877               /* brlt, being SEC_LINKER_CREATED does not go through the
10878                  normal reloc processing.  Symbols and offsets are not
10879                  translated from input file to output file form, so
10880                  set up the offset per the output file.  */
10881               r->r_offset = (br_entry->offset
10882                              + htab->brlt->output_offset
10883                              + htab->brlt->output_section->vma);
10884               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10885               r->r_addend = dest;
10886             }
10887         }
10888
10889       dest = (br_entry->offset
10890               + htab->brlt->output_offset
10891               + htab->brlt->output_section->vma);
10892
10893       off = (dest
10894              - elf_gp (htab->brlt->output_section->owner)
10895              - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10896
10897       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10898         {
10899           info->callbacks->einfo
10900             (_("%P: linkage table error against `%T'\n"),
10901              stub_entry->root.string);
10902           bfd_set_error (bfd_error_bad_value);
10903           htab->stub_error = TRUE;
10904           return FALSE;
10905         }
10906
10907       if (info->emitrelocations)
10908         {
10909           r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
10910           if (r == NULL)
10911             return FALSE;
10912           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10913           if (bfd_big_endian (info->output_bfd))
10914             r[0].r_offset += 2;
10915           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10916             r[0].r_offset += 4;
10917           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10918           r[0].r_addend = dest;
10919           if (PPC_HA (off) != 0)
10920             {
10921               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10922               r[1].r_offset = r[0].r_offset + 4;
10923               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10924               r[1].r_addend = r[0].r_addend;
10925             }
10926         }
10927
10928       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10929         {
10930           if (PPC_HA (off) != 0)
10931             {
10932               size = 16;
10933               bfd_put_32 (htab->params->stub_bfd,
10934                           ADDIS_R12_R2 | PPC_HA (off), loc);
10935               loc += 4;
10936               bfd_put_32 (htab->params->stub_bfd,
10937                           LD_R12_0R12 | PPC_LO (off), loc);
10938             }
10939           else
10940             {
10941               size = 12;
10942               bfd_put_32 (htab->params->stub_bfd,
10943                           LD_R12_0R2 | PPC_LO (off), loc);
10944             }
10945         }
10946       else
10947         {
10948           bfd_vma r2off = get_r2off (info, stub_entry);
10949
10950           if (r2off == (bfd_vma) -1)
10951             {
10952               htab->stub_error = TRUE;
10953               return FALSE;
10954             }
10955
10956           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10957           loc += 4;
10958           size = 16;
10959           if (PPC_HA (off) != 0)
10960             {
10961               size += 4;
10962               bfd_put_32 (htab->params->stub_bfd,
10963                           ADDIS_R12_R2 | PPC_HA (off), loc);
10964               loc += 4;
10965               bfd_put_32 (htab->params->stub_bfd,
10966                           LD_R12_0R12 | PPC_LO (off), loc);
10967             }
10968           else
10969             bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
10970
10971           if (PPC_HA (r2off) != 0)
10972             {
10973               size += 4;
10974               loc += 4;
10975               bfd_put_32 (htab->params->stub_bfd,
10976                           ADDIS_R2_R2 | PPC_HA (r2off), loc);
10977             }
10978           if (PPC_LO (r2off) != 0)
10979             {
10980               size += 4;
10981               loc += 4;
10982               bfd_put_32 (htab->params->stub_bfd,
10983                           ADDI_R2_R2 | PPC_LO (r2off), loc);
10984             }
10985         }
10986       loc += 4;
10987       bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
10988       loc += 4;
10989       bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
10990       break;
10991
10992     case ppc_stub_plt_call:
10993     case ppc_stub_plt_call_r2save:
10994       if (stub_entry->h != NULL
10995           && stub_entry->h->is_func_descriptor
10996           && stub_entry->h->oh != NULL)
10997         {
10998           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10999
11000           /* If the old-ABI "dot-symbol" is undefined make it weak so
11001              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
11002           if (fh->elf.root.type == bfd_link_hash_undefined)
11003             fh->elf.root.type = bfd_link_hash_undefweak;
11004           /* Stop undo_symbol_twiddle changing it back to undefined.  */
11005           fh->was_undefined = 0;
11006         }
11007
11008       /* Now build the stub.  */
11009       dest = stub_entry->plt_ent->plt.offset & ~1;
11010       if (dest >= (bfd_vma) -2)
11011         abort ();
11012
11013       plt = htab->elf.splt;
11014       if (!htab->elf.dynamic_sections_created
11015           || stub_entry->h == NULL
11016           || stub_entry->h->elf.dynindx == -1)
11017         plt = htab->elf.iplt;
11018
11019       dest += plt->output_offset + plt->output_section->vma;
11020
11021       if (stub_entry->h == NULL
11022           && (stub_entry->plt_ent->plt.offset & 1) == 0)
11023         {
11024           Elf_Internal_Rela rela;
11025           bfd_byte *rl;
11026
11027           rela.r_offset = dest;
11028           if (htab->opd_abi)
11029             rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11030           else
11031             rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
11032           rela.r_addend = (stub_entry->target_value
11033                            + stub_entry->target_section->output_offset
11034                            + stub_entry->target_section->output_section->vma);
11035
11036           rl = (htab->elf.irelplt->contents
11037                 + (htab->elf.irelplt->reloc_count++
11038                    * sizeof (Elf64_External_Rela)));
11039           bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
11040           stub_entry->plt_ent->plt.offset |= 1;
11041         }
11042
11043       off = (dest
11044              - elf_gp (plt->output_section->owner)
11045              - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11046
11047       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11048         {
11049           info->callbacks->einfo
11050             (_("%P: linkage table error against `%T'\n"),
11051              stub_entry->h != NULL
11052              ? stub_entry->h->elf.root.root.string
11053              : "<local sym>");
11054           bfd_set_error (bfd_error_bad_value);
11055           htab->stub_error = TRUE;
11056           return FALSE;
11057         }
11058
11059       if (htab->params->plt_stub_align != 0)
11060         {
11061           unsigned pad = plt_stub_pad (htab, stub_entry, off);
11062
11063           stub_entry->group->stub_sec->size += pad;
11064           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11065           loc += pad;
11066         }
11067
11068       r = NULL;
11069       if (info->emitrelocations)
11070         {
11071           r = get_relocs (stub_entry->group->stub_sec,
11072                           ((PPC_HA (off) != 0)
11073                            + (htab->opd_abi
11074                               ? 2 + (htab->params->plt_static_chain
11075                                      && PPC_HA (off + 16) == PPC_HA (off))
11076                               : 1)));
11077           if (r == NULL)
11078             return FALSE;
11079           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11080           if (bfd_big_endian (info->output_bfd))
11081             r[0].r_offset += 2;
11082           r[0].r_addend = dest;
11083         }
11084       if (stub_entry->h != NULL
11085           && (stub_entry->h == htab->tls_get_addr_fd
11086               || stub_entry->h == htab->tls_get_addr)
11087           && htab->params->tls_get_addr_opt)
11088         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11089       else
11090         p = build_plt_stub (htab, stub_entry, loc, off, r);
11091       size = p - loc;
11092       break;
11093
11094     case ppc_stub_save_res:
11095       return TRUE;
11096
11097     default:
11098       BFD_FAIL ();
11099       return FALSE;
11100     }
11101
11102   stub_entry->group->stub_sec->size += size;
11103
11104   if (htab->params->emit_stub_syms)
11105     {
11106       struct elf_link_hash_entry *h;
11107       size_t len1, len2;
11108       char *name;
11109       const char *const stub_str[] = { "long_branch",
11110                                        "long_branch_r2off",
11111                                        "plt_branch",
11112                                        "plt_branch_r2off",
11113                                        "plt_call",
11114                                        "plt_call" };
11115
11116       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11117       len2 = strlen (stub_entry->root.string);
11118       name = bfd_malloc (len1 + len2 + 2);
11119       if (name == NULL)
11120         return FALSE;
11121       memcpy (name, stub_entry->root.string, 9);
11122       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11123       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11124       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11125       if (h == NULL)
11126         return FALSE;
11127       if (h->root.type == bfd_link_hash_new)
11128         {
11129           h->root.type = bfd_link_hash_defined;
11130           h->root.u.def.section = stub_entry->group->stub_sec;
11131           h->root.u.def.value = stub_entry->stub_offset;
11132           h->ref_regular = 1;
11133           h->def_regular = 1;
11134           h->ref_regular_nonweak = 1;
11135           h->forced_local = 1;
11136           h->non_elf = 0;
11137           h->root.linker_def = 1;
11138         }
11139     }
11140
11141   return TRUE;
11142 }
11143
11144 /* As above, but don't actually build the stub.  Just bump offset so
11145    we know stub section sizes, and select plt_branch stubs where
11146    long_branch stubs won't do.  */
11147
11148 static bfd_boolean
11149 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11150 {
11151   struct ppc_stub_hash_entry *stub_entry;
11152   struct bfd_link_info *info;
11153   struct ppc_link_hash_table *htab;
11154   bfd_vma off;
11155   int size;
11156
11157   /* Massage our args to the form they really have.  */
11158   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11159   info = in_arg;
11160
11161   htab = ppc_hash_table (info);
11162   if (htab == NULL)
11163     return FALSE;
11164
11165   if (stub_entry->h != NULL
11166       && stub_entry->h->save_res
11167       && stub_entry->h->elf.root.type == bfd_link_hash_defined
11168       && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11169     {
11170       /* Don't make stubs to out-of-line register save/restore
11171          functions.  Instead, emit copies of the functions.  */
11172       stub_entry->group->needs_save_res = 1;
11173       stub_entry->stub_type = ppc_stub_save_res;
11174       return TRUE;
11175     }
11176
11177   if (stub_entry->stub_type == ppc_stub_plt_call
11178       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11179     {
11180       asection *plt;
11181       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11182       if (off >= (bfd_vma) -2)
11183         abort ();
11184       plt = htab->elf.splt;
11185       if (!htab->elf.dynamic_sections_created
11186           || stub_entry->h == NULL
11187           || stub_entry->h->elf.dynindx == -1)
11188         plt = htab->elf.iplt;
11189       off += (plt->output_offset
11190               + plt->output_section->vma
11191               - elf_gp (plt->output_section->owner)
11192               - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11193
11194       size = plt_stub_size (htab, stub_entry, off);
11195       if (htab->params->plt_stub_align)
11196         size += plt_stub_pad (htab, stub_entry, off);
11197       if (info->emitrelocations)
11198         {
11199           stub_entry->group->stub_sec->reloc_count
11200             += ((PPC_HA (off) != 0)
11201                 + (htab->opd_abi
11202                    ? 2 + (htab->params->plt_static_chain
11203                           && PPC_HA (off + 16) == PPC_HA (off))
11204                    : 1));
11205           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11206         }
11207     }
11208   else
11209     {
11210       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11211          variants.  */
11212       bfd_vma r2off = 0;
11213       bfd_vma local_off = 0;
11214
11215       off = (stub_entry->target_value
11216              + stub_entry->target_section->output_offset
11217              + stub_entry->target_section->output_section->vma);
11218       off -= (stub_entry->group->stub_sec->size
11219               + stub_entry->group->stub_sec->output_offset
11220               + stub_entry->group->stub_sec->output_section->vma);
11221
11222       /* Reset the stub type from the plt variant in case we now
11223          can reach with a shorter stub.  */
11224       if (stub_entry->stub_type >= ppc_stub_plt_branch)
11225         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11226
11227       size = 4;
11228       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11229         {
11230           r2off = get_r2off (info, stub_entry);
11231           if (r2off == (bfd_vma) -1)
11232             {
11233               htab->stub_error = TRUE;
11234               return FALSE;
11235             }
11236           size = 8;
11237           if (PPC_HA (r2off) != 0)
11238             size += 4;
11239           if (PPC_LO (r2off) != 0)
11240             size += 4;
11241           off -= size - 4;
11242         }
11243
11244       local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11245
11246       /* If the branch offset if too big, use a ppc_stub_plt_branch.
11247          Do the same for -R objects without function descriptors.  */
11248       if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11249           || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11250               && r2off == 0
11251               && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11252         {
11253           struct ppc_branch_hash_entry *br_entry;
11254
11255           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11256                                              stub_entry->root.string + 9,
11257                                              TRUE, FALSE);
11258           if (br_entry == NULL)
11259             {
11260               info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
11261                                       stub_entry->root.string);
11262               htab->stub_error = TRUE;
11263               return FALSE;
11264             }
11265
11266           if (br_entry->iter != htab->stub_iteration)
11267             {
11268               br_entry->iter = htab->stub_iteration;
11269               br_entry->offset = htab->brlt->size;
11270               htab->brlt->size += 8;
11271
11272               if (htab->relbrlt != NULL)
11273                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11274               else if (info->emitrelocations)
11275                 {
11276                   htab->brlt->reloc_count += 1;
11277                   htab->brlt->flags |= SEC_RELOC;
11278                 }
11279             }
11280
11281           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11282           off = (br_entry->offset
11283                  + htab->brlt->output_offset
11284                  + htab->brlt->output_section->vma
11285                  - elf_gp (htab->brlt->output_section->owner)
11286                  - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11287
11288           if (info->emitrelocations)
11289             {
11290               stub_entry->group->stub_sec->reloc_count
11291                 += 1 + (PPC_HA (off) != 0);
11292               stub_entry->group->stub_sec->flags |= SEC_RELOC;
11293             }
11294
11295           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11296             {
11297               size = 12;
11298               if (PPC_HA (off) != 0)
11299                 size = 16;
11300             }
11301           else
11302             {
11303               size = 16;
11304               if (PPC_HA (off) != 0)
11305                 size += 4;
11306
11307               if (PPC_HA (r2off) != 0)
11308                 size += 4;
11309               if (PPC_LO (r2off) != 0)
11310                 size += 4;
11311             }
11312         }
11313       else if (info->emitrelocations)
11314         {
11315           stub_entry->group->stub_sec->reloc_count += 1;
11316           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11317         }
11318     }
11319
11320   stub_entry->group->stub_sec->size += size;
11321   return TRUE;
11322 }
11323
11324 /* Set up various things so that we can make a list of input sections
11325    for each output section included in the link.  Returns -1 on error,
11326    0 when no stubs will be needed, and 1 on success.  */
11327
11328 int
11329 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11330 {
11331   unsigned int id;
11332   bfd_size_type amt;
11333   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11334
11335   if (htab == NULL)
11336     return -1;
11337
11338   htab->sec_info_arr_size = bfd_get_next_section_id ();
11339   amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11340   htab->sec_info = bfd_zmalloc (amt);
11341   if (htab->sec_info == NULL)
11342     return -1;
11343
11344   /* Set toc_off for com, und, abs and ind sections.  */
11345   for (id = 0; id < 3; id++)
11346     htab->sec_info[id].toc_off = TOC_BASE_OFF;
11347
11348   return 1;
11349 }
11350
11351 /* Set up for first pass at multitoc partitioning.  */
11352
11353 void
11354 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11355 {
11356   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11357
11358   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11359   htab->toc_bfd = NULL;
11360   htab->toc_first_sec = NULL;
11361 }
11362
11363 /* The linker repeatedly calls this function for each TOC input section
11364    and linker generated GOT section.  Group input bfds such that the toc
11365    within a group is less than 64k in size.  */
11366
11367 bfd_boolean
11368 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11369 {
11370   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11371   bfd_vma addr, off, limit;
11372
11373   if (htab == NULL)
11374     return FALSE;
11375
11376   if (!htab->second_toc_pass)
11377     {
11378       /* Keep track of the first .toc or .got section for this input bfd.  */
11379       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11380
11381       if (new_bfd)
11382         {
11383           htab->toc_bfd = isec->owner;
11384           htab->toc_first_sec = isec;
11385         }
11386
11387       addr = isec->output_offset + isec->output_section->vma;
11388       off = addr - htab->toc_curr;
11389       limit = 0x80008000;
11390       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11391         limit = 0x10000;
11392       if (off + isec->size > limit)
11393         {
11394           addr = (htab->toc_first_sec->output_offset
11395                   + htab->toc_first_sec->output_section->vma);
11396           htab->toc_curr = addr;
11397           htab->toc_curr &= -TOC_BASE_ALIGN;
11398         }
11399
11400       /* toc_curr is the base address of this toc group.  Set elf_gp
11401          for the input section to be the offset relative to the
11402          output toc base plus 0x8000.  Making the input elf_gp an
11403          offset allows us to move the toc as a whole without
11404          recalculating input elf_gp.  */
11405       off = htab->toc_curr - elf_gp (isec->output_section->owner);
11406       off += TOC_BASE_OFF;
11407
11408       /* Die if someone uses a linker script that doesn't keep input
11409          file .toc and .got together.  */
11410       if (new_bfd
11411           && elf_gp (isec->owner) != 0
11412           && elf_gp (isec->owner) != off)
11413         return FALSE;
11414
11415       elf_gp (isec->owner) = off;
11416       return TRUE;
11417     }
11418
11419   /* During the second pass toc_first_sec points to the start of
11420      a toc group, and toc_curr is used to track the old elf_gp.
11421      We use toc_bfd to ensure we only look at each bfd once.  */
11422   if (htab->toc_bfd == isec->owner)
11423     return TRUE;
11424   htab->toc_bfd = isec->owner;
11425
11426   if (htab->toc_first_sec == NULL
11427       || htab->toc_curr != elf_gp (isec->owner))
11428     {
11429       htab->toc_curr = elf_gp (isec->owner);
11430       htab->toc_first_sec = isec;
11431     }
11432   addr = (htab->toc_first_sec->output_offset
11433           + htab->toc_first_sec->output_section->vma);
11434   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11435   elf_gp (isec->owner) = off;
11436
11437   return TRUE;
11438 }
11439
11440 /* Called via elf_link_hash_traverse to merge GOT entries for global
11441    symbol H.  */
11442
11443 static bfd_boolean
11444 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11445 {
11446   if (h->root.type == bfd_link_hash_indirect)
11447     return TRUE;
11448
11449   merge_got_entries (&h->got.glist);
11450
11451   return TRUE;
11452 }
11453
11454 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11455    symbol H.  */
11456
11457 static bfd_boolean
11458 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11459 {
11460   struct got_entry *gent;
11461
11462   if (h->root.type == bfd_link_hash_indirect)
11463     return TRUE;
11464
11465   for (gent = h->got.glist; gent != NULL; gent = gent->next)
11466     if (!gent->is_indirect)
11467       allocate_got (h, (struct bfd_link_info *) inf, gent);
11468   return TRUE;
11469 }
11470
11471 /* Called on the first multitoc pass after the last call to
11472    ppc64_elf_next_toc_section.  This function removes duplicate GOT
11473    entries.  */
11474
11475 bfd_boolean
11476 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11477 {
11478   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11479   struct bfd *ibfd, *ibfd2;
11480   bfd_boolean done_something;
11481
11482   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11483
11484   if (!htab->do_multi_toc)
11485     return FALSE;
11486
11487   /* Merge global sym got entries within a toc group.  */
11488   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11489
11490   /* And tlsld_got.  */
11491   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11492     {
11493       struct got_entry *ent, *ent2;
11494
11495       if (!is_ppc64_elf (ibfd))
11496         continue;
11497
11498       ent = ppc64_tlsld_got (ibfd);
11499       if (!ent->is_indirect
11500           && ent->got.offset != (bfd_vma) -1)
11501         {
11502           for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11503             {
11504               if (!is_ppc64_elf (ibfd2))
11505                 continue;
11506
11507               ent2 = ppc64_tlsld_got (ibfd2);
11508               if (!ent2->is_indirect
11509                   && ent2->got.offset != (bfd_vma) -1
11510                   && elf_gp (ibfd2) == elf_gp (ibfd))
11511                 {
11512                   ent2->is_indirect = TRUE;
11513                   ent2->got.ent = ent;
11514                 }
11515             }
11516         }
11517     }
11518
11519   /* Zap sizes of got sections.  */
11520   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11521   htab->elf.irelplt->size -= htab->got_reli_size;
11522   htab->got_reli_size = 0;
11523
11524   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11525     {
11526       asection *got, *relgot;
11527
11528       if (!is_ppc64_elf (ibfd))
11529         continue;
11530
11531       got = ppc64_elf_tdata (ibfd)->got;
11532       if (got != NULL)
11533         {
11534           got->rawsize = got->size;
11535           got->size = 0;
11536           relgot = ppc64_elf_tdata (ibfd)->relgot;
11537           relgot->rawsize = relgot->size;
11538           relgot->size = 0;
11539         }
11540     }
11541
11542   /* Now reallocate the got, local syms first.  We don't need to
11543      allocate section contents again since we never increase size.  */
11544   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11545     {
11546       struct got_entry **lgot_ents;
11547       struct got_entry **end_lgot_ents;
11548       struct plt_entry **local_plt;
11549       struct plt_entry **end_local_plt;
11550       unsigned char *lgot_masks;
11551       bfd_size_type locsymcount;
11552       Elf_Internal_Shdr *symtab_hdr;
11553       asection *s;
11554
11555       if (!is_ppc64_elf (ibfd))
11556         continue;
11557
11558       lgot_ents = elf_local_got_ents (ibfd);
11559       if (!lgot_ents)
11560         continue;
11561
11562       symtab_hdr = &elf_symtab_hdr (ibfd);
11563       locsymcount = symtab_hdr->sh_info;
11564       end_lgot_ents = lgot_ents + locsymcount;
11565       local_plt = (struct plt_entry **) end_lgot_ents;
11566       end_local_plt = local_plt + locsymcount;
11567       lgot_masks = (unsigned char *) end_local_plt;
11568       s = ppc64_elf_tdata (ibfd)->got;
11569       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11570         {
11571           struct got_entry *ent;
11572
11573           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11574             {
11575               unsigned int ent_size = 8;
11576               unsigned int rel_size = sizeof (Elf64_External_Rela);
11577
11578               ent->got.offset = s->size;
11579               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11580                 {
11581                   ent_size *= 2;
11582                   rel_size *= 2;
11583                 }
11584               s->size += ent_size;
11585               if ((*lgot_masks & PLT_IFUNC) != 0)
11586                 {
11587                   htab->elf.irelplt->size += rel_size;
11588                   htab->got_reli_size += rel_size;
11589                 }
11590               else if (bfd_link_pic (info))
11591                 {
11592                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11593                   srel->size += rel_size;
11594                 }
11595             }
11596         }
11597     }
11598
11599   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11600
11601   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11602     {
11603       struct got_entry *ent;
11604
11605       if (!is_ppc64_elf (ibfd))
11606         continue;
11607
11608       ent = ppc64_tlsld_got (ibfd);
11609       if (!ent->is_indirect
11610           && ent->got.offset != (bfd_vma) -1)
11611         {
11612           asection *s = ppc64_elf_tdata (ibfd)->got;
11613           ent->got.offset = s->size;
11614           s->size += 16;
11615           if (bfd_link_pic (info))
11616             {
11617               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11618               srel->size += sizeof (Elf64_External_Rela);
11619             }
11620         }
11621     }
11622
11623   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11624   if (!done_something)
11625     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11626       {
11627         asection *got;
11628
11629         if (!is_ppc64_elf (ibfd))
11630           continue;
11631
11632         got = ppc64_elf_tdata (ibfd)->got;
11633         if (got != NULL)
11634           {
11635             done_something = got->rawsize != got->size;
11636             if (done_something)
11637               break;
11638           }
11639       }
11640
11641   if (done_something)
11642     (*htab->params->layout_sections_again) ();
11643
11644   /* Set up for second pass over toc sections to recalculate elf_gp
11645      on input sections.  */
11646   htab->toc_bfd = NULL;
11647   htab->toc_first_sec = NULL;
11648   htab->second_toc_pass = TRUE;
11649   return done_something;
11650 }
11651
11652 /* Called after second pass of multitoc partitioning.  */
11653
11654 void
11655 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11656 {
11657   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11658
11659   /* After the second pass, toc_curr tracks the TOC offset used
11660      for code sections below in ppc64_elf_next_input_section.  */
11661   htab->toc_curr = TOC_BASE_OFF;
11662 }
11663
11664 /* No toc references were found in ISEC.  If the code in ISEC makes no
11665    calls, then there's no need to use toc adjusting stubs when branching
11666    into ISEC.  Actually, indirect calls from ISEC are OK as they will
11667    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
11668    needed, and 2 if a cyclical call-graph was found but no other reason
11669    for a stub was detected.  If called from the top level, a return of
11670    2 means the same as a return of 0.  */
11671
11672 static int
11673 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11674 {
11675   int ret;
11676
11677   /* Mark this section as checked.  */
11678   isec->call_check_done = 1;
11679
11680   /* We know none of our code bearing sections will need toc stubs.  */
11681   if ((isec->flags & SEC_LINKER_CREATED) != 0)
11682     return 0;
11683
11684   if (isec->size == 0)
11685     return 0;
11686
11687   if (isec->output_section == NULL)
11688     return 0;
11689
11690   ret = 0;
11691   if (isec->reloc_count != 0)
11692     {
11693       Elf_Internal_Rela *relstart, *rel;
11694       Elf_Internal_Sym *local_syms;
11695       struct ppc_link_hash_table *htab;
11696
11697       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11698                                             info->keep_memory);
11699       if (relstart == NULL)
11700         return -1;
11701
11702       /* Look for branches to outside of this section.  */
11703       local_syms = NULL;
11704       htab = ppc_hash_table (info);
11705       if (htab == NULL)
11706         return -1;
11707
11708       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11709         {
11710           enum elf_ppc64_reloc_type r_type;
11711           unsigned long r_symndx;
11712           struct elf_link_hash_entry *h;
11713           struct ppc_link_hash_entry *eh;
11714           Elf_Internal_Sym *sym;
11715           asection *sym_sec;
11716           struct _opd_sec_data *opd;
11717           bfd_vma sym_value;
11718           bfd_vma dest;
11719
11720           r_type = ELF64_R_TYPE (rel->r_info);
11721           if (r_type != R_PPC64_REL24
11722               && r_type != R_PPC64_REL14
11723               && r_type != R_PPC64_REL14_BRTAKEN
11724               && r_type != R_PPC64_REL14_BRNTAKEN)
11725             continue;
11726
11727           r_symndx = ELF64_R_SYM (rel->r_info);
11728           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11729                           isec->owner))
11730             {
11731               ret = -1;
11732               break;
11733             }
11734
11735           /* Calls to dynamic lib functions go through a plt call stub
11736              that uses r2.  */
11737           eh = (struct ppc_link_hash_entry *) h;
11738           if (eh != NULL
11739               && (eh->elf.plt.plist != NULL
11740                   || (eh->oh != NULL
11741                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11742             {
11743               ret = 1;
11744               break;
11745             }
11746
11747           if (sym_sec == NULL)
11748             /* Ignore other undefined symbols.  */
11749             continue;
11750
11751           /* Assume branches to other sections not included in the
11752              link need stubs too, to cover -R and absolute syms.  */
11753           if (sym_sec->output_section == NULL)
11754             {
11755               ret = 1;
11756               break;
11757             }
11758
11759           if (h == NULL)
11760             sym_value = sym->st_value;
11761           else
11762             {
11763               if (h->root.type != bfd_link_hash_defined
11764                   && h->root.type != bfd_link_hash_defweak)
11765                 abort ();
11766               sym_value = h->root.u.def.value;
11767             }
11768           sym_value += rel->r_addend;
11769
11770           /* If this branch reloc uses an opd sym, find the code section.  */
11771           opd = get_opd_info (sym_sec);
11772           if (opd != NULL)
11773             {
11774               if (h == NULL && opd->adjust != NULL)
11775                 {
11776                   long adjust;
11777
11778                   adjust = opd->adjust[OPD_NDX (sym_value)];
11779                   if (adjust == -1)
11780                     /* Assume deleted functions won't ever be called.  */
11781                     continue;
11782                   sym_value += adjust;
11783                 }
11784
11785               dest = opd_entry_value (sym_sec, sym_value,
11786                                       &sym_sec, NULL, FALSE);
11787               if (dest == (bfd_vma) -1)
11788                 continue;
11789             }
11790           else
11791             dest = (sym_value
11792                     + sym_sec->output_offset
11793                     + sym_sec->output_section->vma);
11794
11795           /* Ignore branch to self.  */
11796           if (sym_sec == isec)
11797             continue;
11798
11799           /* If the called function uses the toc, we need a stub.  */
11800           if (sym_sec->has_toc_reloc
11801               || sym_sec->makes_toc_func_call)
11802             {
11803               ret = 1;
11804               break;
11805             }
11806
11807           /* Assume any branch that needs a long branch stub might in fact
11808              need a plt_branch stub.  A plt_branch stub uses r2.  */
11809           else if (dest - (isec->output_offset
11810                            + isec->output_section->vma
11811                            + rel->r_offset) + (1 << 25)
11812                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11813                                                              ? h->other
11814                                                              : sym->st_other))
11815             {
11816               ret = 1;
11817               break;
11818             }
11819
11820           /* If calling back to a section in the process of being
11821              tested, we can't say for sure that no toc adjusting stubs
11822              are needed, so don't return zero.  */
11823           else if (sym_sec->call_check_in_progress)
11824             ret = 2;
11825
11826           /* Branches to another section that itself doesn't have any TOC
11827              references are OK.  Recursively call ourselves to check.  */
11828           else if (!sym_sec->call_check_done)
11829             {
11830               int recur;
11831
11832               /* Mark current section as indeterminate, so that other
11833                  sections that call back to current won't be marked as
11834                  known.  */
11835               isec->call_check_in_progress = 1;
11836               recur = toc_adjusting_stub_needed (info, sym_sec);
11837               isec->call_check_in_progress = 0;
11838
11839               if (recur != 0)
11840                 {
11841                   ret = recur;
11842                   if (recur != 2)
11843                     break;
11844                 }
11845             }
11846         }
11847
11848       if (local_syms != NULL
11849           && (elf_symtab_hdr (isec->owner).contents
11850               != (unsigned char *) local_syms))
11851         free (local_syms);
11852       if (elf_section_data (isec)->relocs != relstart)
11853         free (relstart);
11854     }
11855
11856   if ((ret & 1) == 0
11857       && isec->map_head.s != NULL
11858       && (strcmp (isec->output_section->name, ".init") == 0
11859           || strcmp (isec->output_section->name, ".fini") == 0))
11860     {
11861       if (isec->map_head.s->has_toc_reloc
11862           || isec->map_head.s->makes_toc_func_call)
11863         ret = 1;
11864       else if (!isec->map_head.s->call_check_done)
11865         {
11866           int recur;
11867           isec->call_check_in_progress = 1;
11868           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11869           isec->call_check_in_progress = 0;
11870           if (recur != 0)
11871             ret = recur;
11872         }
11873     }
11874
11875   if (ret == 1)
11876     isec->makes_toc_func_call = 1;
11877
11878   return ret;
11879 }
11880
11881 /* The linker repeatedly calls this function for each input section,
11882    in the order that input sections are linked into output sections.
11883    Build lists of input sections to determine groupings between which
11884    we may insert linker stubs.  */
11885
11886 bfd_boolean
11887 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11888 {
11889   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11890
11891   if (htab == NULL)
11892     return FALSE;
11893
11894   if ((isec->output_section->flags & SEC_CODE) != 0
11895       && isec->output_section->id < htab->sec_info_arr_size)
11896     {
11897       /* This happens to make the list in reverse order,
11898          which is what we want.  */
11899       htab->sec_info[isec->id].u.list
11900         = htab->sec_info[isec->output_section->id].u.list;
11901       htab->sec_info[isec->output_section->id].u.list = isec;
11902     }
11903
11904   if (htab->multi_toc_needed)
11905     {
11906       /* Analyse sections that aren't already flagged as needing a
11907          valid toc pointer.  Exclude .fixup for the linux kernel.
11908          .fixup contains branches, but only back to the function that
11909          hit an exception.  */
11910       if (!(isec->has_toc_reloc
11911             || (isec->flags & SEC_CODE) == 0
11912             || strcmp (isec->name, ".fixup") == 0
11913             || isec->call_check_done))
11914         {
11915           if (toc_adjusting_stub_needed (info, isec) < 0)
11916             return FALSE;
11917         }
11918       /* Make all sections use the TOC assigned for this object file.
11919          This will be wrong for pasted sections;  We fix that in
11920          check_pasted_section().  */
11921       if (elf_gp (isec->owner) != 0)
11922         htab->toc_curr = elf_gp (isec->owner);
11923     }
11924
11925   htab->sec_info[isec->id].toc_off = htab->toc_curr;
11926   return TRUE;
11927 }
11928
11929 /* Check that all .init and .fini sections use the same toc, if they
11930    have toc relocs.  */
11931
11932 static bfd_boolean
11933 check_pasted_section (struct bfd_link_info *info, const char *name)
11934 {
11935   asection *o = bfd_get_section_by_name (info->output_bfd, name);
11936
11937   if (o != NULL)
11938     {
11939       struct ppc_link_hash_table *htab = ppc_hash_table (info);
11940       bfd_vma toc_off = 0;
11941       asection *i;
11942
11943       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11944         if (i->has_toc_reloc)
11945           {
11946             if (toc_off == 0)
11947               toc_off = htab->sec_info[i->id].toc_off;
11948             else if (toc_off != htab->sec_info[i->id].toc_off)
11949               return FALSE;
11950           }
11951
11952       if (toc_off == 0)
11953         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11954           if (i->makes_toc_func_call)
11955             {
11956               toc_off = htab->sec_info[i->id].toc_off;
11957               break;
11958             }
11959
11960       /* Make sure the whole pasted function uses the same toc offset.  */
11961       if (toc_off != 0)
11962         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11963           htab->sec_info[i->id].toc_off = toc_off;
11964     }
11965   return TRUE;
11966 }
11967
11968 bfd_boolean
11969 ppc64_elf_check_init_fini (struct bfd_link_info *info)
11970 {
11971   return (check_pasted_section (info, ".init")
11972           & check_pasted_section (info, ".fini"));
11973 }
11974
11975 /* See whether we can group stub sections together.  Grouping stub
11976    sections may result in fewer stubs.  More importantly, we need to
11977    put all .init* and .fini* stubs at the beginning of the .init or
11978    .fini output sections respectively, because glibc splits the
11979    _init and _fini functions into multiple parts.  Putting a stub in
11980    the middle of a function is not a good idea.  */
11981
11982 static bfd_boolean
11983 group_sections (struct bfd_link_info *info,
11984                 bfd_size_type stub_group_size,
11985                 bfd_boolean stubs_always_before_branch)
11986 {
11987   struct ppc_link_hash_table *htab;
11988   asection *osec;
11989   bfd_size_type stub14_group_size;
11990   bfd_boolean suppress_size_errors;
11991
11992   htab = ppc_hash_table (info);
11993   if (htab == NULL)
11994     return FALSE;
11995
11996   suppress_size_errors = FALSE;
11997   stub14_group_size = stub_group_size >> 10;
11998   if (stub_group_size == 1)
11999     {
12000       /* Default values.  */
12001       if (stubs_always_before_branch)
12002         {
12003           stub_group_size = 0x1e00000;
12004           stub14_group_size = 0x7800;
12005         }
12006       else
12007         {
12008           stub_group_size = 0x1c00000;
12009           stub14_group_size = 0x7000;
12010         }
12011       suppress_size_errors = TRUE;
12012     }
12013
12014   for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12015     {
12016       asection *tail;
12017
12018       if (osec->id >= htab->sec_info_arr_size)
12019         continue;
12020
12021       tail = htab->sec_info[osec->id].u.list;
12022       while (tail != NULL)
12023         {
12024           asection *curr;
12025           asection *prev;
12026           bfd_size_type total;
12027           bfd_boolean big_sec;
12028           bfd_vma curr_toc;
12029           struct map_stub *group;
12030
12031           curr = tail;
12032           total = tail->size;
12033           big_sec = total > (ppc64_elf_section_data (tail) != NULL
12034                              && ppc64_elf_section_data (tail)->has_14bit_branch
12035                              ? stub14_group_size : stub_group_size);
12036           if (big_sec && !suppress_size_errors)
12037             (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
12038                                      tail->owner, tail);
12039           curr_toc = htab->sec_info[tail->id].toc_off;
12040
12041           while ((prev = htab->sec_info[curr->id].u.list) != NULL
12042                  && ((total += curr->output_offset - prev->output_offset)
12043                      < (ppc64_elf_section_data (prev) != NULL
12044                         && ppc64_elf_section_data (prev)->has_14bit_branch
12045                         ? stub14_group_size : stub_group_size))
12046                  && htab->sec_info[prev->id].toc_off == curr_toc)
12047             curr = prev;
12048
12049           /* OK, the size from the start of CURR to the end is less
12050              than stub_group_size and thus can be handled by one stub
12051              section.  (or the tail section is itself larger than
12052              stub_group_size, in which case we may be toast.)  We
12053              should really be keeping track of the total size of stubs
12054              added here, as stubs contribute to the final output
12055              section size.  That's a little tricky, and this way will
12056              only break if stubs added make the total size more than
12057              2^25, ie. for the default stub_group_size, if stubs total
12058              more than 2097152 bytes, or nearly 75000 plt call stubs.  */
12059           group = bfd_alloc (curr->owner, sizeof (*group));
12060           if (group == NULL)
12061             return FALSE;
12062           group->link_sec = curr;
12063           group->stub_sec = NULL;
12064           group->needs_save_res = 0;
12065           group->next = htab->group;
12066           htab->group = group;
12067           do
12068             {
12069               prev = htab->sec_info[tail->id].u.list;
12070               /* Set up this stub group.  */
12071               htab->sec_info[tail->id].u.group = group;
12072             }
12073           while (tail != curr && (tail = prev) != NULL);
12074
12075           /* But wait, there's more!  Input sections up to stub_group_size
12076              bytes before the stub section can be handled by it too.
12077              Don't do this if we have a really large section after the
12078              stubs, as adding more stubs increases the chance that
12079              branches may not reach into the stub section.  */
12080           if (!stubs_always_before_branch && !big_sec)
12081             {
12082               total = 0;
12083               while (prev != NULL
12084                      && ((total += tail->output_offset - prev->output_offset)
12085                          < (ppc64_elf_section_data (prev) != NULL
12086                             && ppc64_elf_section_data (prev)->has_14bit_branch
12087                             ? stub14_group_size : stub_group_size))
12088                      && htab->sec_info[prev->id].toc_off == curr_toc)
12089                 {
12090                   tail = prev;
12091                   prev = htab->sec_info[tail->id].u.list;
12092                   htab->sec_info[tail->id].u.group = group;
12093                 }
12094             }
12095           tail = prev;
12096         }
12097     }
12098   return TRUE;
12099 }
12100
12101 static const unsigned char glink_eh_frame_cie[] =
12102 {
12103   0, 0, 0, 16,                          /* length.  */
12104   0, 0, 0, 0,                           /* id.  */
12105   1,                                    /* CIE version.  */
12106   'z', 'R', 0,                          /* Augmentation string.  */
12107   4,                                    /* Code alignment.  */
12108   0x78,                                 /* Data alignment.  */
12109   65,                                   /* RA reg.  */
12110   1,                                    /* Augmentation size.  */
12111   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
12112   DW_CFA_def_cfa, 1, 0,                 /* def_cfa: r1 offset 0.  */
12113   0, 0, 0, 0
12114 };
12115
12116 /* Stripping output sections is normally done before dynamic section
12117    symbols have been allocated.  This function is called later, and
12118    handles cases like htab->brlt which is mapped to its own output
12119    section.  */
12120
12121 static void
12122 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12123 {
12124   if (isec->size == 0
12125       && isec->output_section->size == 0
12126       && !(isec->output_section->flags & SEC_KEEP)
12127       && !bfd_section_removed_from_list (info->output_bfd,
12128                                          isec->output_section)
12129       && elf_section_data (isec->output_section)->dynindx == 0)
12130     {
12131       isec->output_section->flags |= SEC_EXCLUDE;
12132       bfd_section_list_remove (info->output_bfd, isec->output_section);
12133       info->output_bfd->section_count--;
12134     }
12135 }
12136
12137 /* Determine and set the size of the stub section for a final link.
12138
12139    The basic idea here is to examine all the relocations looking for
12140    PC-relative calls to a target that is unreachable with a "bl"
12141    instruction.  */
12142
12143 bfd_boolean
12144 ppc64_elf_size_stubs (struct bfd_link_info *info)
12145 {
12146   bfd_size_type stub_group_size;
12147   bfd_boolean stubs_always_before_branch;
12148   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12149
12150   if (htab == NULL)
12151     return FALSE;
12152
12153   if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12154     htab->params->plt_thread_safe = 1;
12155   if (!htab->opd_abi)
12156     htab->params->plt_thread_safe = 0;
12157   else if (htab->params->plt_thread_safe == -1)
12158     {
12159       static const char *const thread_starter[] =
12160         {
12161           "pthread_create",
12162           /* libstdc++ */
12163           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12164           /* librt */
12165           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12166           "mq_notify", "create_timer",
12167           /* libanl */
12168           "getaddrinfo_a",
12169           /* libgomp */
12170           "GOMP_parallel",
12171           "GOMP_parallel_start",
12172           "GOMP_parallel_loop_static",
12173           "GOMP_parallel_loop_static_start",
12174           "GOMP_parallel_loop_dynamic",
12175           "GOMP_parallel_loop_dynamic_start",
12176           "GOMP_parallel_loop_guided",
12177           "GOMP_parallel_loop_guided_start",
12178           "GOMP_parallel_loop_runtime",
12179           "GOMP_parallel_loop_runtime_start",
12180           "GOMP_parallel_sections",
12181           "GOMP_parallel_sections_start",
12182           /* libgo */
12183           "__go_go",
12184         };
12185       unsigned i;
12186
12187       for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12188         {
12189           struct elf_link_hash_entry *h;
12190           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12191                                     FALSE, FALSE, TRUE);
12192           htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12193           if (htab->params->plt_thread_safe)
12194             break;
12195         }
12196     }
12197   stubs_always_before_branch = htab->params->group_size < 0;
12198   if (htab->params->group_size < 0)
12199     stub_group_size = -htab->params->group_size;
12200   else
12201     stub_group_size = htab->params->group_size;
12202
12203   if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12204     return FALSE;
12205
12206 #define STUB_SHRINK_ITER 20
12207   /* Loop until no stubs added.  After iteration 20 of this loop we may
12208      exit on a stub section shrinking.  This is to break out of a
12209      pathological case where adding stubs on one iteration decreases
12210      section gaps (perhaps due to alignment), which then requires
12211      fewer or smaller stubs on the next iteration.  */
12212
12213   while (1)
12214     {
12215       bfd *input_bfd;
12216       unsigned int bfd_indx;
12217       struct map_stub *group;
12218       asection *stub_sec;
12219
12220       htab->stub_iteration += 1;
12221
12222       for (input_bfd = info->input_bfds, bfd_indx = 0;
12223            input_bfd != NULL;
12224            input_bfd = input_bfd->link.next, bfd_indx++)
12225         {
12226           Elf_Internal_Shdr *symtab_hdr;
12227           asection *section;
12228           Elf_Internal_Sym *local_syms = NULL;
12229
12230           if (!is_ppc64_elf (input_bfd))
12231             continue;
12232
12233           /* We'll need the symbol table in a second.  */
12234           symtab_hdr = &elf_symtab_hdr (input_bfd);
12235           if (symtab_hdr->sh_info == 0)
12236             continue;
12237
12238           /* Walk over each section attached to the input bfd.  */
12239           for (section = input_bfd->sections;
12240                section != NULL;
12241                section = section->next)
12242             {
12243               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12244
12245               /* If there aren't any relocs, then there's nothing more
12246                  to do.  */
12247               if ((section->flags & SEC_RELOC) == 0
12248                   || (section->flags & SEC_ALLOC) == 0
12249                   || (section->flags & SEC_LOAD) == 0
12250                   || (section->flags & SEC_CODE) == 0
12251                   || section->reloc_count == 0)
12252                 continue;
12253
12254               /* If this section is a link-once section that will be
12255                  discarded, then don't create any stubs.  */
12256               if (section->output_section == NULL
12257                   || section->output_section->owner != info->output_bfd)
12258                 continue;
12259
12260               /* Get the relocs.  */
12261               internal_relocs
12262                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12263                                              info->keep_memory);
12264               if (internal_relocs == NULL)
12265                 goto error_ret_free_local;
12266
12267               /* Now examine each relocation.  */
12268               irela = internal_relocs;
12269               irelaend = irela + section->reloc_count;
12270               for (; irela < irelaend; irela++)
12271                 {
12272                   enum elf_ppc64_reloc_type r_type;
12273                   unsigned int r_indx;
12274                   enum ppc_stub_type stub_type;
12275                   struct ppc_stub_hash_entry *stub_entry;
12276                   asection *sym_sec, *code_sec;
12277                   bfd_vma sym_value, code_value;
12278                   bfd_vma destination;
12279                   unsigned long local_off;
12280                   bfd_boolean ok_dest;
12281                   struct ppc_link_hash_entry *hash;
12282                   struct ppc_link_hash_entry *fdh;
12283                   struct elf_link_hash_entry *h;
12284                   Elf_Internal_Sym *sym;
12285                   char *stub_name;
12286                   const asection *id_sec;
12287                   struct _opd_sec_data *opd;
12288                   struct plt_entry *plt_ent;
12289
12290                   r_type = ELF64_R_TYPE (irela->r_info);
12291                   r_indx = ELF64_R_SYM (irela->r_info);
12292
12293                   if (r_type >= R_PPC64_max)
12294                     {
12295                       bfd_set_error (bfd_error_bad_value);
12296                       goto error_ret_free_internal;
12297                     }
12298
12299                   /* Only look for stubs on branch instructions.  */
12300                   if (r_type != R_PPC64_REL24
12301                       && r_type != R_PPC64_REL14
12302                       && r_type != R_PPC64_REL14_BRTAKEN
12303                       && r_type != R_PPC64_REL14_BRNTAKEN)
12304                     continue;
12305
12306                   /* Now determine the call target, its name, value,
12307                      section.  */
12308                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12309                                   r_indx, input_bfd))
12310                     goto error_ret_free_internal;
12311                   hash = (struct ppc_link_hash_entry *) h;
12312
12313                   ok_dest = FALSE;
12314                   fdh = NULL;
12315                   sym_value = 0;
12316                   if (hash == NULL)
12317                     {
12318                       sym_value = sym->st_value;
12319                       if (sym_sec != NULL
12320                           && sym_sec->output_section != NULL)
12321                         ok_dest = TRUE;
12322                     }
12323                   else if (hash->elf.root.type == bfd_link_hash_defined
12324                            || hash->elf.root.type == bfd_link_hash_defweak)
12325                     {
12326                       sym_value = hash->elf.root.u.def.value;
12327                       if (sym_sec->output_section != NULL)
12328                         ok_dest = TRUE;
12329                     }
12330                   else if (hash->elf.root.type == bfd_link_hash_undefweak
12331                            || hash->elf.root.type == bfd_link_hash_undefined)
12332                     {
12333                       /* Recognise an old ABI func code entry sym, and
12334                          use the func descriptor sym instead if it is
12335                          defined.  */
12336                       if (hash->elf.root.root.string[0] == '.'
12337                           && (fdh = lookup_fdh (hash, htab)) != NULL)
12338                         {
12339                           if (fdh->elf.root.type == bfd_link_hash_defined
12340                               || fdh->elf.root.type == bfd_link_hash_defweak)
12341                             {
12342                               sym_sec = fdh->elf.root.u.def.section;
12343                               sym_value = fdh->elf.root.u.def.value;
12344                               if (sym_sec->output_section != NULL)
12345                                 ok_dest = TRUE;
12346                             }
12347                           else
12348                             fdh = NULL;
12349                         }
12350                     }
12351                   else
12352                     {
12353                       bfd_set_error (bfd_error_bad_value);
12354                       goto error_ret_free_internal;
12355                     }
12356
12357                   destination = 0;
12358                   local_off = 0;
12359                   if (ok_dest)
12360                     {
12361                       sym_value += irela->r_addend;
12362                       destination = (sym_value
12363                                      + sym_sec->output_offset
12364                                      + sym_sec->output_section->vma);
12365                       local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12366                                                             ? hash->elf.other
12367                                                             : sym->st_other);
12368                     }
12369
12370                   code_sec = sym_sec;
12371                   code_value = sym_value;
12372                   opd = get_opd_info (sym_sec);
12373                   if (opd != NULL)
12374                     {
12375                       bfd_vma dest;
12376
12377                       if (hash == NULL && opd->adjust != NULL)
12378                         {
12379                           long adjust = opd->adjust[OPD_NDX (sym_value)];
12380                           if (adjust == -1)
12381                             continue;
12382                           code_value += adjust;
12383                           sym_value += adjust;
12384                         }
12385                       dest = opd_entry_value (sym_sec, sym_value,
12386                                               &code_sec, &code_value, FALSE);
12387                       if (dest != (bfd_vma) -1)
12388                         {
12389                           destination = dest;
12390                           if (fdh != NULL)
12391                             {
12392                               /* Fixup old ABI sym to point at code
12393                                  entry.  */
12394                               hash->elf.root.type = bfd_link_hash_defweak;
12395                               hash->elf.root.u.def.section = code_sec;
12396                               hash->elf.root.u.def.value = code_value;
12397                             }
12398                         }
12399                     }
12400
12401                   /* Determine what (if any) linker stub is needed.  */
12402                   plt_ent = NULL;
12403                   stub_type = ppc_type_of_stub (section, irela, &hash,
12404                                                 &plt_ent, destination,
12405                                                 local_off);
12406
12407                   if (stub_type != ppc_stub_plt_call)
12408                     {
12409                       /* Check whether we need a TOC adjusting stub.
12410                          Since the linker pastes together pieces from
12411                          different object files when creating the
12412                          _init and _fini functions, it may be that a
12413                          call to what looks like a local sym is in
12414                          fact a call needing a TOC adjustment.  */
12415                       if (code_sec != NULL
12416                           && code_sec->output_section != NULL
12417                           && (htab->sec_info[code_sec->id].toc_off
12418                               != htab->sec_info[section->id].toc_off)
12419                           && (code_sec->has_toc_reloc
12420                               || code_sec->makes_toc_func_call))
12421                         stub_type = ppc_stub_long_branch_r2off;
12422                     }
12423
12424                   if (stub_type == ppc_stub_none)
12425                     continue;
12426
12427                   /* __tls_get_addr calls might be eliminated.  */
12428                   if (stub_type != ppc_stub_plt_call
12429                       && hash != NULL
12430                       && (hash == htab->tls_get_addr
12431                           || hash == htab->tls_get_addr_fd)
12432                       && section->has_tls_reloc
12433                       && irela != internal_relocs)
12434                     {
12435                       /* Get tls info.  */
12436                       unsigned char *tls_mask;
12437
12438                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12439                                          irela - 1, input_bfd))
12440                         goto error_ret_free_internal;
12441                       if (*tls_mask != 0)
12442                         continue;
12443                     }
12444
12445                   if (stub_type == ppc_stub_plt_call
12446                       && irela + 1 < irelaend
12447                       && irela[1].r_offset == irela->r_offset + 4
12448                       && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12449                     {
12450                       if (!tocsave_find (htab, INSERT,
12451                                          &local_syms, irela + 1, input_bfd))
12452                         goto error_ret_free_internal;
12453                     }
12454                   else if (stub_type == ppc_stub_plt_call)
12455                     stub_type = ppc_stub_plt_call_r2save;
12456
12457                   /* Support for grouping stub sections.  */
12458                   id_sec = htab->sec_info[section->id].u.group->link_sec;
12459
12460                   /* Get the name of this stub.  */
12461                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12462                   if (!stub_name)
12463                     goto error_ret_free_internal;
12464
12465                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12466                                                      stub_name, FALSE, FALSE);
12467                   if (stub_entry != NULL)
12468                     {
12469                       /* The proper stub has already been created.  */
12470                       free (stub_name);
12471                       if (stub_type == ppc_stub_plt_call_r2save)
12472                         stub_entry->stub_type = stub_type;
12473                       continue;
12474                     }
12475
12476                   stub_entry = ppc_add_stub (stub_name, section, info);
12477                   if (stub_entry == NULL)
12478                     {
12479                       free (stub_name);
12480                     error_ret_free_internal:
12481                       if (elf_section_data (section)->relocs == NULL)
12482                         free (internal_relocs);
12483                     error_ret_free_local:
12484                       if (local_syms != NULL
12485                           && (symtab_hdr->contents
12486                               != (unsigned char *) local_syms))
12487                         free (local_syms);
12488                       return FALSE;
12489                     }
12490
12491                   stub_entry->stub_type = stub_type;
12492                   if (stub_type != ppc_stub_plt_call
12493                       && stub_type != ppc_stub_plt_call_r2save)
12494                     {
12495                       stub_entry->target_value = code_value;
12496                       stub_entry->target_section = code_sec;
12497                     }
12498                   else
12499                     {
12500                       stub_entry->target_value = sym_value;
12501                       stub_entry->target_section = sym_sec;
12502                     }
12503                   stub_entry->h = hash;
12504                   stub_entry->plt_ent = plt_ent;
12505                   stub_entry->other = hash ? hash->elf.other : sym->st_other;
12506
12507                   if (stub_entry->h != NULL)
12508                     htab->stub_globals += 1;
12509                 }
12510
12511               /* We're done with the internal relocs, free them.  */
12512               if (elf_section_data (section)->relocs != internal_relocs)
12513                 free (internal_relocs);
12514             }
12515
12516           if (local_syms != NULL
12517               && symtab_hdr->contents != (unsigned char *) local_syms)
12518             {
12519               if (!info->keep_memory)
12520                 free (local_syms);
12521               else
12522                 symtab_hdr->contents = (unsigned char *) local_syms;
12523             }
12524         }
12525
12526       /* We may have added some stubs.  Find out the new size of the
12527          stub sections.  */
12528       for (stub_sec = htab->params->stub_bfd->sections;
12529            stub_sec != NULL;
12530            stub_sec = stub_sec->next)
12531         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12532           {
12533             stub_sec->rawsize = stub_sec->size;
12534             stub_sec->size = 0;
12535             stub_sec->reloc_count = 0;
12536             stub_sec->flags &= ~SEC_RELOC;
12537           }
12538
12539       htab->brlt->size = 0;
12540       htab->brlt->reloc_count = 0;
12541       htab->brlt->flags &= ~SEC_RELOC;
12542       if (htab->relbrlt != NULL)
12543         htab->relbrlt->size = 0;
12544
12545       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12546
12547       for (group = htab->group; group != NULL; group = group->next)
12548         if (group->needs_save_res)
12549           group->stub_sec->size += htab->sfpr->size;
12550
12551       if (info->emitrelocations
12552           && htab->glink != NULL && htab->glink->size != 0)
12553         {
12554           htab->glink->reloc_count = 1;
12555           htab->glink->flags |= SEC_RELOC;
12556         }
12557
12558       if (htab->glink_eh_frame != NULL
12559           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12560           && htab->glink_eh_frame->output_section->size != 0)
12561         {
12562           size_t size = 0, align;
12563
12564           for (stub_sec = htab->params->stub_bfd->sections;
12565                stub_sec != NULL;
12566                stub_sec = stub_sec->next)
12567             if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12568               size += 24;
12569           if (htab->glink != NULL && htab->glink->size != 0)
12570             size += 24;
12571           if (size != 0)
12572             size += sizeof (glink_eh_frame_cie);
12573           align = 1;
12574           align <<= htab->glink_eh_frame->output_section->alignment_power;
12575           align -= 1;
12576           size = (size + align) & ~align;
12577           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12578           htab->glink_eh_frame->size = size;
12579         }
12580
12581       if (htab->params->plt_stub_align != 0)
12582         for (stub_sec = htab->params->stub_bfd->sections;
12583              stub_sec != NULL;
12584              stub_sec = stub_sec->next)
12585           if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12586             stub_sec->size = ((stub_sec->size
12587                                + (1 << htab->params->plt_stub_align) - 1)
12588                               & -(1 << htab->params->plt_stub_align));
12589
12590       for (stub_sec = htab->params->stub_bfd->sections;
12591            stub_sec != NULL;
12592            stub_sec = stub_sec->next)
12593         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12594             && stub_sec->rawsize != stub_sec->size
12595             && (htab->stub_iteration <= STUB_SHRINK_ITER
12596                 || stub_sec->rawsize < stub_sec->size))
12597           break;
12598
12599       if (stub_sec == NULL
12600           && (htab->glink_eh_frame == NULL
12601               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12602         break;
12603
12604       /* Ask the linker to do its stuff.  */
12605       (*htab->params->layout_sections_again) ();
12606     }
12607
12608   if (htab->glink_eh_frame != NULL
12609       && htab->glink_eh_frame->size != 0)
12610     {
12611       bfd_vma val;
12612       bfd_byte *p, *last_fde;
12613       size_t last_fde_len, size, align, pad;
12614       asection *stub_sec;
12615
12616       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12617       if (p == NULL)
12618         return FALSE;
12619       htab->glink_eh_frame->contents = p;
12620       last_fde = p;
12621
12622       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12623       /* CIE length (rewrite in case little-endian).  */
12624       last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12625       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12626       p += sizeof (glink_eh_frame_cie);
12627
12628       for (stub_sec = htab->params->stub_bfd->sections;
12629            stub_sec != NULL;
12630            stub_sec = stub_sec->next)
12631         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12632           {
12633             last_fde = p;
12634             last_fde_len = 20;
12635             /* FDE length.  */
12636             bfd_put_32 (htab->elf.dynobj, 20, p);
12637             p += 4;
12638             /* CIE pointer.  */
12639             val = p - htab->glink_eh_frame->contents;
12640             bfd_put_32 (htab->elf.dynobj, val, p);
12641             p += 4;
12642             /* Offset to stub section, written later.  */
12643             p += 4;
12644             /* stub section size.  */
12645             bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
12646             p += 4;
12647             /* Augmentation.  */
12648             p += 1;
12649             /* Pad.  */
12650             p += 7;
12651           }
12652       if (htab->glink != NULL && htab->glink->size != 0)
12653         {
12654           last_fde = p;
12655           last_fde_len = 20;
12656           /* FDE length.  */
12657           bfd_put_32 (htab->elf.dynobj, 20, p);
12658           p += 4;
12659           /* CIE pointer.  */
12660           val = p - htab->glink_eh_frame->contents;
12661           bfd_put_32 (htab->elf.dynobj, val, p);
12662           p += 4;
12663           /* Offset to .glink, written later.  */
12664           p += 4;
12665           /* .glink size.  */
12666           bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12667           p += 4;
12668           /* Augmentation.  */
12669           p += 1;
12670
12671           *p++ = DW_CFA_advance_loc + 1;
12672           *p++ = DW_CFA_register;
12673           *p++ = 65;
12674           *p++ = htab->opd_abi ? 12 : 0;
12675           *p++ = DW_CFA_advance_loc + 4;
12676           *p++ = DW_CFA_restore_extended;
12677           *p++ = 65;
12678         }
12679       /* Subsume any padding into the last FDE if user .eh_frame
12680          sections are aligned more than glink_eh_frame.  Otherwise any
12681          zero padding will be seen as a terminator.  */
12682       size = p - htab->glink_eh_frame->contents;
12683       align = 1;
12684       align <<= htab->glink_eh_frame->output_section->alignment_power;
12685       align -= 1;
12686       pad = ((size + align) & ~align) - size;
12687       htab->glink_eh_frame->size = size + pad;
12688       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12689     }
12690
12691   maybe_strip_output (info, htab->brlt);
12692   if (htab->glink_eh_frame != NULL)
12693     maybe_strip_output (info, htab->glink_eh_frame);
12694
12695   return TRUE;
12696 }
12697
12698 /* Called after we have determined section placement.  If sections
12699    move, we'll be called again.  Provide a value for TOCstart.  */
12700
12701 bfd_vma
12702 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12703 {
12704   asection *s;
12705   bfd_vma TOCstart, adjust;
12706
12707   if (info != NULL)
12708     {
12709       struct elf_link_hash_entry *h;
12710       struct elf_link_hash_table *htab = elf_hash_table (info);
12711
12712       if (is_elf_hash_table (htab)
12713           && htab->hgot != NULL)
12714         h = htab->hgot;
12715       else
12716         {
12717           h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12718           if (is_elf_hash_table (htab))
12719             htab->hgot = h;
12720         }
12721       if (h != NULL
12722           && h->root.type == bfd_link_hash_defined
12723           && !h->root.linker_def
12724           && (!is_elf_hash_table (htab)
12725               || h->def_regular))
12726         {
12727           TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12728                       + h->root.u.def.section->output_offset
12729                       + h->root.u.def.section->output_section->vma);
12730           _bfd_set_gp_value (obfd, TOCstart);
12731           return TOCstart;
12732         }
12733     }
12734
12735   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12736      order.  The TOC starts where the first of these sections starts.  */
12737   s = bfd_get_section_by_name (obfd, ".got");
12738   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12739     s = bfd_get_section_by_name (obfd, ".toc");
12740   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12741     s = bfd_get_section_by_name (obfd, ".tocbss");
12742   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12743     s = bfd_get_section_by_name (obfd, ".plt");
12744   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12745     {
12746       /* This may happen for
12747          o  references to TOC base (SYM@toc / TOC[tc0]) without a
12748          .toc directive
12749          o  bad linker script
12750          o --gc-sections and empty TOC sections
12751
12752          FIXME: Warn user?  */
12753
12754       /* Look for a likely section.  We probably won't even be
12755          using TOCstart.  */
12756       for (s = obfd->sections; s != NULL; s = s->next)
12757         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12758                          | SEC_EXCLUDE))
12759             == (SEC_ALLOC | SEC_SMALL_DATA))
12760           break;
12761       if (s == NULL)
12762         for (s = obfd->sections; s != NULL; s = s->next)
12763           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12764               == (SEC_ALLOC | SEC_SMALL_DATA))
12765             break;
12766       if (s == NULL)
12767         for (s = obfd->sections; s != NULL; s = s->next)
12768           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12769               == SEC_ALLOC)
12770             break;
12771       if (s == NULL)
12772         for (s = obfd->sections; s != NULL; s = s->next)
12773           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12774             break;
12775     }
12776
12777   TOCstart = 0;
12778   if (s != NULL)
12779     TOCstart = s->output_section->vma + s->output_offset;
12780
12781   /* Force alignment.  */
12782   adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12783   TOCstart -= adjust;
12784   _bfd_set_gp_value (obfd, TOCstart);
12785
12786   if (info != NULL && s != NULL)
12787     {
12788       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12789
12790       if (htab != NULL)
12791         {
12792           if (htab->elf.hgot != NULL)
12793             {
12794               htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
12795               htab->elf.hgot->root.u.def.section = s;
12796             }
12797         }
12798       else
12799         {
12800           struct bfd_link_hash_entry *bh = NULL;
12801           _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
12802                                             s, TOC_BASE_OFF - adjust,
12803                                             NULL, FALSE, FALSE, &bh);
12804         }
12805     }
12806   return TOCstart;
12807 }
12808
12809 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12810    write out any global entry stubs.  */
12811
12812 static bfd_boolean
12813 build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12814 {
12815   struct bfd_link_info *info;
12816   struct ppc_link_hash_table *htab;
12817   struct plt_entry *pent;
12818   asection *s;
12819
12820   if (h->root.type == bfd_link_hash_indirect)
12821     return TRUE;
12822
12823   if (!h->pointer_equality_needed)
12824     return TRUE;
12825
12826   if (h->def_regular)
12827     return TRUE;
12828
12829   info = inf;
12830   htab = ppc_hash_table (info);
12831   if (htab == NULL)
12832     return FALSE;
12833
12834   s = htab->glink;
12835   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12836     if (pent->plt.offset != (bfd_vma) -1
12837         && pent->addend == 0)
12838       {
12839         bfd_byte *p;
12840         asection *plt;
12841         bfd_vma off;
12842
12843         p = s->contents + h->root.u.def.value;
12844         plt = htab->elf.splt;
12845         if (!htab->elf.dynamic_sections_created
12846             || h->dynindx == -1)
12847           plt = htab->elf.iplt;
12848         off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12849         off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12850
12851         if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12852           {
12853             info->callbacks->einfo
12854               (_("%P: linkage table error against `%T'\n"),
12855                h->root.root.string);
12856             bfd_set_error (bfd_error_bad_value);
12857             htab->stub_error = TRUE;
12858           }
12859
12860         htab->stub_count[ppc_stub_global_entry - 1] += 1;
12861         if (htab->params->emit_stub_syms)
12862           {
12863             size_t len = strlen (h->root.root.string);
12864             char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12865
12866             if (name == NULL)
12867               return FALSE;
12868
12869             sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12870             h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12871             if (h == NULL)
12872               return FALSE;
12873             if (h->root.type == bfd_link_hash_new)
12874               {
12875                 h->root.type = bfd_link_hash_defined;
12876                 h->root.u.def.section = s;
12877                 h->root.u.def.value = p - s->contents;
12878                 h->ref_regular = 1;
12879                 h->def_regular = 1;
12880                 h->ref_regular_nonweak = 1;
12881                 h->forced_local = 1;
12882                 h->non_elf = 0;
12883                 h->root.linker_def = 1;
12884               }
12885           }
12886
12887         if (PPC_HA (off) != 0)
12888           {
12889             bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12890             p += 4;
12891           }
12892         bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12893         p += 4;
12894         bfd_put_32 (s->owner, MTCTR_R12, p);
12895         p += 4;
12896         bfd_put_32 (s->owner, BCTR, p);
12897         break;
12898       }
12899   return TRUE;
12900 }
12901
12902 /* Build all the stubs associated with the current output file.
12903    The stubs are kept in a hash table attached to the main linker
12904    hash table.  This function is called via gldelf64ppc_finish.  */
12905
12906 bfd_boolean
12907 ppc64_elf_build_stubs (struct bfd_link_info *info,
12908                        char **stats)
12909 {
12910   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12911   struct map_stub *group;
12912   asection *stub_sec;
12913   bfd_byte *p;
12914   int stub_sec_count = 0;
12915
12916   if (htab == NULL)
12917     return FALSE;
12918
12919   /* Allocate memory to hold the linker stubs.  */
12920   for (stub_sec = htab->params->stub_bfd->sections;
12921        stub_sec != NULL;
12922        stub_sec = stub_sec->next)
12923     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12924         && stub_sec->size != 0)
12925       {
12926         stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
12927         if (stub_sec->contents == NULL)
12928           return FALSE;
12929         stub_sec->size = 0;
12930       }
12931
12932   if (htab->glink != NULL && htab->glink->size != 0)
12933     {
12934       unsigned int indx;
12935       bfd_vma plt0;
12936
12937       /* Build the .glink plt call stub.  */
12938       if (htab->params->emit_stub_syms)
12939         {
12940           struct elf_link_hash_entry *h;
12941           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12942                                     TRUE, FALSE, FALSE);
12943           if (h == NULL)
12944             return FALSE;
12945           if (h->root.type == bfd_link_hash_new)
12946             {
12947               h->root.type = bfd_link_hash_defined;
12948               h->root.u.def.section = htab->glink;
12949               h->root.u.def.value = 8;
12950               h->ref_regular = 1;
12951               h->def_regular = 1;
12952               h->ref_regular_nonweak = 1;
12953               h->forced_local = 1;
12954               h->non_elf = 0;
12955               h->root.linker_def = 1;
12956             }
12957         }
12958       plt0 = (htab->elf.splt->output_section->vma
12959               + htab->elf.splt->output_offset
12960               - 16);
12961       if (info->emitrelocations)
12962         {
12963           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12964           if (r == NULL)
12965             return FALSE;
12966           r->r_offset = (htab->glink->output_offset
12967                          + htab->glink->output_section->vma);
12968           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12969           r->r_addend = plt0;
12970         }
12971       p = htab->glink->contents;
12972       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
12973       bfd_put_64 (htab->glink->owner, plt0, p);
12974       p += 8;
12975       if (htab->opd_abi)
12976         {
12977           bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12978           p += 4;
12979           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12980           p += 4;
12981           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12982           p += 4;
12983           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12984           p += 4;
12985           bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12986           p += 4;
12987           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12988           p += 4;
12989           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12990           p += 4;
12991           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12992           p += 4;
12993           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12994           p += 4;
12995           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12996           p += 4;
12997         }
12998       else
12999         {
13000           bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13001           p += 4;
13002           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13003           p += 4;
13004           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13005           p += 4;
13006           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13007           p += 4;
13008           bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13009           p += 4;
13010           bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13011           p += 4;
13012           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13013           p += 4;
13014           bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13015           p += 4;
13016           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13017           p += 4;
13018           bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13019           p += 4;
13020           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13021           p += 4;
13022           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13023           p += 4;
13024         }
13025       bfd_put_32 (htab->glink->owner, BCTR, p);
13026       p += 4;
13027       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
13028         {
13029           bfd_put_32 (htab->glink->owner, NOP, p);
13030           p += 4;
13031         }
13032
13033       /* Build the .glink lazy link call stubs.  */
13034       indx = 0;
13035       while (p < htab->glink->contents + htab->glink->rawsize)
13036         {
13037           if (htab->opd_abi)
13038             {
13039               if (indx < 0x8000)
13040                 {
13041                   bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13042                   p += 4;
13043                 }
13044               else
13045                 {
13046                   bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13047                   p += 4;
13048                   bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13049                               p);
13050                   p += 4;
13051                 }
13052             }
13053           bfd_put_32 (htab->glink->owner,
13054                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13055           indx++;
13056           p += 4;
13057         }
13058
13059       /* Build .glink global entry stubs.  */
13060       if (htab->glink->size > htab->glink->rawsize)
13061         elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
13062     }
13063
13064   if (htab->brlt != NULL && htab->brlt->size != 0)
13065     {
13066       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13067                                          htab->brlt->size);
13068       if (htab->brlt->contents == NULL)
13069         return FALSE;
13070     }
13071   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13072     {
13073       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13074                                             htab->relbrlt->size);
13075       if (htab->relbrlt->contents == NULL)
13076         return FALSE;
13077     }
13078
13079   /* Build the stubs as directed by the stub hash table.  */
13080   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13081
13082   for (group = htab->group; group != NULL; group = group->next)
13083     if (group->needs_save_res)
13084       {
13085         stub_sec = group->stub_sec;
13086         memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13087                 htab->sfpr->size);
13088         if (htab->params->emit_stub_syms)
13089           {
13090             unsigned int i;
13091
13092             for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13093               if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13094                 return FALSE;
13095           }
13096         stub_sec->size += htab->sfpr->size;
13097       }
13098
13099   if (htab->relbrlt != NULL)
13100     htab->relbrlt->reloc_count = 0;
13101
13102   if (htab->params->plt_stub_align != 0)
13103     for (stub_sec = htab->params->stub_bfd->sections;
13104          stub_sec != NULL;
13105          stub_sec = stub_sec->next)
13106       if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13107         stub_sec->size = ((stub_sec->size
13108                            + (1 << htab->params->plt_stub_align) - 1)
13109                           & -(1 << htab->params->plt_stub_align));
13110
13111   for (stub_sec = htab->params->stub_bfd->sections;
13112        stub_sec != NULL;
13113        stub_sec = stub_sec->next)
13114     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13115       {
13116         stub_sec_count += 1;
13117         if (stub_sec->rawsize != stub_sec->size
13118             && (htab->stub_iteration <= STUB_SHRINK_ITER
13119                 || stub_sec->rawsize < stub_sec->size))
13120           break;
13121       }
13122
13123   /* Note that the glink_eh_frame check here is not only testing that
13124      the generated size matched the calculated size but also that
13125      bfd_elf_discard_info didn't make any changes to the section.  */
13126   if (stub_sec != NULL
13127       || (htab->glink_eh_frame != NULL
13128           && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
13129     {
13130       htab->stub_error = TRUE;
13131       info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
13132     }
13133
13134   if (htab->stub_error)
13135     return FALSE;
13136
13137   if (stats != NULL)
13138     {
13139       *stats = bfd_malloc (500);
13140       if (*stats == NULL)
13141         return FALSE;
13142
13143       sprintf (*stats, _("linker stubs in %u group%s\n"
13144                          "  branch       %lu\n"
13145                          "  toc adjust   %lu\n"
13146                          "  long branch  %lu\n"
13147                          "  long toc adj %lu\n"
13148                          "  plt call     %lu\n"
13149                          "  plt call toc %lu\n"
13150                          "  global entry %lu"),
13151                stub_sec_count,
13152                stub_sec_count == 1 ? "" : "s",
13153                htab->stub_count[ppc_stub_long_branch - 1],
13154                htab->stub_count[ppc_stub_long_branch_r2off - 1],
13155                htab->stub_count[ppc_stub_plt_branch - 1],
13156                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13157                htab->stub_count[ppc_stub_plt_call - 1],
13158                htab->stub_count[ppc_stub_plt_call_r2save - 1],
13159                htab->stub_count[ppc_stub_global_entry - 1]);
13160     }
13161   return TRUE;
13162 }
13163
13164 /* This function undoes the changes made by add_symbol_adjust.  */
13165
13166 static bfd_boolean
13167 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
13168 {
13169   struct ppc_link_hash_entry *eh;
13170
13171   if (h->root.type == bfd_link_hash_indirect)
13172     return TRUE;
13173
13174   eh = (struct ppc_link_hash_entry *) h;
13175   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
13176     return TRUE;
13177
13178   eh->elf.root.type = bfd_link_hash_undefined;
13179   return TRUE;
13180 }
13181
13182 void
13183 ppc64_elf_restore_symbols (struct bfd_link_info *info)
13184 {
13185   struct ppc_link_hash_table *htab = ppc_hash_table (info);
13186
13187   if (htab != NULL)
13188     elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
13189 }
13190
13191 /* What to do when ld finds relocations against symbols defined in
13192    discarded sections.  */
13193
13194 static unsigned int
13195 ppc64_elf_action_discarded (asection *sec)
13196 {
13197   if (strcmp (".opd", sec->name) == 0)
13198     return 0;
13199
13200   if (strcmp (".toc", sec->name) == 0)
13201     return 0;
13202
13203   if (strcmp (".toc1", sec->name) == 0)
13204     return 0;
13205
13206   return _bfd_elf_default_action_discarded (sec);
13207 }
13208
13209 /* The RELOCATE_SECTION function is called by the ELF backend linker
13210    to handle the relocations for a section.
13211
13212    The relocs are always passed as Rela structures; if the section
13213    actually uses Rel structures, the r_addend field will always be
13214    zero.
13215
13216    This function is responsible for adjust the section contents as
13217    necessary, and (if using Rela relocs and generating a
13218    relocatable output file) adjusting the reloc addend as
13219    necessary.
13220
13221    This function does not have to worry about setting the reloc
13222    address or the reloc symbol index.
13223
13224    LOCAL_SYMS is a pointer to the swapped in local symbols.
13225
13226    LOCAL_SECTIONS is an array giving the section in the input file
13227    corresponding to the st_shndx field of each local symbol.
13228
13229    The global hash table entry for the global symbols can be found
13230    via elf_sym_hashes (input_bfd).
13231
13232    When generating relocatable output, this function must handle
13233    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
13234    going to be the section symbol corresponding to the output
13235    section, which means that the addend must be adjusted
13236    accordingly.  */
13237
13238 static bfd_boolean
13239 ppc64_elf_relocate_section (bfd *output_bfd,
13240                             struct bfd_link_info *info,
13241                             bfd *input_bfd,
13242                             asection *input_section,
13243                             bfd_byte *contents,
13244                             Elf_Internal_Rela *relocs,
13245                             Elf_Internal_Sym *local_syms,
13246                             asection **local_sections)
13247 {
13248   struct ppc_link_hash_table *htab;
13249   Elf_Internal_Shdr *symtab_hdr;
13250   struct elf_link_hash_entry **sym_hashes;
13251   Elf_Internal_Rela *rel;
13252   Elf_Internal_Rela *wrel;
13253   Elf_Internal_Rela *relend;
13254   Elf_Internal_Rela outrel;
13255   bfd_byte *loc;
13256   struct got_entry **local_got_ents;
13257   bfd_vma TOCstart;
13258   bfd_boolean ret = TRUE;
13259   bfd_boolean is_opd;
13260   /* Assume 'at' branch hints.  */
13261   bfd_boolean is_isa_v2 = TRUE;
13262   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
13263
13264   /* Initialize howto table if needed.  */
13265   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13266     ppc_howto_init ();
13267
13268   htab = ppc_hash_table (info);
13269   if (htab == NULL)
13270     return FALSE;
13271
13272   /* Don't relocate stub sections.  */
13273   if (input_section->owner == htab->params->stub_bfd)
13274     return TRUE;
13275
13276   BFD_ASSERT (is_ppc64_elf (input_bfd));
13277
13278   local_got_ents = elf_local_got_ents (input_bfd);
13279   TOCstart = elf_gp (output_bfd);
13280   symtab_hdr = &elf_symtab_hdr (input_bfd);
13281   sym_hashes = elf_sym_hashes (input_bfd);
13282   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13283
13284   rel = wrel = relocs;
13285   relend = relocs + input_section->reloc_count;
13286   for (; rel < relend; wrel++, rel++)
13287     {
13288       enum elf_ppc64_reloc_type r_type;
13289       bfd_vma addend;
13290       bfd_reloc_status_type r;
13291       Elf_Internal_Sym *sym;
13292       asection *sec;
13293       struct elf_link_hash_entry *h_elf;
13294       struct ppc_link_hash_entry *h;
13295       struct ppc_link_hash_entry *fdh;
13296       const char *sym_name;
13297       unsigned long r_symndx, toc_symndx;
13298       bfd_vma toc_addend;
13299       unsigned char tls_mask, tls_gd, tls_type;
13300       unsigned char sym_type;
13301       bfd_vma relocation;
13302       bfd_boolean unresolved_reloc;
13303       bfd_boolean warned;
13304       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13305       unsigned int insn;
13306       unsigned int mask;
13307       struct ppc_stub_hash_entry *stub_entry;
13308       bfd_vma max_br_offset;
13309       bfd_vma from;
13310       Elf_Internal_Rela orig_rel;
13311       reloc_howto_type *howto;
13312       struct reloc_howto_struct alt_howto;
13313
13314     again:
13315       orig_rel = *rel;
13316
13317       r_type = ELF64_R_TYPE (rel->r_info);
13318       r_symndx = ELF64_R_SYM (rel->r_info);
13319
13320       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13321          symbol of the previous ADDR64 reloc.  The symbol gives us the
13322          proper TOC base to use.  */
13323       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13324           && wrel != relocs
13325           && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13326           && is_opd)
13327         r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13328
13329       sym = NULL;
13330       sec = NULL;
13331       h_elf = NULL;
13332       sym_name = NULL;
13333       unresolved_reloc = FALSE;
13334       warned = FALSE;
13335
13336       if (r_symndx < symtab_hdr->sh_info)
13337         {
13338           /* It's a local symbol.  */
13339           struct _opd_sec_data *opd;
13340
13341           sym = local_syms + r_symndx;
13342           sec = local_sections[r_symndx];
13343           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13344           sym_type = ELF64_ST_TYPE (sym->st_info);
13345           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13346           opd = get_opd_info (sec);
13347           if (opd != NULL && opd->adjust != NULL)
13348             {
13349               long adjust = opd->adjust[OPD_NDX (sym->st_value
13350                                                  + rel->r_addend)];
13351               if (adjust == -1)
13352                 relocation = 0;
13353               else
13354                 {
13355                   /* If this is a relocation against the opd section sym
13356                      and we have edited .opd, adjust the reloc addend so
13357                      that ld -r and ld --emit-relocs output is correct.
13358                      If it is a reloc against some other .opd symbol,
13359                      then the symbol value will be adjusted later.  */
13360                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13361                     rel->r_addend += adjust;
13362                   else
13363                     relocation += adjust;
13364                 }
13365             }
13366         }
13367       else
13368         {
13369           bfd_boolean ignored;
13370
13371           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13372                                    r_symndx, symtab_hdr, sym_hashes,
13373                                    h_elf, sec, relocation,
13374                                    unresolved_reloc, warned, ignored);
13375           sym_name = h_elf->root.root.string;
13376           sym_type = h_elf->type;
13377           if (sec != NULL
13378               && sec->owner == output_bfd
13379               && strcmp (sec->name, ".opd") == 0)
13380             {
13381               /* This is a symbol defined in a linker script.  All
13382                  such are defined in output sections, even those
13383                  defined by simple assignment from a symbol defined in
13384                  an input section.  Transfer the symbol to an
13385                  appropriate input .opd section, so that a branch to
13386                  this symbol will be mapped to the location specified
13387                  by the opd entry.  */
13388               struct bfd_link_order *lo;
13389               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13390                 if (lo->type == bfd_indirect_link_order)
13391                   {
13392                     asection *isec = lo->u.indirect.section;
13393                     if (h_elf->root.u.def.value >= isec->output_offset
13394                         && h_elf->root.u.def.value < (isec->output_offset
13395                                                       + isec->size))
13396                       {
13397                         h_elf->root.u.def.value -= isec->output_offset;
13398                         h_elf->root.u.def.section = isec;
13399                         sec = isec;
13400                         break;
13401                       }
13402                   }
13403             }
13404         }
13405       h = (struct ppc_link_hash_entry *) h_elf;
13406
13407       if (sec != NULL && discarded_section (sec))
13408         {
13409           _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13410                                input_bfd, input_section,
13411                                contents + rel->r_offset);
13412           wrel->r_offset = rel->r_offset;
13413           wrel->r_info = 0;
13414           wrel->r_addend = 0;
13415
13416           /* For ld -r, remove relocations in debug sections against
13417              sections defined in discarded sections.  Not done for
13418              non-debug to preserve relocs in .eh_frame which the
13419              eh_frame editing code expects to be present.  */
13420           if (bfd_link_relocatable (info)
13421               && (input_section->flags & SEC_DEBUGGING))
13422             wrel--;
13423
13424           continue;
13425         }
13426
13427       if (bfd_link_relocatable (info))
13428         goto copy_reloc;
13429
13430       if (h != NULL && &h->elf == htab->elf.hgot)
13431         {
13432           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13433           sec = bfd_abs_section_ptr;
13434           unresolved_reloc = FALSE;
13435         }
13436
13437       /* TLS optimizations.  Replace instruction sequences and relocs
13438          based on information we collected in tls_optimize.  We edit
13439          RELOCS so that --emit-relocs will output something sensible
13440          for the final instruction stream.  */
13441       tls_mask = 0;
13442       tls_gd = 0;
13443       toc_symndx = 0;
13444       if (h != NULL)
13445         tls_mask = h->tls_mask;
13446       else if (local_got_ents != NULL)
13447         {
13448           struct plt_entry **local_plt = (struct plt_entry **)
13449             (local_got_ents + symtab_hdr->sh_info);
13450           unsigned char *lgot_masks = (unsigned char *)
13451             (local_plt + symtab_hdr->sh_info);
13452           tls_mask = lgot_masks[r_symndx];
13453         }
13454       if (tls_mask == 0
13455           && (r_type == R_PPC64_TLS
13456               || r_type == R_PPC64_TLSGD
13457               || r_type == R_PPC64_TLSLD))
13458         {
13459           /* Check for toc tls entries.  */
13460           unsigned char *toc_tls;
13461
13462           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13463                              &local_syms, rel, input_bfd))
13464             return FALSE;
13465
13466           if (toc_tls)
13467             tls_mask = *toc_tls;
13468         }
13469
13470       /* Check that tls relocs are used with tls syms, and non-tls
13471          relocs are used with non-tls syms.  */
13472       if (r_symndx != STN_UNDEF
13473           && r_type != R_PPC64_NONE
13474           && (h == NULL
13475               || h->elf.root.type == bfd_link_hash_defined
13476               || h->elf.root.type == bfd_link_hash_defweak)
13477           && (IS_PPC64_TLS_RELOC (r_type)
13478               != (sym_type == STT_TLS
13479                   || (sym_type == STT_SECTION
13480                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13481         {
13482           if (tls_mask != 0
13483               && (r_type == R_PPC64_TLS
13484                   || r_type == R_PPC64_TLSGD
13485                   || r_type == R_PPC64_TLSLD))
13486             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
13487             ;
13488           else
13489             info->callbacks->einfo
13490               (!IS_PPC64_TLS_RELOC (r_type)
13491                ? _("%P: %H: %s used with TLS symbol `%T'\n")
13492                : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
13493                input_bfd, input_section, rel->r_offset,
13494                ppc64_elf_howto_table[r_type]->name,
13495                sym_name);
13496         }
13497
13498       /* Ensure reloc mapping code below stays sane.  */
13499       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13500           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13501           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
13502           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13503           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13504           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13505           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
13506           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13507           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13508           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13509         abort ();
13510
13511       switch (r_type)
13512         {
13513         default:
13514           break;
13515
13516         case R_PPC64_LO_DS_OPT:
13517           insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
13518           if ((insn & (0x3f << 26)) != 58u << 26)
13519             abort ();
13520           insn += (14u << 26) - (58u << 26);
13521           bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
13522           r_type = R_PPC64_TOC16_LO;
13523           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13524           break;
13525
13526         case R_PPC64_TOC16:
13527         case R_PPC64_TOC16_LO:
13528         case R_PPC64_TOC16_DS:
13529         case R_PPC64_TOC16_LO_DS:
13530           {
13531             /* Check for toc tls entries.  */
13532             unsigned char *toc_tls;
13533             int retval;
13534
13535             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13536                                    &local_syms, rel, input_bfd);
13537             if (retval == 0)
13538               return FALSE;
13539
13540             if (toc_tls)
13541               {
13542                 tls_mask = *toc_tls;
13543                 if (r_type == R_PPC64_TOC16_DS
13544                     || r_type == R_PPC64_TOC16_LO_DS)
13545                   {
13546                     if (tls_mask != 0
13547                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13548                       goto toctprel;
13549                   }
13550                 else
13551                   {
13552                     /* If we found a GD reloc pair, then we might be
13553                        doing a GD->IE transition.  */
13554                     if (retval == 2)
13555                       {
13556                         tls_gd = TLS_TPRELGD;
13557                         if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13558                           goto tls_ldgd_opt;
13559                       }
13560                     else if (retval == 3)
13561                       {
13562                         if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13563                           goto tls_ldgd_opt;
13564                       }
13565                   }
13566               }
13567           }
13568           break;
13569
13570         case R_PPC64_GOT_TPREL16_HI:
13571         case R_PPC64_GOT_TPREL16_HA:
13572           if (tls_mask != 0
13573               && (tls_mask & TLS_TPREL) == 0)
13574             {
13575               rel->r_offset -= d_offset;
13576               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13577               r_type = R_PPC64_NONE;
13578               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13579             }
13580           break;
13581
13582         case R_PPC64_GOT_TPREL16_DS:
13583         case R_PPC64_GOT_TPREL16_LO_DS:
13584           if (tls_mask != 0
13585               && (tls_mask & TLS_TPREL) == 0)
13586             {
13587             toctprel:
13588               insn = bfd_get_32 (output_bfd,
13589                                  contents + rel->r_offset - d_offset);
13590               insn &= 31 << 21;
13591               insn |= 0x3c0d0000;       /* addis 0,13,0 */
13592               bfd_put_32 (output_bfd, insn,
13593                           contents + rel->r_offset - d_offset);
13594               r_type = R_PPC64_TPREL16_HA;
13595               if (toc_symndx != 0)
13596                 {
13597                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13598                   rel->r_addend = toc_addend;
13599                   /* We changed the symbol.  Start over in order to
13600                      get h, sym, sec etc. right.  */
13601                   goto again;
13602                 }
13603               else
13604                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13605             }
13606           break;
13607
13608         case R_PPC64_TLS:
13609           if (tls_mask != 0
13610               && (tls_mask & TLS_TPREL) == 0)
13611             {
13612               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
13613               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13614               if (insn == 0)
13615                 abort ();
13616               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
13617               /* Was PPC64_TLS which sits on insn boundary, now
13618                  PPC64_TPREL16_LO which is at low-order half-word.  */
13619               rel->r_offset += d_offset;
13620               r_type = R_PPC64_TPREL16_LO;
13621               if (toc_symndx != 0)
13622                 {
13623                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13624                   rel->r_addend = toc_addend;
13625                   /* We changed the symbol.  Start over in order to
13626                      get h, sym, sec etc. right.  */
13627                   goto again;
13628                 }
13629               else
13630                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13631             }
13632           break;
13633
13634         case R_PPC64_GOT_TLSGD16_HI:
13635         case R_PPC64_GOT_TLSGD16_HA:
13636           tls_gd = TLS_TPRELGD;
13637           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13638             goto tls_gdld_hi;
13639           break;
13640
13641         case R_PPC64_GOT_TLSLD16_HI:
13642         case R_PPC64_GOT_TLSLD16_HA:
13643           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13644             {
13645             tls_gdld_hi:
13646               if ((tls_mask & tls_gd) != 0)
13647                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13648                           + R_PPC64_GOT_TPREL16_DS);
13649               else
13650                 {
13651                   rel->r_offset -= d_offset;
13652                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13653                   r_type = R_PPC64_NONE;
13654                 }
13655               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13656             }
13657           break;
13658
13659         case R_PPC64_GOT_TLSGD16:
13660         case R_PPC64_GOT_TLSGD16_LO:
13661           tls_gd = TLS_TPRELGD;
13662           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13663             goto tls_ldgd_opt;
13664           break;
13665
13666         case R_PPC64_GOT_TLSLD16:
13667         case R_PPC64_GOT_TLSLD16_LO:
13668           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13669             {
13670               unsigned int insn1, insn2, insn3;
13671               bfd_vma offset;
13672
13673             tls_ldgd_opt:
13674               offset = (bfd_vma) -1;
13675               /* If not using the newer R_PPC64_TLSGD/LD to mark
13676                  __tls_get_addr calls, we must trust that the call
13677                  stays with its arg setup insns, ie. that the next
13678                  reloc is the __tls_get_addr call associated with
13679                  the current reloc.  Edit both insns.  */
13680               if (input_section->has_tls_get_addr_call
13681                   && rel + 1 < relend
13682                   && branch_reloc_hash_match (input_bfd, rel + 1,
13683                                               htab->tls_get_addr,
13684                                               htab->tls_get_addr_fd))
13685                 offset = rel[1].r_offset;
13686               /* We read the low GOT_TLS (or TOC16) insn because we
13687                  need to keep the destination reg.  It may be
13688                  something other than the usual r3, and moved to r3
13689                  before the call by intervening code.  */
13690               insn1 = bfd_get_32 (output_bfd,
13691                                   contents + rel->r_offset - d_offset);
13692               if ((tls_mask & tls_gd) != 0)
13693                 {
13694                   /* IE */
13695                   insn1 &= (0x1f << 21) | (0x1f << 16);
13696                   insn1 |= 58 << 26;    /* ld */
13697                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13698                   if (offset != (bfd_vma) -1)
13699                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13700                   if ((tls_mask & TLS_EXPLICIT) == 0)
13701                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13702                               + R_PPC64_GOT_TPREL16_DS);
13703                   else
13704                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13705                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13706                 }
13707               else
13708                 {
13709                   /* LE */
13710                   insn1 &= 0x1f << 21;
13711                   insn1 |= 0x3c0d0000;  /* addis r,13,0 */
13712                   insn2 = 0x38630000;   /* addi 3,3,0 */
13713                   if (tls_gd == 0)
13714                     {
13715                       /* Was an LD reloc.  */
13716                       if (toc_symndx)
13717                         sec = local_sections[toc_symndx];
13718                       for (r_symndx = 0;
13719                            r_symndx < symtab_hdr->sh_info;
13720                            r_symndx++)
13721                         if (local_sections[r_symndx] == sec)
13722                           break;
13723                       if (r_symndx >= symtab_hdr->sh_info)
13724                         r_symndx = STN_UNDEF;
13725                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13726                       if (r_symndx != STN_UNDEF)
13727                         rel->r_addend -= (local_syms[r_symndx].st_value
13728                                           + sec->output_offset
13729                                           + sec->output_section->vma);
13730                     }
13731                   else if (toc_symndx != 0)
13732                     {
13733                       r_symndx = toc_symndx;
13734                       rel->r_addend = toc_addend;
13735                     }
13736                   r_type = R_PPC64_TPREL16_HA;
13737                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13738                   if (offset != (bfd_vma) -1)
13739                     {
13740                       rel[1].r_info = ELF64_R_INFO (r_symndx,
13741                                                     R_PPC64_TPREL16_LO);
13742                       rel[1].r_offset = offset + d_offset;
13743                       rel[1].r_addend = rel->r_addend;
13744                     }
13745                 }
13746               bfd_put_32 (output_bfd, insn1,
13747                           contents + rel->r_offset - d_offset);
13748               if (offset != (bfd_vma) -1)
13749                 {
13750                   insn3 = bfd_get_32 (output_bfd,
13751                                       contents + offset + 4);
13752                   if (insn3 == NOP
13753                       || insn3 == CROR_151515 || insn3 == CROR_313131)
13754                     {
13755                       rel[1].r_offset += 4;
13756                       bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13757                       insn2 = NOP;
13758                     }
13759                   bfd_put_32 (output_bfd, insn2, contents + offset);
13760                 }
13761               if ((tls_mask & tls_gd) == 0
13762                   && (tls_gd == 0 || toc_symndx != 0))
13763                 {
13764                   /* We changed the symbol.  Start over in order
13765                      to get h, sym, sec etc. right.  */
13766                   goto again;
13767                 }
13768             }
13769           break;
13770
13771         case R_PPC64_TLSGD:
13772           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13773             {
13774               unsigned int insn2, insn3;
13775               bfd_vma offset = rel->r_offset;
13776
13777               if ((tls_mask & TLS_TPRELGD) != 0)
13778                 {
13779                   /* IE */
13780                   r_type = R_PPC64_NONE;
13781                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13782                 }
13783               else
13784                 {
13785                   /* LE */
13786                   if (toc_symndx != 0)
13787                     {
13788                       r_symndx = toc_symndx;
13789                       rel->r_addend = toc_addend;
13790                     }
13791                   r_type = R_PPC64_TPREL16_LO;
13792                   rel->r_offset = offset + d_offset;
13793                   insn2 = 0x38630000;   /* addi 3,3,0 */
13794                 }
13795               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13796               /* Zap the reloc on the _tls_get_addr call too.  */
13797               BFD_ASSERT (offset == rel[1].r_offset);
13798               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13799               insn3 = bfd_get_32 (output_bfd,
13800                                   contents + offset + 4);
13801               if (insn3 == NOP
13802                   || insn3 == CROR_151515 || insn3 == CROR_313131)
13803                 {
13804                   rel->r_offset += 4;
13805                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13806                   insn2 = NOP;
13807                 }
13808               bfd_put_32 (output_bfd, insn2, contents + offset);
13809               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13810                 goto again;
13811             }
13812           break;
13813
13814         case R_PPC64_TLSLD:
13815           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13816             {
13817               unsigned int insn2, insn3;
13818               bfd_vma offset = rel->r_offset;
13819
13820               if (toc_symndx)
13821                 sec = local_sections[toc_symndx];
13822               for (r_symndx = 0;
13823                    r_symndx < symtab_hdr->sh_info;
13824                    r_symndx++)
13825                 if (local_sections[r_symndx] == sec)
13826                   break;
13827               if (r_symndx >= symtab_hdr->sh_info)
13828                 r_symndx = STN_UNDEF;
13829               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13830               if (r_symndx != STN_UNDEF)
13831                 rel->r_addend -= (local_syms[r_symndx].st_value
13832                                   + sec->output_offset
13833                                   + sec->output_section->vma);
13834
13835               r_type = R_PPC64_TPREL16_LO;
13836               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13837               rel->r_offset = offset + d_offset;
13838               /* Zap the reloc on the _tls_get_addr call too.  */
13839               BFD_ASSERT (offset == rel[1].r_offset);
13840               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13841               insn2 = 0x38630000;       /* addi 3,3,0 */
13842               insn3 = bfd_get_32 (output_bfd,
13843                                   contents + offset + 4);
13844               if (insn3 == NOP
13845                   || insn3 == CROR_151515 || insn3 == CROR_313131)
13846                 {
13847                   rel->r_offset += 4;
13848                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13849                   insn2 = NOP;
13850                 }
13851               bfd_put_32 (output_bfd, insn2, contents + offset);
13852               goto again;
13853             }
13854           break;
13855
13856         case R_PPC64_DTPMOD64:
13857           if (rel + 1 < relend
13858               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13859               && rel[1].r_offset == rel->r_offset + 8)
13860             {
13861               if ((tls_mask & TLS_GD) == 0)
13862                 {
13863                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13864                   if ((tls_mask & TLS_TPRELGD) != 0)
13865                     r_type = R_PPC64_TPREL64;
13866                   else
13867                     {
13868                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13869                       r_type = R_PPC64_NONE;
13870                     }
13871                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13872                 }
13873             }
13874           else
13875             {
13876               if ((tls_mask & TLS_LD) == 0)
13877                 {
13878                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13879                   r_type = R_PPC64_NONE;
13880                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13881                 }
13882             }
13883           break;
13884
13885         case R_PPC64_TPREL64:
13886           if ((tls_mask & TLS_TPREL) == 0)
13887             {
13888               r_type = R_PPC64_NONE;
13889               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13890             }
13891           break;
13892
13893         case R_PPC64_ENTRY:
13894           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13895           if (!bfd_link_pic (info)
13896               && !info->traditional_format
13897               && relocation + 0x80008000 <= 0xffffffff)
13898             {
13899               unsigned int insn1, insn2;
13900
13901               insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13902               insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13903               if ((insn1 & ~0xfffc) == LD_R2_0R12
13904                   && insn2 == ADD_R2_R2_R12)
13905                 {
13906                   bfd_put_32 (output_bfd,
13907                               LIS_R2 + PPC_HA (relocation),
13908                               contents + rel->r_offset);
13909                   bfd_put_32 (output_bfd,
13910                               ADDI_R2_R2 + PPC_LO (relocation),
13911                               contents + rel->r_offset + 4);
13912                 }
13913             }
13914           else
13915             {
13916               relocation -= (rel->r_offset
13917                              + input_section->output_offset
13918                              + input_section->output_section->vma);
13919               if (relocation + 0x80008000 <= 0xffffffff)
13920                 {
13921                   unsigned int insn1, insn2;
13922
13923                   insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13924                   insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13925                   if ((insn1 & ~0xfffc) == LD_R2_0R12
13926                       && insn2 == ADD_R2_R2_R12)
13927                     {
13928                       bfd_put_32 (output_bfd,
13929                                   ADDIS_R2_R12 + PPC_HA (relocation),
13930                                   contents + rel->r_offset);
13931                       bfd_put_32 (output_bfd,
13932                                   ADDI_R2_R2 + PPC_LO (relocation),
13933                                   contents + rel->r_offset + 4);
13934                     }
13935                 }
13936             }
13937           break;
13938
13939         case R_PPC64_REL16_HA:
13940           /* If we are generating a non-PIC executable, edit
13941              .  0:      addis 2,12,.TOC.-0b@ha
13942              .          addi 2,2,.TOC.-0b@l
13943              used by ELFv2 global entry points to set up r2, to
13944              .          lis 2,.TOC.@ha
13945              .          addi 2,2,.TOC.@l
13946              if .TOC. is in range.  */
13947           if (!bfd_link_pic (info)
13948               && !info->traditional_format
13949               && !htab->opd_abi
13950               && rel->r_addend == d_offset
13951               && h != NULL && &h->elf == htab->elf.hgot
13952               && rel + 1 < relend
13953               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
13954               && rel[1].r_offset == rel->r_offset + 4
13955               && rel[1].r_addend == rel->r_addend + 4
13956               && relocation + 0x80008000 <= 0xffffffff)
13957             {
13958               unsigned int insn1, insn2;
13959               bfd_vma offset = rel->r_offset - d_offset;
13960               insn1 = bfd_get_32 (output_bfd, contents + offset);
13961               insn2 = bfd_get_32 (output_bfd, contents + offset + 4);
13962               if ((insn1 & 0xffff0000) == ADDIS_R2_R12
13963                   && (insn2 & 0xffff0000) == ADDI_R2_R2)
13964                 {
13965                   r_type = R_PPC64_ADDR16_HA;
13966                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13967                   rel->r_addend -= d_offset;
13968                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
13969                   rel[1].r_addend -= d_offset + 4;
13970                   bfd_put_32 (output_bfd, LIS_R2, contents + offset);
13971                 }
13972             }
13973           break;
13974         }
13975
13976       /* Handle other relocations that tweak non-addend part of insn.  */
13977       insn = 0;
13978       max_br_offset = 1 << 25;
13979       addend = rel->r_addend;
13980       reloc_dest = DEST_NORMAL;
13981       switch (r_type)
13982         {
13983         default:
13984           break;
13985
13986         case R_PPC64_TOCSAVE:
13987           if (relocation + addend == (rel->r_offset
13988                                       + input_section->output_offset
13989                                       + input_section->output_section->vma)
13990               && tocsave_find (htab, NO_INSERT,
13991                                &local_syms, rel, input_bfd))
13992             {
13993               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13994               if (insn == NOP
13995                   || insn == CROR_151515 || insn == CROR_313131)
13996                 bfd_put_32 (input_bfd,
13997                             STD_R2_0R1 + STK_TOC (htab),
13998                             contents + rel->r_offset);
13999             }
14000           break;
14001
14002           /* Branch taken prediction relocations.  */
14003         case R_PPC64_ADDR14_BRTAKEN:
14004         case R_PPC64_REL14_BRTAKEN:
14005           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
14006           /* Fall thru.  */
14007
14008           /* Branch not taken prediction relocations.  */
14009         case R_PPC64_ADDR14_BRNTAKEN:
14010         case R_PPC64_REL14_BRNTAKEN:
14011           insn |= bfd_get_32 (output_bfd,
14012                               contents + rel->r_offset) & ~(0x01 << 21);
14013           /* Fall thru.  */
14014
14015         case R_PPC64_REL14:
14016           max_br_offset = 1 << 15;
14017           /* Fall thru.  */
14018
14019         case R_PPC64_REL24:
14020           /* Calls to functions with a different TOC, such as calls to
14021              shared objects, need to alter the TOC pointer.  This is
14022              done using a linkage stub.  A REL24 branching to these
14023              linkage stubs needs to be followed by a nop, as the nop
14024              will be replaced with an instruction to restore the TOC
14025              base pointer.  */
14026           fdh = h;
14027           if (h != NULL
14028               && h->oh != NULL
14029               && h->oh->is_func_descriptor)
14030             fdh = ppc_follow_link (h->oh);
14031           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14032                                            htab);
14033           if (stub_entry != NULL
14034               && (stub_entry->stub_type == ppc_stub_plt_call
14035                   || stub_entry->stub_type == ppc_stub_plt_call_r2save
14036                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14037                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
14038             {
14039               bfd_boolean can_plt_call = FALSE;
14040
14041               /* All of these stubs will modify r2, so there must be a
14042                  branch and link followed by a nop.  The nop is
14043                  replaced by an insn to restore r2.  */
14044               if (rel->r_offset + 8 <= input_section->size)
14045                 {
14046                   unsigned long br;
14047
14048                   br = bfd_get_32 (input_bfd,
14049                                    contents + rel->r_offset);
14050                   if ((br & 1) != 0)
14051                     {
14052                       unsigned long nop;
14053
14054                       nop = bfd_get_32 (input_bfd,
14055                                         contents + rel->r_offset + 4);
14056                       if (nop == NOP
14057                           || nop == CROR_151515 || nop == CROR_313131)
14058                         {
14059                           if (h != NULL
14060                               && (h == htab->tls_get_addr_fd
14061                                   || h == htab->tls_get_addr)
14062                               && htab->params->tls_get_addr_opt)
14063                             {
14064                               /* Special stub used, leave nop alone.  */
14065                             }
14066                           else
14067                             bfd_put_32 (input_bfd,
14068                                         LD_R2_0R1 + STK_TOC (htab),
14069                                         contents + rel->r_offset + 4);
14070                           can_plt_call = TRUE;
14071                         }
14072                     }
14073                 }
14074
14075               if (!can_plt_call && h != NULL)
14076                 {
14077                   const char *name = h->elf.root.root.string;
14078
14079                   if (*name == '.')
14080                     ++name;
14081
14082                   if (strncmp (name, "__libc_start_main", 17) == 0
14083                       && (name[17] == 0 || name[17] == '@'))
14084                     {
14085                       /* Allow crt1 branch to go via a toc adjusting
14086                          stub.  Other calls that never return could do
14087                          the same, if we could detect such.  */
14088                       can_plt_call = TRUE;
14089                     }
14090                 }
14091
14092               if (!can_plt_call)
14093                 {
14094                   /* g++ as of 20130507 emits self-calls without a
14095                      following nop.  This is arguably wrong since we
14096                      have conflicting information.  On the one hand a
14097                      global symbol and on the other a local call
14098                      sequence, but don't error for this special case.
14099                      It isn't possible to cheaply verify we have
14100                      exactly such a call.  Allow all calls to the same
14101                      section.  */
14102                   asection *code_sec = sec;
14103
14104                   if (get_opd_info (sec) != NULL)
14105                     {
14106                       bfd_vma off = (relocation + addend
14107                                      - sec->output_section->vma
14108                                      - sec->output_offset);
14109
14110                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14111                     }
14112                   if (code_sec == input_section)
14113                     can_plt_call = TRUE;
14114                 }
14115
14116               if (!can_plt_call)
14117                 {
14118                   if (stub_entry->stub_type == ppc_stub_plt_call
14119                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14120                     info->callbacks->einfo
14121                       (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
14122                          "recompile with -fPIC\n"),
14123                        input_bfd, input_section, rel->r_offset, sym_name);
14124                   else
14125                     info->callbacks->einfo
14126                       (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
14127                          "(-mcmodel=small toc adjust stub)\n"),
14128                        input_bfd, input_section, rel->r_offset, sym_name);
14129
14130                   bfd_set_error (bfd_error_bad_value);
14131                   ret = FALSE;
14132                 }
14133
14134               if (can_plt_call
14135                   && (stub_entry->stub_type == ppc_stub_plt_call
14136                       || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14137                 unresolved_reloc = FALSE;
14138             }
14139
14140           if ((stub_entry == NULL
14141                || stub_entry->stub_type == ppc_stub_long_branch
14142                || stub_entry->stub_type == ppc_stub_plt_branch)
14143               && get_opd_info (sec) != NULL)
14144             {
14145               /* The branch destination is the value of the opd entry. */
14146               bfd_vma off = (relocation + addend
14147                              - sec->output_section->vma
14148                              - sec->output_offset);
14149               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14150               if (dest != (bfd_vma) -1)
14151                 {
14152                   relocation = dest;
14153                   addend = 0;
14154                   reloc_dest = DEST_OPD;
14155                 }
14156             }
14157
14158           /* If the branch is out of reach we ought to have a long
14159              branch stub.  */
14160           from = (rel->r_offset
14161                   + input_section->output_offset
14162                   + input_section->output_section->vma);
14163
14164           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14165                                                   ? fdh->elf.other
14166                                                   : sym->st_other);
14167
14168           if (stub_entry != NULL
14169               && (stub_entry->stub_type == ppc_stub_long_branch
14170                   || stub_entry->stub_type == ppc_stub_plt_branch)
14171               && (r_type == R_PPC64_ADDR14_BRTAKEN
14172                   || r_type == R_PPC64_ADDR14_BRNTAKEN
14173                   || (relocation + addend - from + max_br_offset
14174                       < 2 * max_br_offset)))
14175             /* Don't use the stub if this branch is in range.  */
14176             stub_entry = NULL;
14177
14178           if (stub_entry != NULL)
14179             {
14180               /* Munge up the value and addend so that we call the stub
14181                  rather than the procedure directly.  */
14182               asection *stub_sec = stub_entry->group->stub_sec;
14183
14184               if (stub_entry->stub_type == ppc_stub_save_res)
14185                 relocation += (stub_sec->output_offset
14186                                + stub_sec->output_section->vma
14187                                + stub_sec->size - htab->sfpr->size
14188                                - htab->sfpr->output_offset
14189                                - htab->sfpr->output_section->vma);
14190               else
14191                 relocation = (stub_entry->stub_offset
14192                               + stub_sec->output_offset
14193                               + stub_sec->output_section->vma);
14194               addend = 0;
14195               reloc_dest = DEST_STUB;
14196
14197               if ((stub_entry->stub_type == ppc_stub_plt_call
14198                    || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14199                   && (ALWAYS_EMIT_R2SAVE
14200                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14201                   && rel + 1 < relend
14202                   && rel[1].r_offset == rel->r_offset + 4
14203                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14204                 relocation += 4;
14205             }
14206
14207           if (insn != 0)
14208             {
14209               if (is_isa_v2)
14210                 {
14211                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
14212                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
14213                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
14214                   if ((insn & (0x14 << 21)) == (0x04 << 21))
14215                     insn |= 0x02 << 21;
14216                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
14217                     insn |= 0x08 << 21;
14218                   else
14219                     break;
14220                 }
14221               else
14222                 {
14223                   /* Invert 'y' bit if not the default.  */
14224                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
14225                     insn ^= 0x01 << 21;
14226                 }
14227
14228               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
14229             }
14230
14231           /* NOP out calls to undefined weak functions.
14232              We can thus call a weak function without first
14233              checking whether the function is defined.  */
14234           else if (h != NULL
14235                    && h->elf.root.type == bfd_link_hash_undefweak
14236                    && h->elf.dynindx == -1
14237                    && r_type == R_PPC64_REL24
14238                    && relocation == 0
14239                    && addend == 0)
14240             {
14241               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
14242               goto copy_reloc;
14243             }
14244           break;
14245         }
14246
14247       /* Set `addend'.  */
14248       tls_type = 0;
14249       switch (r_type)
14250         {
14251         default:
14252           info->callbacks->einfo
14253             (_("%P: %B: unknown relocation type %d for `%T'\n"),
14254              input_bfd, (int) r_type, sym_name);
14255
14256           bfd_set_error (bfd_error_bad_value);
14257           ret = FALSE;
14258           goto copy_reloc;
14259
14260         case R_PPC64_NONE:
14261         case R_PPC64_TLS:
14262         case R_PPC64_TLSGD:
14263         case R_PPC64_TLSLD:
14264         case R_PPC64_TOCSAVE:
14265         case R_PPC64_GNU_VTINHERIT:
14266         case R_PPC64_GNU_VTENTRY:
14267         case R_PPC64_ENTRY:
14268           goto copy_reloc;
14269
14270           /* GOT16 relocations.  Like an ADDR16 using the symbol's
14271              address in the GOT as relocation value instead of the
14272              symbol's value itself.  Also, create a GOT entry for the
14273              symbol and put the symbol value there.  */
14274         case R_PPC64_GOT_TLSGD16:
14275         case R_PPC64_GOT_TLSGD16_LO:
14276         case R_PPC64_GOT_TLSGD16_HI:
14277         case R_PPC64_GOT_TLSGD16_HA:
14278           tls_type = TLS_TLS | TLS_GD;
14279           goto dogot;
14280
14281         case R_PPC64_GOT_TLSLD16:
14282         case R_PPC64_GOT_TLSLD16_LO:
14283         case R_PPC64_GOT_TLSLD16_HI:
14284         case R_PPC64_GOT_TLSLD16_HA:
14285           tls_type = TLS_TLS | TLS_LD;
14286           goto dogot;
14287
14288         case R_PPC64_GOT_TPREL16_DS:
14289         case R_PPC64_GOT_TPREL16_LO_DS:
14290         case R_PPC64_GOT_TPREL16_HI:
14291         case R_PPC64_GOT_TPREL16_HA:
14292           tls_type = TLS_TLS | TLS_TPREL;
14293           goto dogot;
14294
14295         case R_PPC64_GOT_DTPREL16_DS:
14296         case R_PPC64_GOT_DTPREL16_LO_DS:
14297         case R_PPC64_GOT_DTPREL16_HI:
14298         case R_PPC64_GOT_DTPREL16_HA:
14299           tls_type = TLS_TLS | TLS_DTPREL;
14300           goto dogot;
14301
14302         case R_PPC64_GOT16:
14303         case R_PPC64_GOT16_LO:
14304         case R_PPC64_GOT16_HI:
14305         case R_PPC64_GOT16_HA:
14306         case R_PPC64_GOT16_DS:
14307         case R_PPC64_GOT16_LO_DS:
14308         dogot:
14309           {
14310             /* Relocation is to the entry for this symbol in the global
14311                offset table.  */
14312             asection *got;
14313             bfd_vma *offp;
14314             bfd_vma off;
14315             unsigned long indx = 0;
14316             struct got_entry *ent;
14317
14318             if (tls_type == (TLS_TLS | TLS_LD)
14319                 && (h == NULL
14320                     || !h->elf.def_dynamic))
14321               ent = ppc64_tlsld_got (input_bfd);
14322             else
14323               {
14324
14325                 if (h != NULL)
14326                   {
14327                     bfd_boolean dyn = htab->elf.dynamic_sections_created;
14328                     if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info),
14329                                                           &h->elf)
14330                         || (bfd_link_pic (info)
14331                             && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
14332                       /* This is actually a static link, or it is a
14333                          -Bsymbolic link and the symbol is defined
14334                          locally, or the symbol was forced to be local
14335                          because of a version file.  */
14336                       ;
14337                     else
14338                       {
14339                         BFD_ASSERT (h->elf.dynindx != -1);
14340                         indx = h->elf.dynindx;
14341                         unresolved_reloc = FALSE;
14342                       }
14343                     ent = h->elf.got.glist;
14344                   }
14345                 else
14346                   {
14347                     if (local_got_ents == NULL)
14348                       abort ();
14349                     ent = local_got_ents[r_symndx];
14350                   }
14351
14352                 for (; ent != NULL; ent = ent->next)
14353                   if (ent->addend == orig_rel.r_addend
14354                       && ent->owner == input_bfd
14355                       && ent->tls_type == tls_type)
14356                     break;
14357               }
14358
14359             if (ent == NULL)
14360               abort ();
14361             if (ent->is_indirect)
14362               ent = ent->got.ent;
14363             offp = &ent->got.offset;
14364             got = ppc64_elf_tdata (ent->owner)->got;
14365             if (got == NULL)
14366               abort ();
14367
14368             /* The offset must always be a multiple of 8.  We use the
14369                least significant bit to record whether we have already
14370                processed this entry.  */
14371             off = *offp;
14372             if ((off & 1) != 0)
14373               off &= ~1;
14374             else
14375               {
14376                 /* Generate relocs for the dynamic linker, except in
14377                    the case of TLSLD where we'll use one entry per
14378                    module.  */
14379                 asection *relgot;
14380                 bfd_boolean ifunc;
14381
14382                 *offp = off | 1;
14383                 relgot = NULL;
14384                 ifunc = (h != NULL
14385                          ? h->elf.type == STT_GNU_IFUNC
14386                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14387                 if (ifunc)
14388                   relgot = htab->elf.irelplt;
14389                 else if ((bfd_link_pic (info) || indx != 0)
14390                          && (h == NULL
14391                              || (tls_type == (TLS_TLS | TLS_LD)
14392                                  && !h->elf.def_dynamic)
14393                              || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14394                              || h->elf.root.type != bfd_link_hash_undefweak))
14395                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
14396                 if (relgot != NULL)
14397                   {
14398                     outrel.r_offset = (got->output_section->vma
14399                                        + got->output_offset
14400                                        + off);
14401                     outrel.r_addend = addend;
14402                     if (tls_type & (TLS_LD | TLS_GD))
14403                       {
14404                         outrel.r_addend = 0;
14405                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14406                         if (tls_type == (TLS_TLS | TLS_GD))
14407                           {
14408                             loc = relgot->contents;
14409                             loc += (relgot->reloc_count++
14410                                     * sizeof (Elf64_External_Rela));
14411                             bfd_elf64_swap_reloca_out (output_bfd,
14412                                                        &outrel, loc);
14413                             outrel.r_offset += 8;
14414                             outrel.r_addend = addend;
14415                             outrel.r_info
14416                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14417                           }
14418                       }
14419                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
14420                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14421                     else if (tls_type == (TLS_TLS | TLS_TPREL))
14422                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14423                     else if (indx != 0)
14424                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14425                     else
14426                       {
14427                         if (ifunc)
14428                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14429                         else
14430                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14431
14432                         /* Write the .got section contents for the sake
14433                            of prelink.  */
14434                         loc = got->contents + off;
14435                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14436                                     loc);
14437                       }
14438
14439                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14440                       {
14441                         outrel.r_addend += relocation;
14442                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14443                           {
14444                             if (htab->elf.tls_sec == NULL)
14445                               outrel.r_addend = 0;
14446                             else
14447                               outrel.r_addend -= htab->elf.tls_sec->vma;
14448                           }
14449                       }
14450                     loc = relgot->contents;
14451                     loc += (relgot->reloc_count++
14452                             * sizeof (Elf64_External_Rela));
14453                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14454                   }
14455
14456                 /* Init the .got section contents here if we're not
14457                    emitting a reloc.  */
14458                 else
14459                   {
14460                     relocation += addend;
14461                     if (tls_type == (TLS_TLS | TLS_LD))
14462                       relocation = 1;
14463                     else if (tls_type != 0)
14464                       {
14465                         if (htab->elf.tls_sec == NULL)
14466                           relocation = 0;
14467                         else
14468                           {
14469                             relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14470                             if (tls_type == (TLS_TLS | TLS_TPREL))
14471                               relocation += DTP_OFFSET - TP_OFFSET;
14472                           }
14473
14474                         if (tls_type == (TLS_TLS | TLS_GD))
14475                           {
14476                             bfd_put_64 (output_bfd, relocation,
14477                                         got->contents + off + 8);
14478                             relocation = 1;
14479                           }
14480                       }
14481
14482                     bfd_put_64 (output_bfd, relocation,
14483                                 got->contents + off);
14484                   }
14485               }
14486
14487             if (off >= (bfd_vma) -2)
14488               abort ();
14489
14490             relocation = got->output_section->vma + got->output_offset + off;
14491             addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14492           }
14493           break;
14494
14495         case R_PPC64_PLT16_HA:
14496         case R_PPC64_PLT16_HI:
14497         case R_PPC64_PLT16_LO:
14498         case R_PPC64_PLT32:
14499         case R_PPC64_PLT64:
14500           /* Relocation is to the entry for this symbol in the
14501              procedure linkage table.  */
14502           {
14503             struct plt_entry **plt_list = NULL;
14504             if (h != NULL)
14505               plt_list = &h->elf.plt.plist;
14506             else if (local_got_ents != NULL)
14507               {
14508                 struct plt_entry **local_plt = (struct plt_entry **)
14509                   (local_got_ents + symtab_hdr->sh_info);
14510                 unsigned char *local_got_tls_masks = (unsigned char *)
14511                   (local_plt + symtab_hdr->sh_info);
14512                 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14513                   plt_list = local_plt + r_symndx;
14514               }
14515             if (plt_list)
14516               {
14517                 struct plt_entry *ent;
14518
14519                 for (ent = *plt_list; ent != NULL; ent = ent->next)
14520                   if (ent->plt.offset != (bfd_vma) -1
14521                       && ent->addend == orig_rel.r_addend)
14522                     {
14523                       asection *plt;
14524
14525                       plt = htab->elf.splt;
14526                       if (!htab->elf.dynamic_sections_created
14527                           || h == NULL
14528                           || h->elf.dynindx == -1)
14529                         plt = htab->elf.iplt;
14530                       relocation = (plt->output_section->vma
14531                                     + plt->output_offset
14532                                     + ent->plt.offset);
14533                       addend = 0;
14534                       unresolved_reloc = FALSE;
14535                       break;
14536                     }
14537               }
14538           }
14539           break;
14540
14541         case R_PPC64_TOC:
14542           /* Relocation value is TOC base.  */
14543           relocation = TOCstart;
14544           if (r_symndx == STN_UNDEF)
14545             relocation += htab->sec_info[input_section->id].toc_off;
14546           else if (unresolved_reloc)
14547             ;
14548           else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14549             relocation += htab->sec_info[sec->id].toc_off;
14550           else
14551             unresolved_reloc = TRUE;
14552           goto dodyn;
14553
14554           /* TOC16 relocs.  We want the offset relative to the TOC base,
14555              which is the address of the start of the TOC plus 0x8000.
14556              The TOC consists of sections .got, .toc, .tocbss, and .plt,
14557              in this order.  */
14558         case R_PPC64_TOC16:
14559         case R_PPC64_TOC16_LO:
14560         case R_PPC64_TOC16_HI:
14561         case R_PPC64_TOC16_DS:
14562         case R_PPC64_TOC16_LO_DS:
14563         case R_PPC64_TOC16_HA:
14564           addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14565           break;
14566
14567           /* Relocate against the beginning of the section.  */
14568         case R_PPC64_SECTOFF:
14569         case R_PPC64_SECTOFF_LO:
14570         case R_PPC64_SECTOFF_HI:
14571         case R_PPC64_SECTOFF_DS:
14572         case R_PPC64_SECTOFF_LO_DS:
14573         case R_PPC64_SECTOFF_HA:
14574           if (sec != NULL)
14575             addend -= sec->output_section->vma;
14576           break;
14577
14578         case R_PPC64_REL16:
14579         case R_PPC64_REL16_LO:
14580         case R_PPC64_REL16_HI:
14581         case R_PPC64_REL16_HA:
14582         case R_PPC64_REL16DX_HA:
14583           break;
14584
14585         case R_PPC64_REL14:
14586         case R_PPC64_REL14_BRNTAKEN:
14587         case R_PPC64_REL14_BRTAKEN:
14588         case R_PPC64_REL24:
14589           break;
14590
14591         case R_PPC64_TPREL16:
14592         case R_PPC64_TPREL16_LO:
14593         case R_PPC64_TPREL16_HI:
14594         case R_PPC64_TPREL16_HA:
14595         case R_PPC64_TPREL16_DS:
14596         case R_PPC64_TPREL16_LO_DS:
14597         case R_PPC64_TPREL16_HIGH:
14598         case R_PPC64_TPREL16_HIGHA:
14599         case R_PPC64_TPREL16_HIGHER:
14600         case R_PPC64_TPREL16_HIGHERA:
14601         case R_PPC64_TPREL16_HIGHEST:
14602         case R_PPC64_TPREL16_HIGHESTA:
14603           if (h != NULL
14604               && h->elf.root.type == bfd_link_hash_undefweak
14605               && h->elf.dynindx == -1)
14606             {
14607               /* Make this relocation against an undefined weak symbol
14608                  resolve to zero.  This is really just a tweak, since
14609                  code using weak externs ought to check that they are
14610                  defined before using them.  */
14611               bfd_byte *p = contents + rel->r_offset - d_offset;
14612
14613               insn = bfd_get_32 (output_bfd, p);
14614               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14615               if (insn != 0)
14616                 bfd_put_32 (output_bfd, insn, p);
14617               break;
14618             }
14619           if (htab->elf.tls_sec != NULL)
14620             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14621           if (bfd_link_pic (info))
14622             /* The TPREL16 relocs shouldn't really be used in shared
14623                libs as they will result in DT_TEXTREL being set, but
14624                support them anyway.  */
14625             goto dodyn;
14626           break;
14627
14628         case R_PPC64_DTPREL16:
14629         case R_PPC64_DTPREL16_LO:
14630         case R_PPC64_DTPREL16_HI:
14631         case R_PPC64_DTPREL16_HA:
14632         case R_PPC64_DTPREL16_DS:
14633         case R_PPC64_DTPREL16_LO_DS:
14634         case R_PPC64_DTPREL16_HIGH:
14635         case R_PPC64_DTPREL16_HIGHA:
14636         case R_PPC64_DTPREL16_HIGHER:
14637         case R_PPC64_DTPREL16_HIGHERA:
14638         case R_PPC64_DTPREL16_HIGHEST:
14639         case R_PPC64_DTPREL16_HIGHESTA:
14640           if (htab->elf.tls_sec != NULL)
14641             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14642           break;
14643
14644         case R_PPC64_ADDR64_LOCAL:
14645           addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14646                                               ? h->elf.other
14647                                               : sym->st_other);
14648           break;
14649
14650         case R_PPC64_DTPMOD64:
14651           relocation = 1;
14652           addend = 0;
14653           goto dodyn;
14654
14655         case R_PPC64_TPREL64:
14656           if (htab->elf.tls_sec != NULL)
14657             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14658           goto dodyn;
14659
14660         case R_PPC64_DTPREL64:
14661           if (htab->elf.tls_sec != NULL)
14662             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14663           /* Fall thru */
14664
14665           /* Relocations that may need to be propagated if this is a
14666              dynamic object.  */
14667         case R_PPC64_REL30:
14668         case R_PPC64_REL32:
14669         case R_PPC64_REL64:
14670         case R_PPC64_ADDR14:
14671         case R_PPC64_ADDR14_BRNTAKEN:
14672         case R_PPC64_ADDR14_BRTAKEN:
14673         case R_PPC64_ADDR16:
14674         case R_PPC64_ADDR16_DS:
14675         case R_PPC64_ADDR16_HA:
14676         case R_PPC64_ADDR16_HI:
14677         case R_PPC64_ADDR16_HIGH:
14678         case R_PPC64_ADDR16_HIGHA:
14679         case R_PPC64_ADDR16_HIGHER:
14680         case R_PPC64_ADDR16_HIGHERA:
14681         case R_PPC64_ADDR16_HIGHEST:
14682         case R_PPC64_ADDR16_HIGHESTA:
14683         case R_PPC64_ADDR16_LO:
14684         case R_PPC64_ADDR16_LO_DS:
14685         case R_PPC64_ADDR24:
14686         case R_PPC64_ADDR32:
14687         case R_PPC64_ADDR64:
14688         case R_PPC64_UADDR16:
14689         case R_PPC64_UADDR32:
14690         case R_PPC64_UADDR64:
14691         dodyn:
14692           if ((input_section->flags & SEC_ALLOC) == 0)
14693             break;
14694
14695           if (NO_OPD_RELOCS && is_opd)
14696             break;
14697
14698           if (bfd_link_pic (info)
14699               ? ((h == NULL
14700                   || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14701                   || h->elf.root.type != bfd_link_hash_undefweak)
14702                  && (must_be_dyn_reloc (info, r_type)
14703                      || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
14704               : (h == NULL
14705                  ? ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
14706                  : (h->elf.type == STT_GNU_IFUNC
14707                     ? (abiversion (output_bfd) >= 2
14708                        ? !(h->elf.pointer_equality_needed
14709                            && !h->elf.def_regular
14710                            && h->elf.root.type == bfd_link_hash_defined
14711                            && h->elf.root.u.def.section == htab->glink)
14712                        : !h->elf.needs_copy)
14713                     : (ELIMINATE_COPY_RELOCS
14714                        && !(h->elf.non_got_ref
14715                             || h->elf.def_regular
14716                             || h->elf.dynindx == -1)))))
14717             {
14718               bfd_boolean skip, relocate;
14719               asection *sreloc;
14720               bfd_vma out_off;
14721
14722               /* When generating a dynamic object, these relocations
14723                  are copied into the output file to be resolved at run
14724                  time.  */
14725
14726               skip = FALSE;
14727               relocate = FALSE;
14728
14729               out_off = _bfd_elf_section_offset (output_bfd, info,
14730                                                  input_section, rel->r_offset);
14731               if (out_off == (bfd_vma) -1)
14732                 skip = TRUE;
14733               else if (out_off == (bfd_vma) -2)
14734                 skip = TRUE, relocate = TRUE;
14735               out_off += (input_section->output_section->vma
14736                           + input_section->output_offset);
14737               outrel.r_offset = out_off;
14738               outrel.r_addend = rel->r_addend;
14739
14740               /* Optimize unaligned reloc use.  */
14741               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14742                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14743                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14744               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14745                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14746                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14747               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14748                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14749                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14750
14751               if (skip)
14752                 memset (&outrel, 0, sizeof outrel);
14753               else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14754                        && !is_opd
14755                        && r_type != R_PPC64_TOC)
14756                 {
14757                   BFD_ASSERT (h->elf.dynindx != -1);
14758                   outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14759                 }
14760               else
14761                 {
14762                   /* This symbol is local, or marked to become local,
14763                      or this is an opd section reloc which must point
14764                      at a local function.  */
14765                   outrel.r_addend += relocation;
14766                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14767                     {
14768                       if (is_opd && h != NULL)
14769                         {
14770                           /* Lie about opd entries.  This case occurs
14771                              when building shared libraries and we
14772                              reference a function in another shared
14773                              lib.  The same thing happens for a weak
14774                              definition in an application that's
14775                              overridden by a strong definition in a
14776                              shared lib.  (I believe this is a generic
14777                              bug in binutils handling of weak syms.)
14778                              In these cases we won't use the opd
14779                              entry in this lib.  */
14780                           unresolved_reloc = FALSE;
14781                         }
14782                       if (!is_opd
14783                           && r_type == R_PPC64_ADDR64
14784                           && (h != NULL
14785                               ? h->elf.type == STT_GNU_IFUNC
14786                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14787                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14788                       else
14789                         {
14790                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14791
14792                           /* We need to relocate .opd contents for ld.so.
14793                              Prelink also wants simple and consistent rules
14794                              for relocs.  This make all RELATIVE relocs have
14795                              *r_offset equal to r_addend.  */
14796                           relocate = TRUE;
14797                         }
14798                     }
14799                   else
14800                     {
14801                       long indx = 0;
14802
14803                       if (h != NULL
14804                           ? h->elf.type == STT_GNU_IFUNC
14805                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14806                         {
14807                           info->callbacks->einfo
14808                             (_("%P: %H: %s for indirect "
14809                                "function `%T' unsupported\n"),
14810                              input_bfd, input_section, rel->r_offset,
14811                              ppc64_elf_howto_table[r_type]->name,
14812                              sym_name);
14813                           ret = FALSE;
14814                         }
14815                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14816                         ;
14817                       else if (sec == NULL || sec->owner == NULL)
14818                         {
14819                           bfd_set_error (bfd_error_bad_value);
14820                           return FALSE;
14821                         }
14822                       else
14823                         {
14824                           asection *osec;
14825
14826                           osec = sec->output_section;
14827                           indx = elf_section_data (osec)->dynindx;
14828
14829                           if (indx == 0)
14830                             {
14831                               if ((osec->flags & SEC_READONLY) == 0
14832                                   && htab->elf.data_index_section != NULL)
14833                                 osec = htab->elf.data_index_section;
14834                               else
14835                                 osec = htab->elf.text_index_section;
14836                               indx = elf_section_data (osec)->dynindx;
14837                             }
14838                           BFD_ASSERT (indx != 0);
14839
14840                           /* We are turning this relocation into one
14841                              against a section symbol, so subtract out
14842                              the output section's address but not the
14843                              offset of the input section in the output
14844                              section.  */
14845                           outrel.r_addend -= osec->vma;
14846                         }
14847
14848                       outrel.r_info = ELF64_R_INFO (indx, r_type);
14849                     }
14850                 }
14851
14852               sreloc = elf_section_data (input_section)->sreloc;
14853               if (h != NULL
14854                   ? h->elf.type == STT_GNU_IFUNC
14855                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14856                 sreloc = htab->elf.irelplt;
14857               if (sreloc == NULL)
14858                 abort ();
14859
14860               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14861                   >= sreloc->size)
14862                 abort ();
14863               loc = sreloc->contents;
14864               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14865               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14866
14867               /* If this reloc is against an external symbol, it will
14868                  be computed at runtime, so there's no need to do
14869                  anything now.  However, for the sake of prelink ensure
14870                  that the section contents are a known value.  */
14871               if (! relocate)
14872                 {
14873                   unresolved_reloc = FALSE;
14874                   /* The value chosen here is quite arbitrary as ld.so
14875                      ignores section contents except for the special
14876                      case of .opd where the contents might be accessed
14877                      before relocation.  Choose zero, as that won't
14878                      cause reloc overflow.  */
14879                   relocation = 0;
14880                   addend = 0;
14881                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14882                      to improve backward compatibility with older
14883                      versions of ld.  */
14884                   if (r_type == R_PPC64_ADDR64)
14885                     addend = outrel.r_addend;
14886                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
14887                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
14888                     addend = (input_section->output_section->vma
14889                               + input_section->output_offset
14890                               + rel->r_offset);
14891                 }
14892             }
14893           break;
14894
14895         case R_PPC64_COPY:
14896         case R_PPC64_GLOB_DAT:
14897         case R_PPC64_JMP_SLOT:
14898         case R_PPC64_JMP_IREL:
14899         case R_PPC64_RELATIVE:
14900           /* We shouldn't ever see these dynamic relocs in relocatable
14901              files.  */
14902           /* Fall through.  */
14903
14904         case R_PPC64_PLTGOT16:
14905         case R_PPC64_PLTGOT16_DS:
14906         case R_PPC64_PLTGOT16_HA:
14907         case R_PPC64_PLTGOT16_HI:
14908         case R_PPC64_PLTGOT16_LO:
14909         case R_PPC64_PLTGOT16_LO_DS:
14910         case R_PPC64_PLTREL32:
14911         case R_PPC64_PLTREL64:
14912           /* These ones haven't been implemented yet.  */
14913
14914           info->callbacks->einfo
14915             (_("%P: %B: %s is not supported for `%T'\n"),
14916              input_bfd,
14917              ppc64_elf_howto_table[r_type]->name, sym_name);
14918
14919           bfd_set_error (bfd_error_invalid_operation);
14920           ret = FALSE;
14921           goto copy_reloc;
14922         }
14923
14924       /* Multi-instruction sequences that access the TOC can be
14925          optimized, eg. addis ra,r2,0; addi rb,ra,x;
14926          to             nop;           addi rb,r2,x;  */
14927       switch (r_type)
14928         {
14929         default:
14930           break;
14931
14932         case R_PPC64_GOT_TLSLD16_HI:
14933         case R_PPC64_GOT_TLSGD16_HI:
14934         case R_PPC64_GOT_TPREL16_HI:
14935         case R_PPC64_GOT_DTPREL16_HI:
14936         case R_PPC64_GOT16_HI:
14937         case R_PPC64_TOC16_HI:
14938           /* These relocs would only be useful if building up an
14939              offset to later add to r2, perhaps in an indexed
14940              addressing mode instruction.  Don't try to optimize.
14941              Unfortunately, the possibility of someone building up an
14942              offset like this or even with the HA relocs, means that
14943              we need to check the high insn when optimizing the low
14944              insn.  */
14945           break;
14946
14947         case R_PPC64_GOT_TLSLD16_HA:
14948         case R_PPC64_GOT_TLSGD16_HA:
14949         case R_PPC64_GOT_TPREL16_HA:
14950         case R_PPC64_GOT_DTPREL16_HA:
14951         case R_PPC64_GOT16_HA:
14952         case R_PPC64_TOC16_HA:
14953           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14954               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14955             {
14956               bfd_byte *p = contents + (rel->r_offset & ~3);
14957               bfd_put_32 (input_bfd, NOP, p);
14958             }
14959           break;
14960
14961         case R_PPC64_GOT_TLSLD16_LO:
14962         case R_PPC64_GOT_TLSGD16_LO:
14963         case R_PPC64_GOT_TPREL16_LO_DS:
14964         case R_PPC64_GOT_DTPREL16_LO_DS:
14965         case R_PPC64_GOT16_LO:
14966         case R_PPC64_GOT16_LO_DS:
14967         case R_PPC64_TOC16_LO:
14968         case R_PPC64_TOC16_LO_DS:
14969           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14970               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14971             {
14972               bfd_byte *p = contents + (rel->r_offset & ~3);
14973               insn = bfd_get_32 (input_bfd, p);
14974               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14975                 {
14976                   /* Transform addic to addi when we change reg.  */
14977                   insn &= ~((0x3f << 26) | (0x1f << 16));
14978                   insn |= (14u << 26) | (2 << 16);
14979                 }
14980               else
14981                 {
14982                   insn &= ~(0x1f << 16);
14983                   insn |= 2 << 16;
14984                 }
14985               bfd_put_32 (input_bfd, insn, p);
14986             }
14987           break;
14988         }
14989
14990       /* Do any further special processing.  */
14991       howto = ppc64_elf_howto_table[(int) r_type];
14992       switch (r_type)
14993         {
14994         default:
14995           break;
14996
14997         case R_PPC64_REL16_HA:
14998         case R_PPC64_REL16DX_HA:
14999         case R_PPC64_ADDR16_HA:
15000         case R_PPC64_ADDR16_HIGHA:
15001         case R_PPC64_ADDR16_HIGHERA:
15002         case R_PPC64_ADDR16_HIGHESTA:
15003         case R_PPC64_TOC16_HA:
15004         case R_PPC64_SECTOFF_HA:
15005         case R_PPC64_TPREL16_HA:
15006         case R_PPC64_TPREL16_HIGHA:
15007         case R_PPC64_TPREL16_HIGHERA:
15008         case R_PPC64_TPREL16_HIGHESTA:
15009         case R_PPC64_DTPREL16_HA:
15010         case R_PPC64_DTPREL16_HIGHA:
15011         case R_PPC64_DTPREL16_HIGHERA:
15012         case R_PPC64_DTPREL16_HIGHESTA:
15013           /* It's just possible that this symbol is a weak symbol
15014              that's not actually defined anywhere. In that case,
15015              'sec' would be NULL, and we should leave the symbol
15016              alone (it will be set to zero elsewhere in the link).  */
15017           if (sec == NULL)
15018             break;
15019           /* Fall thru */
15020
15021         case R_PPC64_GOT16_HA:
15022         case R_PPC64_PLTGOT16_HA:
15023         case R_PPC64_PLT16_HA:
15024         case R_PPC64_GOT_TLSGD16_HA:
15025         case R_PPC64_GOT_TLSLD16_HA:
15026         case R_PPC64_GOT_TPREL16_HA:
15027         case R_PPC64_GOT_DTPREL16_HA:
15028           /* Add 0x10000 if sign bit in 0:15 is set.
15029              Bits 0:15 are not used.  */
15030           addend += 0x8000;
15031           break;
15032
15033         case R_PPC64_ADDR16_DS:
15034         case R_PPC64_ADDR16_LO_DS:
15035         case R_PPC64_GOT16_DS:
15036         case R_PPC64_GOT16_LO_DS:
15037         case R_PPC64_PLT16_LO_DS:
15038         case R_PPC64_SECTOFF_DS:
15039         case R_PPC64_SECTOFF_LO_DS:
15040         case R_PPC64_TOC16_DS:
15041         case R_PPC64_TOC16_LO_DS:
15042         case R_PPC64_PLTGOT16_DS:
15043         case R_PPC64_PLTGOT16_LO_DS:
15044         case R_PPC64_GOT_TPREL16_DS:
15045         case R_PPC64_GOT_TPREL16_LO_DS:
15046         case R_PPC64_GOT_DTPREL16_DS:
15047         case R_PPC64_GOT_DTPREL16_LO_DS:
15048         case R_PPC64_TPREL16_DS:
15049         case R_PPC64_TPREL16_LO_DS:
15050         case R_PPC64_DTPREL16_DS:
15051         case R_PPC64_DTPREL16_LO_DS:
15052           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15053           mask = 3;
15054           /* If this reloc is against an lq, lxv, or stxv insn, then
15055              the value must be a multiple of 16.  This is somewhat of
15056              a hack, but the "correct" way to do this by defining _DQ
15057              forms of all the _DS relocs bloats all reloc switches in
15058              this file.  It doesn't make much sense to use these
15059              relocs in data, so testing the insn should be safe.  */
15060           if ((insn & (0x3f << 26)) == (56u << 26)
15061               || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15062             mask = 15;
15063           relocation += addend;
15064           addend = insn & (mask ^ 3);
15065           if ((relocation & mask) != 0)
15066             {
15067               relocation ^= relocation & mask;
15068               info->callbacks->einfo
15069                 (_("%P: %H: error: %s not a multiple of %u\n"),
15070                  input_bfd, input_section, rel->r_offset,
15071                  howto->name,
15072                  mask + 1);
15073               bfd_set_error (bfd_error_bad_value);
15074               ret = FALSE;
15075               goto copy_reloc;
15076             }
15077           break;
15078         }
15079
15080       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15081          because such sections are not SEC_ALLOC and thus ld.so will
15082          not process them.  */
15083       if (unresolved_reloc
15084           && !((input_section->flags & SEC_DEBUGGING) != 0
15085                && h->elf.def_dynamic)
15086           && _bfd_elf_section_offset (output_bfd, info, input_section,
15087                                       rel->r_offset) != (bfd_vma) -1)
15088         {
15089           info->callbacks->einfo
15090             (_("%P: %H: unresolvable %s against `%T'\n"),
15091              input_bfd, input_section, rel->r_offset,
15092              howto->name,
15093              h->elf.root.root.string);
15094           ret = FALSE;
15095         }
15096
15097       /* 16-bit fields in insns mostly have signed values, but a
15098          few insns have 16-bit unsigned values.  Really, we should
15099          have different reloc types.  */
15100       if (howto->complain_on_overflow != complain_overflow_dont
15101           && howto->dst_mask == 0xffff
15102           && (input_section->flags & SEC_CODE) != 0)
15103         {
15104           enum complain_overflow complain = complain_overflow_signed;
15105
15106           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15107           if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15108             complain = complain_overflow_bitfield;
15109           else if (howto->rightshift == 0
15110                    ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15111                       || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15112                       || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15113                    : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15114                       || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15115                       || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15116             complain = complain_overflow_unsigned;
15117           if (howto->complain_on_overflow != complain)
15118             {
15119               alt_howto = *howto;
15120               alt_howto.complain_on_overflow = complain;
15121               howto = &alt_howto;
15122             }
15123         }
15124
15125       if (r_type == R_PPC64_REL16DX_HA)
15126         {
15127           /* Split field reloc isn't handled by _bfd_final_link_relocate.  */
15128           if (rel->r_offset + 4 > input_section->size)
15129             r = bfd_reloc_outofrange;
15130           else
15131             {
15132               relocation += addend;
15133               relocation -= (rel->r_offset
15134                              + input_section->output_offset
15135                              + input_section->output_section->vma);
15136               relocation = (bfd_signed_vma) relocation >> 16;
15137               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15138               insn &= ~0x1fffc1;
15139               insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15140               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15141               r = bfd_reloc_ok;
15142               if (relocation + 0x8000 > 0xffff)
15143                 r = bfd_reloc_overflow;
15144             }
15145         }
15146       else
15147         r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15148                                       rel->r_offset, relocation, addend);
15149
15150       if (r != bfd_reloc_ok)
15151         {
15152           char *more_info = NULL;
15153           const char *reloc_name = howto->name;
15154
15155           if (reloc_dest != DEST_NORMAL)
15156             {
15157               more_info = bfd_malloc (strlen (reloc_name) + 8);
15158               if (more_info != NULL)
15159                 {
15160                   strcpy (more_info, reloc_name);
15161                   strcat (more_info, (reloc_dest == DEST_OPD
15162                                       ? " (OPD)" : " (stub)"));
15163                   reloc_name = more_info;
15164                 }
15165             }
15166
15167           if (r == bfd_reloc_overflow)
15168             {
15169               /* On code like "if (foo) foo();" don't report overflow
15170                  on a branch to zero when foo is undefined.  */
15171               if (!warned
15172                   && (reloc_dest == DEST_STUB
15173                       || !(h != NULL
15174                            && (h->elf.root.type == bfd_link_hash_undefweak
15175                                || h->elf.root.type == bfd_link_hash_undefined)
15176                            && is_branch_reloc (r_type))))
15177                 info->callbacks->reloc_overflow (info, &h->elf.root,
15178                                                  sym_name, reloc_name,
15179                                                  orig_rel.r_addend,
15180                                                  input_bfd, input_section,
15181                                                  rel->r_offset);
15182             }
15183           else
15184             {
15185               info->callbacks->einfo
15186                 (_("%P: %H: %s against `%T': error %d\n"),
15187                  input_bfd, input_section, rel->r_offset,
15188                  reloc_name, sym_name, (int) r);
15189               ret = FALSE;
15190             }
15191           if (more_info != NULL)
15192             free (more_info);
15193         }
15194     copy_reloc:
15195       if (wrel != rel)
15196         *wrel = *rel;
15197     }
15198
15199   if (wrel != rel)
15200     {
15201       Elf_Internal_Shdr *rel_hdr;
15202       size_t deleted = rel - wrel;
15203
15204       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15205       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15206       if (rel_hdr->sh_size == 0)
15207         {
15208           /* It is too late to remove an empty reloc section.  Leave
15209              one NONE reloc.
15210              ??? What is wrong with an empty section???  */
15211           rel_hdr->sh_size = rel_hdr->sh_entsize;
15212           deleted -= 1;
15213         }
15214       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15215       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15216       input_section->reloc_count -= deleted;
15217     }
15218
15219   /* If we're emitting relocations, then shortly after this function
15220      returns, reloc offsets and addends for this section will be
15221      adjusted.  Worse, reloc symbol indices will be for the output
15222      file rather than the input.  Save a copy of the relocs for
15223      opd_entry_value.  */
15224   if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15225     {
15226       bfd_size_type amt;
15227       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15228       rel = bfd_alloc (input_bfd, amt);
15229       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15230       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15231       if (rel == NULL)
15232         return FALSE;
15233       memcpy (rel, relocs, amt);
15234     }
15235   return ret;
15236 }
15237
15238 /* Adjust the value of any local symbols in opd sections.  */
15239
15240 static int
15241 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15242                               const char *name ATTRIBUTE_UNUSED,
15243                               Elf_Internal_Sym *elfsym,
15244                               asection *input_sec,
15245                               struct elf_link_hash_entry *h)
15246 {
15247   struct _opd_sec_data *opd;
15248   long adjust;
15249   bfd_vma value;
15250
15251   if (h != NULL)
15252     return 1;
15253
15254   opd = get_opd_info (input_sec);
15255   if (opd == NULL || opd->adjust == NULL)
15256     return 1;
15257
15258   value = elfsym->st_value - input_sec->output_offset;
15259   if (!bfd_link_relocatable (info))
15260     value -= input_sec->output_section->vma;
15261
15262   adjust = opd->adjust[OPD_NDX (value)];
15263   if (adjust == -1)
15264     return 2;
15265
15266   elfsym->st_value += adjust;
15267   return 1;
15268 }
15269
15270 /* Finish up dynamic symbol handling.  We set the contents of various
15271    dynamic sections here.  */
15272
15273 static bfd_boolean
15274 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15275                                  struct bfd_link_info *info,
15276                                  struct elf_link_hash_entry *h,
15277                                  Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
15278 {
15279   struct ppc_link_hash_table *htab;
15280   struct plt_entry *ent;
15281   Elf_Internal_Rela rela;
15282   bfd_byte *loc;
15283
15284   htab = ppc_hash_table (info);
15285   if (htab == NULL)
15286     return FALSE;
15287
15288   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15289     if (ent->plt.offset != (bfd_vma) -1)
15290       {
15291         /* This symbol has an entry in the procedure linkage
15292            table.  Set it up.  */
15293         if (!htab->elf.dynamic_sections_created
15294             || h->dynindx == -1)
15295           {
15296             BFD_ASSERT (h->type == STT_GNU_IFUNC
15297                         && h->def_regular
15298                         && (h->root.type == bfd_link_hash_defined
15299                             || h->root.type == bfd_link_hash_defweak));
15300             rela.r_offset = (htab->elf.iplt->output_section->vma
15301                              + htab->elf.iplt->output_offset
15302                              + ent->plt.offset);
15303             if (htab->opd_abi)
15304               rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15305             else
15306               rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15307             rela.r_addend = (h->root.u.def.value
15308                              + h->root.u.def.section->output_offset
15309                              + h->root.u.def.section->output_section->vma
15310                              + ent->addend);
15311             loc = (htab->elf.irelplt->contents
15312                    + (htab->elf.irelplt->reloc_count++
15313                       * sizeof (Elf64_External_Rela)));
15314           }
15315         else
15316           {
15317             rela.r_offset = (htab->elf.splt->output_section->vma
15318                              + htab->elf.splt->output_offset
15319                              + ent->plt.offset);
15320             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15321             rela.r_addend = ent->addend;
15322             loc = (htab->elf.srelplt->contents
15323                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15324                       / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
15325           }
15326         bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15327
15328         if (!htab->opd_abi)
15329           {
15330             if (!h->def_regular)
15331               {
15332                 /* Mark the symbol as undefined, rather than as
15333                    defined in glink.  Leave the value if there were
15334                    any relocations where pointer equality matters
15335                    (this is a clue for the dynamic linker, to make
15336                    function pointer comparisons work between an
15337                    application and shared library), otherwise set it
15338                    to zero.  */
15339                 sym->st_shndx = SHN_UNDEF;
15340                 if (!h->pointer_equality_needed)
15341                   sym->st_value = 0;
15342                 else if (!h->ref_regular_nonweak)
15343                   {
15344                     /* This breaks function pointer comparisons, but
15345                        that is better than breaking tests for a NULL
15346                        function pointer.  */
15347                     sym->st_value = 0;
15348                   }
15349               }
15350           }
15351       }
15352
15353   if (h->needs_copy)
15354     {
15355       /* This symbol needs a copy reloc.  Set it up.  */
15356
15357       if (h->dynindx == -1
15358           || (h->root.type != bfd_link_hash_defined
15359               && h->root.type != bfd_link_hash_defweak)
15360           || htab->relbss == NULL)
15361         abort ();
15362
15363       rela.r_offset = (h->root.u.def.value
15364                        + h->root.u.def.section->output_section->vma
15365                        + h->root.u.def.section->output_offset);
15366       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15367       rela.r_addend = 0;
15368       loc = htab->relbss->contents;
15369       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
15370       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15371     }
15372
15373   return TRUE;
15374 }
15375
15376 /* Used to decide how to sort relocs in an optimal manner for the
15377    dynamic linker, before writing them out.  */
15378
15379 static enum elf_reloc_type_class
15380 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15381                             const asection *rel_sec,
15382                             const Elf_Internal_Rela *rela)
15383 {
15384   enum elf_ppc64_reloc_type r_type;
15385   struct ppc_link_hash_table *htab = ppc_hash_table (info);
15386
15387   if (rel_sec == htab->elf.irelplt)
15388     return reloc_class_ifunc;
15389
15390   r_type = ELF64_R_TYPE (rela->r_info);
15391   switch (r_type)
15392     {
15393     case R_PPC64_RELATIVE:
15394       return reloc_class_relative;
15395     case R_PPC64_JMP_SLOT:
15396       return reloc_class_plt;
15397     case R_PPC64_COPY:
15398       return reloc_class_copy;
15399     default:
15400       return reloc_class_normal;
15401     }
15402 }
15403
15404 /* Finish up the dynamic sections.  */
15405
15406 static bfd_boolean
15407 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15408                                    struct bfd_link_info *info)
15409 {
15410   struct ppc_link_hash_table *htab;
15411   bfd *dynobj;
15412   asection *sdyn;
15413
15414   htab = ppc_hash_table (info);
15415   if (htab == NULL)
15416     return FALSE;
15417
15418   dynobj = htab->elf.dynobj;
15419   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15420
15421   if (htab->elf.dynamic_sections_created)
15422     {
15423       Elf64_External_Dyn *dyncon, *dynconend;
15424
15425       if (sdyn == NULL || htab->elf.sgot == NULL)
15426         abort ();
15427
15428       dyncon = (Elf64_External_Dyn *) sdyn->contents;
15429       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15430       for (; dyncon < dynconend; dyncon++)
15431         {
15432           Elf_Internal_Dyn dyn;
15433           asection *s;
15434
15435           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15436
15437           switch (dyn.d_tag)
15438             {
15439             default:
15440               continue;
15441
15442             case DT_PPC64_GLINK:
15443               s = htab->glink;
15444               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15445               /* We stupidly defined DT_PPC64_GLINK to be the start
15446                  of glink rather than the first entry point, which is
15447                  what ld.so needs, and now have a bigger stub to
15448                  support automatic multiple TOCs.  */
15449               dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
15450               break;
15451
15452             case DT_PPC64_OPD:
15453               s = bfd_get_section_by_name (output_bfd, ".opd");
15454               if (s == NULL)
15455                 continue;
15456               dyn.d_un.d_ptr = s->vma;
15457               break;
15458
15459             case DT_PPC64_OPT:
15460               if (htab->do_multi_toc && htab->multi_toc_needed)
15461                 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15462               break;
15463
15464             case DT_PPC64_OPDSZ:
15465               s = bfd_get_section_by_name (output_bfd, ".opd");
15466               if (s == NULL)
15467                 continue;
15468               dyn.d_un.d_val = s->size;
15469               break;
15470
15471             case DT_PLTGOT:
15472               s = htab->elf.splt;
15473               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15474               break;
15475
15476             case DT_JMPREL:
15477               s = htab->elf.srelplt;
15478               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15479               break;
15480
15481             case DT_PLTRELSZ:
15482               dyn.d_un.d_val = htab->elf.srelplt->size;
15483               break;
15484
15485             case DT_RELASZ:
15486               /* Don't count procedure linkage table relocs in the
15487                  overall reloc count.  */
15488               s = htab->elf.srelplt;
15489               if (s == NULL)
15490                 continue;
15491               dyn.d_un.d_val -= s->size;
15492               break;
15493
15494             case DT_RELA:
15495               /* We may not be using the standard ELF linker script.
15496                  If .rela.plt is the first .rela section, we adjust
15497                  DT_RELA to not include it.  */
15498               s = htab->elf.srelplt;
15499               if (s == NULL)
15500                 continue;
15501               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
15502                 continue;
15503               dyn.d_un.d_ptr += s->size;
15504               break;
15505             }
15506
15507           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15508         }
15509     }
15510
15511   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0)
15512     {
15513       /* Fill in the first entry in the global offset table.
15514          We use it to hold the link-time TOCbase.  */
15515       bfd_put_64 (output_bfd,
15516                   elf_gp (output_bfd) + TOC_BASE_OFF,
15517                   htab->elf.sgot->contents);
15518
15519       /* Set .got entry size.  */
15520       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
15521     }
15522
15523   if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
15524     {
15525       /* Set .plt entry size.  */
15526       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15527         = PLT_ENTRY_SIZE (htab);
15528     }
15529
15530   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15531      brlt ourselves if emitrelocations.  */
15532   if (htab->brlt != NULL
15533       && htab->brlt->reloc_count != 0
15534       && !_bfd_elf_link_output_relocs (output_bfd,
15535                                        htab->brlt,
15536                                        elf_section_data (htab->brlt)->rela.hdr,
15537                                        elf_section_data (htab->brlt)->relocs,
15538                                        NULL))
15539     return FALSE;
15540
15541   if (htab->glink != NULL
15542       && htab->glink->reloc_count != 0
15543       && !_bfd_elf_link_output_relocs (output_bfd,
15544                                        htab->glink,
15545                                        elf_section_data (htab->glink)->rela.hdr,
15546                                        elf_section_data (htab->glink)->relocs,
15547                                        NULL))
15548     return FALSE;
15549
15550   if (htab->glink_eh_frame != NULL
15551       && htab->glink_eh_frame->size != 0)
15552     {
15553       bfd_vma val;
15554       bfd_byte *p;
15555       asection *stub_sec;
15556
15557       p = htab->glink_eh_frame->contents + sizeof (glink_eh_frame_cie);
15558       for (stub_sec = htab->params->stub_bfd->sections;
15559            stub_sec != NULL;
15560            stub_sec = stub_sec->next)
15561         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
15562           {
15563             /* FDE length.  */
15564             p += 4;
15565             /* CIE pointer.  */
15566             p += 4;
15567             /* Offset to stub section.  */
15568             val = (stub_sec->output_section->vma
15569                    + stub_sec->output_offset);
15570             val -= (htab->glink_eh_frame->output_section->vma
15571                     + htab->glink_eh_frame->output_offset
15572                     + (p - htab->glink_eh_frame->contents));
15573             if (val + 0x80000000 > 0xffffffff)
15574               {
15575                 info->callbacks->einfo
15576                   (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15577                    stub_sec->name);
15578                 return FALSE;
15579               }
15580             bfd_put_32 (dynobj, val, p);
15581             p += 4;
15582             /* stub section size.  */
15583             p += 4;
15584             /* Augmentation.  */
15585             p += 1;
15586             /* Pad.  */
15587             p += 7;
15588           }
15589       if (htab->glink != NULL && htab->glink->size != 0)
15590         {
15591           /* FDE length.  */
15592           p += 4;
15593           /* CIE pointer.  */
15594           p += 4;
15595           /* Offset to .glink.  */
15596           val = (htab->glink->output_section->vma
15597                  + htab->glink->output_offset
15598                  + 8);
15599           val -= (htab->glink_eh_frame->output_section->vma
15600                   + htab->glink_eh_frame->output_offset
15601                   + (p - htab->glink_eh_frame->contents));
15602           if (val + 0x80000000 > 0xffffffff)
15603             {
15604               info->callbacks->einfo
15605                 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15606                  htab->glink->name);
15607               return FALSE;
15608             }
15609           bfd_put_32 (dynobj, val, p);
15610           p += 4;
15611           /* .glink size.  */
15612           p += 4;
15613           /* Augmentation.  */
15614           p += 1;
15615           /* Ops.  */
15616           p += 7;
15617         }
15618
15619       if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15620           && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15621                                                htab->glink_eh_frame,
15622                                                htab->glink_eh_frame->contents))
15623         return FALSE;
15624     }
15625
15626   /* We need to handle writing out multiple GOT sections ourselves,
15627      since we didn't add them to DYNOBJ.  We know dynobj is the first
15628      bfd.  */
15629   while ((dynobj = dynobj->link.next) != NULL)
15630     {
15631       asection *s;
15632
15633       if (!is_ppc64_elf (dynobj))
15634         continue;
15635
15636       s = ppc64_elf_tdata (dynobj)->got;
15637       if (s != NULL
15638           && s->size != 0
15639           && s->output_section != bfd_abs_section_ptr
15640           && !bfd_set_section_contents (output_bfd, s->output_section,
15641                                         s->contents, s->output_offset,
15642                                         s->size))
15643         return FALSE;
15644       s = ppc64_elf_tdata (dynobj)->relgot;
15645       if (s != NULL
15646           && s->size != 0
15647           && s->output_section != bfd_abs_section_ptr
15648           && !bfd_set_section_contents (output_bfd, s->output_section,
15649                                         s->contents, s->output_offset,
15650                                         s->size))
15651         return FALSE;
15652     }
15653
15654   return TRUE;
15655 }
15656
15657 #include "elf64-target.h"
15658
15659 /* FreeBSD support */
15660
15661 #undef  TARGET_LITTLE_SYM
15662 #undef  TARGET_LITTLE_NAME
15663
15664 #undef  TARGET_BIG_SYM
15665 #define TARGET_BIG_SYM  powerpc_elf64_fbsd_vec
15666 #undef  TARGET_BIG_NAME
15667 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15668
15669 #undef  ELF_OSABI
15670 #define ELF_OSABI       ELFOSABI_FREEBSD
15671
15672 #undef  elf64_bed
15673 #define elf64_bed       elf64_powerpc_fbsd_bed
15674
15675 #include "elf64-target.h"
15676