Optimise PowerPC64 r2 adjusting stubs
[external/binutils.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2    Copyright (C) 1999-2015 Free Software Foundation, Inc.
3    Written by Linus Nordberg, Swox AB <info@swox.com>,
4    based on elf32-ppc.c by Ian Lance Taylor.
5    Largely rewritten by Alan Modra.
6
7    This file is part of BFD, the Binary File Descriptor library.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License along
20    with this program; if not, write to the Free Software Foundation, Inc.,
21    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
22
23
24 /* The 64-bit PowerPC ELF ABI may be found at
25    http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26    http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
27
28 #include "sysdep.h"
29 #include <stdarg.h>
30 #include "bfd.h"
31 #include "bfdlink.h"
32 #include "libbfd.h"
33 #include "elf-bfd.h"
34 #include "elf/ppc64.h"
35 #include "elf64-ppc.h"
36 #include "dwarf2.h"
37
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
55   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_vma opd_entry_value
57   (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
58
59 #define TARGET_LITTLE_SYM       powerpc_elf64_le_vec
60 #define TARGET_LITTLE_NAME      "elf64-powerpcle"
61 #define TARGET_BIG_SYM          powerpc_elf64_vec
62 #define TARGET_BIG_NAME         "elf64-powerpc"
63 #define ELF_ARCH                bfd_arch_powerpc
64 #define ELF_TARGET_ID           PPC64_ELF_DATA
65 #define ELF_MACHINE_CODE        EM_PPC64
66 #define ELF_MAXPAGESIZE         0x10000
67 #define ELF_COMMONPAGESIZE      0x10000
68 #define elf_info_to_howto       ppc64_elf_info_to_howto
69
70 #define elf_backend_want_got_sym 0
71 #define elf_backend_want_plt_sym 0
72 #define elf_backend_plt_alignment 3
73 #define elf_backend_plt_not_loaded 1
74 #define elf_backend_got_header_size 8
75 #define elf_backend_can_gc_sections 1
76 #define elf_backend_can_refcount 1
77 #define elf_backend_rela_normal 1
78 #define elf_backend_default_execstack 0
79
80 #define bfd_elf64_mkobject                    ppc64_elf_mkobject
81 #define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
82 #define bfd_elf64_bfd_reloc_name_lookup       ppc64_elf_reloc_name_lookup
83 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
84 #define bfd_elf64_bfd_print_private_bfd_data  ppc64_elf_print_private_bfd_data
85 #define bfd_elf64_new_section_hook            ppc64_elf_new_section_hook
86 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
87 #define bfd_elf64_get_synthetic_symtab        ppc64_elf_get_synthetic_symtab
88 #define bfd_elf64_bfd_link_just_syms          ppc64_elf_link_just_syms
89
90 #define elf_backend_object_p                  ppc64_elf_object_p
91 #define elf_backend_grok_prstatus             ppc64_elf_grok_prstatus
92 #define elf_backend_grok_psinfo               ppc64_elf_grok_psinfo
93 #define elf_backend_write_core_note           ppc64_elf_write_core_note
94 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
95 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
96 #define elf_backend_add_symbol_hook           ppc64_elf_add_symbol_hook
97 #define elf_backend_check_directives          ppc64_elf_before_check_relocs
98 #define elf_backend_notice_as_needed          ppc64_elf_notice_as_needed
99 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
100 #define elf_backend_check_relocs              ppc64_elf_check_relocs
101 #define elf_backend_gc_keep                   ppc64_elf_gc_keep
102 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
103 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
104 #define elf_backend_gc_sweep_hook             ppc64_elf_gc_sweep_hook
105 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
106 #define elf_backend_hide_symbol               ppc64_elf_hide_symbol
107 #define elf_backend_maybe_function_sym        ppc64_elf_maybe_function_sym
108 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
109 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
110 #define elf_backend_hash_symbol               ppc64_elf_hash_symbol
111 #define elf_backend_init_index_section        _bfd_elf_init_2_index_sections
112 #define elf_backend_action_discarded          ppc64_elf_action_discarded
113 #define elf_backend_relocate_section          ppc64_elf_relocate_section
114 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
115 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
116 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
117 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
118 #define elf_backend_special_sections          ppc64_elf_special_sections
119 #define elf_backend_merge_symbol_attribute    ppc64_elf_merge_symbol_attribute
120
121 /* The name of the dynamic interpreter.  This is put in the .interp
122    section.  */
123 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
124
125 /* The size in bytes of an entry in the procedure linkage table.  */
126 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
127
128 /* The initial size of the plt reserved for the dynamic linker.  */
129 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
130
131 /* Offsets to some stack save slots.  */
132 #define STK_LR 16
133 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
134 /* This one is dodgy.  ELFv2 does not have a linker word, so use the
135    CR save slot.  Used only by optimised __tls_get_addr call stub,
136    relying on __tls_get_addr_opt not saving CR..  */
137 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
138
139 /* TOC base pointers offset from start of TOC.  */
140 #define TOC_BASE_OFF    0x8000
141 /* TOC base alignment.  */
142 #define TOC_BASE_ALIGN  256
143
144 /* Offset of tp and dtp pointers from start of TLS block.  */
145 #define TP_OFFSET       0x7000
146 #define DTP_OFFSET      0x8000
147
148 /* .plt call stub instructions.  The normal stub is like this, but
149    sometimes the .plt entry crosses a 64k boundary and we need to
150    insert an addi to adjust r11.  */
151 #define STD_R2_0R1      0xf8410000      /* std   %r2,0+40(%r1)       */
152 #define ADDIS_R11_R2    0x3d620000      /* addis %r11,%r2,xxx@ha     */
153 #define LD_R12_0R11     0xe98b0000      /* ld    %r12,xxx+0@l(%r11)  */
154 #define MTCTR_R12       0x7d8903a6      /* mtctr %r12                */
155 #define LD_R2_0R11      0xe84b0000      /* ld    %r2,xxx+8@l(%r11)   */
156 #define LD_R11_0R11     0xe96b0000      /* ld    %r11,xxx+16@l(%r11) */
157 #define BCTR            0x4e800420      /* bctr                      */
158
159 #define ADDI_R11_R11    0x396b0000      /* addi %r11,%r11,off@l  */
160 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
161 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
162
163 #define XOR_R2_R12_R12  0x7d826278      /* xor   %r2,%r12,%r12   */
164 #define ADD_R11_R11_R2  0x7d6b1214      /* add   %r11,%r11,%r2   */
165 #define XOR_R11_R12_R12 0x7d8b6278      /* xor   %r11,%r12,%r12  */
166 #define ADD_R2_R2_R11   0x7c425a14      /* add   %r2,%r2,%r11    */
167 #define CMPLDI_R2_0     0x28220000      /* cmpldi %r2,0          */
168 #define BNECTR          0x4ca20420      /* bnectr+               */
169 #define BNECTR_P4       0x4ce20420      /* bnectr+               */
170
171 #define LD_R12_0R2      0xe9820000      /* ld    %r12,xxx+0(%r2) */
172 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
173 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
174
175 #define LD_R2_0R1       0xe8410000      /* ld    %r2,0(%r1)      */
176
177 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha     */
178 #define ADDIS_R12_R12   0x3d8c0000      /* addis %r12,%r12,xxx@ha */
179 #define LD_R12_0R12     0xe98c0000      /* ld    %r12,xxx@l(%r12) */
180
181 /* glink call stub instructions.  We enter with the index in R0.  */
182 #define GLINK_CALL_STUB_SIZE (16*4)
183                                         /* 0:                           */
184                                         /*  .quad plt0-1f               */
185                                         /* __glink:                     */
186 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
187 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
188                                         /* 1:                           */
189 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
190                                         /*  ld %2,(0b-1b)(%11)          */
191 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
192 #define ADD_R11_R2_R11  0x7d625a14      /*  add %11,%2,%11              */
193                                         /*  ld %12,0(%11)               */
194                                         /*  ld %2,8(%11)                */
195                                         /*  mtctr %12                   */
196                                         /*  ld %11,16(%11)              */
197                                         /*  bctr                        */
198 #define MFLR_R0         0x7c0802a6      /*  mflr %r0                    */
199 #define MTLR_R0         0x7c0803a6      /*  mtlr %r0                    */
200 #define SUB_R12_R12_R11 0x7d8b6050      /*  subf %r12,%r11,%r12         */
201 #define ADDI_R0_R12     0x380c0000      /*  addi %r0,%r12,0             */
202 #define SRDI_R0_R0_2    0x7800f082      /*  rldicl %r0,%r0,62,2         */
203
204 /* Pad with this.  */
205 #define NOP             0x60000000
206
207 /* Some other nops.  */
208 #define CROR_151515     0x4def7b82
209 #define CROR_313131     0x4ffffb82
210
211 /* .glink entries for the first 32k functions are two instructions.  */
212 #define LI_R0_0         0x38000000      /* li    %r0,0          */
213 #define B_DOT           0x48000000      /* b     .              */
214
215 /* After that, we need two instructions to load the index, followed by
216    a branch.  */
217 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
218 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
219
220 /* Instructions used by the save and restore reg functions.  */
221 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
222 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
223 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
224 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
225 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
226 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
227 #define LI_R12_0        0x39800000      /* li    %r12,0         */
228 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
229 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
230 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
231 #define BLR             0x4e800020      /* blr                  */
232
233 /* Since .opd is an array of descriptors and each entry will end up
234    with identical R_PPC64_RELATIVE relocs, there is really no need to
235    propagate .opd relocs;  The dynamic linker should be taught to
236    relocate .opd without reloc entries.  */
237 #ifndef NO_OPD_RELOCS
238 #define NO_OPD_RELOCS 0
239 #endif
240
241 static inline int
242 abiversion (bfd *abfd)
243 {
244   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
245 }
246
247 static inline void
248 set_abiversion (bfd *abfd, int ver)
249 {
250   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
251   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
252 }
253 \f
254 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
255
256 /* Relocation HOWTO's.  */
257 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
258
259 static reloc_howto_type ppc64_elf_howto_raw[] = {
260   /* This reloc does nothing.  */
261   HOWTO (R_PPC64_NONE,          /* type */
262          0,                     /* rightshift */
263          3,                     /* size (0 = byte, 1 = short, 2 = long) */
264          0,                     /* bitsize */
265          FALSE,                 /* pc_relative */
266          0,                     /* bitpos */
267          complain_overflow_dont, /* complain_on_overflow */
268          bfd_elf_generic_reloc, /* special_function */
269          "R_PPC64_NONE",        /* name */
270          FALSE,                 /* partial_inplace */
271          0,                     /* src_mask */
272          0,                     /* dst_mask */
273          FALSE),                /* pcrel_offset */
274
275   /* A standard 32 bit relocation.  */
276   HOWTO (R_PPC64_ADDR32,        /* type */
277          0,                     /* rightshift */
278          2,                     /* size (0 = byte, 1 = short, 2 = long) */
279          32,                    /* bitsize */
280          FALSE,                 /* pc_relative */
281          0,                     /* bitpos */
282          complain_overflow_bitfield, /* complain_on_overflow */
283          bfd_elf_generic_reloc, /* special_function */
284          "R_PPC64_ADDR32",      /* name */
285          FALSE,                 /* partial_inplace */
286          0,                     /* src_mask */
287          0xffffffff,            /* dst_mask */
288          FALSE),                /* pcrel_offset */
289
290   /* An absolute 26 bit branch; the lower two bits must be zero.
291      FIXME: we don't check that, we just clear them.  */
292   HOWTO (R_PPC64_ADDR24,        /* type */
293          0,                     /* rightshift */
294          2,                     /* size (0 = byte, 1 = short, 2 = long) */
295          26,                    /* bitsize */
296          FALSE,                 /* pc_relative */
297          0,                     /* bitpos */
298          complain_overflow_bitfield, /* complain_on_overflow */
299          bfd_elf_generic_reloc, /* special_function */
300          "R_PPC64_ADDR24",      /* name */
301          FALSE,                 /* partial_inplace */
302          0,                     /* src_mask */
303          0x03fffffc,            /* dst_mask */
304          FALSE),                /* pcrel_offset */
305
306   /* A standard 16 bit relocation.  */
307   HOWTO (R_PPC64_ADDR16,        /* type */
308          0,                     /* rightshift */
309          1,                     /* size (0 = byte, 1 = short, 2 = long) */
310          16,                    /* bitsize */
311          FALSE,                 /* pc_relative */
312          0,                     /* bitpos */
313          complain_overflow_bitfield, /* complain_on_overflow */
314          bfd_elf_generic_reloc, /* special_function */
315          "R_PPC64_ADDR16",      /* name */
316          FALSE,                 /* partial_inplace */
317          0,                     /* src_mask */
318          0xffff,                /* dst_mask */
319          FALSE),                /* pcrel_offset */
320
321   /* A 16 bit relocation without overflow.  */
322   HOWTO (R_PPC64_ADDR16_LO,     /* type */
323          0,                     /* rightshift */
324          1,                     /* size (0 = byte, 1 = short, 2 = long) */
325          16,                    /* bitsize */
326          FALSE,                 /* pc_relative */
327          0,                     /* bitpos */
328          complain_overflow_dont,/* complain_on_overflow */
329          bfd_elf_generic_reloc, /* special_function */
330          "R_PPC64_ADDR16_LO",   /* name */
331          FALSE,                 /* partial_inplace */
332          0,                     /* src_mask */
333          0xffff,                /* dst_mask */
334          FALSE),                /* pcrel_offset */
335
336   /* Bits 16-31 of an address.  */
337   HOWTO (R_PPC64_ADDR16_HI,     /* type */
338          16,                    /* rightshift */
339          1,                     /* size (0 = byte, 1 = short, 2 = long) */
340          16,                    /* bitsize */
341          FALSE,                 /* pc_relative */
342          0,                     /* bitpos */
343          complain_overflow_signed, /* complain_on_overflow */
344          bfd_elf_generic_reloc, /* special_function */
345          "R_PPC64_ADDR16_HI",   /* name */
346          FALSE,                 /* partial_inplace */
347          0,                     /* src_mask */
348          0xffff,                /* dst_mask */
349          FALSE),                /* pcrel_offset */
350
351   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
352      bits, treated as a signed number, is negative.  */
353   HOWTO (R_PPC64_ADDR16_HA,     /* type */
354          16,                    /* rightshift */
355          1,                     /* size (0 = byte, 1 = short, 2 = long) */
356          16,                    /* bitsize */
357          FALSE,                 /* pc_relative */
358          0,                     /* bitpos */
359          complain_overflow_signed, /* complain_on_overflow */
360          ppc64_elf_ha_reloc,    /* special_function */
361          "R_PPC64_ADDR16_HA",   /* name */
362          FALSE,                 /* partial_inplace */
363          0,                     /* src_mask */
364          0xffff,                /* dst_mask */
365          FALSE),                /* pcrel_offset */
366
367   /* An absolute 16 bit branch; the lower two bits must be zero.
368      FIXME: we don't check that, we just clear them.  */
369   HOWTO (R_PPC64_ADDR14,        /* type */
370          0,                     /* rightshift */
371          2,                     /* size (0 = byte, 1 = short, 2 = long) */
372          16,                    /* bitsize */
373          FALSE,                 /* pc_relative */
374          0,                     /* bitpos */
375          complain_overflow_signed, /* complain_on_overflow */
376          ppc64_elf_branch_reloc, /* special_function */
377          "R_PPC64_ADDR14",      /* name */
378          FALSE,                 /* partial_inplace */
379          0,                     /* src_mask */
380          0x0000fffc,            /* dst_mask */
381          FALSE),                /* pcrel_offset */
382
383   /* An absolute 16 bit branch, for which bit 10 should be set to
384      indicate that the branch is expected to be taken.  The lower two
385      bits must be zero.  */
386   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
387          0,                     /* rightshift */
388          2,                     /* size (0 = byte, 1 = short, 2 = long) */
389          16,                    /* bitsize */
390          FALSE,                 /* pc_relative */
391          0,                     /* bitpos */
392          complain_overflow_signed, /* complain_on_overflow */
393          ppc64_elf_brtaken_reloc, /* special_function */
394          "R_PPC64_ADDR14_BRTAKEN",/* name */
395          FALSE,                 /* partial_inplace */
396          0,                     /* src_mask */
397          0x0000fffc,            /* dst_mask */
398          FALSE),                /* pcrel_offset */
399
400   /* An absolute 16 bit branch, for which bit 10 should be set to
401      indicate that the branch is not expected to be taken.  The lower
402      two bits must be zero.  */
403   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
404          0,                     /* rightshift */
405          2,                     /* size (0 = byte, 1 = short, 2 = long) */
406          16,                    /* bitsize */
407          FALSE,                 /* pc_relative */
408          0,                     /* bitpos */
409          complain_overflow_signed, /* complain_on_overflow */
410          ppc64_elf_brtaken_reloc, /* special_function */
411          "R_PPC64_ADDR14_BRNTAKEN",/* name */
412          FALSE,                 /* partial_inplace */
413          0,                     /* src_mask */
414          0x0000fffc,            /* dst_mask */
415          FALSE),                /* pcrel_offset */
416
417   /* A relative 26 bit branch; the lower two bits must be zero.  */
418   HOWTO (R_PPC64_REL24,         /* type */
419          0,                     /* rightshift */
420          2,                     /* size (0 = byte, 1 = short, 2 = long) */
421          26,                    /* bitsize */
422          TRUE,                  /* pc_relative */
423          0,                     /* bitpos */
424          complain_overflow_signed, /* complain_on_overflow */
425          ppc64_elf_branch_reloc, /* special_function */
426          "R_PPC64_REL24",       /* name */
427          FALSE,                 /* partial_inplace */
428          0,                     /* src_mask */
429          0x03fffffc,            /* dst_mask */
430          TRUE),                 /* pcrel_offset */
431
432   /* A relative 16 bit branch; the lower two bits must be zero.  */
433   HOWTO (R_PPC64_REL14,         /* type */
434          0,                     /* rightshift */
435          2,                     /* size (0 = byte, 1 = short, 2 = long) */
436          16,                    /* bitsize */
437          TRUE,                  /* pc_relative */
438          0,                     /* bitpos */
439          complain_overflow_signed, /* complain_on_overflow */
440          ppc64_elf_branch_reloc, /* special_function */
441          "R_PPC64_REL14",       /* name */
442          FALSE,                 /* partial_inplace */
443          0,                     /* src_mask */
444          0x0000fffc,            /* dst_mask */
445          TRUE),                 /* pcrel_offset */
446
447   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
448      the branch is expected to be taken.  The lower two bits must be
449      zero.  */
450   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
451          0,                     /* rightshift */
452          2,                     /* size (0 = byte, 1 = short, 2 = long) */
453          16,                    /* bitsize */
454          TRUE,                  /* pc_relative */
455          0,                     /* bitpos */
456          complain_overflow_signed, /* complain_on_overflow */
457          ppc64_elf_brtaken_reloc, /* special_function */
458          "R_PPC64_REL14_BRTAKEN", /* name */
459          FALSE,                 /* partial_inplace */
460          0,                     /* src_mask */
461          0x0000fffc,            /* dst_mask */
462          TRUE),                 /* pcrel_offset */
463
464   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
465      the branch is not expected to be taken.  The lower two bits must
466      be zero.  */
467   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
468          0,                     /* rightshift */
469          2,                     /* size (0 = byte, 1 = short, 2 = long) */
470          16,                    /* bitsize */
471          TRUE,                  /* pc_relative */
472          0,                     /* bitpos */
473          complain_overflow_signed, /* complain_on_overflow */
474          ppc64_elf_brtaken_reloc, /* special_function */
475          "R_PPC64_REL14_BRNTAKEN",/* name */
476          FALSE,                 /* partial_inplace */
477          0,                     /* src_mask */
478          0x0000fffc,            /* dst_mask */
479          TRUE),                 /* pcrel_offset */
480
481   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
482      symbol.  */
483   HOWTO (R_PPC64_GOT16,         /* type */
484          0,                     /* rightshift */
485          1,                     /* size (0 = byte, 1 = short, 2 = long) */
486          16,                    /* bitsize */
487          FALSE,                 /* pc_relative */
488          0,                     /* bitpos */
489          complain_overflow_signed, /* complain_on_overflow */
490          ppc64_elf_unhandled_reloc, /* special_function */
491          "R_PPC64_GOT16",       /* name */
492          FALSE,                 /* partial_inplace */
493          0,                     /* src_mask */
494          0xffff,                /* dst_mask */
495          FALSE),                /* pcrel_offset */
496
497   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
498      the symbol.  */
499   HOWTO (R_PPC64_GOT16_LO,      /* type */
500          0,                     /* rightshift */
501          1,                     /* size (0 = byte, 1 = short, 2 = long) */
502          16,                    /* bitsize */
503          FALSE,                 /* pc_relative */
504          0,                     /* bitpos */
505          complain_overflow_dont, /* complain_on_overflow */
506          ppc64_elf_unhandled_reloc, /* special_function */
507          "R_PPC64_GOT16_LO",    /* name */
508          FALSE,                 /* partial_inplace */
509          0,                     /* src_mask */
510          0xffff,                /* dst_mask */
511          FALSE),                /* pcrel_offset */
512
513   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
514      the symbol.  */
515   HOWTO (R_PPC64_GOT16_HI,      /* type */
516          16,                    /* rightshift */
517          1,                     /* size (0 = byte, 1 = short, 2 = long) */
518          16,                    /* bitsize */
519          FALSE,                 /* pc_relative */
520          0,                     /* bitpos */
521          complain_overflow_signed,/* complain_on_overflow */
522          ppc64_elf_unhandled_reloc, /* special_function */
523          "R_PPC64_GOT16_HI",    /* name */
524          FALSE,                 /* partial_inplace */
525          0,                     /* src_mask */
526          0xffff,                /* dst_mask */
527          FALSE),                /* pcrel_offset */
528
529   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
530      the symbol.  */
531   HOWTO (R_PPC64_GOT16_HA,      /* type */
532          16,                    /* rightshift */
533          1,                     /* size (0 = byte, 1 = short, 2 = long) */
534          16,                    /* bitsize */
535          FALSE,                 /* pc_relative */
536          0,                     /* bitpos */
537          complain_overflow_signed,/* complain_on_overflow */
538          ppc64_elf_unhandled_reloc, /* special_function */
539          "R_PPC64_GOT16_HA",    /* name */
540          FALSE,                 /* partial_inplace */
541          0,                     /* src_mask */
542          0xffff,                /* dst_mask */
543          FALSE),                /* pcrel_offset */
544
545   /* This is used only by the dynamic linker.  The symbol should exist
546      both in the object being run and in some shared library.  The
547      dynamic linker copies the data addressed by the symbol from the
548      shared library into the object, because the object being
549      run has to have the data at some particular address.  */
550   HOWTO (R_PPC64_COPY,          /* type */
551          0,                     /* rightshift */
552          0,                     /* this one is variable size */
553          0,                     /* bitsize */
554          FALSE,                 /* pc_relative */
555          0,                     /* bitpos */
556          complain_overflow_dont, /* complain_on_overflow */
557          ppc64_elf_unhandled_reloc, /* special_function */
558          "R_PPC64_COPY",        /* name */
559          FALSE,                 /* partial_inplace */
560          0,                     /* src_mask */
561          0,                     /* dst_mask */
562          FALSE),                /* pcrel_offset */
563
564   /* Like R_PPC64_ADDR64, but used when setting global offset table
565      entries.  */
566   HOWTO (R_PPC64_GLOB_DAT,      /* type */
567          0,                     /* rightshift */
568          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
569          64,                    /* bitsize */
570          FALSE,                 /* pc_relative */
571          0,                     /* bitpos */
572          complain_overflow_dont, /* complain_on_overflow */
573          ppc64_elf_unhandled_reloc,  /* special_function */
574          "R_PPC64_GLOB_DAT",    /* name */
575          FALSE,                 /* partial_inplace */
576          0,                     /* src_mask */
577          ONES (64),             /* dst_mask */
578          FALSE),                /* pcrel_offset */
579
580   /* Created by the link editor.  Marks a procedure linkage table
581      entry for a symbol.  */
582   HOWTO (R_PPC64_JMP_SLOT,      /* type */
583          0,                     /* rightshift */
584          0,                     /* size (0 = byte, 1 = short, 2 = long) */
585          0,                     /* bitsize */
586          FALSE,                 /* pc_relative */
587          0,                     /* bitpos */
588          complain_overflow_dont, /* complain_on_overflow */
589          ppc64_elf_unhandled_reloc, /* special_function */
590          "R_PPC64_JMP_SLOT",    /* name */
591          FALSE,                 /* partial_inplace */
592          0,                     /* src_mask */
593          0,                     /* dst_mask */
594          FALSE),                /* pcrel_offset */
595
596   /* Used only by the dynamic linker.  When the object is run, this
597      doubleword64 is set to the load address of the object, plus the
598      addend.  */
599   HOWTO (R_PPC64_RELATIVE,      /* type */
600          0,                     /* rightshift */
601          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
602          64,                    /* bitsize */
603          FALSE,                 /* pc_relative */
604          0,                     /* bitpos */
605          complain_overflow_dont, /* complain_on_overflow */
606          bfd_elf_generic_reloc, /* special_function */
607          "R_PPC64_RELATIVE",    /* name */
608          FALSE,                 /* partial_inplace */
609          0,                     /* src_mask */
610          ONES (64),             /* dst_mask */
611          FALSE),                /* pcrel_offset */
612
613   /* Like R_PPC64_ADDR32, but may be unaligned.  */
614   HOWTO (R_PPC64_UADDR32,       /* type */
615          0,                     /* rightshift */
616          2,                     /* size (0 = byte, 1 = short, 2 = long) */
617          32,                    /* bitsize */
618          FALSE,                 /* pc_relative */
619          0,                     /* bitpos */
620          complain_overflow_bitfield, /* complain_on_overflow */
621          bfd_elf_generic_reloc, /* special_function */
622          "R_PPC64_UADDR32",     /* name */
623          FALSE,                 /* partial_inplace */
624          0,                     /* src_mask */
625          0xffffffff,            /* dst_mask */
626          FALSE),                /* pcrel_offset */
627
628   /* Like R_PPC64_ADDR16, but may be unaligned.  */
629   HOWTO (R_PPC64_UADDR16,       /* type */
630          0,                     /* rightshift */
631          1,                     /* size (0 = byte, 1 = short, 2 = long) */
632          16,                    /* bitsize */
633          FALSE,                 /* pc_relative */
634          0,                     /* bitpos */
635          complain_overflow_bitfield, /* complain_on_overflow */
636          bfd_elf_generic_reloc, /* special_function */
637          "R_PPC64_UADDR16",     /* name */
638          FALSE,                 /* partial_inplace */
639          0,                     /* src_mask */
640          0xffff,                /* dst_mask */
641          FALSE),                /* pcrel_offset */
642
643   /* 32-bit PC relative.  */
644   HOWTO (R_PPC64_REL32,         /* type */
645          0,                     /* rightshift */
646          2,                     /* size (0 = byte, 1 = short, 2 = long) */
647          32,                    /* bitsize */
648          TRUE,                  /* pc_relative */
649          0,                     /* bitpos */
650          complain_overflow_signed, /* complain_on_overflow */
651          bfd_elf_generic_reloc, /* special_function */
652          "R_PPC64_REL32",       /* name */
653          FALSE,                 /* partial_inplace */
654          0,                     /* src_mask */
655          0xffffffff,            /* dst_mask */
656          TRUE),                 /* pcrel_offset */
657
658   /* 32-bit relocation to the symbol's procedure linkage table.  */
659   HOWTO (R_PPC64_PLT32,         /* type */
660          0,                     /* rightshift */
661          2,                     /* size (0 = byte, 1 = short, 2 = long) */
662          32,                    /* bitsize */
663          FALSE,                 /* pc_relative */
664          0,                     /* bitpos */
665          complain_overflow_bitfield, /* complain_on_overflow */
666          ppc64_elf_unhandled_reloc, /* special_function */
667          "R_PPC64_PLT32",       /* name */
668          FALSE,                 /* partial_inplace */
669          0,                     /* src_mask */
670          0xffffffff,            /* dst_mask */
671          FALSE),                /* pcrel_offset */
672
673   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
674      FIXME: R_PPC64_PLTREL32 not supported.  */
675   HOWTO (R_PPC64_PLTREL32,      /* type */
676          0,                     /* rightshift */
677          2,                     /* size (0 = byte, 1 = short, 2 = long) */
678          32,                    /* bitsize */
679          TRUE,                  /* pc_relative */
680          0,                     /* bitpos */
681          complain_overflow_signed, /* complain_on_overflow */
682          bfd_elf_generic_reloc, /* special_function */
683          "R_PPC64_PLTREL32",    /* name */
684          FALSE,                 /* partial_inplace */
685          0,                     /* src_mask */
686          0xffffffff,            /* dst_mask */
687          TRUE),                 /* pcrel_offset */
688
689   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
690      the symbol.  */
691   HOWTO (R_PPC64_PLT16_LO,      /* type */
692          0,                     /* rightshift */
693          1,                     /* size (0 = byte, 1 = short, 2 = long) */
694          16,                    /* bitsize */
695          FALSE,                 /* pc_relative */
696          0,                     /* bitpos */
697          complain_overflow_dont, /* complain_on_overflow */
698          ppc64_elf_unhandled_reloc, /* special_function */
699          "R_PPC64_PLT16_LO",    /* name */
700          FALSE,                 /* partial_inplace */
701          0,                     /* src_mask */
702          0xffff,                /* dst_mask */
703          FALSE),                /* pcrel_offset */
704
705   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
706      the symbol.  */
707   HOWTO (R_PPC64_PLT16_HI,      /* type */
708          16,                    /* rightshift */
709          1,                     /* size (0 = byte, 1 = short, 2 = long) */
710          16,                    /* bitsize */
711          FALSE,                 /* pc_relative */
712          0,                     /* bitpos */
713          complain_overflow_signed, /* complain_on_overflow */
714          ppc64_elf_unhandled_reloc, /* special_function */
715          "R_PPC64_PLT16_HI",    /* name */
716          FALSE,                 /* partial_inplace */
717          0,                     /* src_mask */
718          0xffff,                /* dst_mask */
719          FALSE),                /* pcrel_offset */
720
721   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
722      the symbol.  */
723   HOWTO (R_PPC64_PLT16_HA,      /* type */
724          16,                    /* rightshift */
725          1,                     /* size (0 = byte, 1 = short, 2 = long) */
726          16,                    /* bitsize */
727          FALSE,                 /* pc_relative */
728          0,                     /* bitpos */
729          complain_overflow_signed, /* complain_on_overflow */
730          ppc64_elf_unhandled_reloc, /* special_function */
731          "R_PPC64_PLT16_HA",    /* name */
732          FALSE,                 /* partial_inplace */
733          0,                     /* src_mask */
734          0xffff,                /* dst_mask */
735          FALSE),                /* pcrel_offset */
736
737   /* 16-bit section relative relocation.  */
738   HOWTO (R_PPC64_SECTOFF,       /* type */
739          0,                     /* rightshift */
740          1,                     /* size (0 = byte, 1 = short, 2 = long) */
741          16,                    /* bitsize */
742          FALSE,                 /* pc_relative */
743          0,                     /* bitpos */
744          complain_overflow_signed, /* complain_on_overflow */
745          ppc64_elf_sectoff_reloc, /* special_function */
746          "R_PPC64_SECTOFF",     /* name */
747          FALSE,                 /* partial_inplace */
748          0,                     /* src_mask */
749          0xffff,                /* dst_mask */
750          FALSE),                /* pcrel_offset */
751
752   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
753   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
754          0,                     /* rightshift */
755          1,                     /* size (0 = byte, 1 = short, 2 = long) */
756          16,                    /* bitsize */
757          FALSE,                 /* pc_relative */
758          0,                     /* bitpos */
759          complain_overflow_dont, /* complain_on_overflow */
760          ppc64_elf_sectoff_reloc, /* special_function */
761          "R_PPC64_SECTOFF_LO",  /* name */
762          FALSE,                 /* partial_inplace */
763          0,                     /* src_mask */
764          0xffff,                /* dst_mask */
765          FALSE),                /* pcrel_offset */
766
767   /* 16-bit upper half section relative relocation.  */
768   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
769          16,                    /* rightshift */
770          1,                     /* size (0 = byte, 1 = short, 2 = long) */
771          16,                    /* bitsize */
772          FALSE,                 /* pc_relative */
773          0,                     /* bitpos */
774          complain_overflow_signed, /* complain_on_overflow */
775          ppc64_elf_sectoff_reloc, /* special_function */
776          "R_PPC64_SECTOFF_HI",  /* name */
777          FALSE,                 /* partial_inplace */
778          0,                     /* src_mask */
779          0xffff,                /* dst_mask */
780          FALSE),                /* pcrel_offset */
781
782   /* 16-bit upper half adjusted section relative relocation.  */
783   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
784          16,                    /* rightshift */
785          1,                     /* size (0 = byte, 1 = short, 2 = long) */
786          16,                    /* bitsize */
787          FALSE,                 /* pc_relative */
788          0,                     /* bitpos */
789          complain_overflow_signed, /* complain_on_overflow */
790          ppc64_elf_sectoff_ha_reloc, /* special_function */
791          "R_PPC64_SECTOFF_HA",  /* name */
792          FALSE,                 /* partial_inplace */
793          0,                     /* src_mask */
794          0xffff,                /* dst_mask */
795          FALSE),                /* pcrel_offset */
796
797   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
798   HOWTO (R_PPC64_REL30,         /* type */
799          2,                     /* rightshift */
800          2,                     /* size (0 = byte, 1 = short, 2 = long) */
801          30,                    /* bitsize */
802          TRUE,                  /* pc_relative */
803          0,                     /* bitpos */
804          complain_overflow_dont, /* complain_on_overflow */
805          bfd_elf_generic_reloc, /* special_function */
806          "R_PPC64_REL30",       /* name */
807          FALSE,                 /* partial_inplace */
808          0,                     /* src_mask */
809          0xfffffffc,            /* dst_mask */
810          TRUE),                 /* pcrel_offset */
811
812   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
813
814   /* A standard 64-bit relocation.  */
815   HOWTO (R_PPC64_ADDR64,        /* type */
816          0,                     /* rightshift */
817          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
818          64,                    /* bitsize */
819          FALSE,                 /* pc_relative */
820          0,                     /* bitpos */
821          complain_overflow_dont, /* complain_on_overflow */
822          bfd_elf_generic_reloc, /* special_function */
823          "R_PPC64_ADDR64",      /* name */
824          FALSE,                 /* partial_inplace */
825          0,                     /* src_mask */
826          ONES (64),             /* dst_mask */
827          FALSE),                /* pcrel_offset */
828
829   /* The bits 32-47 of an address.  */
830   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
831          32,                    /* rightshift */
832          1,                     /* size (0 = byte, 1 = short, 2 = long) */
833          16,                    /* bitsize */
834          FALSE,                 /* pc_relative */
835          0,                     /* bitpos */
836          complain_overflow_dont, /* complain_on_overflow */
837          bfd_elf_generic_reloc, /* special_function */
838          "R_PPC64_ADDR16_HIGHER", /* name */
839          FALSE,                 /* partial_inplace */
840          0,                     /* src_mask */
841          0xffff,                /* dst_mask */
842          FALSE),                /* pcrel_offset */
843
844   /* The bits 32-47 of an address, plus 1 if the contents of the low
845      16 bits, treated as a signed number, is negative.  */
846   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
847          32,                    /* rightshift */
848          1,                     /* size (0 = byte, 1 = short, 2 = long) */
849          16,                    /* bitsize */
850          FALSE,                 /* pc_relative */
851          0,                     /* bitpos */
852          complain_overflow_dont, /* complain_on_overflow */
853          ppc64_elf_ha_reloc,    /* special_function */
854          "R_PPC64_ADDR16_HIGHERA", /* name */
855          FALSE,                 /* partial_inplace */
856          0,                     /* src_mask */
857          0xffff,                /* dst_mask */
858          FALSE),                /* pcrel_offset */
859
860   /* The bits 48-63 of an address.  */
861   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
862          48,                    /* rightshift */
863          1,                     /* size (0 = byte, 1 = short, 2 = long) */
864          16,                    /* bitsize */
865          FALSE,                 /* pc_relative */
866          0,                     /* bitpos */
867          complain_overflow_dont, /* complain_on_overflow */
868          bfd_elf_generic_reloc, /* special_function */
869          "R_PPC64_ADDR16_HIGHEST", /* name */
870          FALSE,                 /* partial_inplace */
871          0,                     /* src_mask */
872          0xffff,                /* dst_mask */
873          FALSE),                /* pcrel_offset */
874
875   /* The bits 48-63 of an address, plus 1 if the contents of the low
876      16 bits, treated as a signed number, is negative.  */
877   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
878          48,                    /* rightshift */
879          1,                     /* size (0 = byte, 1 = short, 2 = long) */
880          16,                    /* bitsize */
881          FALSE,                 /* pc_relative */
882          0,                     /* bitpos */
883          complain_overflow_dont, /* complain_on_overflow */
884          ppc64_elf_ha_reloc,    /* special_function */
885          "R_PPC64_ADDR16_HIGHESTA", /* name */
886          FALSE,                 /* partial_inplace */
887          0,                     /* src_mask */
888          0xffff,                /* dst_mask */
889          FALSE),                /* pcrel_offset */
890
891   /* Like ADDR64, but may be unaligned.  */
892   HOWTO (R_PPC64_UADDR64,       /* type */
893          0,                     /* rightshift */
894          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
895          64,                    /* bitsize */
896          FALSE,                 /* pc_relative */
897          0,                     /* bitpos */
898          complain_overflow_dont, /* complain_on_overflow */
899          bfd_elf_generic_reloc, /* special_function */
900          "R_PPC64_UADDR64",     /* name */
901          FALSE,                 /* partial_inplace */
902          0,                     /* src_mask */
903          ONES (64),             /* dst_mask */
904          FALSE),                /* pcrel_offset */
905
906   /* 64-bit relative relocation.  */
907   HOWTO (R_PPC64_REL64,         /* type */
908          0,                     /* rightshift */
909          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
910          64,                    /* bitsize */
911          TRUE,                  /* pc_relative */
912          0,                     /* bitpos */
913          complain_overflow_dont, /* complain_on_overflow */
914          bfd_elf_generic_reloc, /* special_function */
915          "R_PPC64_REL64",       /* name */
916          FALSE,                 /* partial_inplace */
917          0,                     /* src_mask */
918          ONES (64),             /* dst_mask */
919          TRUE),                 /* pcrel_offset */
920
921   /* 64-bit relocation to the symbol's procedure linkage table.  */
922   HOWTO (R_PPC64_PLT64,         /* type */
923          0,                     /* rightshift */
924          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
925          64,                    /* bitsize */
926          FALSE,                 /* pc_relative */
927          0,                     /* bitpos */
928          complain_overflow_dont, /* complain_on_overflow */
929          ppc64_elf_unhandled_reloc, /* special_function */
930          "R_PPC64_PLT64",       /* name */
931          FALSE,                 /* partial_inplace */
932          0,                     /* src_mask */
933          ONES (64),             /* dst_mask */
934          FALSE),                /* pcrel_offset */
935
936   /* 64-bit PC relative relocation to the symbol's procedure linkage
937      table.  */
938   /* FIXME: R_PPC64_PLTREL64 not supported.  */
939   HOWTO (R_PPC64_PLTREL64,      /* type */
940          0,                     /* rightshift */
941          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
942          64,                    /* bitsize */
943          TRUE,                  /* pc_relative */
944          0,                     /* bitpos */
945          complain_overflow_dont, /* complain_on_overflow */
946          ppc64_elf_unhandled_reloc, /* special_function */
947          "R_PPC64_PLTREL64",    /* name */
948          FALSE,                 /* partial_inplace */
949          0,                     /* src_mask */
950          ONES (64),             /* dst_mask */
951          TRUE),                 /* pcrel_offset */
952
953   /* 16 bit TOC-relative relocation.  */
954
955   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
956   HOWTO (R_PPC64_TOC16,         /* type */
957          0,                     /* rightshift */
958          1,                     /* size (0 = byte, 1 = short, 2 = long) */
959          16,                    /* bitsize */
960          FALSE,                 /* pc_relative */
961          0,                     /* bitpos */
962          complain_overflow_signed, /* complain_on_overflow */
963          ppc64_elf_toc_reloc,   /* special_function */
964          "R_PPC64_TOC16",       /* name */
965          FALSE,                 /* partial_inplace */
966          0,                     /* src_mask */
967          0xffff,                /* dst_mask */
968          FALSE),                /* pcrel_offset */
969
970   /* 16 bit TOC-relative relocation without overflow.  */
971
972   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
973   HOWTO (R_PPC64_TOC16_LO,      /* type */
974          0,                     /* rightshift */
975          1,                     /* size (0 = byte, 1 = short, 2 = long) */
976          16,                    /* bitsize */
977          FALSE,                 /* pc_relative */
978          0,                     /* bitpos */
979          complain_overflow_dont, /* complain_on_overflow */
980          ppc64_elf_toc_reloc,   /* special_function */
981          "R_PPC64_TOC16_LO",    /* name */
982          FALSE,                 /* partial_inplace */
983          0,                     /* src_mask */
984          0xffff,                /* dst_mask */
985          FALSE),                /* pcrel_offset */
986
987   /* 16 bit TOC-relative relocation, high 16 bits.  */
988
989   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
990   HOWTO (R_PPC64_TOC16_HI,      /* type */
991          16,                    /* rightshift */
992          1,                     /* size (0 = byte, 1 = short, 2 = long) */
993          16,                    /* bitsize */
994          FALSE,                 /* pc_relative */
995          0,                     /* bitpos */
996          complain_overflow_signed, /* complain_on_overflow */
997          ppc64_elf_toc_reloc,   /* special_function */
998          "R_PPC64_TOC16_HI",    /* name */
999          FALSE,                 /* partial_inplace */
1000          0,                     /* src_mask */
1001          0xffff,                /* dst_mask */
1002          FALSE),                /* pcrel_offset */
1003
1004   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1005      contents of the low 16 bits, treated as a signed number, is
1006      negative.  */
1007
1008   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
1009   HOWTO (R_PPC64_TOC16_HA,      /* type */
1010          16,                    /* rightshift */
1011          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1012          16,                    /* bitsize */
1013          FALSE,                 /* pc_relative */
1014          0,                     /* bitpos */
1015          complain_overflow_signed, /* complain_on_overflow */
1016          ppc64_elf_toc_ha_reloc, /* special_function */
1017          "R_PPC64_TOC16_HA",    /* name */
1018          FALSE,                 /* partial_inplace */
1019          0,                     /* src_mask */
1020          0xffff,                /* dst_mask */
1021          FALSE),                /* pcrel_offset */
1022
1023   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
1024
1025   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
1026   HOWTO (R_PPC64_TOC,           /* type */
1027          0,                     /* rightshift */
1028          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1029          64,                    /* bitsize */
1030          FALSE,                 /* pc_relative */
1031          0,                     /* bitpos */
1032          complain_overflow_dont, /* complain_on_overflow */
1033          ppc64_elf_toc64_reloc, /* special_function */
1034          "R_PPC64_TOC",         /* name */
1035          FALSE,                 /* partial_inplace */
1036          0,                     /* src_mask */
1037          ONES (64),             /* dst_mask */
1038          FALSE),                /* pcrel_offset */
1039
1040   /* Like R_PPC64_GOT16, but also informs the link editor that the
1041      value to relocate may (!) refer to a PLT entry which the link
1042      editor (a) may replace with the symbol value.  If the link editor
1043      is unable to fully resolve the symbol, it may (b) create a PLT
1044      entry and store the address to the new PLT entry in the GOT.
1045      This permits lazy resolution of function symbols at run time.
1046      The link editor may also skip all of this and just (c) emit a
1047      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
1048   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
1049     HOWTO (R_PPC64_PLTGOT16,    /* type */
1050          0,                     /* rightshift */
1051          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1052          16,                    /* bitsize */
1053          FALSE,                 /* pc_relative */
1054          0,                     /* bitpos */
1055          complain_overflow_signed, /* complain_on_overflow */
1056          ppc64_elf_unhandled_reloc, /* special_function */
1057          "R_PPC64_PLTGOT16",    /* name */
1058          FALSE,                 /* partial_inplace */
1059          0,                     /* src_mask */
1060          0xffff,                /* dst_mask */
1061          FALSE),                /* pcrel_offset */
1062
1063   /* Like R_PPC64_PLTGOT16, but without overflow.  */
1064   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1065   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1066          0,                     /* rightshift */
1067          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1068          16,                    /* bitsize */
1069          FALSE,                 /* pc_relative */
1070          0,                     /* bitpos */
1071          complain_overflow_dont, /* complain_on_overflow */
1072          ppc64_elf_unhandled_reloc, /* special_function */
1073          "R_PPC64_PLTGOT16_LO", /* name */
1074          FALSE,                 /* partial_inplace */
1075          0,                     /* src_mask */
1076          0xffff,                /* dst_mask */
1077          FALSE),                /* pcrel_offset */
1078
1079   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1080   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1081   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1082          16,                    /* rightshift */
1083          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1084          16,                    /* bitsize */
1085          FALSE,                 /* pc_relative */
1086          0,                     /* bitpos */
1087          complain_overflow_signed, /* complain_on_overflow */
1088          ppc64_elf_unhandled_reloc, /* special_function */
1089          "R_PPC64_PLTGOT16_HI", /* name */
1090          FALSE,                 /* partial_inplace */
1091          0,                     /* src_mask */
1092          0xffff,                /* dst_mask */
1093          FALSE),                /* pcrel_offset */
1094
1095   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1096      1 if the contents of the low 16 bits, treated as a signed number,
1097      is negative.  */
1098   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1099   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1100          16,                    /* rightshift */
1101          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1102          16,                    /* bitsize */
1103          FALSE,                 /* pc_relative */
1104          0,                     /* bitpos */
1105          complain_overflow_signed, /* complain_on_overflow */
1106          ppc64_elf_unhandled_reloc, /* special_function */
1107          "R_PPC64_PLTGOT16_HA", /* name */
1108          FALSE,                 /* partial_inplace */
1109          0,                     /* src_mask */
1110          0xffff,                /* dst_mask */
1111          FALSE),                /* pcrel_offset */
1112
1113   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1114   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1115          0,                     /* rightshift */
1116          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1117          16,                    /* bitsize */
1118          FALSE,                 /* pc_relative */
1119          0,                     /* bitpos */
1120          complain_overflow_signed, /* complain_on_overflow */
1121          bfd_elf_generic_reloc, /* special_function */
1122          "R_PPC64_ADDR16_DS",   /* name */
1123          FALSE,                 /* partial_inplace */
1124          0,                     /* src_mask */
1125          0xfffc,                /* dst_mask */
1126          FALSE),                /* pcrel_offset */
1127
1128   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1129   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1130          0,                     /* rightshift */
1131          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1132          16,                    /* bitsize */
1133          FALSE,                 /* pc_relative */
1134          0,                     /* bitpos */
1135          complain_overflow_dont,/* complain_on_overflow */
1136          bfd_elf_generic_reloc, /* special_function */
1137          "R_PPC64_ADDR16_LO_DS",/* name */
1138          FALSE,                 /* partial_inplace */
1139          0,                     /* src_mask */
1140          0xfffc,                /* dst_mask */
1141          FALSE),                /* pcrel_offset */
1142
1143   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1144   HOWTO (R_PPC64_GOT16_DS,      /* type */
1145          0,                     /* rightshift */
1146          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1147          16,                    /* bitsize */
1148          FALSE,                 /* pc_relative */
1149          0,                     /* bitpos */
1150          complain_overflow_signed, /* complain_on_overflow */
1151          ppc64_elf_unhandled_reloc, /* special_function */
1152          "R_PPC64_GOT16_DS",    /* name */
1153          FALSE,                 /* partial_inplace */
1154          0,                     /* src_mask */
1155          0xfffc,                /* dst_mask */
1156          FALSE),                /* pcrel_offset */
1157
1158   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1159   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1160          0,                     /* rightshift */
1161          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1162          16,                    /* bitsize */
1163          FALSE,                 /* pc_relative */
1164          0,                     /* bitpos */
1165          complain_overflow_dont, /* complain_on_overflow */
1166          ppc64_elf_unhandled_reloc, /* special_function */
1167          "R_PPC64_GOT16_LO_DS", /* name */
1168          FALSE,                 /* partial_inplace */
1169          0,                     /* src_mask */
1170          0xfffc,                /* dst_mask */
1171          FALSE),                /* pcrel_offset */
1172
1173   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1174   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1175          0,                     /* rightshift */
1176          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1177          16,                    /* bitsize */
1178          FALSE,                 /* pc_relative */
1179          0,                     /* bitpos */
1180          complain_overflow_dont, /* complain_on_overflow */
1181          ppc64_elf_unhandled_reloc, /* special_function */
1182          "R_PPC64_PLT16_LO_DS", /* name */
1183          FALSE,                 /* partial_inplace */
1184          0,                     /* src_mask */
1185          0xfffc,                /* dst_mask */
1186          FALSE),                /* pcrel_offset */
1187
1188   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1189   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1190          0,                     /* rightshift */
1191          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1192          16,                    /* bitsize */
1193          FALSE,                 /* pc_relative */
1194          0,                     /* bitpos */
1195          complain_overflow_signed, /* complain_on_overflow */
1196          ppc64_elf_sectoff_reloc, /* special_function */
1197          "R_PPC64_SECTOFF_DS",  /* name */
1198          FALSE,                 /* partial_inplace */
1199          0,                     /* src_mask */
1200          0xfffc,                /* dst_mask */
1201          FALSE),                /* pcrel_offset */
1202
1203   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1204   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1205          0,                     /* rightshift */
1206          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1207          16,                    /* bitsize */
1208          FALSE,                 /* pc_relative */
1209          0,                     /* bitpos */
1210          complain_overflow_dont, /* complain_on_overflow */
1211          ppc64_elf_sectoff_reloc, /* special_function */
1212          "R_PPC64_SECTOFF_LO_DS",/* name */
1213          FALSE,                 /* partial_inplace */
1214          0,                     /* src_mask */
1215          0xfffc,                /* dst_mask */
1216          FALSE),                /* pcrel_offset */
1217
1218   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1219   HOWTO (R_PPC64_TOC16_DS,      /* type */
1220          0,                     /* rightshift */
1221          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1222          16,                    /* bitsize */
1223          FALSE,                 /* pc_relative */
1224          0,                     /* bitpos */
1225          complain_overflow_signed, /* complain_on_overflow */
1226          ppc64_elf_toc_reloc,   /* special_function */
1227          "R_PPC64_TOC16_DS",    /* name */
1228          FALSE,                 /* partial_inplace */
1229          0,                     /* src_mask */
1230          0xfffc,                /* dst_mask */
1231          FALSE),                /* pcrel_offset */
1232
1233   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1234   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1235          0,                     /* rightshift */
1236          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1237          16,                    /* bitsize */
1238          FALSE,                 /* pc_relative */
1239          0,                     /* bitpos */
1240          complain_overflow_dont, /* complain_on_overflow */
1241          ppc64_elf_toc_reloc,   /* special_function */
1242          "R_PPC64_TOC16_LO_DS", /* name */
1243          FALSE,                 /* partial_inplace */
1244          0,                     /* src_mask */
1245          0xfffc,                /* dst_mask */
1246          FALSE),                /* pcrel_offset */
1247
1248   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1249   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1250   HOWTO (R_PPC64_PLTGOT16_DS,   /* type */
1251          0,                     /* rightshift */
1252          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1253          16,                    /* bitsize */
1254          FALSE,                 /* pc_relative */
1255          0,                     /* bitpos */
1256          complain_overflow_signed, /* complain_on_overflow */
1257          ppc64_elf_unhandled_reloc, /* special_function */
1258          "R_PPC64_PLTGOT16_DS", /* name */
1259          FALSE,                 /* partial_inplace */
1260          0,                     /* src_mask */
1261          0xfffc,                /* dst_mask */
1262          FALSE),                /* pcrel_offset */
1263
1264   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1265   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1266   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1267          0,                     /* rightshift */
1268          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1269          16,                    /* bitsize */
1270          FALSE,                 /* pc_relative */
1271          0,                     /* bitpos */
1272          complain_overflow_dont, /* complain_on_overflow */
1273          ppc64_elf_unhandled_reloc, /* special_function */
1274          "R_PPC64_PLTGOT16_LO_DS",/* name */
1275          FALSE,                 /* partial_inplace */
1276          0,                     /* src_mask */
1277          0xfffc,                /* dst_mask */
1278          FALSE),                /* pcrel_offset */
1279
1280   /* Marker relocs for TLS.  */
1281   HOWTO (R_PPC64_TLS,
1282          0,                     /* rightshift */
1283          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1284          32,                    /* bitsize */
1285          FALSE,                 /* pc_relative */
1286          0,                     /* bitpos */
1287          complain_overflow_dont, /* complain_on_overflow */
1288          bfd_elf_generic_reloc, /* special_function */
1289          "R_PPC64_TLS",         /* name */
1290          FALSE,                 /* partial_inplace */
1291          0,                     /* src_mask */
1292          0,                     /* dst_mask */
1293          FALSE),                /* pcrel_offset */
1294
1295   HOWTO (R_PPC64_TLSGD,
1296          0,                     /* rightshift */
1297          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1298          32,                    /* bitsize */
1299          FALSE,                 /* pc_relative */
1300          0,                     /* bitpos */
1301          complain_overflow_dont, /* complain_on_overflow */
1302          bfd_elf_generic_reloc, /* special_function */
1303          "R_PPC64_TLSGD",       /* name */
1304          FALSE,                 /* partial_inplace */
1305          0,                     /* src_mask */
1306          0,                     /* dst_mask */
1307          FALSE),                /* pcrel_offset */
1308
1309   HOWTO (R_PPC64_TLSLD,
1310          0,                     /* rightshift */
1311          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1312          32,                    /* bitsize */
1313          FALSE,                 /* pc_relative */
1314          0,                     /* bitpos */
1315          complain_overflow_dont, /* complain_on_overflow */
1316          bfd_elf_generic_reloc, /* special_function */
1317          "R_PPC64_TLSLD",       /* name */
1318          FALSE,                 /* partial_inplace */
1319          0,                     /* src_mask */
1320          0,                     /* dst_mask */
1321          FALSE),                /* pcrel_offset */
1322
1323   HOWTO (R_PPC64_TOCSAVE,
1324          0,                     /* rightshift */
1325          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1326          32,                    /* bitsize */
1327          FALSE,                 /* pc_relative */
1328          0,                     /* bitpos */
1329          complain_overflow_dont, /* complain_on_overflow */
1330          bfd_elf_generic_reloc, /* special_function */
1331          "R_PPC64_TOCSAVE",     /* name */
1332          FALSE,                 /* partial_inplace */
1333          0,                     /* src_mask */
1334          0,                     /* dst_mask */
1335          FALSE),                /* pcrel_offset */
1336
1337   /* Computes the load module index of the load module that contains the
1338      definition of its TLS sym.  */
1339   HOWTO (R_PPC64_DTPMOD64,
1340          0,                     /* rightshift */
1341          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1342          64,                    /* bitsize */
1343          FALSE,                 /* pc_relative */
1344          0,                     /* bitpos */
1345          complain_overflow_dont, /* complain_on_overflow */
1346          ppc64_elf_unhandled_reloc, /* special_function */
1347          "R_PPC64_DTPMOD64",    /* name */
1348          FALSE,                 /* partial_inplace */
1349          0,                     /* src_mask */
1350          ONES (64),             /* dst_mask */
1351          FALSE),                /* pcrel_offset */
1352
1353   /* Computes a dtv-relative displacement, the difference between the value
1354      of sym+add and the base address of the thread-local storage block that
1355      contains the definition of sym, minus 0x8000.  */
1356   HOWTO (R_PPC64_DTPREL64,
1357          0,                     /* rightshift */
1358          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1359          64,                    /* bitsize */
1360          FALSE,                 /* pc_relative */
1361          0,                     /* bitpos */
1362          complain_overflow_dont, /* complain_on_overflow */
1363          ppc64_elf_unhandled_reloc, /* special_function */
1364          "R_PPC64_DTPREL64",    /* name */
1365          FALSE,                 /* partial_inplace */
1366          0,                     /* src_mask */
1367          ONES (64),             /* dst_mask */
1368          FALSE),                /* pcrel_offset */
1369
1370   /* A 16 bit dtprel reloc.  */
1371   HOWTO (R_PPC64_DTPREL16,
1372          0,                     /* rightshift */
1373          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1374          16,                    /* bitsize */
1375          FALSE,                 /* pc_relative */
1376          0,                     /* bitpos */
1377          complain_overflow_signed, /* complain_on_overflow */
1378          ppc64_elf_unhandled_reloc, /* special_function */
1379          "R_PPC64_DTPREL16",    /* name */
1380          FALSE,                 /* partial_inplace */
1381          0,                     /* src_mask */
1382          0xffff,                /* dst_mask */
1383          FALSE),                /* pcrel_offset */
1384
1385   /* Like DTPREL16, but no overflow.  */
1386   HOWTO (R_PPC64_DTPREL16_LO,
1387          0,                     /* rightshift */
1388          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1389          16,                    /* bitsize */
1390          FALSE,                 /* pc_relative */
1391          0,                     /* bitpos */
1392          complain_overflow_dont, /* complain_on_overflow */
1393          ppc64_elf_unhandled_reloc, /* special_function */
1394          "R_PPC64_DTPREL16_LO", /* name */
1395          FALSE,                 /* partial_inplace */
1396          0,                     /* src_mask */
1397          0xffff,                /* dst_mask */
1398          FALSE),                /* pcrel_offset */
1399
1400   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1401   HOWTO (R_PPC64_DTPREL16_HI,
1402          16,                    /* rightshift */
1403          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1404          16,                    /* bitsize */
1405          FALSE,                 /* pc_relative */
1406          0,                     /* bitpos */
1407          complain_overflow_signed, /* complain_on_overflow */
1408          ppc64_elf_unhandled_reloc, /* special_function */
1409          "R_PPC64_DTPREL16_HI", /* name */
1410          FALSE,                 /* partial_inplace */
1411          0,                     /* src_mask */
1412          0xffff,                /* dst_mask */
1413          FALSE),                /* pcrel_offset */
1414
1415   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1416   HOWTO (R_PPC64_DTPREL16_HA,
1417          16,                    /* rightshift */
1418          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1419          16,                    /* bitsize */
1420          FALSE,                 /* pc_relative */
1421          0,                     /* bitpos */
1422          complain_overflow_signed, /* complain_on_overflow */
1423          ppc64_elf_unhandled_reloc, /* special_function */
1424          "R_PPC64_DTPREL16_HA", /* name */
1425          FALSE,                 /* partial_inplace */
1426          0,                     /* src_mask */
1427          0xffff,                /* dst_mask */
1428          FALSE),                /* pcrel_offset */
1429
1430   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1431   HOWTO (R_PPC64_DTPREL16_HIGHER,
1432          32,                    /* rightshift */
1433          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1434          16,                    /* bitsize */
1435          FALSE,                 /* pc_relative */
1436          0,                     /* bitpos */
1437          complain_overflow_dont, /* complain_on_overflow */
1438          ppc64_elf_unhandled_reloc, /* special_function */
1439          "R_PPC64_DTPREL16_HIGHER", /* name */
1440          FALSE,                 /* partial_inplace */
1441          0,                     /* src_mask */
1442          0xffff,                /* dst_mask */
1443          FALSE),                /* pcrel_offset */
1444
1445   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1446   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1447          32,                    /* rightshift */
1448          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1449          16,                    /* bitsize */
1450          FALSE,                 /* pc_relative */
1451          0,                     /* bitpos */
1452          complain_overflow_dont, /* complain_on_overflow */
1453          ppc64_elf_unhandled_reloc, /* special_function */
1454          "R_PPC64_DTPREL16_HIGHERA", /* name */
1455          FALSE,                 /* partial_inplace */
1456          0,                     /* src_mask */
1457          0xffff,                /* dst_mask */
1458          FALSE),                /* pcrel_offset */
1459
1460   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1461   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1462          48,                    /* rightshift */
1463          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1464          16,                    /* bitsize */
1465          FALSE,                 /* pc_relative */
1466          0,                     /* bitpos */
1467          complain_overflow_dont, /* complain_on_overflow */
1468          ppc64_elf_unhandled_reloc, /* special_function */
1469          "R_PPC64_DTPREL16_HIGHEST", /* name */
1470          FALSE,                 /* partial_inplace */
1471          0,                     /* src_mask */
1472          0xffff,                /* dst_mask */
1473          FALSE),                /* pcrel_offset */
1474
1475   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1476   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1477          48,                    /* rightshift */
1478          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1479          16,                    /* bitsize */
1480          FALSE,                 /* pc_relative */
1481          0,                     /* bitpos */
1482          complain_overflow_dont, /* complain_on_overflow */
1483          ppc64_elf_unhandled_reloc, /* special_function */
1484          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1485          FALSE,                 /* partial_inplace */
1486          0,                     /* src_mask */
1487          0xffff,                /* dst_mask */
1488          FALSE),                /* pcrel_offset */
1489
1490   /* Like DTPREL16, but for insns with a DS field.  */
1491   HOWTO (R_PPC64_DTPREL16_DS,
1492          0,                     /* rightshift */
1493          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1494          16,                    /* bitsize */
1495          FALSE,                 /* pc_relative */
1496          0,                     /* bitpos */
1497          complain_overflow_signed, /* complain_on_overflow */
1498          ppc64_elf_unhandled_reloc, /* special_function */
1499          "R_PPC64_DTPREL16_DS", /* name */
1500          FALSE,                 /* partial_inplace */
1501          0,                     /* src_mask */
1502          0xfffc,                /* dst_mask */
1503          FALSE),                /* pcrel_offset */
1504
1505   /* Like DTPREL16_DS, but no overflow.  */
1506   HOWTO (R_PPC64_DTPREL16_LO_DS,
1507          0,                     /* rightshift */
1508          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1509          16,                    /* bitsize */
1510          FALSE,                 /* pc_relative */
1511          0,                     /* bitpos */
1512          complain_overflow_dont, /* complain_on_overflow */
1513          ppc64_elf_unhandled_reloc, /* special_function */
1514          "R_PPC64_DTPREL16_LO_DS", /* name */
1515          FALSE,                 /* partial_inplace */
1516          0,                     /* src_mask */
1517          0xfffc,                /* dst_mask */
1518          FALSE),                /* pcrel_offset */
1519
1520   /* Computes a tp-relative displacement, the difference between the value of
1521      sym+add and the value of the thread pointer (r13).  */
1522   HOWTO (R_PPC64_TPREL64,
1523          0,                     /* rightshift */
1524          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1525          64,                    /* bitsize */
1526          FALSE,                 /* pc_relative */
1527          0,                     /* bitpos */
1528          complain_overflow_dont, /* complain_on_overflow */
1529          ppc64_elf_unhandled_reloc, /* special_function */
1530          "R_PPC64_TPREL64",     /* name */
1531          FALSE,                 /* partial_inplace */
1532          0,                     /* src_mask */
1533          ONES (64),             /* dst_mask */
1534          FALSE),                /* pcrel_offset */
1535
1536   /* A 16 bit tprel reloc.  */
1537   HOWTO (R_PPC64_TPREL16,
1538          0,                     /* rightshift */
1539          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1540          16,                    /* bitsize */
1541          FALSE,                 /* pc_relative */
1542          0,                     /* bitpos */
1543          complain_overflow_signed, /* complain_on_overflow */
1544          ppc64_elf_unhandled_reloc, /* special_function */
1545          "R_PPC64_TPREL16",     /* name */
1546          FALSE,                 /* partial_inplace */
1547          0,                     /* src_mask */
1548          0xffff,                /* dst_mask */
1549          FALSE),                /* pcrel_offset */
1550
1551   /* Like TPREL16, but no overflow.  */
1552   HOWTO (R_PPC64_TPREL16_LO,
1553          0,                     /* rightshift */
1554          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1555          16,                    /* bitsize */
1556          FALSE,                 /* pc_relative */
1557          0,                     /* bitpos */
1558          complain_overflow_dont, /* complain_on_overflow */
1559          ppc64_elf_unhandled_reloc, /* special_function */
1560          "R_PPC64_TPREL16_LO",  /* name */
1561          FALSE,                 /* partial_inplace */
1562          0,                     /* src_mask */
1563          0xffff,                /* dst_mask */
1564          FALSE),                /* pcrel_offset */
1565
1566   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1567   HOWTO (R_PPC64_TPREL16_HI,
1568          16,                    /* rightshift */
1569          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1570          16,                    /* bitsize */
1571          FALSE,                 /* pc_relative */
1572          0,                     /* bitpos */
1573          complain_overflow_signed, /* complain_on_overflow */
1574          ppc64_elf_unhandled_reloc, /* special_function */
1575          "R_PPC64_TPREL16_HI",  /* name */
1576          FALSE,                 /* partial_inplace */
1577          0,                     /* src_mask */
1578          0xffff,                /* dst_mask */
1579          FALSE),                /* pcrel_offset */
1580
1581   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1582   HOWTO (R_PPC64_TPREL16_HA,
1583          16,                    /* rightshift */
1584          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1585          16,                    /* bitsize */
1586          FALSE,                 /* pc_relative */
1587          0,                     /* bitpos */
1588          complain_overflow_signed, /* complain_on_overflow */
1589          ppc64_elf_unhandled_reloc, /* special_function */
1590          "R_PPC64_TPREL16_HA",  /* name */
1591          FALSE,                 /* partial_inplace */
1592          0,                     /* src_mask */
1593          0xffff,                /* dst_mask */
1594          FALSE),                /* pcrel_offset */
1595
1596   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1597   HOWTO (R_PPC64_TPREL16_HIGHER,
1598          32,                    /* rightshift */
1599          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1600          16,                    /* bitsize */
1601          FALSE,                 /* pc_relative */
1602          0,                     /* bitpos */
1603          complain_overflow_dont, /* complain_on_overflow */
1604          ppc64_elf_unhandled_reloc, /* special_function */
1605          "R_PPC64_TPREL16_HIGHER",      /* name */
1606          FALSE,                 /* partial_inplace */
1607          0,                     /* src_mask */
1608          0xffff,                /* dst_mask */
1609          FALSE),                /* pcrel_offset */
1610
1611   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1612   HOWTO (R_PPC64_TPREL16_HIGHERA,
1613          32,                    /* rightshift */
1614          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1615          16,                    /* bitsize */
1616          FALSE,                 /* pc_relative */
1617          0,                     /* bitpos */
1618          complain_overflow_dont, /* complain_on_overflow */
1619          ppc64_elf_unhandled_reloc, /* special_function */
1620          "R_PPC64_TPREL16_HIGHERA", /* name */
1621          FALSE,                 /* partial_inplace */
1622          0,                     /* src_mask */
1623          0xffff,                /* dst_mask */
1624          FALSE),                /* pcrel_offset */
1625
1626   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1627   HOWTO (R_PPC64_TPREL16_HIGHEST,
1628          48,                    /* rightshift */
1629          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1630          16,                    /* bitsize */
1631          FALSE,                 /* pc_relative */
1632          0,                     /* bitpos */
1633          complain_overflow_dont, /* complain_on_overflow */
1634          ppc64_elf_unhandled_reloc, /* special_function */
1635          "R_PPC64_TPREL16_HIGHEST", /* name */
1636          FALSE,                 /* partial_inplace */
1637          0,                     /* src_mask */
1638          0xffff,                /* dst_mask */
1639          FALSE),                /* pcrel_offset */
1640
1641   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1642   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1643          48,                    /* rightshift */
1644          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1645          16,                    /* bitsize */
1646          FALSE,                 /* pc_relative */
1647          0,                     /* bitpos */
1648          complain_overflow_dont, /* complain_on_overflow */
1649          ppc64_elf_unhandled_reloc, /* special_function */
1650          "R_PPC64_TPREL16_HIGHESTA", /* name */
1651          FALSE,                 /* partial_inplace */
1652          0,                     /* src_mask */
1653          0xffff,                /* dst_mask */
1654          FALSE),                /* pcrel_offset */
1655
1656   /* Like TPREL16, but for insns with a DS field.  */
1657   HOWTO (R_PPC64_TPREL16_DS,
1658          0,                     /* rightshift */
1659          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1660          16,                    /* bitsize */
1661          FALSE,                 /* pc_relative */
1662          0,                     /* bitpos */
1663          complain_overflow_signed, /* complain_on_overflow */
1664          ppc64_elf_unhandled_reloc, /* special_function */
1665          "R_PPC64_TPREL16_DS",  /* name */
1666          FALSE,                 /* partial_inplace */
1667          0,                     /* src_mask */
1668          0xfffc,                /* dst_mask */
1669          FALSE),                /* pcrel_offset */
1670
1671   /* Like TPREL16_DS, but no overflow.  */
1672   HOWTO (R_PPC64_TPREL16_LO_DS,
1673          0,                     /* rightshift */
1674          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1675          16,                    /* bitsize */
1676          FALSE,                 /* pc_relative */
1677          0,                     /* bitpos */
1678          complain_overflow_dont, /* complain_on_overflow */
1679          ppc64_elf_unhandled_reloc, /* special_function */
1680          "R_PPC64_TPREL16_LO_DS", /* name */
1681          FALSE,                 /* partial_inplace */
1682          0,                     /* src_mask */
1683          0xfffc,                /* dst_mask */
1684          FALSE),                /* pcrel_offset */
1685
1686   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1687      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1688      to the first entry relative to the TOC base (r2).  */
1689   HOWTO (R_PPC64_GOT_TLSGD16,
1690          0,                     /* rightshift */
1691          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1692          16,                    /* bitsize */
1693          FALSE,                 /* pc_relative */
1694          0,                     /* bitpos */
1695          complain_overflow_signed, /* complain_on_overflow */
1696          ppc64_elf_unhandled_reloc, /* special_function */
1697          "R_PPC64_GOT_TLSGD16", /* name */
1698          FALSE,                 /* partial_inplace */
1699          0,                     /* src_mask */
1700          0xffff,                /* dst_mask */
1701          FALSE),                /* pcrel_offset */
1702
1703   /* Like GOT_TLSGD16, but no overflow.  */
1704   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1705          0,                     /* rightshift */
1706          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1707          16,                    /* bitsize */
1708          FALSE,                 /* pc_relative */
1709          0,                     /* bitpos */
1710          complain_overflow_dont, /* complain_on_overflow */
1711          ppc64_elf_unhandled_reloc, /* special_function */
1712          "R_PPC64_GOT_TLSGD16_LO", /* name */
1713          FALSE,                 /* partial_inplace */
1714          0,                     /* src_mask */
1715          0xffff,                /* dst_mask */
1716          FALSE),                /* pcrel_offset */
1717
1718   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1719   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1720          16,                    /* rightshift */
1721          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1722          16,                    /* bitsize */
1723          FALSE,                 /* pc_relative */
1724          0,                     /* bitpos */
1725          complain_overflow_signed, /* complain_on_overflow */
1726          ppc64_elf_unhandled_reloc, /* special_function */
1727          "R_PPC64_GOT_TLSGD16_HI", /* name */
1728          FALSE,                 /* partial_inplace */
1729          0,                     /* src_mask */
1730          0xffff,                /* dst_mask */
1731          FALSE),                /* pcrel_offset */
1732
1733   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1734   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1735          16,                    /* rightshift */
1736          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1737          16,                    /* bitsize */
1738          FALSE,                 /* pc_relative */
1739          0,                     /* bitpos */
1740          complain_overflow_signed, /* complain_on_overflow */
1741          ppc64_elf_unhandled_reloc, /* special_function */
1742          "R_PPC64_GOT_TLSGD16_HA", /* name */
1743          FALSE,                 /* partial_inplace */
1744          0,                     /* src_mask */
1745          0xffff,                /* dst_mask */
1746          FALSE),                /* pcrel_offset */
1747
1748   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1749      with values (sym+add)@dtpmod and zero, and computes the offset to the
1750      first entry relative to the TOC base (r2).  */
1751   HOWTO (R_PPC64_GOT_TLSLD16,
1752          0,                     /* rightshift */
1753          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1754          16,                    /* bitsize */
1755          FALSE,                 /* pc_relative */
1756          0,                     /* bitpos */
1757          complain_overflow_signed, /* complain_on_overflow */
1758          ppc64_elf_unhandled_reloc, /* special_function */
1759          "R_PPC64_GOT_TLSLD16", /* name */
1760          FALSE,                 /* partial_inplace */
1761          0,                     /* src_mask */
1762          0xffff,                /* dst_mask */
1763          FALSE),                /* pcrel_offset */
1764
1765   /* Like GOT_TLSLD16, but no overflow.  */
1766   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1767          0,                     /* rightshift */
1768          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1769          16,                    /* bitsize */
1770          FALSE,                 /* pc_relative */
1771          0,                     /* bitpos */
1772          complain_overflow_dont, /* complain_on_overflow */
1773          ppc64_elf_unhandled_reloc, /* special_function */
1774          "R_PPC64_GOT_TLSLD16_LO", /* name */
1775          FALSE,                 /* partial_inplace */
1776          0,                     /* src_mask */
1777          0xffff,                /* dst_mask */
1778          FALSE),                /* pcrel_offset */
1779
1780   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1781   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1782          16,                    /* rightshift */
1783          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1784          16,                    /* bitsize */
1785          FALSE,                 /* pc_relative */
1786          0,                     /* bitpos */
1787          complain_overflow_signed, /* complain_on_overflow */
1788          ppc64_elf_unhandled_reloc, /* special_function */
1789          "R_PPC64_GOT_TLSLD16_HI", /* name */
1790          FALSE,                 /* partial_inplace */
1791          0,                     /* src_mask */
1792          0xffff,                /* dst_mask */
1793          FALSE),                /* pcrel_offset */
1794
1795   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1796   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1797          16,                    /* rightshift */
1798          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1799          16,                    /* bitsize */
1800          FALSE,                 /* pc_relative */
1801          0,                     /* bitpos */
1802          complain_overflow_signed, /* complain_on_overflow */
1803          ppc64_elf_unhandled_reloc, /* special_function */
1804          "R_PPC64_GOT_TLSLD16_HA", /* name */
1805          FALSE,                 /* partial_inplace */
1806          0,                     /* src_mask */
1807          0xffff,                /* dst_mask */
1808          FALSE),                /* pcrel_offset */
1809
1810   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1811      the offset to the entry relative to the TOC base (r2).  */
1812   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1813          0,                     /* rightshift */
1814          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1815          16,                    /* bitsize */
1816          FALSE,                 /* pc_relative */
1817          0,                     /* bitpos */
1818          complain_overflow_signed, /* complain_on_overflow */
1819          ppc64_elf_unhandled_reloc, /* special_function */
1820          "R_PPC64_GOT_DTPREL16_DS", /* name */
1821          FALSE,                 /* partial_inplace */
1822          0,                     /* src_mask */
1823          0xfffc,                /* dst_mask */
1824          FALSE),                /* pcrel_offset */
1825
1826   /* Like GOT_DTPREL16_DS, but no overflow.  */
1827   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1828          0,                     /* rightshift */
1829          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1830          16,                    /* bitsize */
1831          FALSE,                 /* pc_relative */
1832          0,                     /* bitpos */
1833          complain_overflow_dont, /* complain_on_overflow */
1834          ppc64_elf_unhandled_reloc, /* special_function */
1835          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1836          FALSE,                 /* partial_inplace */
1837          0,                     /* src_mask */
1838          0xfffc,                /* dst_mask */
1839          FALSE),                /* pcrel_offset */
1840
1841   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1842   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1843          16,                    /* rightshift */
1844          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1845          16,                    /* bitsize */
1846          FALSE,                 /* pc_relative */
1847          0,                     /* bitpos */
1848          complain_overflow_signed, /* complain_on_overflow */
1849          ppc64_elf_unhandled_reloc, /* special_function */
1850          "R_PPC64_GOT_DTPREL16_HI", /* name */
1851          FALSE,                 /* partial_inplace */
1852          0,                     /* src_mask */
1853          0xffff,                /* dst_mask */
1854          FALSE),                /* pcrel_offset */
1855
1856   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1857   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1858          16,                    /* rightshift */
1859          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1860          16,                    /* bitsize */
1861          FALSE,                 /* pc_relative */
1862          0,                     /* bitpos */
1863          complain_overflow_signed, /* complain_on_overflow */
1864          ppc64_elf_unhandled_reloc, /* special_function */
1865          "R_PPC64_GOT_DTPREL16_HA", /* name */
1866          FALSE,                 /* partial_inplace */
1867          0,                     /* src_mask */
1868          0xffff,                /* dst_mask */
1869          FALSE),                /* pcrel_offset */
1870
1871   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1872      offset to the entry relative to the TOC base (r2).  */
1873   HOWTO (R_PPC64_GOT_TPREL16_DS,
1874          0,                     /* rightshift */
1875          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1876          16,                    /* bitsize */
1877          FALSE,                 /* pc_relative */
1878          0,                     /* bitpos */
1879          complain_overflow_signed, /* complain_on_overflow */
1880          ppc64_elf_unhandled_reloc, /* special_function */
1881          "R_PPC64_GOT_TPREL16_DS", /* name */
1882          FALSE,                 /* partial_inplace */
1883          0,                     /* src_mask */
1884          0xfffc,                /* dst_mask */
1885          FALSE),                /* pcrel_offset */
1886
1887   /* Like GOT_TPREL16_DS, but no overflow.  */
1888   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1889          0,                     /* rightshift */
1890          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1891          16,                    /* bitsize */
1892          FALSE,                 /* pc_relative */
1893          0,                     /* bitpos */
1894          complain_overflow_dont, /* complain_on_overflow */
1895          ppc64_elf_unhandled_reloc, /* special_function */
1896          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1897          FALSE,                 /* partial_inplace */
1898          0,                     /* src_mask */
1899          0xfffc,                /* dst_mask */
1900          FALSE),                /* pcrel_offset */
1901
1902   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1903   HOWTO (R_PPC64_GOT_TPREL16_HI,
1904          16,                    /* rightshift */
1905          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1906          16,                    /* bitsize */
1907          FALSE,                 /* pc_relative */
1908          0,                     /* bitpos */
1909          complain_overflow_signed, /* complain_on_overflow */
1910          ppc64_elf_unhandled_reloc, /* special_function */
1911          "R_PPC64_GOT_TPREL16_HI", /* name */
1912          FALSE,                 /* partial_inplace */
1913          0,                     /* src_mask */
1914          0xffff,                /* dst_mask */
1915          FALSE),                /* pcrel_offset */
1916
1917   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1918   HOWTO (R_PPC64_GOT_TPREL16_HA,
1919          16,                    /* rightshift */
1920          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1921          16,                    /* bitsize */
1922          FALSE,                 /* pc_relative */
1923          0,                     /* bitpos */
1924          complain_overflow_signed, /* complain_on_overflow */
1925          ppc64_elf_unhandled_reloc, /* special_function */
1926          "R_PPC64_GOT_TPREL16_HA", /* name */
1927          FALSE,                 /* partial_inplace */
1928          0,                     /* src_mask */
1929          0xffff,                /* dst_mask */
1930          FALSE),                /* pcrel_offset */
1931
1932   HOWTO (R_PPC64_JMP_IREL,      /* type */
1933          0,                     /* rightshift */
1934          0,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1935          0,                     /* bitsize */
1936          FALSE,                 /* pc_relative */
1937          0,                     /* bitpos */
1938          complain_overflow_dont, /* complain_on_overflow */
1939          ppc64_elf_unhandled_reloc, /* special_function */
1940          "R_PPC64_JMP_IREL",    /* name */
1941          FALSE,                 /* partial_inplace */
1942          0,                     /* src_mask */
1943          0,                     /* dst_mask */
1944          FALSE),                /* pcrel_offset */
1945
1946   HOWTO (R_PPC64_IRELATIVE,     /* type */
1947          0,                     /* rightshift */
1948          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1949          64,                    /* bitsize */
1950          FALSE,                 /* pc_relative */
1951          0,                     /* bitpos */
1952          complain_overflow_dont, /* complain_on_overflow */
1953          bfd_elf_generic_reloc, /* special_function */
1954          "R_PPC64_IRELATIVE",   /* name */
1955          FALSE,                 /* partial_inplace */
1956          0,                     /* src_mask */
1957          ONES (64),             /* dst_mask */
1958          FALSE),                /* pcrel_offset */
1959
1960   /* A 16 bit relative relocation.  */
1961   HOWTO (R_PPC64_REL16,         /* type */
1962          0,                     /* rightshift */
1963          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1964          16,                    /* bitsize */
1965          TRUE,                  /* pc_relative */
1966          0,                     /* bitpos */
1967          complain_overflow_signed, /* complain_on_overflow */
1968          bfd_elf_generic_reloc, /* special_function */
1969          "R_PPC64_REL16",       /* name */
1970          FALSE,                 /* partial_inplace */
1971          0,                     /* src_mask */
1972          0xffff,                /* dst_mask */
1973          TRUE),                 /* pcrel_offset */
1974
1975   /* A 16 bit relative relocation without overflow.  */
1976   HOWTO (R_PPC64_REL16_LO,      /* type */
1977          0,                     /* rightshift */
1978          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1979          16,                    /* bitsize */
1980          TRUE,                  /* pc_relative */
1981          0,                     /* bitpos */
1982          complain_overflow_dont,/* complain_on_overflow */
1983          bfd_elf_generic_reloc, /* special_function */
1984          "R_PPC64_REL16_LO",    /* name */
1985          FALSE,                 /* partial_inplace */
1986          0,                     /* src_mask */
1987          0xffff,                /* dst_mask */
1988          TRUE),                 /* pcrel_offset */
1989
1990   /* The high order 16 bits of a relative address.  */
1991   HOWTO (R_PPC64_REL16_HI,      /* type */
1992          16,                    /* rightshift */
1993          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1994          16,                    /* bitsize */
1995          TRUE,                  /* pc_relative */
1996          0,                     /* bitpos */
1997          complain_overflow_signed, /* complain_on_overflow */
1998          bfd_elf_generic_reloc, /* special_function */
1999          "R_PPC64_REL16_HI",    /* name */
2000          FALSE,                 /* partial_inplace */
2001          0,                     /* src_mask */
2002          0xffff,                /* dst_mask */
2003          TRUE),                 /* pcrel_offset */
2004
2005   /* The high order 16 bits of a relative address, plus 1 if the contents of
2006      the low 16 bits, treated as a signed number, is negative.  */
2007   HOWTO (R_PPC64_REL16_HA,      /* type */
2008          16,                    /* rightshift */
2009          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2010          16,                    /* bitsize */
2011          TRUE,                  /* pc_relative */
2012          0,                     /* bitpos */
2013          complain_overflow_signed, /* complain_on_overflow */
2014          ppc64_elf_ha_reloc,    /* special_function */
2015          "R_PPC64_REL16_HA",    /* name */
2016          FALSE,                 /* partial_inplace */
2017          0,                     /* src_mask */
2018          0xffff,                /* dst_mask */
2019          TRUE),                 /* pcrel_offset */
2020
2021   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
2022   HOWTO (R_PPC64_ADDR16_HIGH,   /* type */
2023          16,                    /* rightshift */
2024          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2025          16,                    /* bitsize */
2026          FALSE,                 /* pc_relative */
2027          0,                     /* bitpos */
2028          complain_overflow_dont, /* complain_on_overflow */
2029          bfd_elf_generic_reloc, /* special_function */
2030          "R_PPC64_ADDR16_HIGH", /* name */
2031          FALSE,                 /* partial_inplace */
2032          0,                     /* src_mask */
2033          0xffff,                /* dst_mask */
2034          FALSE),                /* pcrel_offset */
2035
2036   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
2037   HOWTO (R_PPC64_ADDR16_HIGHA,  /* type */
2038          16,                    /* rightshift */
2039          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2040          16,                    /* bitsize */
2041          FALSE,                 /* pc_relative */
2042          0,                     /* bitpos */
2043          complain_overflow_dont, /* complain_on_overflow */
2044          ppc64_elf_ha_reloc,    /* special_function */
2045          "R_PPC64_ADDR16_HIGHA",        /* name */
2046          FALSE,                 /* partial_inplace */
2047          0,                     /* src_mask */
2048          0xffff,                /* dst_mask */
2049          FALSE),                /* pcrel_offset */
2050
2051   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
2052   HOWTO (R_PPC64_DTPREL16_HIGH,
2053          16,                    /* rightshift */
2054          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2055          16,                    /* bitsize */
2056          FALSE,                 /* pc_relative */
2057          0,                     /* bitpos */
2058          complain_overflow_dont, /* complain_on_overflow */
2059          ppc64_elf_unhandled_reloc, /* special_function */
2060          "R_PPC64_DTPREL16_HIGH", /* name */
2061          FALSE,                 /* partial_inplace */
2062          0,                     /* src_mask */
2063          0xffff,                /* dst_mask */
2064          FALSE),                /* pcrel_offset */
2065
2066   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
2067   HOWTO (R_PPC64_DTPREL16_HIGHA,
2068          16,                    /* rightshift */
2069          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2070          16,                    /* bitsize */
2071          FALSE,                 /* pc_relative */
2072          0,                     /* bitpos */
2073          complain_overflow_dont, /* complain_on_overflow */
2074          ppc64_elf_unhandled_reloc, /* special_function */
2075          "R_PPC64_DTPREL16_HIGHA", /* name */
2076          FALSE,                 /* partial_inplace */
2077          0,                     /* src_mask */
2078          0xffff,                /* dst_mask */
2079          FALSE),                /* pcrel_offset */
2080
2081   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
2082   HOWTO (R_PPC64_TPREL16_HIGH,
2083          16,                    /* rightshift */
2084          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2085          16,                    /* bitsize */
2086          FALSE,                 /* pc_relative */
2087          0,                     /* bitpos */
2088          complain_overflow_dont, /* complain_on_overflow */
2089          ppc64_elf_unhandled_reloc, /* special_function */
2090          "R_PPC64_TPREL16_HIGH",        /* name */
2091          FALSE,                 /* partial_inplace */
2092          0,                     /* src_mask */
2093          0xffff,                /* dst_mask */
2094          FALSE),                /* pcrel_offset */
2095
2096   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
2097   HOWTO (R_PPC64_TPREL16_HIGHA,
2098          16,                    /* rightshift */
2099          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2100          16,                    /* bitsize */
2101          FALSE,                 /* pc_relative */
2102          0,                     /* bitpos */
2103          complain_overflow_dont, /* complain_on_overflow */
2104          ppc64_elf_unhandled_reloc, /* special_function */
2105          "R_PPC64_TPREL16_HIGHA",       /* name */
2106          FALSE,                 /* partial_inplace */
2107          0,                     /* src_mask */
2108          0xffff,                /* dst_mask */
2109          FALSE),                /* pcrel_offset */
2110
2111   /* Like ADDR64, but use local entry point of function.  */
2112   HOWTO (R_PPC64_ADDR64_LOCAL,  /* type */
2113          0,                     /* rightshift */
2114          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2115          64,                    /* bitsize */
2116          FALSE,                 /* pc_relative */
2117          0,                     /* bitpos */
2118          complain_overflow_dont, /* complain_on_overflow */
2119          bfd_elf_generic_reloc, /* special_function */
2120          "R_PPC64_ADDR64_LOCAL", /* name */
2121          FALSE,                 /* partial_inplace */
2122          0,                     /* src_mask */
2123          ONES (64),             /* dst_mask */
2124          FALSE),                /* pcrel_offset */
2125
2126   /* GNU extension to record C++ vtable hierarchy.  */
2127   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2128          0,                     /* rightshift */
2129          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2130          0,                     /* bitsize */
2131          FALSE,                 /* pc_relative */
2132          0,                     /* bitpos */
2133          complain_overflow_dont, /* complain_on_overflow */
2134          NULL,                  /* special_function */
2135          "R_PPC64_GNU_VTINHERIT", /* name */
2136          FALSE,                 /* partial_inplace */
2137          0,                     /* src_mask */
2138          0,                     /* dst_mask */
2139          FALSE),                /* pcrel_offset */
2140
2141   /* GNU extension to record C++ vtable member usage.  */
2142   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
2143          0,                     /* rightshift */
2144          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2145          0,                     /* bitsize */
2146          FALSE,                 /* pc_relative */
2147          0,                     /* bitpos */
2148          complain_overflow_dont, /* complain_on_overflow */
2149          NULL,                  /* special_function */
2150          "R_PPC64_GNU_VTENTRY", /* name */
2151          FALSE,                 /* partial_inplace */
2152          0,                     /* src_mask */
2153          0,                     /* dst_mask */
2154          FALSE),                /* pcrel_offset */
2155 };
2156
2157 \f
2158 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2159    be done.  */
2160
2161 static void
2162 ppc_howto_init (void)
2163 {
2164   unsigned int i, type;
2165
2166   for (i = 0;
2167        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2168        i++)
2169     {
2170       type = ppc64_elf_howto_raw[i].type;
2171       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2172                           / sizeof (ppc64_elf_howto_table[0])));
2173       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2174     }
2175 }
2176
2177 static reloc_howto_type *
2178 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2179                              bfd_reloc_code_real_type code)
2180 {
2181   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2182
2183   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2184     /* Initialize howto table if needed.  */
2185     ppc_howto_init ();
2186
2187   switch (code)
2188     {
2189     default:
2190       return NULL;
2191
2192     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
2193       break;
2194     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
2195       break;
2196     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
2197       break;
2198     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
2199       break;
2200     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
2201       break;
2202     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
2203       break;
2204     case BFD_RELOC_PPC64_ADDR16_HIGH:           r = R_PPC64_ADDR16_HIGH;
2205       break;
2206     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
2207       break;
2208     case BFD_RELOC_PPC64_ADDR16_HIGHA:          r = R_PPC64_ADDR16_HIGHA;
2209       break;
2210     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
2211       break;
2212     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
2213       break;
2214     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
2215       break;
2216     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
2217       break;
2218     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
2219       break;
2220     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
2221       break;
2222     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
2223       break;
2224     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
2225       break;
2226     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
2227       break;
2228     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
2229       break;
2230     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
2231       break;
2232     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
2233       break;
2234     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
2235       break;
2236     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
2237       break;
2238     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
2239       break;
2240     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
2241       break;
2242     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
2243       break;
2244     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
2245       break;
2246     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
2247       break;
2248     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
2249       break;
2250     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
2251       break;
2252     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
2253       break;
2254     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
2255       break;
2256     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
2257       break;
2258     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
2259       break;
2260     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
2261       break;
2262     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
2263       break;
2264     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
2265       break;
2266     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
2267       break;
2268     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
2269       break;
2270     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
2271       break;
2272     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
2273       break;
2274     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
2275       break;
2276     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
2277       break;
2278     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
2279       break;
2280     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
2281       break;
2282     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
2283       break;
2284     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
2285       break;
2286     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
2287       break;
2288     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
2289       break;
2290     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
2291       break;
2292     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
2293       break;
2294     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
2295       break;
2296     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
2297       break;
2298     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
2299       break;
2300     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
2301       break;
2302     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
2303       break;
2304     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2305       break;
2306     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2307       break;
2308     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2309       break;
2310     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2311       break;
2312     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2313       break;
2314     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2315       break;
2316     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
2317       break;
2318     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
2319       break;
2320     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2321       break;
2322     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2323       break;
2324     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2325       break;
2326     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2327       break;
2328     case BFD_RELOC_PPC64_TPREL16_HIGH:          r = R_PPC64_TPREL16_HIGH;
2329       break;
2330     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2331       break;
2332     case BFD_RELOC_PPC64_TPREL16_HIGHA:         r = R_PPC64_TPREL16_HIGHA;
2333       break;
2334     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2335       break;
2336     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2337       break;
2338     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2339       break;
2340     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2341       break;
2342     case BFD_RELOC_PPC64_DTPREL16_HIGH:         r = R_PPC64_DTPREL16_HIGH;
2343       break;
2344     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2345       break;
2346     case BFD_RELOC_PPC64_DTPREL16_HIGHA:        r = R_PPC64_DTPREL16_HIGHA;
2347       break;
2348     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2349       break;
2350     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2351       break;
2352     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2353       break;
2354     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2355       break;
2356     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2357       break;
2358     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2359       break;
2360     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2361       break;
2362     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2363       break;
2364     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2365       break;
2366     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2367       break;
2368     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2369       break;
2370     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2371       break;
2372     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2373       break;
2374     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2375       break;
2376     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2377       break;
2378     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2379       break;
2380     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2381       break;
2382     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2383       break;
2384     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2385       break;
2386     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2387       break;
2388     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2389       break;
2390     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2391       break;
2392     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2393       break;
2394     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2395       break;
2396     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2397       break;
2398     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2399       break;
2400     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2401       break;
2402     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2403       break;
2404     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2405       break;
2406     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
2407       break;
2408     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
2409       break;
2410     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
2411       break;
2412     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
2413       break;
2414     case BFD_RELOC_PPC64_ADDR64_LOCAL:          r = R_PPC64_ADDR64_LOCAL;
2415       break;
2416     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2417       break;
2418     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2419       break;
2420     }
2421
2422   return ppc64_elf_howto_table[r];
2423 };
2424
2425 static reloc_howto_type *
2426 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2427                              const char *r_name)
2428 {
2429   unsigned int i;
2430
2431   for (i = 0;
2432        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2433        i++)
2434     if (ppc64_elf_howto_raw[i].name != NULL
2435         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2436       return &ppc64_elf_howto_raw[i];
2437
2438   return NULL;
2439 }
2440
2441 /* Set the howto pointer for a PowerPC ELF reloc.  */
2442
2443 static void
2444 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2445                          Elf_Internal_Rela *dst)
2446 {
2447   unsigned int type;
2448
2449   /* Initialize howto table if needed.  */
2450   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2451     ppc_howto_init ();
2452
2453   type = ELF64_R_TYPE (dst->r_info);
2454   if (type >= (sizeof (ppc64_elf_howto_table)
2455                / sizeof (ppc64_elf_howto_table[0])))
2456     {
2457       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2458                              abfd, (int) type);
2459       type = R_PPC64_NONE;
2460     }
2461   cache_ptr->howto = ppc64_elf_howto_table[type];
2462 }
2463
2464 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2465
2466 static bfd_reloc_status_type
2467 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2468                     void *data, asection *input_section,
2469                     bfd *output_bfd, char **error_message)
2470 {
2471   /* If this is a relocatable link (output_bfd test tells us), just
2472      call the generic function.  Any adjustment will be done at final
2473      link time.  */
2474   if (output_bfd != NULL)
2475     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2476                                   input_section, output_bfd, error_message);
2477
2478   /* Adjust the addend for sign extension of the low 16 bits.
2479      We won't actually be using the low 16 bits, so trashing them
2480      doesn't matter.  */
2481   reloc_entry->addend += 0x8000;
2482   return bfd_reloc_continue;
2483 }
2484
2485 static bfd_reloc_status_type
2486 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2487                         void *data, asection *input_section,
2488                         bfd *output_bfd, char **error_message)
2489 {
2490   if (output_bfd != NULL)
2491     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2492                                   input_section, output_bfd, error_message);
2493
2494   if (strcmp (symbol->section->name, ".opd") == 0
2495       && (symbol->section->owner->flags & DYNAMIC) == 0)
2496     {
2497       bfd_vma dest = opd_entry_value (symbol->section,
2498                                       symbol->value + reloc_entry->addend,
2499                                       NULL, NULL, FALSE);
2500       if (dest != (bfd_vma) -1)
2501         reloc_entry->addend = dest - (symbol->value
2502                                       + symbol->section->output_section->vma
2503                                       + symbol->section->output_offset);
2504     }
2505   else
2506     {
2507       elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2508
2509       if (symbol->section->owner != abfd
2510           && abiversion (symbol->section->owner) >= 2)
2511         {
2512           unsigned int i;
2513
2514           for (i = 0; i < symbol->section->owner->symcount; ++i)
2515             {
2516               asymbol *symdef = symbol->section->owner->outsymbols[i];
2517
2518               if (strcmp (symdef->name, symbol->name) == 0)
2519                 {
2520                   elfsym = (elf_symbol_type *) symdef;
2521                   break;
2522                 }
2523             }
2524         }
2525       reloc_entry->addend
2526         += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2527     }
2528   return bfd_reloc_continue;
2529 }
2530
2531 static bfd_reloc_status_type
2532 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2533                          void *data, asection *input_section,
2534                          bfd *output_bfd, char **error_message)
2535 {
2536   long insn;
2537   enum elf_ppc64_reloc_type r_type;
2538   bfd_size_type octets;
2539   /* Assume 'at' branch hints.  */
2540   bfd_boolean is_isa_v2 = TRUE;
2541
2542   /* If this is a relocatable link (output_bfd test tells us), just
2543      call the generic function.  Any adjustment will be done at final
2544      link time.  */
2545   if (output_bfd != NULL)
2546     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2547                                   input_section, output_bfd, error_message);
2548
2549   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2550   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2551   insn &= ~(0x01 << 21);
2552   r_type = reloc_entry->howto->type;
2553   if (r_type == R_PPC64_ADDR14_BRTAKEN
2554       || r_type == R_PPC64_REL14_BRTAKEN)
2555     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2556
2557   if (is_isa_v2)
2558     {
2559       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2560          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2561          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2562       if ((insn & (0x14 << 21)) == (0x04 << 21))
2563         insn |= 0x02 << 21;
2564       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2565         insn |= 0x08 << 21;
2566       else
2567         goto out;
2568     }
2569   else
2570     {
2571       bfd_vma target = 0;
2572       bfd_vma from;
2573
2574       if (!bfd_is_com_section (symbol->section))
2575         target = symbol->value;
2576       target += symbol->section->output_section->vma;
2577       target += symbol->section->output_offset;
2578       target += reloc_entry->addend;
2579
2580       from = (reloc_entry->address
2581               + input_section->output_offset
2582               + input_section->output_section->vma);
2583
2584       /* Invert 'y' bit if not the default.  */
2585       if ((bfd_signed_vma) (target - from) < 0)
2586         insn ^= 0x01 << 21;
2587     }
2588   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2589  out:
2590   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2591                                  input_section, output_bfd, error_message);
2592 }
2593
2594 static bfd_reloc_status_type
2595 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2596                          void *data, asection *input_section,
2597                          bfd *output_bfd, char **error_message)
2598 {
2599   /* If this is a relocatable link (output_bfd test tells us), just
2600      call the generic function.  Any adjustment will be done at final
2601      link time.  */
2602   if (output_bfd != NULL)
2603     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2604                                   input_section, output_bfd, error_message);
2605
2606   /* Subtract the symbol section base address.  */
2607   reloc_entry->addend -= symbol->section->output_section->vma;
2608   return bfd_reloc_continue;
2609 }
2610
2611 static bfd_reloc_status_type
2612 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2613                             void *data, asection *input_section,
2614                             bfd *output_bfd, char **error_message)
2615 {
2616   /* If this is a relocatable link (output_bfd test tells us), just
2617      call the generic function.  Any adjustment will be done at final
2618      link time.  */
2619   if (output_bfd != NULL)
2620     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2621                                   input_section, output_bfd, error_message);
2622
2623   /* Subtract the symbol section base address.  */
2624   reloc_entry->addend -= symbol->section->output_section->vma;
2625
2626   /* Adjust the addend for sign extension of the low 16 bits.  */
2627   reloc_entry->addend += 0x8000;
2628   return bfd_reloc_continue;
2629 }
2630
2631 static bfd_reloc_status_type
2632 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2633                      void *data, asection *input_section,
2634                      bfd *output_bfd, char **error_message)
2635 {
2636   bfd_vma TOCstart;
2637
2638   /* If this is a relocatable link (output_bfd test tells us), just
2639      call the generic function.  Any adjustment will be done at final
2640      link time.  */
2641   if (output_bfd != NULL)
2642     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2643                                   input_section, output_bfd, error_message);
2644
2645   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2646   if (TOCstart == 0)
2647     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2648
2649   /* Subtract the TOC base address.  */
2650   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2651   return bfd_reloc_continue;
2652 }
2653
2654 static bfd_reloc_status_type
2655 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2656                         void *data, asection *input_section,
2657                         bfd *output_bfd, char **error_message)
2658 {
2659   bfd_vma TOCstart;
2660
2661   /* If this is a relocatable link (output_bfd test tells us), just
2662      call the generic function.  Any adjustment will be done at final
2663      link time.  */
2664   if (output_bfd != NULL)
2665     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2666                                   input_section, output_bfd, error_message);
2667
2668   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2669   if (TOCstart == 0)
2670     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2671
2672   /* Subtract the TOC base address.  */
2673   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2674
2675   /* Adjust the addend for sign extension of the low 16 bits.  */
2676   reloc_entry->addend += 0x8000;
2677   return bfd_reloc_continue;
2678 }
2679
2680 static bfd_reloc_status_type
2681 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2682                        void *data, asection *input_section,
2683                        bfd *output_bfd, char **error_message)
2684 {
2685   bfd_vma TOCstart;
2686   bfd_size_type octets;
2687
2688   /* If this is a relocatable link (output_bfd test tells us), just
2689      call the generic function.  Any adjustment will be done at final
2690      link time.  */
2691   if (output_bfd != NULL)
2692     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2693                                   input_section, output_bfd, error_message);
2694
2695   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2696   if (TOCstart == 0)
2697     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2698
2699   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2700   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2701   return bfd_reloc_ok;
2702 }
2703
2704 static bfd_reloc_status_type
2705 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2706                            void *data, asection *input_section,
2707                            bfd *output_bfd, char **error_message)
2708 {
2709   /* If this is a relocatable link (output_bfd test tells us), just
2710      call the generic function.  Any adjustment will be done at final
2711      link time.  */
2712   if (output_bfd != NULL)
2713     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2714                                   input_section, output_bfd, error_message);
2715
2716   if (error_message != NULL)
2717     {
2718       static char buf[60];
2719       sprintf (buf, "generic linker can't handle %s",
2720                reloc_entry->howto->name);
2721       *error_message = buf;
2722     }
2723   return bfd_reloc_dangerous;
2724 }
2725
2726 /* Track GOT entries needed for a given symbol.  We might need more
2727    than one got entry per symbol.  */
2728 struct got_entry
2729 {
2730   struct got_entry *next;
2731
2732   /* The symbol addend that we'll be placing in the GOT.  */
2733   bfd_vma addend;
2734
2735   /* Unlike other ELF targets, we use separate GOT entries for the same
2736      symbol referenced from different input files.  This is to support
2737      automatic multiple TOC/GOT sections, where the TOC base can vary
2738      from one input file to another.  After partitioning into TOC groups
2739      we merge entries within the group.
2740
2741      Point to the BFD owning this GOT entry.  */
2742   bfd *owner;
2743
2744   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2745      TLS_TPREL or TLS_DTPREL for tls entries.  */
2746   unsigned char tls_type;
2747
2748   /* Non-zero if got.ent points to real entry.  */
2749   unsigned char is_indirect;
2750
2751   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2752   union
2753     {
2754       bfd_signed_vma refcount;
2755       bfd_vma offset;
2756       struct got_entry *ent;
2757     } got;
2758 };
2759
2760 /* The same for PLT.  */
2761 struct plt_entry
2762 {
2763   struct plt_entry *next;
2764
2765   bfd_vma addend;
2766
2767   union
2768     {
2769       bfd_signed_vma refcount;
2770       bfd_vma offset;
2771     } plt;
2772 };
2773
2774 struct ppc64_elf_obj_tdata
2775 {
2776   struct elf_obj_tdata elf;
2777
2778   /* Shortcuts to dynamic linker sections.  */
2779   asection *got;
2780   asection *relgot;
2781
2782   /* Used during garbage collection.  We attach global symbols defined
2783      on removed .opd entries to this section so that the sym is removed.  */
2784   asection *deleted_section;
2785
2786   /* TLS local dynamic got entry handling.  Support for multiple GOT
2787      sections means we potentially need one of these for each input bfd.  */
2788   struct got_entry tlsld_got;
2789
2790   union {
2791     /* A copy of relocs before they are modified for --emit-relocs.  */
2792     Elf_Internal_Rela *relocs;
2793
2794     /* Section contents.  */
2795     bfd_byte *contents;
2796   } opd;
2797
2798   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2799      the reloc to be in the range -32768 to 32767.  */
2800   unsigned int has_small_toc_reloc : 1;
2801
2802   /* Set if toc/got ha relocs detected not using r2, or lo reloc
2803      instruction not one we handle.  */
2804   unsigned int unexpected_toc_insn : 1;
2805 };
2806
2807 #define ppc64_elf_tdata(bfd) \
2808   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2809
2810 #define ppc64_tlsld_got(bfd) \
2811   (&ppc64_elf_tdata (bfd)->tlsld_got)
2812
2813 #define is_ppc64_elf(bfd) \
2814   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2815    && elf_object_id (bfd) == PPC64_ELF_DATA)
2816
2817 /* Override the generic function because we store some extras.  */
2818
2819 static bfd_boolean
2820 ppc64_elf_mkobject (bfd *abfd)
2821 {
2822   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2823                                   PPC64_ELF_DATA);
2824 }
2825
2826 /* Fix bad default arch selected for a 64 bit input bfd when the
2827    default is 32 bit.  */
2828
2829 static bfd_boolean
2830 ppc64_elf_object_p (bfd *abfd)
2831 {
2832   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2833     {
2834       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2835
2836       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2837         {
2838           /* Relies on arch after 32 bit default being 64 bit default.  */
2839           abfd->arch_info = abfd->arch_info->next;
2840           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2841         }
2842     }
2843   return TRUE;
2844 }
2845
2846 /* Support for core dump NOTE sections.  */
2847
2848 static bfd_boolean
2849 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2850 {
2851   size_t offset, size;
2852
2853   if (note->descsz != 504)
2854     return FALSE;
2855
2856   /* pr_cursig */
2857   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2858
2859   /* pr_pid */
2860   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2861
2862   /* pr_reg */
2863   offset = 112;
2864   size = 384;
2865
2866   /* Make a ".reg/999" section.  */
2867   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2868                                           size, note->descpos + offset);
2869 }
2870
2871 static bfd_boolean
2872 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2873 {
2874   if (note->descsz != 136)
2875     return FALSE;
2876
2877   elf_tdata (abfd)->core->pid
2878     = bfd_get_32 (abfd, note->descdata + 24);
2879   elf_tdata (abfd)->core->program
2880     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2881   elf_tdata (abfd)->core->command
2882     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2883
2884   return TRUE;
2885 }
2886
2887 static char *
2888 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2889                            ...)
2890 {
2891   switch (note_type)
2892     {
2893     default:
2894       return NULL;
2895
2896     case NT_PRPSINFO:
2897       {
2898         char data[136];
2899         va_list ap;
2900
2901         va_start (ap, note_type);
2902         memset (data, 0, sizeof (data));
2903         strncpy (data + 40, va_arg (ap, const char *), 16);
2904         strncpy (data + 56, va_arg (ap, const char *), 80);
2905         va_end (ap);
2906         return elfcore_write_note (abfd, buf, bufsiz,
2907                                    "CORE", note_type, data, sizeof (data));
2908       }
2909
2910     case NT_PRSTATUS:
2911       {
2912         char data[504];
2913         va_list ap;
2914         long pid;
2915         int cursig;
2916         const void *greg;
2917
2918         va_start (ap, note_type);
2919         memset (data, 0, 112);
2920         pid = va_arg (ap, long);
2921         bfd_put_32 (abfd, pid, data + 32);
2922         cursig = va_arg (ap, int);
2923         bfd_put_16 (abfd, cursig, data + 12);
2924         greg = va_arg (ap, const void *);
2925         memcpy (data + 112, greg, 384);
2926         memset (data + 496, 0, 8);
2927         va_end (ap);
2928         return elfcore_write_note (abfd, buf, bufsiz,
2929                                    "CORE", note_type, data, sizeof (data));
2930       }
2931     }
2932 }
2933
2934 /* Add extra PPC sections.  */
2935
2936 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2937 {
2938   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
2939   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2940   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2941   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2942   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2943   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2944   { NULL,                     0,  0, 0,            0 }
2945 };
2946
2947 enum _ppc64_sec_type {
2948   sec_normal = 0,
2949   sec_opd = 1,
2950   sec_toc = 2
2951 };
2952
2953 struct _ppc64_elf_section_data
2954 {
2955   struct bfd_elf_section_data elf;
2956
2957   union
2958   {
2959     /* An array with one entry for each opd function descriptor,
2960        and some spares since opd entries may be either 16 or 24 bytes.  */
2961 #define OPD_NDX(OFF) ((OFF) >> 4)
2962     struct _opd_sec_data
2963     {
2964       /* Points to the function code section for local opd entries.  */
2965       asection **func_sec;
2966
2967       /* After editing .opd, adjust references to opd local syms.  */
2968       long *adjust;
2969     } opd;
2970
2971     /* An array for toc sections, indexed by offset/8.  */
2972     struct _toc_sec_data
2973     {
2974       /* Specifies the relocation symbol index used at a given toc offset.  */
2975       unsigned *symndx;
2976
2977       /* And the relocation addend.  */
2978       bfd_vma *add;
2979     } toc;
2980   } u;
2981
2982   enum _ppc64_sec_type sec_type:2;
2983
2984   /* Flag set when small branches are detected.  Used to
2985      select suitable defaults for the stub group size.  */
2986   unsigned int has_14bit_branch:1;
2987 };
2988
2989 #define ppc64_elf_section_data(sec) \
2990   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2991
2992 static bfd_boolean
2993 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2994 {
2995   if (!sec->used_by_bfd)
2996     {
2997       struct _ppc64_elf_section_data *sdata;
2998       bfd_size_type amt = sizeof (*sdata);
2999
3000       sdata = bfd_zalloc (abfd, amt);
3001       if (sdata == NULL)
3002         return FALSE;
3003       sec->used_by_bfd = sdata;
3004     }
3005
3006   return _bfd_elf_new_section_hook (abfd, sec);
3007 }
3008
3009 static struct _opd_sec_data *
3010 get_opd_info (asection * sec)
3011 {
3012   if (sec != NULL
3013       && ppc64_elf_section_data (sec) != NULL
3014       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3015     return &ppc64_elf_section_data (sec)->u.opd;
3016   return NULL;
3017 }
3018 \f
3019 /* Parameters for the qsort hook.  */
3020 static bfd_boolean synthetic_relocatable;
3021
3022 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
3023
3024 static int
3025 compare_symbols (const void *ap, const void *bp)
3026 {
3027   const asymbol *a = * (const asymbol **) ap;
3028   const asymbol *b = * (const asymbol **) bp;
3029
3030   /* Section symbols first.  */
3031   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3032     return -1;
3033   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3034     return 1;
3035
3036   /* then .opd symbols.  */
3037   if (strcmp (a->section->name, ".opd") == 0
3038       && strcmp (b->section->name, ".opd") != 0)
3039     return -1;
3040   if (strcmp (a->section->name, ".opd") != 0
3041       && strcmp (b->section->name, ".opd") == 0)
3042     return 1;
3043
3044   /* then other code symbols.  */
3045   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3046       == (SEC_CODE | SEC_ALLOC)
3047       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3048          != (SEC_CODE | SEC_ALLOC))
3049     return -1;
3050
3051   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3052       != (SEC_CODE | SEC_ALLOC)
3053       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3054          == (SEC_CODE | SEC_ALLOC))
3055     return 1;
3056
3057   if (synthetic_relocatable)
3058     {
3059       if (a->section->id < b->section->id)
3060         return -1;
3061
3062       if (a->section->id > b->section->id)
3063         return 1;
3064     }
3065
3066   if (a->value + a->section->vma < b->value + b->section->vma)
3067     return -1;
3068
3069   if (a->value + a->section->vma > b->value + b->section->vma)
3070     return 1;
3071
3072   /* For syms with the same value, prefer strong dynamic global function
3073      syms over other syms.  */
3074   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3075     return -1;
3076
3077   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3078     return 1;
3079
3080   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3081     return -1;
3082
3083   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3084     return 1;
3085
3086   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3087     return -1;
3088
3089   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3090     return 1;
3091
3092   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3093     return -1;
3094
3095   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3096     return 1;
3097
3098   return 0;
3099 }
3100
3101 /* Search SYMS for a symbol of the given VALUE.  */
3102
3103 static asymbol *
3104 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3105 {
3106   long mid;
3107
3108   if (id == (unsigned) -1)
3109     {
3110       while (lo < hi)
3111         {
3112           mid = (lo + hi) >> 1;
3113           if (syms[mid]->value + syms[mid]->section->vma < value)
3114             lo = mid + 1;
3115           else if (syms[mid]->value + syms[mid]->section->vma > value)
3116             hi = mid;
3117           else
3118             return syms[mid];
3119         }
3120     }
3121   else
3122     {
3123       while (lo < hi)
3124         {
3125           mid = (lo + hi) >> 1;
3126           if (syms[mid]->section->id < id)
3127             lo = mid + 1;
3128           else if (syms[mid]->section->id > id)
3129             hi = mid;
3130           else if (syms[mid]->value < value)
3131             lo = mid + 1;
3132           else if (syms[mid]->value > value)
3133             hi = mid;
3134           else
3135             return syms[mid];
3136         }
3137     }
3138   return NULL;
3139 }
3140
3141 static bfd_boolean
3142 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3143 {
3144   bfd_vma vma = *(bfd_vma *) ptr;
3145   return ((section->flags & SEC_ALLOC) != 0
3146           && section->vma <= vma
3147           && vma < section->vma + section->size);
3148 }
3149
3150 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3151    entry syms.  Also generate @plt symbols for the glink branch table.
3152    Returns count of synthetic symbols in RET or -1 on error.  */
3153
3154 static long
3155 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3156                                 long static_count, asymbol **static_syms,
3157                                 long dyn_count, asymbol **dyn_syms,
3158                                 asymbol **ret)
3159 {
3160   asymbol *s;
3161   long i;
3162   long count;
3163   char *names;
3164   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3165   asection *opd = NULL;
3166   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3167   asymbol **syms;
3168   int abi = abiversion (abfd);
3169
3170   *ret = NULL;
3171
3172   if (abi < 2)
3173     {
3174       opd = bfd_get_section_by_name (abfd, ".opd");
3175       if (opd == NULL && abi == 1)
3176         return 0;
3177     }
3178
3179   symcount = static_count;
3180   if (!relocatable)
3181     symcount += dyn_count;
3182   if (symcount == 0)
3183     return 0;
3184
3185   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3186   if (syms == NULL)
3187     return -1;
3188
3189   if (!relocatable && static_count != 0 && dyn_count != 0)
3190     {
3191       /* Use both symbol tables.  */
3192       memcpy (syms, static_syms, static_count * sizeof (*syms));
3193       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3194     }
3195   else if (!relocatable && static_count == 0)
3196     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3197   else
3198     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3199
3200   synthetic_relocatable = relocatable;
3201   qsort (syms, symcount, sizeof (*syms), compare_symbols);
3202
3203   if (!relocatable && symcount > 1)
3204     {
3205       long j;
3206       /* Trim duplicate syms, since we may have merged the normal and
3207          dynamic symbols.  Actually, we only care about syms that have
3208          different values, so trim any with the same value.  */
3209       for (i = 1, j = 1; i < symcount; ++i)
3210         if (syms[i - 1]->value + syms[i - 1]->section->vma
3211             != syms[i]->value + syms[i]->section->vma)
3212           syms[j++] = syms[i];
3213       symcount = j;
3214     }
3215
3216   i = 0;
3217   if (strcmp (syms[i]->section->name, ".opd") == 0)
3218     ++i;
3219   codesecsym = i;
3220
3221   for (; i < symcount; ++i)
3222     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3223          != (SEC_CODE | SEC_ALLOC))
3224         || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3225       break;
3226   codesecsymend = i;
3227
3228   for (; i < symcount; ++i)
3229     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3230       break;
3231   secsymend = i;
3232
3233   for (; i < symcount; ++i)
3234     if (strcmp (syms[i]->section->name, ".opd") != 0)
3235       break;
3236   opdsymend = i;
3237
3238   for (; i < symcount; ++i)
3239     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3240         != (SEC_CODE | SEC_ALLOC))
3241       break;
3242   symcount = i;
3243
3244   count = 0;
3245
3246   if (relocatable)
3247     {
3248       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3249       arelent *r;
3250       size_t size;
3251       long relcount;
3252
3253       if (opdsymend == secsymend)
3254         goto done;
3255
3256       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3257       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3258       if (relcount == 0)
3259         goto done;
3260
3261       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3262         {
3263           count = -1;
3264           goto done;
3265         }
3266
3267       size = 0;
3268       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3269         {
3270           asymbol *sym;
3271
3272           while (r < opd->relocation + relcount
3273                  && r->address < syms[i]->value + opd->vma)
3274             ++r;
3275
3276           if (r == opd->relocation + relcount)
3277             break;
3278
3279           if (r->address != syms[i]->value + opd->vma)
3280             continue;
3281
3282           if (r->howto->type != R_PPC64_ADDR64)
3283             continue;
3284
3285           sym = *r->sym_ptr_ptr;
3286           if (!sym_exists_at (syms, opdsymend, symcount,
3287                               sym->section->id, sym->value + r->addend))
3288             {
3289               ++count;
3290               size += sizeof (asymbol);
3291               size += strlen (syms[i]->name) + 2;
3292             }
3293         }
3294
3295       if (size == 0)
3296         goto done;
3297       s = *ret = bfd_malloc (size);
3298       if (s == NULL)
3299         {
3300           count = -1;
3301           goto done;
3302         }
3303
3304       names = (char *) (s + count);
3305
3306       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3307         {
3308           asymbol *sym;
3309
3310           while (r < opd->relocation + relcount
3311                  && r->address < syms[i]->value + opd->vma)
3312             ++r;
3313
3314           if (r == opd->relocation + relcount)
3315             break;
3316
3317           if (r->address != syms[i]->value + opd->vma)
3318             continue;
3319
3320           if (r->howto->type != R_PPC64_ADDR64)
3321             continue;
3322
3323           sym = *r->sym_ptr_ptr;
3324           if (!sym_exists_at (syms, opdsymend, symcount,
3325                               sym->section->id, sym->value + r->addend))
3326             {
3327               size_t len;
3328
3329               *s = *syms[i];
3330               s->flags |= BSF_SYNTHETIC;
3331               s->section = sym->section;
3332               s->value = sym->value + r->addend;
3333               s->name = names;
3334               *names++ = '.';
3335               len = strlen (syms[i]->name);
3336               memcpy (names, syms[i]->name, len + 1);
3337               names += len + 1;
3338               /* Have udata.p point back to the original symbol this
3339                  synthetic symbol was derived from.  */
3340               s->udata.p = syms[i];
3341               s++;
3342             }
3343         }
3344     }
3345   else
3346     {
3347       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3348       bfd_byte *contents = NULL;
3349       size_t size;
3350       long plt_count = 0;
3351       bfd_vma glink_vma = 0, resolv_vma = 0;
3352       asection *dynamic, *glink = NULL, *relplt = NULL;
3353       arelent *p;
3354
3355       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3356         {
3357         free_contents_and_exit_err:
3358           count = -1;
3359         free_contents_and_exit:
3360           if (contents)
3361             free (contents);
3362           goto done;
3363         }
3364
3365       size = 0;
3366       for (i = secsymend; i < opdsymend; ++i)
3367         {
3368           bfd_vma ent;
3369
3370           /* Ignore bogus symbols.  */
3371           if (syms[i]->value > opd->size - 8)
3372             continue;
3373
3374           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3375           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3376             {
3377               ++count;
3378               size += sizeof (asymbol);
3379               size += strlen (syms[i]->name) + 2;
3380             }
3381         }
3382
3383       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
3384       if (dyn_count != 0
3385           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3386         {
3387           bfd_byte *dynbuf, *extdyn, *extdynend;
3388           size_t extdynsize;
3389           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3390
3391           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3392             goto free_contents_and_exit_err;
3393
3394           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3395           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3396
3397           extdyn = dynbuf;
3398           extdynend = extdyn + dynamic->size;
3399           for (; extdyn < extdynend; extdyn += extdynsize)
3400             {
3401               Elf_Internal_Dyn dyn;
3402               (*swap_dyn_in) (abfd, extdyn, &dyn);
3403
3404               if (dyn.d_tag == DT_NULL)
3405                 break;
3406
3407               if (dyn.d_tag == DT_PPC64_GLINK)
3408                 {
3409                   /* The first glink stub starts at offset 32; see
3410                      comment in ppc64_elf_finish_dynamic_sections. */
3411                   glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
3412                   /* The .glink section usually does not survive the final
3413                      link; search for the section (usually .text) where the
3414                      glink stubs now reside.  */
3415                   glink = bfd_sections_find_if (abfd, section_covers_vma,
3416                                                 &glink_vma);
3417                   break;
3418                 }
3419             }
3420
3421           free (dynbuf);
3422         }
3423
3424       if (glink != NULL)
3425         {
3426           /* Determine __glink trampoline by reading the relative branch
3427              from the first glink stub.  */
3428           bfd_byte buf[4];
3429           unsigned int off = 0;
3430
3431           while (bfd_get_section_contents (abfd, glink, buf,
3432                                            glink_vma + off - glink->vma, 4))
3433             {
3434               unsigned int insn = bfd_get_32 (abfd, buf);
3435               insn ^= B_DOT;
3436               if ((insn & ~0x3fffffc) == 0)
3437                 {
3438                   resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3439                   break;
3440                 }
3441               off += 4;
3442               if (off > 4)
3443                 break;
3444             }
3445
3446           if (resolv_vma)
3447             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3448
3449           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3450           if (relplt != NULL)
3451             {
3452               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3453               if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3454                 goto free_contents_and_exit_err;
3455
3456               plt_count = relplt->size / sizeof (Elf64_External_Rela);
3457               size += plt_count * sizeof (asymbol);
3458
3459               p = relplt->relocation;
3460               for (i = 0; i < plt_count; i++, p++)
3461                 {
3462                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3463                   if (p->addend != 0)
3464                     size += sizeof ("+0x") - 1 + 16;
3465                 }
3466             }
3467         }
3468
3469       if (size == 0)
3470         goto free_contents_and_exit;
3471       s = *ret = bfd_malloc (size);
3472       if (s == NULL)
3473         goto free_contents_and_exit_err;
3474
3475       names = (char *) (s + count + plt_count + (resolv_vma != 0));
3476
3477       for (i = secsymend; i < opdsymend; ++i)
3478         {
3479           bfd_vma ent;
3480
3481           if (syms[i]->value > opd->size - 8)
3482             continue;
3483
3484           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3485           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3486             {
3487               long lo, hi;
3488               size_t len;
3489               asection *sec = abfd->sections;
3490
3491               *s = *syms[i];
3492               lo = codesecsym;
3493               hi = codesecsymend;
3494               while (lo < hi)
3495                 {
3496                   long mid = (lo + hi) >> 1;
3497                   if (syms[mid]->section->vma < ent)
3498                     lo = mid + 1;
3499                   else if (syms[mid]->section->vma > ent)
3500                     hi = mid;
3501                   else
3502                     {
3503                       sec = syms[mid]->section;
3504                       break;
3505                     }
3506                 }
3507
3508               if (lo >= hi && lo > codesecsym)
3509                 sec = syms[lo - 1]->section;
3510
3511               for (; sec != NULL; sec = sec->next)
3512                 {
3513                   if (sec->vma > ent)
3514                     break;
3515                   /* SEC_LOAD may not be set if SEC is from a separate debug
3516                      info file.  */
3517                   if ((sec->flags & SEC_ALLOC) == 0)
3518                     break;
3519                   if ((sec->flags & SEC_CODE) != 0)
3520                     s->section = sec;
3521                 }
3522               s->flags |= BSF_SYNTHETIC;
3523               s->value = ent - s->section->vma;
3524               s->name = names;
3525               *names++ = '.';
3526               len = strlen (syms[i]->name);
3527               memcpy (names, syms[i]->name, len + 1);
3528               names += len + 1;
3529               /* Have udata.p point back to the original symbol this
3530                  synthetic symbol was derived from.  */
3531               s->udata.p = syms[i];
3532               s++;
3533             }
3534         }
3535       free (contents);
3536
3537       if (glink != NULL && relplt != NULL)
3538         {
3539           if (resolv_vma)
3540             {
3541               /* Add a symbol for the main glink trampoline.  */
3542               memset (s, 0, sizeof *s);
3543               s->the_bfd = abfd;
3544               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3545               s->section = glink;
3546               s->value = resolv_vma - glink->vma;
3547               s->name = names;
3548               memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3549               names += sizeof ("__glink_PLTresolve");
3550               s++;
3551               count++;
3552             }
3553
3554           /* FIXME: It would be very much nicer to put sym@plt on the
3555              stub rather than on the glink branch table entry.  The
3556              objdump disassembler would then use a sensible symbol
3557              name on plt calls.  The difficulty in doing so is
3558              a) finding the stubs, and,
3559              b) matching stubs against plt entries, and,
3560              c) there can be multiple stubs for a given plt entry.
3561
3562              Solving (a) could be done by code scanning, but older
3563              ppc64 binaries used different stubs to current code.
3564              (b) is the tricky one since you need to known the toc
3565              pointer for at least one function that uses a pic stub to
3566              be able to calculate the plt address referenced.
3567              (c) means gdb would need to set multiple breakpoints (or
3568              find the glink branch itself) when setting breakpoints
3569              for pending shared library loads.  */
3570           p = relplt->relocation;
3571           for (i = 0; i < plt_count; i++, p++)
3572             {
3573               size_t len;
3574
3575               *s = **p->sym_ptr_ptr;
3576               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
3577                  we are defining a symbol, ensure one of them is set.  */
3578               if ((s->flags & BSF_LOCAL) == 0)
3579                 s->flags |= BSF_GLOBAL;
3580               s->flags |= BSF_SYNTHETIC;
3581               s->section = glink;
3582               s->value = glink_vma - glink->vma;
3583               s->name = names;
3584               s->udata.p = NULL;
3585               len = strlen ((*p->sym_ptr_ptr)->name);
3586               memcpy (names, (*p->sym_ptr_ptr)->name, len);
3587               names += len;
3588               if (p->addend != 0)
3589                 {
3590                   memcpy (names, "+0x", sizeof ("+0x") - 1);
3591                   names += sizeof ("+0x") - 1;
3592                   bfd_sprintf_vma (abfd, names, p->addend);
3593                   names += strlen (names);
3594                 }
3595               memcpy (names, "@plt", sizeof ("@plt"));
3596               names += sizeof ("@plt");
3597               s++;
3598               if (abi < 2)
3599                 {
3600                   glink_vma += 8;
3601                   if (i >= 0x8000)
3602                     glink_vma += 4;
3603                 }
3604               else
3605                 glink_vma += 4;
3606             }
3607           count += plt_count;
3608         }
3609     }
3610
3611  done:
3612   free (syms);
3613   return count;
3614 }
3615 \f
3616 /* The following functions are specific to the ELF linker, while
3617    functions above are used generally.  Those named ppc64_elf_* are
3618    called by the main ELF linker code.  They appear in this file more
3619    or less in the order in which they are called.  eg.
3620    ppc64_elf_check_relocs is called early in the link process,
3621    ppc64_elf_finish_dynamic_sections is one of the last functions
3622    called.
3623
3624    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3625    functions have both a function code symbol and a function descriptor
3626    symbol.  A call to foo in a relocatable object file looks like:
3627
3628    .            .text
3629    .    x:
3630    .            bl      .foo
3631    .            nop
3632
3633    The function definition in another object file might be:
3634
3635    .            .section .opd
3636    .    foo:    .quad   .foo
3637    .            .quad   .TOC.@tocbase
3638    .            .quad   0
3639    .
3640    .            .text
3641    .    .foo:   blr
3642
3643    When the linker resolves the call during a static link, the branch
3644    unsurprisingly just goes to .foo and the .opd information is unused.
3645    If the function definition is in a shared library, things are a little
3646    different:  The call goes via a plt call stub, the opd information gets
3647    copied to the plt, and the linker patches the nop.
3648
3649    .    x:
3650    .            bl      .foo_stub
3651    .            ld      2,40(1)
3652    .
3653    .
3654    .    .foo_stub:
3655    .            std     2,40(1)                 # in practice, the call stub
3656    .            addis   11,2,Lfoo@toc@ha        # is slightly optimized, but
3657    .            addi    11,11,Lfoo@toc@l        # this is the general idea
3658    .            ld      12,0(11)
3659    .            ld      2,8(11)
3660    .            mtctr   12
3661    .            ld      11,16(11)
3662    .            bctr
3663    .
3664    .            .section .plt
3665    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
3666
3667    The "reloc ()" notation is supposed to indicate that the linker emits
3668    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3669    copying.
3670
3671    What are the difficulties here?  Well, firstly, the relocations
3672    examined by the linker in check_relocs are against the function code
3673    sym .foo, while the dynamic relocation in the plt is emitted against
3674    the function descriptor symbol, foo.  Somewhere along the line, we need
3675    to carefully copy dynamic link information from one symbol to the other.
3676    Secondly, the generic part of the elf linker will make .foo a dynamic
3677    symbol as is normal for most other backends.  We need foo dynamic
3678    instead, at least for an application final link.  However, when
3679    creating a shared library containing foo, we need to have both symbols
3680    dynamic so that references to .foo are satisfied during the early
3681    stages of linking.  Otherwise the linker might decide to pull in a
3682    definition from some other object, eg. a static library.
3683
3684    Update: As of August 2004, we support a new convention.  Function
3685    calls may use the function descriptor symbol, ie. "bl foo".  This
3686    behaves exactly as "bl .foo".  */
3687
3688 /* Of those relocs that might be copied as dynamic relocs, this function
3689    selects those that must be copied when linking a shared library,
3690    even when the symbol is local.  */
3691
3692 static int
3693 must_be_dyn_reloc (struct bfd_link_info *info,
3694                    enum elf_ppc64_reloc_type r_type)
3695 {
3696   switch (r_type)
3697     {
3698     default:
3699       return 1;
3700
3701     case R_PPC64_REL32:
3702     case R_PPC64_REL64:
3703     case R_PPC64_REL30:
3704       return 0;
3705
3706     case R_PPC64_TPREL16:
3707     case R_PPC64_TPREL16_LO:
3708     case R_PPC64_TPREL16_HI:
3709     case R_PPC64_TPREL16_HA:
3710     case R_PPC64_TPREL16_DS:
3711     case R_PPC64_TPREL16_LO_DS:
3712     case R_PPC64_TPREL16_HIGH:
3713     case R_PPC64_TPREL16_HIGHA:
3714     case R_PPC64_TPREL16_HIGHER:
3715     case R_PPC64_TPREL16_HIGHERA:
3716     case R_PPC64_TPREL16_HIGHEST:
3717     case R_PPC64_TPREL16_HIGHESTA:
3718     case R_PPC64_TPREL64:
3719       return !bfd_link_executable (info);
3720     }
3721 }
3722
3723 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3724    copying dynamic variables from a shared lib into an app's dynbss
3725    section, and instead use a dynamic relocation to point into the
3726    shared lib.  With code that gcc generates, it's vital that this be
3727    enabled;  In the PowerPC64 ABI, the address of a function is actually
3728    the address of a function descriptor, which resides in the .opd
3729    section.  gcc uses the descriptor directly rather than going via the
3730    GOT as some other ABI's do, which means that initialized function
3731    pointers must reference the descriptor.  Thus, a function pointer
3732    initialized to the address of a function in a shared library will
3733    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3734    redefines the function descriptor symbol to point to the copy.  This
3735    presents a problem as a plt entry for that function is also
3736    initialized from the function descriptor symbol and the copy reloc
3737    may not be initialized first.  */
3738 #define ELIMINATE_COPY_RELOCS 1
3739
3740 /* Section name for stubs is the associated section name plus this
3741    string.  */
3742 #define STUB_SUFFIX ".stub"
3743
3744 /* Linker stubs.
3745    ppc_stub_long_branch:
3746    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3747    destination, but a 24 bit branch in a stub section will reach.
3748    .    b       dest
3749
3750    ppc_stub_plt_branch:
3751    Similar to the above, but a 24 bit branch in the stub section won't
3752    reach its destination.
3753    .    addis   %r11,%r2,xxx@toc@ha
3754    .    ld      %r12,xxx@toc@l(%r11)
3755    .    mtctr   %r12
3756    .    bctr
3757
3758    ppc_stub_plt_call:
3759    Used to call a function in a shared library.  If it so happens that
3760    the plt entry referenced crosses a 64k boundary, then an extra
3761    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3762    .    std     %r2,40(%r1)
3763    .    addis   %r11,%r2,xxx@toc@ha
3764    .    ld      %r12,xxx+0@toc@l(%r11)
3765    .    mtctr   %r12
3766    .    ld      %r2,xxx+8@toc@l(%r11)
3767    .    ld      %r11,xxx+16@toc@l(%r11)
3768    .    bctr
3769
3770    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3771    code to adjust the value and save r2 to support multiple toc sections.
3772    A ppc_stub_long_branch with an r2 offset looks like:
3773    .    std     %r2,40(%r1)
3774    .    addis   %r2,%r2,off@ha
3775    .    addi    %r2,%r2,off@l
3776    .    b       dest
3777
3778    A ppc_stub_plt_branch with an r2 offset looks like:
3779    .    std     %r2,40(%r1)
3780    .    addis   %r11,%r2,xxx@toc@ha
3781    .    ld      %r12,xxx@toc@l(%r11)
3782    .    addis   %r2,%r2,off@ha
3783    .    addi    %r2,%r2,off@l
3784    .    mtctr   %r12
3785    .    bctr
3786
3787    In cases where the "addis" instruction would add zero, the "addis" is
3788    omitted and following instructions modified slightly in some cases.
3789 */
3790
3791 enum ppc_stub_type {
3792   ppc_stub_none,
3793   ppc_stub_long_branch,
3794   ppc_stub_long_branch_r2off,
3795   ppc_stub_plt_branch,
3796   ppc_stub_plt_branch_r2off,
3797   ppc_stub_plt_call,
3798   ppc_stub_plt_call_r2save,
3799   ppc_stub_global_entry
3800 };
3801
3802 /* Information on stub grouping.  */
3803 struct map_stub
3804 {
3805   /* The stub section.  */
3806   asection *stub_sec;
3807   /* This is the section to which stubs in the group will be attached.  */
3808   asection *link_sec;
3809 };
3810
3811 struct ppc_stub_hash_entry {
3812
3813   /* Base hash table entry structure.  */
3814   struct bfd_hash_entry root;
3815
3816   enum ppc_stub_type stub_type;
3817
3818   /* Group information.  */
3819   struct map_stub *group;
3820
3821   /* Offset within stub_sec of the beginning of this stub.  */
3822   bfd_vma stub_offset;
3823
3824   /* Given the symbol's value and its section we can determine its final
3825      value when building the stubs (so the stub knows where to jump.  */
3826   bfd_vma target_value;
3827   asection *target_section;
3828
3829   /* The symbol table entry, if any, that this was derived from.  */
3830   struct ppc_link_hash_entry *h;
3831   struct plt_entry *plt_ent;
3832
3833   /* Symbol st_other.  */
3834   unsigned char other;
3835 };
3836
3837 struct ppc_branch_hash_entry {
3838
3839   /* Base hash table entry structure.  */
3840   struct bfd_hash_entry root;
3841
3842   /* Offset within branch lookup table.  */
3843   unsigned int offset;
3844
3845   /* Generation marker.  */
3846   unsigned int iter;
3847 };
3848
3849 /* Used to track dynamic relocations for local symbols.  */
3850 struct ppc_dyn_relocs
3851 {
3852   struct ppc_dyn_relocs *next;
3853
3854   /* The input section of the reloc.  */
3855   asection *sec;
3856
3857   /* Total number of relocs copied for the input section.  */
3858   unsigned int count : 31;
3859
3860   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
3861   unsigned int ifunc : 1;
3862 };
3863
3864 struct ppc_link_hash_entry
3865 {
3866   struct elf_link_hash_entry elf;
3867
3868   union {
3869     /* A pointer to the most recently used stub hash entry against this
3870        symbol.  */
3871     struct ppc_stub_hash_entry *stub_cache;
3872
3873     /* A pointer to the next symbol starting with a '.'  */
3874     struct ppc_link_hash_entry *next_dot_sym;
3875   } u;
3876
3877   /* Track dynamic relocs copied for this symbol.  */
3878   struct elf_dyn_relocs *dyn_relocs;
3879
3880   /* Link between function code and descriptor symbols.  */
3881   struct ppc_link_hash_entry *oh;
3882
3883   /* Flag function code and descriptor symbols.  */
3884   unsigned int is_func:1;
3885   unsigned int is_func_descriptor:1;
3886   unsigned int fake:1;
3887
3888   /* Whether global opd/toc sym has been adjusted or not.
3889      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3890      should be set for all globals defined in any opd/toc section.  */
3891   unsigned int adjust_done:1;
3892
3893   /* Set if we twiddled this symbol to weak at some stage.  */
3894   unsigned int was_undefined:1;
3895
3896   /* Contexts in which symbol is used in the GOT (or TOC).
3897      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3898      corresponding relocs are encountered during check_relocs.
3899      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3900      indicate the corresponding GOT entry type is not needed.
3901      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3902      a TPREL one.  We use a separate flag rather than setting TPREL
3903      just for convenience in distinguishing the two cases.  */
3904 #define TLS_GD           1      /* GD reloc. */
3905 #define TLS_LD           2      /* LD reloc. */
3906 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
3907 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
3908 #define TLS_TLS         16      /* Any TLS reloc.  */
3909 #define TLS_EXPLICIT    32      /* Marks TOC section TLS relocs. */
3910 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
3911 #define PLT_IFUNC      128      /* STT_GNU_IFUNC.  */
3912   unsigned char tls_mask;
3913 };
3914
3915 /* ppc64 ELF linker hash table.  */
3916
3917 struct ppc_link_hash_table
3918 {
3919   struct elf_link_hash_table elf;
3920
3921   /* The stub hash table.  */
3922   struct bfd_hash_table stub_hash_table;
3923
3924   /* Another hash table for plt_branch stubs.  */
3925   struct bfd_hash_table branch_hash_table;
3926
3927   /* Hash table for function prologue tocsave.  */
3928   htab_t tocsave_htab;
3929
3930   /* Various options and other info passed from the linker.  */
3931   struct ppc64_elf_params *params;
3932
3933   /* The size of sec_info below.  */
3934   unsigned int sec_info_arr_size;
3935
3936   /* Per-section array of extra section info.  Done this way rather
3937      than as part of ppc64_elf_section_data so we have the info for
3938      non-ppc64 sections.  */
3939   struct
3940   {
3941     /* Along with elf_gp, specifies the TOC pointer used by this section.  */
3942     bfd_vma toc_off;
3943
3944     union
3945     {
3946       /* The section group that this section belongs to.  */
3947       struct map_stub *group;
3948       /* A temp section list pointer.  */
3949       asection *list;
3950     } u;
3951   } *sec_info;
3952
3953   /* Temp used when calculating TOC pointers.  */
3954   bfd_vma toc_curr;
3955   bfd *toc_bfd;
3956   asection *toc_first_sec;
3957
3958   /* Used when adding symbols.  */
3959   struct ppc_link_hash_entry *dot_syms;
3960
3961   /* Shortcuts to get to dynamic linker sections.  */
3962   asection *dynbss;
3963   asection *relbss;
3964   asection *glink;
3965   asection *sfpr;
3966   asection *brlt;
3967   asection *relbrlt;
3968   asection *glink_eh_frame;
3969
3970   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
3971   struct ppc_link_hash_entry *tls_get_addr;
3972   struct ppc_link_hash_entry *tls_get_addr_fd;
3973
3974   /* The size of reliplt used by got entry relocs.  */
3975   bfd_size_type got_reli_size;
3976
3977   /* Statistics.  */
3978   unsigned long stub_count[ppc_stub_global_entry];
3979
3980   /* Number of stubs against global syms.  */
3981   unsigned long stub_globals;
3982
3983   /* Set if we're linking code with function descriptors.  */
3984   unsigned int opd_abi:1;
3985
3986   /* Support for multiple toc sections.  */
3987   unsigned int do_multi_toc:1;
3988   unsigned int multi_toc_needed:1;
3989   unsigned int second_toc_pass:1;
3990   unsigned int do_toc_opt:1;
3991
3992   /* Set on error.  */
3993   unsigned int stub_error:1;
3994
3995   /* Temp used by ppc64_elf_before_check_relocs.  */
3996   unsigned int twiddled_syms:1;
3997
3998   /* Incremented every time we size stubs.  */
3999   unsigned int stub_iteration;
4000
4001   /* Small local sym cache.  */
4002   struct sym_cache sym_cache;
4003 };
4004
4005 /* Rename some of the generic section flags to better document how they
4006    are used here.  */
4007
4008 /* Nonzero if this section has TLS related relocations.  */
4009 #define has_tls_reloc sec_flg0
4010
4011 /* Nonzero if this section has a call to __tls_get_addr.  */
4012 #define has_tls_get_addr_call sec_flg1
4013
4014 /* Nonzero if this section has any toc or got relocs.  */
4015 #define has_toc_reloc sec_flg2
4016
4017 /* Nonzero if this section has a call to another section that uses
4018    the toc or got.  */
4019 #define makes_toc_func_call sec_flg3
4020
4021 /* Recursion protection when determining above flag.  */
4022 #define call_check_in_progress sec_flg4
4023 #define call_check_done sec_flg5
4024
4025 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
4026
4027 #define ppc_hash_table(p) \
4028   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4029   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4030
4031 #define ppc_stub_hash_lookup(table, string, create, copy) \
4032   ((struct ppc_stub_hash_entry *) \
4033    bfd_hash_lookup ((table), (string), (create), (copy)))
4034
4035 #define ppc_branch_hash_lookup(table, string, create, copy) \
4036   ((struct ppc_branch_hash_entry *) \
4037    bfd_hash_lookup ((table), (string), (create), (copy)))
4038
4039 /* Create an entry in the stub hash table.  */
4040
4041 static struct bfd_hash_entry *
4042 stub_hash_newfunc (struct bfd_hash_entry *entry,
4043                    struct bfd_hash_table *table,
4044                    const char *string)
4045 {
4046   /* Allocate the structure if it has not already been allocated by a
4047      subclass.  */
4048   if (entry == NULL)
4049     {
4050       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4051       if (entry == NULL)
4052         return entry;
4053     }
4054
4055   /* Call the allocation method of the superclass.  */
4056   entry = bfd_hash_newfunc (entry, table, string);
4057   if (entry != NULL)
4058     {
4059       struct ppc_stub_hash_entry *eh;
4060
4061       /* Initialize the local fields.  */
4062       eh = (struct ppc_stub_hash_entry *) entry;
4063       eh->stub_type = ppc_stub_none;
4064       eh->group = NULL;
4065       eh->stub_offset = 0;
4066       eh->target_value = 0;
4067       eh->target_section = NULL;
4068       eh->h = NULL;
4069       eh->plt_ent = NULL;
4070       eh->other = 0;
4071     }
4072
4073   return entry;
4074 }
4075
4076 /* Create an entry in the branch hash table.  */
4077
4078 static struct bfd_hash_entry *
4079 branch_hash_newfunc (struct bfd_hash_entry *entry,
4080                      struct bfd_hash_table *table,
4081                      const char *string)
4082 {
4083   /* Allocate the structure if it has not already been allocated by a
4084      subclass.  */
4085   if (entry == NULL)
4086     {
4087       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4088       if (entry == NULL)
4089         return entry;
4090     }
4091
4092   /* Call the allocation method of the superclass.  */
4093   entry = bfd_hash_newfunc (entry, table, string);
4094   if (entry != NULL)
4095     {
4096       struct ppc_branch_hash_entry *eh;
4097
4098       /* Initialize the local fields.  */
4099       eh = (struct ppc_branch_hash_entry *) entry;
4100       eh->offset = 0;
4101       eh->iter = 0;
4102     }
4103
4104   return entry;
4105 }
4106
4107 /* Create an entry in a ppc64 ELF linker hash table.  */
4108
4109 static struct bfd_hash_entry *
4110 link_hash_newfunc (struct bfd_hash_entry *entry,
4111                    struct bfd_hash_table *table,
4112                    const char *string)
4113 {
4114   /* Allocate the structure if it has not already been allocated by a
4115      subclass.  */
4116   if (entry == NULL)
4117     {
4118       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4119       if (entry == NULL)
4120         return entry;
4121     }
4122
4123   /* Call the allocation method of the superclass.  */
4124   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4125   if (entry != NULL)
4126     {
4127       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4128
4129       memset (&eh->u.stub_cache, 0,
4130               (sizeof (struct ppc_link_hash_entry)
4131                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4132
4133       /* When making function calls, old ABI code references function entry
4134          points (dot symbols), while new ABI code references the function
4135          descriptor symbol.  We need to make any combination of reference and
4136          definition work together, without breaking archive linking.
4137
4138          For a defined function "foo" and an undefined call to "bar":
4139          An old object defines "foo" and ".foo", references ".bar" (possibly
4140          "bar" too).
4141          A new object defines "foo" and references "bar".
4142
4143          A new object thus has no problem with its undefined symbols being
4144          satisfied by definitions in an old object.  On the other hand, the
4145          old object won't have ".bar" satisfied by a new object.
4146
4147          Keep a list of newly added dot-symbols.  */
4148
4149       if (string[0] == '.')
4150         {
4151           struct ppc_link_hash_table *htab;
4152
4153           htab = (struct ppc_link_hash_table *) table;
4154           eh->u.next_dot_sym = htab->dot_syms;
4155           htab->dot_syms = eh;
4156         }
4157     }
4158
4159   return entry;
4160 }
4161
4162 struct tocsave_entry {
4163   asection *sec;
4164   bfd_vma offset;
4165 };
4166
4167 static hashval_t
4168 tocsave_htab_hash (const void *p)
4169 {
4170   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4171   return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4172 }
4173
4174 static int
4175 tocsave_htab_eq (const void *p1, const void *p2)
4176 {
4177   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4178   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4179   return e1->sec == e2->sec && e1->offset == e2->offset;
4180 }
4181
4182 /* Destroy a ppc64 ELF linker hash table.  */
4183
4184 static void
4185 ppc64_elf_link_hash_table_free (bfd *obfd)
4186 {
4187   struct ppc_link_hash_table *htab;
4188
4189   htab = (struct ppc_link_hash_table *) obfd->link.hash;
4190   if (htab->tocsave_htab)
4191     htab_delete (htab->tocsave_htab);
4192   bfd_hash_table_free (&htab->branch_hash_table);
4193   bfd_hash_table_free (&htab->stub_hash_table);
4194   _bfd_elf_link_hash_table_free (obfd);
4195 }
4196
4197 /* Create a ppc64 ELF linker hash table.  */
4198
4199 static struct bfd_link_hash_table *
4200 ppc64_elf_link_hash_table_create (bfd *abfd)
4201 {
4202   struct ppc_link_hash_table *htab;
4203   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4204
4205   htab = bfd_zmalloc (amt);
4206   if (htab == NULL)
4207     return NULL;
4208
4209   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4210                                       sizeof (struct ppc_link_hash_entry),
4211                                       PPC64_ELF_DATA))
4212     {
4213       free (htab);
4214       return NULL;
4215     }
4216
4217   /* Init the stub hash table too.  */
4218   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4219                             sizeof (struct ppc_stub_hash_entry)))
4220     {
4221       _bfd_elf_link_hash_table_free (abfd);
4222       return NULL;
4223     }
4224
4225   /* And the branch hash table.  */
4226   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4227                             sizeof (struct ppc_branch_hash_entry)))
4228     {
4229       bfd_hash_table_free (&htab->stub_hash_table);
4230       _bfd_elf_link_hash_table_free (abfd);
4231       return NULL;
4232     }
4233
4234   htab->tocsave_htab = htab_try_create (1024,
4235                                         tocsave_htab_hash,
4236                                         tocsave_htab_eq,
4237                                         NULL);
4238   if (htab->tocsave_htab == NULL)
4239     {
4240       ppc64_elf_link_hash_table_free (abfd);
4241       return NULL;
4242     }
4243   htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4244
4245   /* Initializing two fields of the union is just cosmetic.  We really
4246      only care about glist, but when compiled on a 32-bit host the
4247      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
4248      debugger inspection of these fields look nicer.  */
4249   htab->elf.init_got_refcount.refcount = 0;
4250   htab->elf.init_got_refcount.glist = NULL;
4251   htab->elf.init_plt_refcount.refcount = 0;
4252   htab->elf.init_plt_refcount.glist = NULL;
4253   htab->elf.init_got_offset.offset = 0;
4254   htab->elf.init_got_offset.glist = NULL;
4255   htab->elf.init_plt_offset.offset = 0;
4256   htab->elf.init_plt_offset.glist = NULL;
4257
4258   return &htab->elf.root;
4259 }
4260
4261 /* Create sections for linker generated code.  */
4262
4263 static bfd_boolean
4264 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4265 {
4266   struct ppc_link_hash_table *htab;
4267   flagword flags;
4268
4269   htab = ppc_hash_table (info);
4270
4271   /* Create .sfpr for code to save and restore fp regs.  */
4272   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4273            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4274   htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4275                                                    flags);
4276   if (htab->sfpr == NULL
4277       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4278     return FALSE;
4279
4280   /* Create .glink for lazy dynamic linking support.  */
4281   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4282                                                     flags);
4283   if (htab->glink == NULL
4284       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4285     return FALSE;
4286
4287   if (!info->no_ld_generated_unwind_info)
4288     {
4289       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4290                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4291       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4292                                                                  ".eh_frame",
4293                                                                  flags);
4294       if (htab->glink_eh_frame == NULL
4295           || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4296         return FALSE;
4297     }
4298
4299   flags = SEC_ALLOC | SEC_LINKER_CREATED;
4300   htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4301   if (htab->elf.iplt == NULL
4302       || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4303     return FALSE;
4304
4305   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4306            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4307   htab->elf.irelplt
4308     = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4309   if (htab->elf.irelplt == NULL
4310       || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4311     return FALSE;
4312
4313   /* Create branch lookup table for plt_branch stubs.  */
4314   flags = (SEC_ALLOC | SEC_LOAD
4315            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4316   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4317                                                    flags);
4318   if (htab->brlt == NULL
4319       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4320     return FALSE;
4321
4322   if (!bfd_link_pic (info))
4323     return TRUE;
4324
4325   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4326            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4327   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4328                                                       ".rela.branch_lt",
4329                                                       flags);
4330   if (htab->relbrlt == NULL
4331       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4332     return FALSE;
4333
4334   return TRUE;
4335 }
4336
4337 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
4338
4339 bfd_boolean
4340 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4341                          struct ppc64_elf_params *params)
4342 {
4343   struct ppc_link_hash_table *htab;
4344
4345   elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4346
4347 /* Always hook our dynamic sections into the first bfd, which is the
4348    linker created stub bfd.  This ensures that the GOT header is at
4349    the start of the output TOC section.  */
4350   htab = ppc_hash_table (info);
4351   if (htab == NULL)
4352     return FALSE;
4353   htab->elf.dynobj = params->stub_bfd;
4354   htab->params = params;
4355
4356   if (bfd_link_relocatable (info))
4357     return TRUE;
4358
4359   return create_linkage_sections (htab->elf.dynobj, info);
4360 }
4361
4362 /* Build a name for an entry in the stub hash table.  */
4363
4364 static char *
4365 ppc_stub_name (const asection *input_section,
4366                const asection *sym_sec,
4367                const struct ppc_link_hash_entry *h,
4368                const Elf_Internal_Rela *rel)
4369 {
4370   char *stub_name;
4371   ssize_t len;
4372
4373   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4374      offsets from a sym as a branch target?  In fact, we could
4375      probably assume the addend is always zero.  */
4376   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4377
4378   if (h)
4379     {
4380       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4381       stub_name = bfd_malloc (len);
4382       if (stub_name == NULL)
4383         return stub_name;
4384
4385       len = sprintf (stub_name, "%08x.%s+%x",
4386                      input_section->id & 0xffffffff,
4387                      h->elf.root.root.string,
4388                      (int) rel->r_addend & 0xffffffff);
4389     }
4390   else
4391     {
4392       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4393       stub_name = bfd_malloc (len);
4394       if (stub_name == NULL)
4395         return stub_name;
4396
4397       len = sprintf (stub_name, "%08x.%x:%x+%x",
4398                      input_section->id & 0xffffffff,
4399                      sym_sec->id & 0xffffffff,
4400                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4401                      (int) rel->r_addend & 0xffffffff);
4402     }
4403   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4404     stub_name[len - 2] = 0;
4405   return stub_name;
4406 }
4407
4408 /* Look up an entry in the stub hash.  Stub entries are cached because
4409    creating the stub name takes a bit of time.  */
4410
4411 static struct ppc_stub_hash_entry *
4412 ppc_get_stub_entry (const asection *input_section,
4413                     const asection *sym_sec,
4414                     struct ppc_link_hash_entry *h,
4415                     const Elf_Internal_Rela *rel,
4416                     struct ppc_link_hash_table *htab)
4417 {
4418   struct ppc_stub_hash_entry *stub_entry;
4419   struct map_stub *group;
4420
4421   /* If this input section is part of a group of sections sharing one
4422      stub section, then use the id of the first section in the group.
4423      Stub names need to include a section id, as there may well be
4424      more than one stub used to reach say, printf, and we need to
4425      distinguish between them.  */
4426   group = htab->sec_info[input_section->id].u.group;
4427
4428   if (h != NULL && h->u.stub_cache != NULL
4429       && h->u.stub_cache->h == h
4430       && h->u.stub_cache->group == group)
4431     {
4432       stub_entry = h->u.stub_cache;
4433     }
4434   else
4435     {
4436       char *stub_name;
4437
4438       stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4439       if (stub_name == NULL)
4440         return NULL;
4441
4442       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4443                                          stub_name, FALSE, FALSE);
4444       if (h != NULL)
4445         h->u.stub_cache = stub_entry;
4446
4447       free (stub_name);
4448     }
4449
4450   return stub_entry;
4451 }
4452
4453 /* Add a new stub entry to the stub hash.  Not all fields of the new
4454    stub entry are initialised.  */
4455
4456 static struct ppc_stub_hash_entry *
4457 ppc_add_stub (const char *stub_name,
4458               asection *section,
4459               struct bfd_link_info *info)
4460 {
4461   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4462   struct map_stub *group;
4463   asection *link_sec;
4464   asection *stub_sec;
4465   struct ppc_stub_hash_entry *stub_entry;
4466
4467   group = htab->sec_info[section->id].u.group;
4468   link_sec = group->link_sec;
4469   stub_sec = group->stub_sec;
4470   if (stub_sec == NULL)
4471     {
4472       size_t namelen;
4473       bfd_size_type len;
4474       char *s_name;
4475
4476       namelen = strlen (link_sec->name);
4477       len = namelen + sizeof (STUB_SUFFIX);
4478       s_name = bfd_alloc (htab->params->stub_bfd, len);
4479       if (s_name == NULL)
4480         return NULL;
4481
4482       memcpy (s_name, link_sec->name, namelen);
4483       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4484       stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4485       if (stub_sec == NULL)
4486         return NULL;
4487       group->stub_sec = stub_sec;
4488     }
4489
4490   /* Enter this entry into the linker stub hash table.  */
4491   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4492                                      TRUE, FALSE);
4493   if (stub_entry == NULL)
4494     {
4495       info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4496                               section->owner, stub_name);
4497       return NULL;
4498     }
4499
4500   stub_entry->group = group;
4501   stub_entry->stub_offset = 0;
4502   return stub_entry;
4503 }
4504
4505 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4506    not already done.  */
4507
4508 static bfd_boolean
4509 create_got_section (bfd *abfd, struct bfd_link_info *info)
4510 {
4511   asection *got, *relgot;
4512   flagword flags;
4513   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4514
4515   if (!is_ppc64_elf (abfd))
4516     return FALSE;
4517   if (htab == NULL)
4518     return FALSE;
4519
4520   if (!htab->elf.sgot
4521       && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4522     return FALSE;
4523
4524   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4525            | SEC_LINKER_CREATED);
4526
4527   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4528   if (!got
4529       || !bfd_set_section_alignment (abfd, got, 3))
4530     return FALSE;
4531
4532   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4533                                                flags | SEC_READONLY);
4534   if (!relgot
4535       || ! bfd_set_section_alignment (abfd, relgot, 3))
4536     return FALSE;
4537
4538   ppc64_elf_tdata (abfd)->got = got;
4539   ppc64_elf_tdata (abfd)->relgot = relgot;
4540   return TRUE;
4541 }
4542
4543 /* Create the dynamic sections, and set up shortcuts.  */
4544
4545 static bfd_boolean
4546 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4547 {
4548   struct ppc_link_hash_table *htab;
4549
4550   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4551     return FALSE;
4552
4553   htab = ppc_hash_table (info);
4554   if (htab == NULL)
4555     return FALSE;
4556
4557   htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
4558   if (!bfd_link_pic (info))
4559     htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
4560
4561   if (!htab->elf.sgot || !htab->elf.splt || !htab->elf.srelplt || !htab->dynbss
4562       || (!bfd_link_pic (info) && !htab->relbss))
4563     abort ();
4564
4565   return TRUE;
4566 }
4567
4568 /* Follow indirect and warning symbol links.  */
4569
4570 static inline struct bfd_link_hash_entry *
4571 follow_link (struct bfd_link_hash_entry *h)
4572 {
4573   while (h->type == bfd_link_hash_indirect
4574          || h->type == bfd_link_hash_warning)
4575     h = h->u.i.link;
4576   return h;
4577 }
4578
4579 static inline struct elf_link_hash_entry *
4580 elf_follow_link (struct elf_link_hash_entry *h)
4581 {
4582   return (struct elf_link_hash_entry *) follow_link (&h->root);
4583 }
4584
4585 static inline struct ppc_link_hash_entry *
4586 ppc_follow_link (struct ppc_link_hash_entry *h)
4587 {
4588   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4589 }
4590
4591 /* Merge PLT info on FROM with that on TO.  */
4592
4593 static void
4594 move_plt_plist (struct ppc_link_hash_entry *from,
4595                 struct ppc_link_hash_entry *to)
4596 {
4597   if (from->elf.plt.plist != NULL)
4598     {
4599       if (to->elf.plt.plist != NULL)
4600         {
4601           struct plt_entry **entp;
4602           struct plt_entry *ent;
4603
4604           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4605             {
4606               struct plt_entry *dent;
4607
4608               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4609                 if (dent->addend == ent->addend)
4610                   {
4611                     dent->plt.refcount += ent->plt.refcount;
4612                     *entp = ent->next;
4613                     break;
4614                   }
4615               if (dent == NULL)
4616                 entp = &ent->next;
4617             }
4618           *entp = to->elf.plt.plist;
4619         }
4620
4621       to->elf.plt.plist = from->elf.plt.plist;
4622       from->elf.plt.plist = NULL;
4623     }
4624 }
4625
4626 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
4627
4628 static void
4629 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4630                                 struct elf_link_hash_entry *dir,
4631                                 struct elf_link_hash_entry *ind)
4632 {
4633   struct ppc_link_hash_entry *edir, *eind;
4634
4635   edir = (struct ppc_link_hash_entry *) dir;
4636   eind = (struct ppc_link_hash_entry *) ind;
4637
4638   edir->is_func |= eind->is_func;
4639   edir->is_func_descriptor |= eind->is_func_descriptor;
4640   edir->tls_mask |= eind->tls_mask;
4641   if (eind->oh != NULL)
4642     edir->oh = ppc_follow_link (eind->oh);
4643
4644   /* If called to transfer flags for a weakdef during processing
4645      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4646      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
4647   if (!(ELIMINATE_COPY_RELOCS
4648         && eind->elf.root.type != bfd_link_hash_indirect
4649         && edir->elf.dynamic_adjusted))
4650     edir->elf.non_got_ref |= eind->elf.non_got_ref;
4651
4652   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4653   edir->elf.ref_regular |= eind->elf.ref_regular;
4654   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4655   edir->elf.needs_plt |= eind->elf.needs_plt;
4656   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4657
4658   /* Copy over any dynamic relocs we may have on the indirect sym.  */
4659   if (eind->dyn_relocs != NULL)
4660     {
4661       if (edir->dyn_relocs != NULL)
4662         {
4663           struct elf_dyn_relocs **pp;
4664           struct elf_dyn_relocs *p;
4665
4666           /* Add reloc counts against the indirect sym to the direct sym
4667              list.  Merge any entries against the same section.  */
4668           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4669             {
4670               struct elf_dyn_relocs *q;
4671
4672               for (q = edir->dyn_relocs; q != NULL; q = q->next)
4673                 if (q->sec == p->sec)
4674                   {
4675                     q->pc_count += p->pc_count;
4676                     q->count += p->count;
4677                     *pp = p->next;
4678                     break;
4679                   }
4680               if (q == NULL)
4681                 pp = &p->next;
4682             }
4683           *pp = edir->dyn_relocs;
4684         }
4685
4686       edir->dyn_relocs = eind->dyn_relocs;
4687       eind->dyn_relocs = NULL;
4688     }
4689
4690   /* If we were called to copy over info for a weak sym, that's all.
4691      You might think dyn_relocs need not be copied over;  After all,
4692      both syms will be dynamic or both non-dynamic so we're just
4693      moving reloc accounting around.  However, ELIMINATE_COPY_RELOCS
4694      code in ppc64_elf_adjust_dynamic_symbol needs to check for
4695      dyn_relocs in read-only sections, and it does so on what is the
4696      DIR sym here.  */
4697   if (eind->elf.root.type != bfd_link_hash_indirect)
4698     return;
4699
4700   /* Copy over got entries that we may have already seen to the
4701      symbol which just became indirect.  */
4702   if (eind->elf.got.glist != NULL)
4703     {
4704       if (edir->elf.got.glist != NULL)
4705         {
4706           struct got_entry **entp;
4707           struct got_entry *ent;
4708
4709           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4710             {
4711               struct got_entry *dent;
4712
4713               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4714                 if (dent->addend == ent->addend
4715                     && dent->owner == ent->owner
4716                     && dent->tls_type == ent->tls_type)
4717                   {
4718                     dent->got.refcount += ent->got.refcount;
4719                     *entp = ent->next;
4720                     break;
4721                   }
4722               if (dent == NULL)
4723                 entp = &ent->next;
4724             }
4725           *entp = edir->elf.got.glist;
4726         }
4727
4728       edir->elf.got.glist = eind->elf.got.glist;
4729       eind->elf.got.glist = NULL;
4730     }
4731
4732   /* And plt entries.  */
4733   move_plt_plist (eind, edir);
4734
4735   if (eind->elf.dynindx != -1)
4736     {
4737       if (edir->elf.dynindx != -1)
4738         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4739                                 edir->elf.dynstr_index);
4740       edir->elf.dynindx = eind->elf.dynindx;
4741       edir->elf.dynstr_index = eind->elf.dynstr_index;
4742       eind->elf.dynindx = -1;
4743       eind->elf.dynstr_index = 0;
4744     }
4745 }
4746
4747 /* Find the function descriptor hash entry from the given function code
4748    hash entry FH.  Link the entries via their OH fields.  */
4749
4750 static struct ppc_link_hash_entry *
4751 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4752 {
4753   struct ppc_link_hash_entry *fdh = fh->oh;
4754
4755   if (fdh == NULL)
4756     {
4757       const char *fd_name = fh->elf.root.root.string + 1;
4758
4759       fdh = (struct ppc_link_hash_entry *)
4760         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4761       if (fdh == NULL)
4762         return fdh;
4763
4764       fdh->is_func_descriptor = 1;
4765       fdh->oh = fh;
4766       fh->is_func = 1;
4767       fh->oh = fdh;
4768     }
4769
4770   return ppc_follow_link (fdh);
4771 }
4772
4773 /* Make a fake function descriptor sym for the code sym FH.  */
4774
4775 static struct ppc_link_hash_entry *
4776 make_fdh (struct bfd_link_info *info,
4777           struct ppc_link_hash_entry *fh)
4778 {
4779   bfd *abfd;
4780   asymbol *newsym;
4781   struct bfd_link_hash_entry *bh;
4782   struct ppc_link_hash_entry *fdh;
4783
4784   abfd = fh->elf.root.u.undef.abfd;
4785   newsym = bfd_make_empty_symbol (abfd);
4786   newsym->name = fh->elf.root.root.string + 1;
4787   newsym->section = bfd_und_section_ptr;
4788   newsym->value = 0;
4789   newsym->flags = BSF_WEAK;
4790
4791   bh = NULL;
4792   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4793                                          newsym->flags, newsym->section,
4794                                          newsym->value, NULL, FALSE, FALSE,
4795                                          &bh))
4796     return NULL;
4797
4798   fdh = (struct ppc_link_hash_entry *) bh;
4799   fdh->elf.non_elf = 0;
4800   fdh->fake = 1;
4801   fdh->is_func_descriptor = 1;
4802   fdh->oh = fh;
4803   fh->is_func = 1;
4804   fh->oh = fdh;
4805   return fdh;
4806 }
4807
4808 /* Fix function descriptor symbols defined in .opd sections to be
4809    function type.  */
4810
4811 static bfd_boolean
4812 ppc64_elf_add_symbol_hook (bfd *ibfd,
4813                            struct bfd_link_info *info,
4814                            Elf_Internal_Sym *isym,
4815                            const char **name,
4816                            flagword *flags ATTRIBUTE_UNUSED,
4817                            asection **sec,
4818                            bfd_vma *value)
4819 {
4820   if ((ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4821        || ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4822       && (ibfd->flags & DYNAMIC) == 0
4823       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4824     elf_tdata (info->output_bfd)->has_gnu_symbols = elf_gnu_symbol_any;
4825
4826   if (*sec != NULL
4827       && strcmp ((*sec)->name, ".opd") == 0)
4828     {
4829       asection *code_sec;
4830
4831       if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4832             || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4833         isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4834
4835       /* If the symbol is a function defined in .opd, and the function
4836          code is in a discarded group, let it appear to be undefined.  */
4837       if (!bfd_link_relocatable (info)
4838           && (*sec)->reloc_count != 0
4839           && opd_entry_value (*sec, *value, &code_sec, NULL,
4840                               FALSE) != (bfd_vma) -1
4841           && discarded_section (code_sec))
4842         {
4843           *sec = bfd_und_section_ptr;
4844           isym->st_shndx = SHN_UNDEF;
4845         }
4846     }
4847   else if (*sec != NULL
4848            && strcmp ((*sec)->name, ".toc") == 0
4849            && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4850     {
4851       struct ppc_link_hash_table *htab = ppc_hash_table (info);
4852       if (htab != NULL)
4853         htab->params->object_in_toc = 1;
4854     }
4855
4856   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4857     {
4858       if (abiversion (ibfd) == 0)
4859         set_abiversion (ibfd, 2);
4860       else if (abiversion (ibfd) == 1)
4861         {
4862           info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4863                                     " for ABI version 1\n"), name);
4864           bfd_set_error (bfd_error_bad_value);
4865           return FALSE;
4866         }
4867     }
4868
4869   return TRUE;
4870 }
4871
4872 /* Merge non-visibility st_other attributes: local entry point.  */
4873
4874 static void
4875 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4876                                   const Elf_Internal_Sym *isym,
4877                                   bfd_boolean definition,
4878                                   bfd_boolean dynamic)
4879 {
4880   if (definition && !dynamic)
4881     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4882                 | ELF_ST_VISIBILITY (h->other));
4883 }
4884
4885 /* This function makes an old ABI object reference to ".bar" cause the
4886    inclusion of a new ABI object archive that defines "bar".
4887    NAME is a symbol defined in an archive.  Return a symbol in the hash
4888    table that might be satisfied by the archive symbols.  */
4889
4890 static struct elf_link_hash_entry *
4891 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4892                                  struct bfd_link_info *info,
4893                                  const char *name)
4894 {
4895   struct elf_link_hash_entry *h;
4896   char *dot_name;
4897   size_t len;
4898
4899   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4900   if (h != NULL
4901       /* Don't return this sym if it is a fake function descriptor
4902          created by add_symbol_adjust.  */
4903       && !(h->root.type == bfd_link_hash_undefweak
4904            && ((struct ppc_link_hash_entry *) h)->fake))
4905     return h;
4906
4907   if (name[0] == '.')
4908     return h;
4909
4910   len = strlen (name);
4911   dot_name = bfd_alloc (abfd, len + 2);
4912   if (dot_name == NULL)
4913     return (struct elf_link_hash_entry *) 0 - 1;
4914   dot_name[0] = '.';
4915   memcpy (dot_name + 1, name, len + 1);
4916   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4917   bfd_release (abfd, dot_name);
4918   return h;
4919 }
4920
4921 /* This function satisfies all old ABI object references to ".bar" if a
4922    new ABI object defines "bar".  Well, at least, undefined dot symbols
4923    are made weak.  This stops later archive searches from including an
4924    object if we already have a function descriptor definition.  It also
4925    prevents the linker complaining about undefined symbols.
4926    We also check and correct mismatched symbol visibility here.  The
4927    most restrictive visibility of the function descriptor and the
4928    function entry symbol is used.  */
4929
4930 static bfd_boolean
4931 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4932 {
4933   struct ppc_link_hash_table *htab;
4934   struct ppc_link_hash_entry *fdh;
4935
4936   if (eh->elf.root.type == bfd_link_hash_indirect)
4937     return TRUE;
4938
4939   if (eh->elf.root.type == bfd_link_hash_warning)
4940     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4941
4942   if (eh->elf.root.root.string[0] != '.')
4943     abort ();
4944
4945   htab = ppc_hash_table (info);
4946   if (htab == NULL)
4947     return FALSE;
4948
4949   fdh = lookup_fdh (eh, htab);
4950   if (fdh == NULL)
4951     {
4952       if (!bfd_link_relocatable (info)
4953           && (eh->elf.root.type == bfd_link_hash_undefined
4954               || eh->elf.root.type == bfd_link_hash_undefweak)
4955           && eh->elf.ref_regular)
4956         {
4957           /* Make an undefweak function descriptor sym, which is enough to
4958              pull in an --as-needed shared lib, but won't cause link
4959              errors.  Archives are handled elsewhere.  */
4960           fdh = make_fdh (info, eh);
4961           if (fdh == NULL)
4962             return FALSE;
4963           fdh->elf.ref_regular = 1;
4964         }
4965     }
4966   else
4967     {
4968       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4969       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4970       if (entry_vis < descr_vis)
4971         fdh->elf.other += entry_vis - descr_vis;
4972       else if (entry_vis > descr_vis)
4973         eh->elf.other += descr_vis - entry_vis;
4974
4975       if ((fdh->elf.root.type == bfd_link_hash_defined
4976            || fdh->elf.root.type == bfd_link_hash_defweak)
4977           && eh->elf.root.type == bfd_link_hash_undefined)
4978         {
4979           eh->elf.root.type = bfd_link_hash_undefweak;
4980           eh->was_undefined = 1;
4981           htab->twiddled_syms = 1;
4982         }
4983     }
4984
4985   return TRUE;
4986 }
4987
4988 /* Set up opd section info and abiversion for IBFD, and process list
4989    of dot-symbols we made in link_hash_newfunc.  */
4990
4991 static bfd_boolean
4992 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4993 {
4994   struct ppc_link_hash_table *htab;
4995   struct ppc_link_hash_entry **p, *eh;
4996   asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4997
4998   if (opd != NULL && opd->size != 0)
4999     {
5000       if (abiversion (ibfd) == 0)
5001         set_abiversion (ibfd, 1);
5002       else if (abiversion (ibfd) == 2)
5003         {
5004           info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5005                                     " version %d\n"),
5006                                   ibfd, abiversion (ibfd));
5007           bfd_set_error (bfd_error_bad_value);
5008           return FALSE;
5009         }
5010
5011       if ((ibfd->flags & DYNAMIC) == 0
5012           && (opd->flags & SEC_RELOC) != 0
5013           && opd->reloc_count != 0
5014           && !bfd_is_abs_section (opd->output_section))
5015         {
5016           /* Garbage collection needs some extra help with .opd sections.
5017              We don't want to necessarily keep everything referenced by
5018              relocs in .opd, as that would keep all functions.  Instead,
5019              if we reference an .opd symbol (a function descriptor), we
5020              want to keep the function code symbol's section.  This is
5021              easy for global symbols, but for local syms we need to keep
5022              information about the associated function section.  */
5023           bfd_size_type amt;
5024           asection **opd_sym_map;
5025
5026           amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5027           opd_sym_map = bfd_zalloc (ibfd, amt);
5028           if (opd_sym_map == NULL)
5029             return FALSE;
5030           ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5031           BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5032           ppc64_elf_section_data (opd)->sec_type = sec_opd;
5033         }
5034     }
5035
5036   if (!is_ppc64_elf (info->output_bfd))
5037     return TRUE;
5038   htab = ppc_hash_table (info);
5039   if (htab == NULL)
5040     return FALSE;
5041
5042   /* For input files without an explicit abiversion in e_flags
5043      we should have flagged any with symbol st_other bits set
5044      as ELFv1 and above flagged those with .opd as ELFv2.
5045      Set the output abiversion if not yet set, and for any input
5046      still ambiguous, take its abiversion from the output.
5047      Differences in ABI are reported later.  */
5048   if (abiversion (info->output_bfd) == 0)
5049     set_abiversion (info->output_bfd, abiversion (ibfd));
5050   else if (abiversion (ibfd) == 0)
5051     set_abiversion (ibfd, abiversion (info->output_bfd));
5052
5053   p = &htab->dot_syms;
5054   while ((eh = *p) != NULL)
5055     {
5056       *p = NULL;
5057       if (&eh->elf == htab->elf.hgot)
5058         ;
5059       else if (htab->elf.hgot == NULL
5060                && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5061         htab->elf.hgot = &eh->elf;
5062       else if (!add_symbol_adjust (eh, info))
5063         return FALSE;
5064       p = &eh->u.next_dot_sym;
5065     }
5066
5067   /* Clear the list for non-ppc64 input files.  */
5068   p = &htab->dot_syms;
5069   while ((eh = *p) != NULL)
5070     {
5071       *p = NULL;
5072       p = &eh->u.next_dot_sym;
5073     }
5074
5075   /* We need to fix the undefs list for any syms we have twiddled to
5076      undef_weak.  */
5077   if (htab->twiddled_syms)
5078     {
5079       bfd_link_repair_undef_list (&htab->elf.root);
5080       htab->twiddled_syms = 0;
5081     }
5082   return TRUE;
5083 }
5084
5085 /* Undo hash table changes when an --as-needed input file is determined
5086    not to be needed.  */
5087
5088 static bfd_boolean
5089 ppc64_elf_notice_as_needed (bfd *ibfd,
5090                             struct bfd_link_info *info,
5091                             enum notice_asneeded_action act)
5092 {
5093   if (act == notice_not_needed)
5094     {
5095       struct ppc_link_hash_table *htab = ppc_hash_table (info);
5096
5097       if (htab == NULL)
5098         return FALSE;
5099
5100       htab->dot_syms = NULL;
5101     }
5102   return _bfd_elf_notice_as_needed (ibfd, info, act);
5103 }
5104
5105 /* If --just-symbols against a final linked binary, then assume we need
5106    toc adjusting stubs when calling functions defined there.  */
5107
5108 static void
5109 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5110 {
5111   if ((sec->flags & SEC_CODE) != 0
5112       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5113       && is_ppc64_elf (sec->owner))
5114     {
5115       if (abiversion (sec->owner) >= 2
5116           || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5117         sec->has_toc_reloc = 1;
5118     }
5119   _bfd_elf_link_just_syms (sec, info);
5120 }
5121
5122 static struct plt_entry **
5123 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5124                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5125 {
5126   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5127   struct plt_entry **local_plt;
5128   unsigned char *local_got_tls_masks;
5129
5130   if (local_got_ents == NULL)
5131     {
5132       bfd_size_type size = symtab_hdr->sh_info;
5133
5134       size *= (sizeof (*local_got_ents)
5135                + sizeof (*local_plt)
5136                + sizeof (*local_got_tls_masks));
5137       local_got_ents = bfd_zalloc (abfd, size);
5138       if (local_got_ents == NULL)
5139         return NULL;
5140       elf_local_got_ents (abfd) = local_got_ents;
5141     }
5142
5143   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5144     {
5145       struct got_entry *ent;
5146
5147       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5148         if (ent->addend == r_addend
5149             && ent->owner == abfd
5150             && ent->tls_type == tls_type)
5151           break;
5152       if (ent == NULL)
5153         {
5154           bfd_size_type amt = sizeof (*ent);
5155           ent = bfd_alloc (abfd, amt);
5156           if (ent == NULL)
5157             return FALSE;
5158           ent->next = local_got_ents[r_symndx];
5159           ent->addend = r_addend;
5160           ent->owner = abfd;
5161           ent->tls_type = tls_type;
5162           ent->is_indirect = FALSE;
5163           ent->got.refcount = 0;
5164           local_got_ents[r_symndx] = ent;
5165         }
5166       ent->got.refcount += 1;
5167     }
5168
5169   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5170   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5171   local_got_tls_masks[r_symndx] |= tls_type;
5172
5173   return local_plt + r_symndx;
5174 }
5175
5176 static bfd_boolean
5177 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5178 {
5179   struct plt_entry *ent;
5180
5181   for (ent = *plist; ent != NULL; ent = ent->next)
5182     if (ent->addend == addend)
5183       break;
5184   if (ent == NULL)
5185     {
5186       bfd_size_type amt = sizeof (*ent);
5187       ent = bfd_alloc (abfd, amt);
5188       if (ent == NULL)
5189         return FALSE;
5190       ent->next = *plist;
5191       ent->addend = addend;
5192       ent->plt.refcount = 0;
5193       *plist = ent;
5194     }
5195   ent->plt.refcount += 1;
5196   return TRUE;
5197 }
5198
5199 static bfd_boolean
5200 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5201 {
5202   return (r_type == R_PPC64_REL24
5203           || r_type == R_PPC64_REL14
5204           || r_type == R_PPC64_REL14_BRTAKEN
5205           || r_type == R_PPC64_REL14_BRNTAKEN
5206           || r_type == R_PPC64_ADDR24
5207           || r_type == R_PPC64_ADDR14
5208           || r_type == R_PPC64_ADDR14_BRTAKEN
5209           || r_type == R_PPC64_ADDR14_BRNTAKEN);
5210 }
5211
5212 /* Look through the relocs for a section during the first phase, and
5213    calculate needed space in the global offset table, procedure
5214    linkage table, and dynamic reloc sections.  */
5215
5216 static bfd_boolean
5217 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5218                         asection *sec, const Elf_Internal_Rela *relocs)
5219 {
5220   struct ppc_link_hash_table *htab;
5221   Elf_Internal_Shdr *symtab_hdr;
5222   struct elf_link_hash_entry **sym_hashes;
5223   const Elf_Internal_Rela *rel;
5224   const Elf_Internal_Rela *rel_end;
5225   asection *sreloc;
5226   asection **opd_sym_map;
5227   struct elf_link_hash_entry *tga, *dottga;
5228
5229   if (bfd_link_relocatable (info))
5230     return TRUE;
5231
5232   /* Don't do anything special with non-loaded, non-alloced sections.
5233      In particular, any relocs in such sections should not affect GOT
5234      and PLT reference counting (ie. we don't allow them to create GOT
5235      or PLT entries), there's no possibility or desire to optimize TLS
5236      relocs, and there's not much point in propagating relocs to shared
5237      libs that the dynamic linker won't relocate.  */
5238   if ((sec->flags & SEC_ALLOC) == 0)
5239     return TRUE;
5240
5241   BFD_ASSERT (is_ppc64_elf (abfd));
5242
5243   htab = ppc_hash_table (info);
5244   if (htab == NULL)
5245     return FALSE;
5246
5247   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5248                               FALSE, FALSE, TRUE);
5249   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5250                                  FALSE, FALSE, TRUE);
5251   symtab_hdr = &elf_symtab_hdr (abfd);
5252   sym_hashes = elf_sym_hashes (abfd);
5253   sreloc = NULL;
5254   opd_sym_map = NULL;
5255   if (ppc64_elf_section_data (sec) != NULL
5256       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5257     opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5258
5259   rel_end = relocs + sec->reloc_count;
5260   for (rel = relocs; rel < rel_end; rel++)
5261     {
5262       unsigned long r_symndx;
5263       struct elf_link_hash_entry *h;
5264       enum elf_ppc64_reloc_type r_type;
5265       int tls_type;
5266       struct _ppc64_elf_section_data *ppc64_sec;
5267       struct plt_entry **ifunc;
5268
5269       r_symndx = ELF64_R_SYM (rel->r_info);
5270       if (r_symndx < symtab_hdr->sh_info)
5271         h = NULL;
5272       else
5273         {
5274           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5275           h = elf_follow_link (h);
5276
5277           /* PR15323, ref flags aren't set for references in the same
5278              object.  */
5279           h->root.non_ir_ref = 1;
5280
5281           if (h == htab->elf.hgot)
5282             sec->has_toc_reloc = 1;
5283         }
5284
5285       tls_type = 0;
5286       ifunc = NULL;
5287       if (h != NULL)
5288         {
5289           if (h->type == STT_GNU_IFUNC)
5290             {
5291               h->needs_plt = 1;
5292               ifunc = &h->plt.plist;
5293             }
5294         }
5295       else
5296         {
5297           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5298                                                           abfd, r_symndx);
5299           if (isym == NULL)
5300             return FALSE;
5301
5302           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5303             {
5304               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5305                                              rel->r_addend, PLT_IFUNC);
5306               if (ifunc == NULL)
5307                 return FALSE;
5308             }
5309         }
5310       r_type = ELF64_R_TYPE (rel->r_info);
5311       if (is_branch_reloc (r_type))
5312         {
5313           if (h != NULL && (h == tga || h == dottga))
5314             {
5315               if (rel != relocs
5316                   && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5317                       || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5318                 /* We have a new-style __tls_get_addr call with a marker
5319                    reloc.  */
5320                 ;
5321               else
5322                 /* Mark this section as having an old-style call.  */
5323                 sec->has_tls_get_addr_call = 1;
5324             }
5325
5326           /* STT_GNU_IFUNC symbols must have a PLT entry.  */
5327           if (ifunc != NULL
5328               && !update_plt_info (abfd, ifunc, rel->r_addend))
5329             return FALSE;
5330         }
5331
5332       switch (r_type)
5333         {
5334         case R_PPC64_TLSGD:
5335         case R_PPC64_TLSLD:
5336           /* These special tls relocs tie a call to __tls_get_addr with
5337              its parameter symbol.  */
5338           break;
5339
5340         case R_PPC64_GOT_TLSLD16:
5341         case R_PPC64_GOT_TLSLD16_LO:
5342         case R_PPC64_GOT_TLSLD16_HI:
5343         case R_PPC64_GOT_TLSLD16_HA:
5344           tls_type = TLS_TLS | TLS_LD;
5345           goto dogottls;
5346
5347         case R_PPC64_GOT_TLSGD16:
5348         case R_PPC64_GOT_TLSGD16_LO:
5349         case R_PPC64_GOT_TLSGD16_HI:
5350         case R_PPC64_GOT_TLSGD16_HA:
5351           tls_type = TLS_TLS | TLS_GD;
5352           goto dogottls;
5353
5354         case R_PPC64_GOT_TPREL16_DS:
5355         case R_PPC64_GOT_TPREL16_LO_DS:
5356         case R_PPC64_GOT_TPREL16_HI:
5357         case R_PPC64_GOT_TPREL16_HA:
5358           if (bfd_link_pic (info))
5359             info->flags |= DF_STATIC_TLS;
5360           tls_type = TLS_TLS | TLS_TPREL;
5361           goto dogottls;
5362
5363         case R_PPC64_GOT_DTPREL16_DS:
5364         case R_PPC64_GOT_DTPREL16_LO_DS:
5365         case R_PPC64_GOT_DTPREL16_HI:
5366         case R_PPC64_GOT_DTPREL16_HA:
5367           tls_type = TLS_TLS | TLS_DTPREL;
5368         dogottls:
5369           sec->has_tls_reloc = 1;
5370           /* Fall thru */
5371
5372         case R_PPC64_GOT16:
5373         case R_PPC64_GOT16_DS:
5374         case R_PPC64_GOT16_HA:
5375         case R_PPC64_GOT16_HI:
5376         case R_PPC64_GOT16_LO:
5377         case R_PPC64_GOT16_LO_DS:
5378           /* This symbol requires a global offset table entry.  */
5379           sec->has_toc_reloc = 1;
5380           if (r_type == R_PPC64_GOT_TLSLD16
5381               || r_type == R_PPC64_GOT_TLSGD16
5382               || r_type == R_PPC64_GOT_TPREL16_DS
5383               || r_type == R_PPC64_GOT_DTPREL16_DS
5384               || r_type == R_PPC64_GOT16
5385               || r_type == R_PPC64_GOT16_DS)
5386             {
5387               htab->do_multi_toc = 1;
5388               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5389             }
5390
5391           if (ppc64_elf_tdata (abfd)->got == NULL
5392               && !create_got_section (abfd, info))
5393             return FALSE;
5394
5395           if (h != NULL)
5396             {
5397               struct ppc_link_hash_entry *eh;
5398               struct got_entry *ent;
5399
5400               eh = (struct ppc_link_hash_entry *) h;
5401               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5402                 if (ent->addend == rel->r_addend
5403                     && ent->owner == abfd
5404                     && ent->tls_type == tls_type)
5405                   break;
5406               if (ent == NULL)
5407                 {
5408                   bfd_size_type amt = sizeof (*ent);
5409                   ent = bfd_alloc (abfd, amt);
5410                   if (ent == NULL)
5411                     return FALSE;
5412                   ent->next = eh->elf.got.glist;
5413                   ent->addend = rel->r_addend;
5414                   ent->owner = abfd;
5415                   ent->tls_type = tls_type;
5416                   ent->is_indirect = FALSE;
5417                   ent->got.refcount = 0;
5418                   eh->elf.got.glist = ent;
5419                 }
5420               ent->got.refcount += 1;
5421               eh->tls_mask |= tls_type;
5422             }
5423           else
5424             /* This is a global offset table entry for a local symbol.  */
5425             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5426                                         rel->r_addend, tls_type))
5427               return FALSE;
5428
5429           /* We may also need a plt entry if the symbol turns out to be
5430              an ifunc.  */
5431           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5432             {
5433               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5434                 return FALSE;
5435             }
5436           break;
5437
5438         case R_PPC64_PLT16_HA:
5439         case R_PPC64_PLT16_HI:
5440         case R_PPC64_PLT16_LO:
5441         case R_PPC64_PLT32:
5442         case R_PPC64_PLT64:
5443           /* This symbol requires a procedure linkage table entry.  We
5444              actually build the entry in adjust_dynamic_symbol,
5445              because this might be a case of linking PIC code without
5446              linking in any dynamic objects, in which case we don't
5447              need to generate a procedure linkage table after all.  */
5448           if (h == NULL)
5449             {
5450               /* It does not make sense to have a procedure linkage
5451                  table entry for a local symbol.  */
5452               bfd_set_error (bfd_error_bad_value);
5453               return FALSE;
5454             }
5455           else
5456             {
5457               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5458                 return FALSE;
5459               h->needs_plt = 1;
5460               if (h->root.root.string[0] == '.'
5461                   && h->root.root.string[1] != '\0')
5462                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5463             }
5464           break;
5465
5466           /* The following relocations don't need to propagate the
5467              relocation if linking a shared object since they are
5468              section relative.  */
5469         case R_PPC64_SECTOFF:
5470         case R_PPC64_SECTOFF_LO:
5471         case R_PPC64_SECTOFF_HI:
5472         case R_PPC64_SECTOFF_HA:
5473         case R_PPC64_SECTOFF_DS:
5474         case R_PPC64_SECTOFF_LO_DS:
5475         case R_PPC64_DTPREL16:
5476         case R_PPC64_DTPREL16_LO:
5477         case R_PPC64_DTPREL16_HI:
5478         case R_PPC64_DTPREL16_HA:
5479         case R_PPC64_DTPREL16_DS:
5480         case R_PPC64_DTPREL16_LO_DS:
5481         case R_PPC64_DTPREL16_HIGH:
5482         case R_PPC64_DTPREL16_HIGHA:
5483         case R_PPC64_DTPREL16_HIGHER:
5484         case R_PPC64_DTPREL16_HIGHERA:
5485         case R_PPC64_DTPREL16_HIGHEST:
5486         case R_PPC64_DTPREL16_HIGHESTA:
5487           break;
5488
5489           /* Nor do these.  */
5490         case R_PPC64_REL16:
5491         case R_PPC64_REL16_LO:
5492         case R_PPC64_REL16_HI:
5493         case R_PPC64_REL16_HA:
5494           break;
5495
5496           /* Not supported as a dynamic relocation.  */
5497         case R_PPC64_ADDR64_LOCAL:
5498           if (bfd_link_pic (info))
5499             {
5500               if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5501                 ppc_howto_init ();
5502               info->callbacks->einfo (_("%P: %H: %s reloc unsupported "
5503                                         "in shared libraries and PIEs.\n"),
5504                                       abfd, sec, rel->r_offset,
5505                                       ppc64_elf_howto_table[r_type]->name);
5506               bfd_set_error (bfd_error_bad_value);
5507               return FALSE;
5508             }
5509           break;
5510
5511         case R_PPC64_TOC16:
5512         case R_PPC64_TOC16_DS:
5513           htab->do_multi_toc = 1;
5514           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5515         case R_PPC64_TOC16_LO:
5516         case R_PPC64_TOC16_HI:
5517         case R_PPC64_TOC16_HA:
5518         case R_PPC64_TOC16_LO_DS:
5519           sec->has_toc_reloc = 1;
5520           break;
5521
5522           /* This relocation describes the C++ object vtable hierarchy.
5523              Reconstruct it for later use during GC.  */
5524         case R_PPC64_GNU_VTINHERIT:
5525           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5526             return FALSE;
5527           break;
5528
5529           /* This relocation describes which C++ vtable entries are actually
5530              used.  Record for later use during GC.  */
5531         case R_PPC64_GNU_VTENTRY:
5532           BFD_ASSERT (h != NULL);
5533           if (h != NULL
5534               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5535             return FALSE;
5536           break;
5537
5538         case R_PPC64_REL14:
5539         case R_PPC64_REL14_BRTAKEN:
5540         case R_PPC64_REL14_BRNTAKEN:
5541           {
5542             asection *dest = NULL;
5543
5544             /* Heuristic: If jumping outside our section, chances are
5545                we are going to need a stub.  */
5546             if (h != NULL)
5547               {
5548                 /* If the sym is weak it may be overridden later, so
5549                    don't assume we know where a weak sym lives.  */
5550                 if (h->root.type == bfd_link_hash_defined)
5551                   dest = h->root.u.def.section;
5552               }
5553             else
5554               {
5555                 Elf_Internal_Sym *isym;
5556
5557                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5558                                               abfd, r_symndx);
5559                 if (isym == NULL)
5560                   return FALSE;
5561
5562                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5563               }
5564
5565             if (dest != sec)
5566               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5567           }
5568           /* Fall through.  */
5569
5570         case R_PPC64_REL24:
5571           if (h != NULL && ifunc == NULL)
5572             {
5573               /* We may need a .plt entry if the function this reloc
5574                  refers to is in a shared lib.  */
5575               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5576                 return FALSE;
5577               h->needs_plt = 1;
5578               if (h->root.root.string[0] == '.'
5579                   && h->root.root.string[1] != '\0')
5580                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5581               if (h == tga || h == dottga)
5582                 sec->has_tls_reloc = 1;
5583             }
5584           break;
5585
5586         case R_PPC64_TPREL64:
5587           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5588           if (bfd_link_pic (info))
5589             info->flags |= DF_STATIC_TLS;
5590           goto dotlstoc;
5591
5592         case R_PPC64_DTPMOD64:
5593           if (rel + 1 < rel_end
5594               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5595               && rel[1].r_offset == rel->r_offset + 8)
5596             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5597           else
5598             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5599           goto dotlstoc;
5600
5601         case R_PPC64_DTPREL64:
5602           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5603           if (rel != relocs
5604               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5605               && rel[-1].r_offset == rel->r_offset - 8)
5606             /* This is the second reloc of a dtpmod, dtprel pair.
5607                Don't mark with TLS_DTPREL.  */
5608             goto dodyn;
5609
5610         dotlstoc:
5611           sec->has_tls_reloc = 1;
5612           if (h != NULL)
5613             {
5614               struct ppc_link_hash_entry *eh;
5615               eh = (struct ppc_link_hash_entry *) h;
5616               eh->tls_mask |= tls_type;
5617             }
5618           else
5619             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5620                                         rel->r_addend, tls_type))
5621               return FALSE;
5622
5623           ppc64_sec = ppc64_elf_section_data (sec);
5624           if (ppc64_sec->sec_type != sec_toc)
5625             {
5626               bfd_size_type amt;
5627
5628               /* One extra to simplify get_tls_mask.  */
5629               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5630               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5631               if (ppc64_sec->u.toc.symndx == NULL)
5632                 return FALSE;
5633               amt = sec->size * sizeof (bfd_vma) / 8;
5634               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5635               if (ppc64_sec->u.toc.add == NULL)
5636                 return FALSE;
5637               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5638               ppc64_sec->sec_type = sec_toc;
5639             }
5640           BFD_ASSERT (rel->r_offset % 8 == 0);
5641           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5642           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5643
5644           /* Mark the second slot of a GD or LD entry.
5645              -1 to indicate GD and -2 to indicate LD.  */
5646           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5647             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5648           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5649             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5650           goto dodyn;
5651
5652         case R_PPC64_TPREL16:
5653         case R_PPC64_TPREL16_LO:
5654         case R_PPC64_TPREL16_HI:
5655         case R_PPC64_TPREL16_HA:
5656         case R_PPC64_TPREL16_DS:
5657         case R_PPC64_TPREL16_LO_DS:
5658         case R_PPC64_TPREL16_HIGH:
5659         case R_PPC64_TPREL16_HIGHA:
5660         case R_PPC64_TPREL16_HIGHER:
5661         case R_PPC64_TPREL16_HIGHERA:
5662         case R_PPC64_TPREL16_HIGHEST:
5663         case R_PPC64_TPREL16_HIGHESTA:
5664           if (bfd_link_pic (info))
5665             {
5666               info->flags |= DF_STATIC_TLS;
5667               goto dodyn;
5668             }
5669           break;
5670
5671         case R_PPC64_ADDR64:
5672           if (opd_sym_map != NULL
5673               && rel + 1 < rel_end
5674               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5675             {
5676               if (h != NULL)
5677                 {
5678                   if (h->root.root.string[0] == '.'
5679                       && h->root.root.string[1] != 0
5680                       && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5681                     ;
5682                   else
5683                     ((struct ppc_link_hash_entry *) h)->is_func = 1;
5684                 }
5685               else
5686                 {
5687                   asection *s;
5688                   Elf_Internal_Sym *isym;
5689
5690                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5691                                                 abfd, r_symndx);
5692                   if (isym == NULL)
5693                     return FALSE;
5694
5695                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5696                   if (s != NULL && s != sec)
5697                     opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5698                 }
5699             }
5700           /* Fall through.  */
5701
5702         case R_PPC64_ADDR16:
5703         case R_PPC64_ADDR16_DS:
5704         case R_PPC64_ADDR16_HA:
5705         case R_PPC64_ADDR16_HI:
5706         case R_PPC64_ADDR16_HIGH:
5707         case R_PPC64_ADDR16_HIGHA:
5708         case R_PPC64_ADDR16_HIGHER:
5709         case R_PPC64_ADDR16_HIGHERA:
5710         case R_PPC64_ADDR16_HIGHEST:
5711         case R_PPC64_ADDR16_HIGHESTA:
5712         case R_PPC64_ADDR16_LO:
5713         case R_PPC64_ADDR16_LO_DS:
5714           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5715               && rel->r_addend == 0)
5716             {
5717               /* We may need a .plt entry if this reloc refers to a
5718                  function in a shared lib.  */
5719               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5720                 return FALSE;
5721               h->pointer_equality_needed = 1;
5722             }
5723           /* Fall through.  */
5724
5725         case R_PPC64_REL30:
5726         case R_PPC64_REL32:
5727         case R_PPC64_REL64:
5728         case R_PPC64_ADDR14:
5729         case R_PPC64_ADDR14_BRNTAKEN:
5730         case R_PPC64_ADDR14_BRTAKEN:
5731         case R_PPC64_ADDR24:
5732         case R_PPC64_ADDR32:
5733         case R_PPC64_UADDR16:
5734         case R_PPC64_UADDR32:
5735         case R_PPC64_UADDR64:
5736         case R_PPC64_TOC:
5737           if (h != NULL && !bfd_link_pic (info))
5738             /* We may need a copy reloc.  */
5739             h->non_got_ref = 1;
5740
5741           /* Don't propagate .opd relocs.  */
5742           if (NO_OPD_RELOCS && opd_sym_map != NULL)
5743             break;
5744
5745           /* If we are creating a shared library, and this is a reloc
5746              against a global symbol, or a non PC relative reloc
5747              against a local symbol, then we need to copy the reloc
5748              into the shared library.  However, if we are linking with
5749              -Bsymbolic, we do not need to copy a reloc against a
5750              global symbol which is defined in an object we are
5751              including in the link (i.e., DEF_REGULAR is set).  At
5752              this point we have not seen all the input files, so it is
5753              possible that DEF_REGULAR is not set now but will be set
5754              later (it is never cleared).  In case of a weak definition,
5755              DEF_REGULAR may be cleared later by a strong definition in
5756              a shared library.  We account for that possibility below by
5757              storing information in the dyn_relocs field of the hash
5758              table entry.  A similar situation occurs when creating
5759              shared libraries and symbol visibility changes render the
5760              symbol local.
5761
5762              If on the other hand, we are creating an executable, we
5763              may need to keep relocations for symbols satisfied by a
5764              dynamic library if we manage to avoid copy relocs for the
5765              symbol.  */
5766         dodyn:
5767           if ((bfd_link_pic (info)
5768                && (must_be_dyn_reloc (info, r_type)
5769                    || (h != NULL
5770                        && (!SYMBOLIC_BIND (info, h)
5771                            || h->root.type == bfd_link_hash_defweak
5772                            || !h->def_regular))))
5773               || (ELIMINATE_COPY_RELOCS
5774                   && !bfd_link_pic (info)
5775                   && h != NULL
5776                   && (h->root.type == bfd_link_hash_defweak
5777                       || !h->def_regular))
5778               || (!bfd_link_pic (info)
5779                   && ifunc != NULL))
5780             {
5781               /* We must copy these reloc types into the output file.
5782                  Create a reloc section in dynobj and make room for
5783                  this reloc.  */
5784               if (sreloc == NULL)
5785                 {
5786                   sreloc = _bfd_elf_make_dynamic_reloc_section
5787                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5788
5789                   if (sreloc == NULL)
5790                     return FALSE;
5791                 }
5792
5793               /* If this is a global symbol, we count the number of
5794                  relocations we need for this symbol.  */
5795               if (h != NULL)
5796                 {
5797                   struct elf_dyn_relocs *p;
5798                   struct elf_dyn_relocs **head;
5799
5800                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5801                   p = *head;
5802                   if (p == NULL || p->sec != sec)
5803                     {
5804                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5805                       if (p == NULL)
5806                         return FALSE;
5807                       p->next = *head;
5808                       *head = p;
5809                       p->sec = sec;
5810                       p->count = 0;
5811                       p->pc_count = 0;
5812                     }
5813                   p->count += 1;
5814                   if (!must_be_dyn_reloc (info, r_type))
5815                     p->pc_count += 1;
5816                 }
5817               else
5818                 {
5819                   /* Track dynamic relocs needed for local syms too.
5820                      We really need local syms available to do this
5821                      easily.  Oh well.  */
5822                   struct ppc_dyn_relocs *p;
5823                   struct ppc_dyn_relocs **head;
5824                   bfd_boolean is_ifunc;
5825                   asection *s;
5826                   void *vpp;
5827                   Elf_Internal_Sym *isym;
5828
5829                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5830                                                 abfd, r_symndx);
5831                   if (isym == NULL)
5832                     return FALSE;
5833
5834                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5835                   if (s == NULL)
5836                     s = sec;
5837
5838                   vpp = &elf_section_data (s)->local_dynrel;
5839                   head = (struct ppc_dyn_relocs **) vpp;
5840                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5841                   p = *head;
5842                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5843                     p = p->next;
5844                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5845                     {
5846                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5847                       if (p == NULL)
5848                         return FALSE;
5849                       p->next = *head;
5850                       *head = p;
5851                       p->sec = sec;
5852                       p->ifunc = is_ifunc;
5853                       p->count = 0;
5854                     }
5855                   p->count += 1;
5856                 }
5857             }
5858           break;
5859
5860         default:
5861           break;
5862         }
5863     }
5864
5865   return TRUE;
5866 }
5867
5868 /* Merge backend specific data from an object file to the output
5869    object file when linking.  */
5870
5871 static bfd_boolean
5872 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5873 {
5874   unsigned long iflags, oflags;
5875
5876   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5877     return TRUE;
5878
5879   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5880     return TRUE;
5881
5882   if (!_bfd_generic_verify_endian_match (ibfd, obfd))
5883     return FALSE;
5884
5885   iflags = elf_elfheader (ibfd)->e_flags;
5886   oflags = elf_elfheader (obfd)->e_flags;
5887
5888   if (iflags & ~EF_PPC64_ABI)
5889     {
5890       (*_bfd_error_handler)
5891         (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
5892       bfd_set_error (bfd_error_bad_value);
5893       return FALSE;
5894     }
5895   else if (iflags != oflags && iflags != 0)
5896     {
5897       (*_bfd_error_handler)
5898         (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
5899          ibfd, iflags, oflags);
5900       bfd_set_error (bfd_error_bad_value);
5901       return FALSE;
5902     }
5903
5904   /* Merge Tag_compatibility attributes and any common GNU ones.  */
5905   _bfd_elf_merge_object_attributes (ibfd, obfd);
5906
5907   return TRUE;
5908 }
5909
5910 static bfd_boolean
5911 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5912 {
5913   /* Print normal ELF private data.  */
5914   _bfd_elf_print_private_bfd_data (abfd, ptr);
5915
5916   if (elf_elfheader (abfd)->e_flags != 0)
5917     {
5918       FILE *file = ptr;
5919
5920       /* xgettext:c-format */
5921       fprintf (file, _("private flags = 0x%lx:"),
5922                elf_elfheader (abfd)->e_flags);
5923
5924       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5925         fprintf (file, _(" [abiv%ld]"),
5926                  elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5927       fputc ('\n', file);
5928     }
5929
5930   return TRUE;
5931 }
5932
5933 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
5934    of the code entry point, and its section, which must be in the same
5935    object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
5936
5937 static bfd_vma
5938 opd_entry_value (asection *opd_sec,
5939                  bfd_vma offset,
5940                  asection **code_sec,
5941                  bfd_vma *code_off,
5942                  bfd_boolean in_code_sec)
5943 {
5944   bfd *opd_bfd = opd_sec->owner;
5945   Elf_Internal_Rela *relocs;
5946   Elf_Internal_Rela *lo, *hi, *look;
5947   bfd_vma val;
5948
5949   /* No relocs implies we are linking a --just-symbols object, or looking
5950      at a final linked executable with addr2line or somesuch.  */
5951   if (opd_sec->reloc_count == 0)
5952     {
5953       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5954
5955       if (contents == NULL)
5956         {
5957           if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5958             return (bfd_vma) -1;
5959           ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5960         }
5961
5962       /* PR 17512: file: 64b9dfbb.  */
5963       if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5964         return (bfd_vma) -1;
5965
5966       val = bfd_get_64 (opd_bfd, contents + offset);
5967       if (code_sec != NULL)
5968         {
5969           asection *sec, *likely = NULL;
5970
5971           if (in_code_sec)
5972             {
5973               sec = *code_sec;
5974               if (sec->vma <= val
5975                   && val < sec->vma + sec->size)
5976                 likely = sec;
5977               else
5978                 val = -1;
5979             }
5980           else
5981             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5982               if (sec->vma <= val
5983                   && (sec->flags & SEC_LOAD) != 0
5984                   && (sec->flags & SEC_ALLOC) != 0)
5985                 likely = sec;
5986           if (likely != NULL)
5987             {
5988               *code_sec = likely;
5989               if (code_off != NULL)
5990                 *code_off = val - likely->vma;
5991             }
5992         }
5993       return val;
5994     }
5995
5996   BFD_ASSERT (is_ppc64_elf (opd_bfd));
5997
5998   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5999   if (relocs == NULL)
6000     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6001   /* PR 17512: file: df8e1fd6.  */
6002   if (relocs == NULL)
6003     return (bfd_vma) -1;
6004
6005   /* Go find the opd reloc at the sym address.  */
6006   lo = relocs;
6007   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6008   val = (bfd_vma) -1;
6009   while (lo < hi)
6010     {
6011       look = lo + (hi - lo) / 2;
6012       if (look->r_offset < offset)
6013         lo = look + 1;
6014       else if (look->r_offset > offset)
6015         hi = look;
6016       else
6017         {
6018           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6019
6020           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6021               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6022             {
6023               unsigned long symndx = ELF64_R_SYM (look->r_info);
6024               asection *sec = NULL;
6025
6026               if (symndx >= symtab_hdr->sh_info
6027                   && elf_sym_hashes (opd_bfd) != NULL)
6028                 {
6029                   struct elf_link_hash_entry **sym_hashes;
6030                   struct elf_link_hash_entry *rh;
6031
6032                   sym_hashes = elf_sym_hashes (opd_bfd);
6033                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
6034                   if (rh != NULL)
6035                     {
6036                       rh = elf_follow_link (rh);
6037                       if (rh->root.type != bfd_link_hash_defined
6038                           && rh->root.type != bfd_link_hash_defweak)
6039                         break;
6040                       if (rh->root.u.def.section->owner == opd_bfd)
6041                         {
6042                           val = rh->root.u.def.value;
6043                           sec = rh->root.u.def.section;
6044                         }
6045                     }
6046                 }
6047
6048               if (sec == NULL)
6049                 {
6050                   Elf_Internal_Sym *sym;
6051
6052                   if (symndx < symtab_hdr->sh_info)
6053                     {
6054                       sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6055                       if (sym == NULL)
6056                         {
6057                           size_t symcnt = symtab_hdr->sh_info;
6058                           sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6059                                                       symcnt, 0,
6060                                                       NULL, NULL, NULL);
6061                           if (sym == NULL)
6062                             break;
6063                           symtab_hdr->contents = (bfd_byte *) sym;
6064                         }
6065                       sym += symndx;
6066                     }
6067                   else
6068                     {
6069                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6070                                                   1, symndx,
6071                                                   NULL, NULL, NULL);
6072                       if (sym == NULL)
6073                         break;
6074                     }
6075                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6076                   if (sec == NULL)
6077                     break;
6078                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6079                   val = sym->st_value;
6080                 }
6081
6082               val += look->r_addend;
6083               if (code_off != NULL)
6084                 *code_off = val;
6085               if (code_sec != NULL)
6086                 {
6087                   if (in_code_sec && *code_sec != sec)
6088                     return -1;
6089                   else
6090                     *code_sec = sec;
6091                 }
6092               if (sec->output_section != NULL)
6093                 val += sec->output_section->vma + sec->output_offset;
6094             }
6095           break;
6096         }
6097     }
6098
6099   return val;
6100 }
6101
6102 /* If the ELF symbol SYM might be a function in SEC, return the
6103    function size and set *CODE_OFF to the function's entry point,
6104    otherwise return zero.  */
6105
6106 static bfd_size_type
6107 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6108                               bfd_vma *code_off)
6109 {
6110   bfd_size_type size;
6111
6112   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6113                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6114     return 0;
6115
6116   size = 0;
6117   if (!(sym->flags & BSF_SYNTHETIC))
6118     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6119
6120   if (strcmp (sym->section->name, ".opd") == 0)
6121     {
6122       if (opd_entry_value (sym->section, sym->value,
6123                            &sec, code_off, TRUE) == (bfd_vma) -1)
6124         return 0;
6125       /* An old ABI binary with dot-syms has a size of 24 on the .opd
6126          symbol.  This size has nothing to do with the code size of the
6127          function, which is what we're supposed to return, but the
6128          code size isn't available without looking up the dot-sym.
6129          However, doing that would be a waste of time particularly
6130          since elf_find_function will look at the dot-sym anyway.
6131          Now, elf_find_function will keep the largest size of any
6132          function sym found at the code address of interest, so return
6133          1 here to avoid it incorrectly caching a larger function size
6134          for a small function.  This does mean we return the wrong
6135          size for a new-ABI function of size 24, but all that does is
6136          disable caching for such functions.  */
6137       if (size == 24)
6138         size = 1;
6139     }
6140   else
6141     {
6142       if (sym->section != sec)
6143         return 0;
6144       *code_off = sym->value;
6145     }
6146   if (size == 0)
6147     size = 1;
6148   return size;
6149 }
6150
6151 /* Return true if symbol is defined in a regular object file.  */
6152
6153 static bfd_boolean
6154 is_static_defined (struct elf_link_hash_entry *h)
6155 {
6156   return ((h->root.type == bfd_link_hash_defined
6157            || h->root.type == bfd_link_hash_defweak)
6158           && h->root.u.def.section != NULL
6159           && h->root.u.def.section->output_section != NULL);
6160 }
6161
6162 /* If FDH is a function descriptor symbol, return the associated code
6163    entry symbol if it is defined.  Return NULL otherwise.  */
6164
6165 static struct ppc_link_hash_entry *
6166 defined_code_entry (struct ppc_link_hash_entry *fdh)
6167 {
6168   if (fdh->is_func_descriptor)
6169     {
6170       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6171       if (fh->elf.root.type == bfd_link_hash_defined
6172           || fh->elf.root.type == bfd_link_hash_defweak)
6173         return fh;
6174     }
6175   return NULL;
6176 }
6177
6178 /* If FH is a function code entry symbol, return the associated
6179    function descriptor symbol if it is defined.  Return NULL otherwise.  */
6180
6181 static struct ppc_link_hash_entry *
6182 defined_func_desc (struct ppc_link_hash_entry *fh)
6183 {
6184   if (fh->oh != NULL
6185       && fh->oh->is_func_descriptor)
6186     {
6187       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6188       if (fdh->elf.root.type == bfd_link_hash_defined
6189           || fdh->elf.root.type == bfd_link_hash_defweak)
6190         return fdh;
6191     }
6192   return NULL;
6193 }
6194
6195 /* Mark all our entry sym sections, both opd and code section.  */
6196
6197 static void
6198 ppc64_elf_gc_keep (struct bfd_link_info *info)
6199 {
6200   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6201   struct bfd_sym_chain *sym;
6202
6203   if (htab == NULL)
6204     return;
6205
6206   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6207     {
6208       struct ppc_link_hash_entry *eh, *fh;
6209       asection *sec;
6210
6211       eh = (struct ppc_link_hash_entry *)
6212         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6213       if (eh == NULL)
6214         continue;
6215       if (eh->elf.root.type != bfd_link_hash_defined
6216           && eh->elf.root.type != bfd_link_hash_defweak)
6217         continue;
6218
6219       fh = defined_code_entry (eh);
6220       if (fh != NULL)
6221         {
6222           sec = fh->elf.root.u.def.section;
6223           sec->flags |= SEC_KEEP;
6224         }
6225       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6226                && opd_entry_value (eh->elf.root.u.def.section,
6227                                    eh->elf.root.u.def.value,
6228                                    &sec, NULL, FALSE) != (bfd_vma) -1)
6229         sec->flags |= SEC_KEEP;
6230
6231       sec = eh->elf.root.u.def.section;
6232       sec->flags |= SEC_KEEP;
6233     }
6234 }
6235
6236 /* Mark sections containing dynamically referenced symbols.  When
6237    building shared libraries, we must assume that any visible symbol is
6238    referenced.  */
6239
6240 static bfd_boolean
6241 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6242 {
6243   struct bfd_link_info *info = (struct bfd_link_info *) inf;
6244   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6245   struct ppc_link_hash_entry *fdh;
6246   struct bfd_elf_dynamic_list *d = info->dynamic_list;
6247
6248   /* Dynamic linking info is on the func descriptor sym.  */
6249   fdh = defined_func_desc (eh);
6250   if (fdh != NULL)
6251     eh = fdh;
6252
6253   if ((eh->elf.root.type == bfd_link_hash_defined
6254        || eh->elf.root.type == bfd_link_hash_defweak)
6255       && (eh->elf.ref_dynamic
6256           || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6257               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6258               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6259               && (!bfd_link_executable (info)
6260                   || info->export_dynamic
6261                   || (eh->elf.dynamic
6262                       && d != NULL
6263                       && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6264               && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6265                   || !bfd_hide_sym_by_version (info->version_info,
6266                                                eh->elf.root.root.string)))))
6267     {
6268       asection *code_sec;
6269       struct ppc_link_hash_entry *fh;
6270
6271       eh->elf.root.u.def.section->flags |= SEC_KEEP;
6272
6273       /* Function descriptor syms cause the associated
6274          function code sym section to be marked.  */
6275       fh = defined_code_entry (eh);
6276       if (fh != NULL)
6277         {
6278           code_sec = fh->elf.root.u.def.section;
6279           code_sec->flags |= SEC_KEEP;
6280         }
6281       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6282                && opd_entry_value (eh->elf.root.u.def.section,
6283                                    eh->elf.root.u.def.value,
6284                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
6285         code_sec->flags |= SEC_KEEP;
6286     }
6287
6288   return TRUE;
6289 }
6290
6291 /* Return the section that should be marked against GC for a given
6292    relocation.  */
6293
6294 static asection *
6295 ppc64_elf_gc_mark_hook (asection *sec,
6296                         struct bfd_link_info *info,
6297                         Elf_Internal_Rela *rel,
6298                         struct elf_link_hash_entry *h,
6299                         Elf_Internal_Sym *sym)
6300 {
6301   asection *rsec;
6302
6303   /* Syms return NULL if we're marking .opd, so we avoid marking all
6304      function sections, as all functions are referenced in .opd.  */
6305   rsec = NULL;
6306   if (get_opd_info (sec) != NULL)
6307     return rsec;
6308
6309   if (h != NULL)
6310     {
6311       enum elf_ppc64_reloc_type r_type;
6312       struct ppc_link_hash_entry *eh, *fh, *fdh;
6313
6314       r_type = ELF64_R_TYPE (rel->r_info);
6315       switch (r_type)
6316         {
6317         case R_PPC64_GNU_VTINHERIT:
6318         case R_PPC64_GNU_VTENTRY:
6319           break;
6320
6321         default:
6322           switch (h->root.type)
6323             {
6324             case bfd_link_hash_defined:
6325             case bfd_link_hash_defweak:
6326               eh = (struct ppc_link_hash_entry *) h;
6327               fdh = defined_func_desc (eh);
6328               if (fdh != NULL)
6329                 eh = fdh;
6330
6331               /* Function descriptor syms cause the associated
6332                  function code sym section to be marked.  */
6333               fh = defined_code_entry (eh);
6334               if (fh != NULL)
6335                 {
6336                   /* They also mark their opd section.  */
6337                   eh->elf.root.u.def.section->gc_mark = 1;
6338
6339                   rsec = fh->elf.root.u.def.section;
6340                 }
6341               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6342                        && opd_entry_value (eh->elf.root.u.def.section,
6343                                            eh->elf.root.u.def.value,
6344                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
6345                 eh->elf.root.u.def.section->gc_mark = 1;
6346               else
6347                 rsec = h->root.u.def.section;
6348               break;
6349
6350             case bfd_link_hash_common:
6351               rsec = h->root.u.c.p->section;
6352               break;
6353
6354             default:
6355               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6356             }
6357         }
6358     }
6359   else
6360     {
6361       struct _opd_sec_data *opd;
6362
6363       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6364       opd = get_opd_info (rsec);
6365       if (opd != NULL && opd->func_sec != NULL)
6366         {
6367           rsec->gc_mark = 1;
6368
6369           rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6370         }
6371     }
6372
6373   return rsec;
6374 }
6375
6376 /* Update the .got, .plt. and dynamic reloc reference counts for the
6377    section being removed.  */
6378
6379 static bfd_boolean
6380 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6381                          asection *sec, const Elf_Internal_Rela *relocs)
6382 {
6383   struct ppc_link_hash_table *htab;
6384   Elf_Internal_Shdr *symtab_hdr;
6385   struct elf_link_hash_entry **sym_hashes;
6386   struct got_entry **local_got_ents;
6387   const Elf_Internal_Rela *rel, *relend;
6388
6389   if (bfd_link_relocatable (info))
6390     return TRUE;
6391
6392   if ((sec->flags & SEC_ALLOC) == 0)
6393     return TRUE;
6394
6395   elf_section_data (sec)->local_dynrel = NULL;
6396
6397   htab = ppc_hash_table (info);
6398   if (htab == NULL)
6399     return FALSE;
6400
6401   symtab_hdr = &elf_symtab_hdr (abfd);
6402   sym_hashes = elf_sym_hashes (abfd);
6403   local_got_ents = elf_local_got_ents (abfd);
6404
6405   relend = relocs + sec->reloc_count;
6406   for (rel = relocs; rel < relend; rel++)
6407     {
6408       unsigned long r_symndx;
6409       enum elf_ppc64_reloc_type r_type;
6410       struct elf_link_hash_entry *h = NULL;
6411       unsigned char tls_type = 0;
6412
6413       r_symndx = ELF64_R_SYM (rel->r_info);
6414       r_type = ELF64_R_TYPE (rel->r_info);
6415       if (r_symndx >= symtab_hdr->sh_info)
6416         {
6417           struct ppc_link_hash_entry *eh;
6418           struct elf_dyn_relocs **pp;
6419           struct elf_dyn_relocs *p;
6420
6421           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6422           h = elf_follow_link (h);
6423           eh = (struct ppc_link_hash_entry *) h;
6424
6425           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6426             if (p->sec == sec)
6427               {
6428                 /* Everything must go for SEC.  */
6429                 *pp = p->next;
6430                 break;
6431               }
6432         }
6433
6434       if (is_branch_reloc (r_type))
6435         {
6436           struct plt_entry **ifunc = NULL;
6437           if (h != NULL)
6438             {
6439               if (h->type == STT_GNU_IFUNC)
6440                 ifunc = &h->plt.plist;
6441             }
6442           else if (local_got_ents != NULL)
6443             {
6444               struct plt_entry **local_plt = (struct plt_entry **)
6445                 (local_got_ents + symtab_hdr->sh_info);
6446               unsigned char *local_got_tls_masks = (unsigned char *)
6447                 (local_plt + symtab_hdr->sh_info);
6448               if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6449                 ifunc = local_plt + r_symndx;
6450             }
6451           if (ifunc != NULL)
6452             {
6453               struct plt_entry *ent;
6454
6455               for (ent = *ifunc; ent != NULL; ent = ent->next)
6456                 if (ent->addend == rel->r_addend)
6457                   break;
6458               if (ent == NULL)
6459                 abort ();
6460               if (ent->plt.refcount > 0)
6461                 ent->plt.refcount -= 1;
6462               continue;
6463             }
6464         }
6465
6466       switch (r_type)
6467         {
6468         case R_PPC64_GOT_TLSLD16:
6469         case R_PPC64_GOT_TLSLD16_LO:
6470         case R_PPC64_GOT_TLSLD16_HI:
6471         case R_PPC64_GOT_TLSLD16_HA:
6472           tls_type = TLS_TLS | TLS_LD;
6473           goto dogot;
6474
6475         case R_PPC64_GOT_TLSGD16:
6476         case R_PPC64_GOT_TLSGD16_LO:
6477         case R_PPC64_GOT_TLSGD16_HI:
6478         case R_PPC64_GOT_TLSGD16_HA:
6479           tls_type = TLS_TLS | TLS_GD;
6480           goto dogot;
6481
6482         case R_PPC64_GOT_TPREL16_DS:
6483         case R_PPC64_GOT_TPREL16_LO_DS:
6484         case R_PPC64_GOT_TPREL16_HI:
6485         case R_PPC64_GOT_TPREL16_HA:
6486           tls_type = TLS_TLS | TLS_TPREL;
6487           goto dogot;
6488
6489         case R_PPC64_GOT_DTPREL16_DS:
6490         case R_PPC64_GOT_DTPREL16_LO_DS:
6491         case R_PPC64_GOT_DTPREL16_HI:
6492         case R_PPC64_GOT_DTPREL16_HA:
6493           tls_type = TLS_TLS | TLS_DTPREL;
6494           goto dogot;
6495
6496         case R_PPC64_GOT16:
6497         case R_PPC64_GOT16_DS:
6498         case R_PPC64_GOT16_HA:
6499         case R_PPC64_GOT16_HI:
6500         case R_PPC64_GOT16_LO:
6501         case R_PPC64_GOT16_LO_DS:
6502         dogot:
6503           {
6504             struct got_entry *ent;
6505
6506             if (h != NULL)
6507               ent = h->got.glist;
6508             else
6509               ent = local_got_ents[r_symndx];
6510
6511             for (; ent != NULL; ent = ent->next)
6512               if (ent->addend == rel->r_addend
6513                   && ent->owner == abfd
6514                   && ent->tls_type == tls_type)
6515                 break;
6516             if (ent == NULL)
6517               abort ();
6518             if (ent->got.refcount > 0)
6519               ent->got.refcount -= 1;
6520           }
6521           break;
6522
6523         case R_PPC64_PLT16_HA:
6524         case R_PPC64_PLT16_HI:
6525         case R_PPC64_PLT16_LO:
6526         case R_PPC64_PLT32:
6527         case R_PPC64_PLT64:
6528         case R_PPC64_REL14:
6529         case R_PPC64_REL14_BRNTAKEN:
6530         case R_PPC64_REL14_BRTAKEN:
6531         case R_PPC64_REL24:
6532           if (h != NULL)
6533             {
6534               struct plt_entry *ent;
6535
6536               for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6537                 if (ent->addend == rel->r_addend)
6538                   break;
6539               if (ent != NULL && ent->plt.refcount > 0)
6540                 ent->plt.refcount -= 1;
6541             }
6542           break;
6543
6544         default:
6545           break;
6546         }
6547     }
6548   return TRUE;
6549 }
6550
6551 /* The maximum size of .sfpr.  */
6552 #define SFPR_MAX (218*4)
6553
6554 struct sfpr_def_parms
6555 {
6556   const char name[12];
6557   unsigned char lo, hi;
6558   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6559   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6560 };
6561
6562 /* Auto-generate _save*, _rest* functions in .sfpr.  */
6563
6564 static bfd_boolean
6565 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6566 {
6567   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6568   unsigned int i;
6569   size_t len = strlen (parm->name);
6570   bfd_boolean writing = FALSE;
6571   char sym[16];
6572
6573   if (htab == NULL)
6574     return FALSE;
6575
6576   memcpy (sym, parm->name, len);
6577   sym[len + 2] = 0;
6578
6579   for (i = parm->lo; i <= parm->hi; i++)
6580     {
6581       struct elf_link_hash_entry *h;
6582
6583       sym[len + 0] = i / 10 + '0';
6584       sym[len + 1] = i % 10 + '0';
6585       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6586       if (h != NULL
6587           && !h->def_regular)
6588         {
6589           h->root.type = bfd_link_hash_defined;
6590           h->root.u.def.section = htab->sfpr;
6591           h->root.u.def.value = htab->sfpr->size;
6592           h->type = STT_FUNC;
6593           h->def_regular = 1;
6594           _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6595           writing = TRUE;
6596           if (htab->sfpr->contents == NULL)
6597             {
6598               htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6599               if (htab->sfpr->contents == NULL)
6600                 return FALSE;
6601             }
6602         }
6603       if (writing)
6604         {
6605           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6606           if (i != parm->hi)
6607             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6608           else
6609             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6610           htab->sfpr->size = p - htab->sfpr->contents;
6611         }
6612     }
6613
6614   return TRUE;
6615 }
6616
6617 static bfd_byte *
6618 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6619 {
6620   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6621   return p + 4;
6622 }
6623
6624 static bfd_byte *
6625 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6626 {
6627   p = savegpr0 (abfd, p, r);
6628   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6629   p = p + 4;
6630   bfd_put_32 (abfd, BLR, p);
6631   return p + 4;
6632 }
6633
6634 static bfd_byte *
6635 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6636 {
6637   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6638   return p + 4;
6639 }
6640
6641 static bfd_byte *
6642 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6643 {
6644   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6645   p = p + 4;
6646   p = restgpr0 (abfd, p, r);
6647   bfd_put_32 (abfd, MTLR_R0, p);
6648   p = p + 4;
6649   if (r == 29)
6650     {
6651       p = restgpr0 (abfd, p, 30);
6652       p = restgpr0 (abfd, p, 31);
6653     }
6654   bfd_put_32 (abfd, BLR, p);
6655   return p + 4;
6656 }
6657
6658 static bfd_byte *
6659 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6660 {
6661   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6662   return p + 4;
6663 }
6664
6665 static bfd_byte *
6666 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6667 {
6668   p = savegpr1 (abfd, p, r);
6669   bfd_put_32 (abfd, BLR, p);
6670   return p + 4;
6671 }
6672
6673 static bfd_byte *
6674 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6675 {
6676   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6677   return p + 4;
6678 }
6679
6680 static bfd_byte *
6681 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6682 {
6683   p = restgpr1 (abfd, p, r);
6684   bfd_put_32 (abfd, BLR, p);
6685   return p + 4;
6686 }
6687
6688 static bfd_byte *
6689 savefpr (bfd *abfd, bfd_byte *p, int r)
6690 {
6691   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6692   return p + 4;
6693 }
6694
6695 static bfd_byte *
6696 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6697 {
6698   p = savefpr (abfd, p, r);
6699   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6700   p = p + 4;
6701   bfd_put_32 (abfd, BLR, p);
6702   return p + 4;
6703 }
6704
6705 static bfd_byte *
6706 restfpr (bfd *abfd, bfd_byte *p, int r)
6707 {
6708   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6709   return p + 4;
6710 }
6711
6712 static bfd_byte *
6713 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6714 {
6715   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6716   p = p + 4;
6717   p = restfpr (abfd, p, r);
6718   bfd_put_32 (abfd, MTLR_R0, p);
6719   p = p + 4;
6720   if (r == 29)
6721     {
6722       p = restfpr (abfd, p, 30);
6723       p = restfpr (abfd, p, 31);
6724     }
6725   bfd_put_32 (abfd, BLR, p);
6726   return p + 4;
6727 }
6728
6729 static bfd_byte *
6730 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6731 {
6732   p = savefpr (abfd, p, r);
6733   bfd_put_32 (abfd, BLR, p);
6734   return p + 4;
6735 }
6736
6737 static bfd_byte *
6738 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6739 {
6740   p = restfpr (abfd, p, r);
6741   bfd_put_32 (abfd, BLR, p);
6742   return p + 4;
6743 }
6744
6745 static bfd_byte *
6746 savevr (bfd *abfd, bfd_byte *p, int r)
6747 {
6748   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6749   p = p + 4;
6750   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6751   return p + 4;
6752 }
6753
6754 static bfd_byte *
6755 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6756 {
6757   p = savevr (abfd, p, r);
6758   bfd_put_32 (abfd, BLR, p);
6759   return p + 4;
6760 }
6761
6762 static bfd_byte *
6763 restvr (bfd *abfd, bfd_byte *p, int r)
6764 {
6765   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6766   p = p + 4;
6767   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6768   return p + 4;
6769 }
6770
6771 static bfd_byte *
6772 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6773 {
6774   p = restvr (abfd, p, r);
6775   bfd_put_32 (abfd, BLR, p);
6776   return p + 4;
6777 }
6778
6779 /* Called via elf_link_hash_traverse to transfer dynamic linking
6780    information on function code symbol entries to their corresponding
6781    function descriptor symbol entries.  */
6782
6783 static bfd_boolean
6784 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6785 {
6786   struct bfd_link_info *info;
6787   struct ppc_link_hash_table *htab;
6788   struct plt_entry *ent;
6789   struct ppc_link_hash_entry *fh;
6790   struct ppc_link_hash_entry *fdh;
6791   bfd_boolean force_local;
6792
6793   fh = (struct ppc_link_hash_entry *) h;
6794   if (fh->elf.root.type == bfd_link_hash_indirect)
6795     return TRUE;
6796
6797   info = inf;
6798   htab = ppc_hash_table (info);
6799   if (htab == NULL)
6800     return FALSE;
6801
6802   /* Resolve undefined references to dot-symbols as the value
6803      in the function descriptor, if we have one in a regular object.
6804      This is to satisfy cases like ".quad .foo".  Calls to functions
6805      in dynamic objects are handled elsewhere.  */
6806   if (fh->elf.root.type == bfd_link_hash_undefweak
6807       && fh->was_undefined
6808       && (fdh = defined_func_desc (fh)) != NULL
6809       && get_opd_info (fdh->elf.root.u.def.section) != NULL
6810       && opd_entry_value (fdh->elf.root.u.def.section,
6811                           fdh->elf.root.u.def.value,
6812                           &fh->elf.root.u.def.section,
6813                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6814     {
6815       fh->elf.root.type = fdh->elf.root.type;
6816       fh->elf.forced_local = 1;
6817       fh->elf.def_regular = fdh->elf.def_regular;
6818       fh->elf.def_dynamic = fdh->elf.def_dynamic;
6819     }
6820
6821   /* If this is a function code symbol, transfer dynamic linking
6822      information to the function descriptor symbol.  */
6823   if (!fh->is_func)
6824     return TRUE;
6825
6826   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6827     if (ent->plt.refcount > 0)
6828       break;
6829   if (ent == NULL
6830       || fh->elf.root.root.string[0] != '.'
6831       || fh->elf.root.root.string[1] == '\0')
6832     return TRUE;
6833
6834   /* Find the corresponding function descriptor symbol.  Create it
6835      as undefined if necessary.  */
6836
6837   fdh = lookup_fdh (fh, htab);
6838   if (fdh == NULL
6839       && !bfd_link_executable (info)
6840       && (fh->elf.root.type == bfd_link_hash_undefined
6841           || fh->elf.root.type == bfd_link_hash_undefweak))
6842     {
6843       fdh = make_fdh (info, fh);
6844       if (fdh == NULL)
6845         return FALSE;
6846     }
6847
6848   /* Fake function descriptors are made undefweak.  If the function
6849      code symbol is strong undefined, make the fake sym the same.
6850      If the function code symbol is defined, then force the fake
6851      descriptor local;  We can't support overriding of symbols in a
6852      shared library on a fake descriptor.  */
6853
6854   if (fdh != NULL
6855       && fdh->fake
6856       && fdh->elf.root.type == bfd_link_hash_undefweak)
6857     {
6858       if (fh->elf.root.type == bfd_link_hash_undefined)
6859         {
6860           fdh->elf.root.type = bfd_link_hash_undefined;
6861           bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6862         }
6863       else if (fh->elf.root.type == bfd_link_hash_defined
6864                || fh->elf.root.type == bfd_link_hash_defweak)
6865         {
6866           _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6867         }
6868     }
6869
6870   if (fdh != NULL
6871       && !fdh->elf.forced_local
6872       && (!bfd_link_executable (info)
6873           || fdh->elf.def_dynamic
6874           || fdh->elf.ref_dynamic
6875           || (fdh->elf.root.type == bfd_link_hash_undefweak
6876               && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6877     {
6878       if (fdh->elf.dynindx == -1)
6879         if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6880           return FALSE;
6881       fdh->elf.ref_regular |= fh->elf.ref_regular;
6882       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6883       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6884       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6885       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6886         {
6887           move_plt_plist (fh, fdh);
6888           fdh->elf.needs_plt = 1;
6889         }
6890       fdh->is_func_descriptor = 1;
6891       fdh->oh = fh;
6892       fh->oh = fdh;
6893     }
6894
6895   /* Now that the info is on the function descriptor, clear the
6896      function code sym info.  Any function code syms for which we
6897      don't have a definition in a regular file, we force local.
6898      This prevents a shared library from exporting syms that have
6899      been imported from another library.  Function code syms that
6900      are really in the library we must leave global to prevent the
6901      linker dragging in a definition from a static library.  */
6902   force_local = (!fh->elf.def_regular
6903                  || fdh == NULL
6904                  || !fdh->elf.def_regular
6905                  || fdh->elf.forced_local);
6906   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6907
6908   return TRUE;
6909 }
6910
6911 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
6912    this hook to a) provide some gcc support functions, and b) transfer
6913    dynamic linking information gathered so far on function code symbol
6914    entries, to their corresponding function descriptor symbol entries.  */
6915
6916 static bfd_boolean
6917 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6918                             struct bfd_link_info *info)
6919 {
6920   struct ppc_link_hash_table *htab;
6921   unsigned int i;
6922   static const struct sfpr_def_parms funcs[] =
6923     {
6924       { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6925       { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6926       { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6927       { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6928       { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6929       { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6930       { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6931       { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6932       { "._savef", 14, 31, savefpr, savefpr1_tail },
6933       { "._restf", 14, 31, restfpr, restfpr1_tail },
6934       { "_savevr_", 20, 31, savevr, savevr_tail },
6935       { "_restvr_", 20, 31, restvr, restvr_tail }
6936     };
6937
6938   htab = ppc_hash_table (info);
6939   if (htab == NULL)
6940     return FALSE;
6941
6942   if (!bfd_link_relocatable (info)
6943       && htab->elf.hgot != NULL)
6944     {
6945       _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6946       /* Make .TOC. defined so as to prevent it being made dynamic.
6947          The wrong value here is fixed later in ppc64_elf_set_toc.  */
6948       if (!htab->elf.hgot->def_regular
6949           || htab->elf.hgot->root.type != bfd_link_hash_defined)
6950         {
6951           htab->elf.hgot->root.type = bfd_link_hash_defined;
6952           htab->elf.hgot->root.u.def.value = 0;
6953           htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6954           htab->elf.hgot->def_regular = 1;
6955           htab->elf.hgot->root.linker_def = 1;
6956         }
6957       htab->elf.hgot->type = STT_OBJECT;
6958       htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
6959                                | STV_HIDDEN);
6960     }
6961
6962   if (htab->sfpr == NULL)
6963     /* We don't have any relocs.  */
6964     return TRUE;
6965
6966   /* Provide any missing _save* and _rest* functions.  */
6967   htab->sfpr->size = 0;
6968   if (htab->params->save_restore_funcs)
6969     for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6970       if (!sfpr_define (info, &funcs[i]))
6971         return FALSE;
6972
6973   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6974
6975   if (htab->sfpr->size == 0)
6976     htab->sfpr->flags |= SEC_EXCLUDE;
6977
6978   return TRUE;
6979 }
6980
6981 /* Return true if we have dynamic relocs that apply to read-only sections.  */
6982
6983 static bfd_boolean
6984 readonly_dynrelocs (struct elf_link_hash_entry *h)
6985 {
6986   struct ppc_link_hash_entry *eh;
6987   struct elf_dyn_relocs *p;
6988
6989   eh = (struct ppc_link_hash_entry *) h;
6990   for (p = eh->dyn_relocs; p != NULL; p = p->next)
6991     {
6992       asection *s = p->sec->output_section;
6993
6994       if (s != NULL && (s->flags & SEC_READONLY) != 0)
6995         return TRUE;
6996     }
6997   return FALSE;
6998 }
6999
7000 /* Adjust a symbol defined by a dynamic object and referenced by a
7001    regular object.  The current definition is in some section of the
7002    dynamic object, but we're not including those sections.  We have to
7003    change the definition to something the rest of the link can
7004    understand.  */
7005
7006 static bfd_boolean
7007 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7008                                  struct elf_link_hash_entry *h)
7009 {
7010   struct ppc_link_hash_table *htab;
7011   asection *s;
7012
7013   htab = ppc_hash_table (info);
7014   if (htab == NULL)
7015     return FALSE;
7016
7017   /* Deal with function syms.  */
7018   if (h->type == STT_FUNC
7019       || h->type == STT_GNU_IFUNC
7020       || h->needs_plt)
7021     {
7022       /* Clear procedure linkage table information for any symbol that
7023          won't need a .plt entry.  */
7024       struct plt_entry *ent;
7025       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7026         if (ent->plt.refcount > 0)
7027           break;
7028       if (ent == NULL
7029           || (h->type != STT_GNU_IFUNC
7030               && (SYMBOL_CALLS_LOCAL (info, h)
7031                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7032                       && h->root.type == bfd_link_hash_undefweak))))
7033         {
7034           h->plt.plist = NULL;
7035           h->needs_plt = 0;
7036           h->pointer_equality_needed = 0;
7037         }
7038       else if (abiversion (info->output_bfd) == 2)
7039         {
7040           /* Taking a function's address in a read/write section
7041              doesn't require us to define the function symbol in the
7042              executable on a global entry stub.  A dynamic reloc can
7043              be used instead.  */
7044           if (h->pointer_equality_needed
7045               && h->type != STT_GNU_IFUNC
7046               && !readonly_dynrelocs (h))
7047             {
7048               h->pointer_equality_needed = 0;
7049               h->non_got_ref = 0;
7050             }
7051
7052           /* After adjust_dynamic_symbol, non_got_ref set in the
7053              non-shared case means that we have allocated space in
7054              .dynbss for the symbol and thus dyn_relocs for this
7055              symbol should be discarded.
7056              If we get here we know we are making a PLT entry for this
7057              symbol, and in an executable we'd normally resolve
7058              relocations against this symbol to the PLT entry.  Allow
7059              dynamic relocs if the reference is weak, and the dynamic
7060              relocs will not cause text relocation.  */
7061           else if (!h->ref_regular_nonweak
7062                    && h->non_got_ref
7063                    && h->type != STT_GNU_IFUNC
7064                    && !readonly_dynrelocs (h))
7065             h->non_got_ref = 0;
7066
7067           /* If making a plt entry, then we don't need copy relocs.  */
7068           return TRUE;
7069         }
7070     }
7071   else
7072     h->plt.plist = NULL;
7073
7074   /* If this is a weak symbol, and there is a real definition, the
7075      processor independent code will have arranged for us to see the
7076      real definition first, and we can just use the same value.  */
7077   if (h->u.weakdef != NULL)
7078     {
7079       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7080                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
7081       h->root.u.def.section = h->u.weakdef->root.u.def.section;
7082       h->root.u.def.value = h->u.weakdef->root.u.def.value;
7083       if (ELIMINATE_COPY_RELOCS)
7084         h->non_got_ref = h->u.weakdef->non_got_ref;
7085       return TRUE;
7086     }
7087
7088   /* If we are creating a shared library, we must presume that the
7089      only references to the symbol are via the global offset table.
7090      For such cases we need not do anything here; the relocations will
7091      be handled correctly by relocate_section.  */
7092   if (bfd_link_pic (info))
7093     return TRUE;
7094
7095   /* If there are no references to this symbol that do not use the
7096      GOT, we don't need to generate a copy reloc.  */
7097   if (!h->non_got_ref)
7098     return TRUE;
7099
7100   /* Don't generate a copy reloc for symbols defined in the executable.  */
7101   if (!h->def_dynamic || !h->ref_regular || h->def_regular)
7102     return TRUE;
7103
7104   /* If -z nocopyreloc was given, don't generate them either.  */
7105   if (info->nocopyreloc)
7106     {
7107       h->non_got_ref = 0;
7108       return TRUE;
7109     }
7110
7111   /* If we didn't find any dynamic relocs in read-only sections, then
7112      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
7113   if (ELIMINATE_COPY_RELOCS && !readonly_dynrelocs (h))
7114     {
7115       h->non_got_ref = 0;
7116       return TRUE;
7117     }
7118
7119   /* Protected variables do not work with .dynbss.  The copy in
7120      .dynbss won't be used by the shared library with the protected
7121      definition for the variable.  Text relocations are preferable
7122      to an incorrect program.  */
7123   if (h->protected_def)
7124     {
7125       h->non_got_ref = 0;
7126       return TRUE;
7127     }
7128
7129   if (h->plt.plist != NULL)
7130     {
7131       /* We should never get here, but unfortunately there are versions
7132          of gcc out there that improperly (for this ABI) put initialized
7133          function pointers, vtable refs and suchlike in read-only
7134          sections.  Allow them to proceed, but warn that this might
7135          break at runtime.  */
7136       info->callbacks->einfo
7137         (_("%P: copy reloc against `%T' requires lazy plt linking; "
7138            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7139          h->root.root.string);
7140     }
7141
7142   /* This is a reference to a symbol defined by a dynamic object which
7143      is not a function.  */
7144
7145   /* We must allocate the symbol in our .dynbss section, which will
7146      become part of the .bss section of the executable.  There will be
7147      an entry for this symbol in the .dynsym section.  The dynamic
7148      object will contain position independent code, so all references
7149      from the dynamic object to this symbol will go through the global
7150      offset table.  The dynamic linker will use the .dynsym entry to
7151      determine the address it must put in the global offset table, so
7152      both the dynamic object and the regular object will refer to the
7153      same memory location for the variable.  */
7154
7155   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7156      to copy the initial value out of the dynamic object and into the
7157      runtime process image.  We need to remember the offset into the
7158      .rela.bss section we are going to use.  */
7159   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7160     {
7161       htab->relbss->size += sizeof (Elf64_External_Rela);
7162       h->needs_copy = 1;
7163     }
7164
7165   s = htab->dynbss;
7166
7167   return _bfd_elf_adjust_dynamic_copy (info, h, s);
7168 }
7169
7170 /* If given a function descriptor symbol, hide both the function code
7171    sym and the descriptor.  */
7172 static void
7173 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7174                        struct elf_link_hash_entry *h,
7175                        bfd_boolean force_local)
7176 {
7177   struct ppc_link_hash_entry *eh;
7178   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7179
7180   eh = (struct ppc_link_hash_entry *) h;
7181   if (eh->is_func_descriptor)
7182     {
7183       struct ppc_link_hash_entry *fh = eh->oh;
7184
7185       if (fh == NULL)
7186         {
7187           const char *p, *q;
7188           struct ppc_link_hash_table *htab;
7189           char save;
7190
7191           /* We aren't supposed to use alloca in BFD because on
7192              systems which do not have alloca the version in libiberty
7193              calls xmalloc, which might cause the program to crash
7194              when it runs out of memory.  This function doesn't have a
7195              return status, so there's no way to gracefully return an
7196              error.  So cheat.  We know that string[-1] can be safely
7197              accessed;  It's either a string in an ELF string table,
7198              or allocated in an objalloc structure.  */
7199
7200           p = eh->elf.root.root.string - 1;
7201           save = *p;
7202           *(char *) p = '.';
7203           htab = ppc_hash_table (info);
7204           if (htab == NULL)
7205             return;
7206
7207           fh = (struct ppc_link_hash_entry *)
7208             elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7209           *(char *) p = save;
7210
7211           /* Unfortunately, if it so happens that the string we were
7212              looking for was allocated immediately before this string,
7213              then we overwrote the string terminator.  That's the only
7214              reason the lookup should fail.  */
7215           if (fh == NULL)
7216             {
7217               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7218               while (q >= eh->elf.root.root.string && *q == *p)
7219                 --q, --p;
7220               if (q < eh->elf.root.root.string && *p == '.')
7221                 fh = (struct ppc_link_hash_entry *)
7222                   elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7223             }
7224           if (fh != NULL)
7225             {
7226               eh->oh = fh;
7227               fh->oh = eh;
7228             }
7229         }
7230       if (fh != NULL)
7231         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7232     }
7233 }
7234
7235 static bfd_boolean
7236 get_sym_h (struct elf_link_hash_entry **hp,
7237            Elf_Internal_Sym **symp,
7238            asection **symsecp,
7239            unsigned char **tls_maskp,
7240            Elf_Internal_Sym **locsymsp,
7241            unsigned long r_symndx,
7242            bfd *ibfd)
7243 {
7244   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7245
7246   if (r_symndx >= symtab_hdr->sh_info)
7247     {
7248       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7249       struct elf_link_hash_entry *h;
7250
7251       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7252       h = elf_follow_link (h);
7253
7254       if (hp != NULL)
7255         *hp = h;
7256
7257       if (symp != NULL)
7258         *symp = NULL;
7259
7260       if (symsecp != NULL)
7261         {
7262           asection *symsec = NULL;
7263           if (h->root.type == bfd_link_hash_defined
7264               || h->root.type == bfd_link_hash_defweak)
7265             symsec = h->root.u.def.section;
7266           *symsecp = symsec;
7267         }
7268
7269       if (tls_maskp != NULL)
7270         {
7271           struct ppc_link_hash_entry *eh;
7272
7273           eh = (struct ppc_link_hash_entry *) h;
7274           *tls_maskp = &eh->tls_mask;
7275         }
7276     }
7277   else
7278     {
7279       Elf_Internal_Sym *sym;
7280       Elf_Internal_Sym *locsyms = *locsymsp;
7281
7282       if (locsyms == NULL)
7283         {
7284           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7285           if (locsyms == NULL)
7286             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7287                                             symtab_hdr->sh_info,
7288                                             0, NULL, NULL, NULL);
7289           if (locsyms == NULL)
7290             return FALSE;
7291           *locsymsp = locsyms;
7292         }
7293       sym = locsyms + r_symndx;
7294
7295       if (hp != NULL)
7296         *hp = NULL;
7297
7298       if (symp != NULL)
7299         *symp = sym;
7300
7301       if (symsecp != NULL)
7302         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7303
7304       if (tls_maskp != NULL)
7305         {
7306           struct got_entry **lgot_ents;
7307           unsigned char *tls_mask;
7308
7309           tls_mask = NULL;
7310           lgot_ents = elf_local_got_ents (ibfd);
7311           if (lgot_ents != NULL)
7312             {
7313               struct plt_entry **local_plt = (struct plt_entry **)
7314                 (lgot_ents + symtab_hdr->sh_info);
7315               unsigned char *lgot_masks = (unsigned char *)
7316                 (local_plt + symtab_hdr->sh_info);
7317               tls_mask = &lgot_masks[r_symndx];
7318             }
7319           *tls_maskp = tls_mask;
7320         }
7321     }
7322   return TRUE;
7323 }
7324
7325 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
7326    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7327    type suitable for optimization, and 1 otherwise.  */
7328
7329 static int
7330 get_tls_mask (unsigned char **tls_maskp,
7331               unsigned long *toc_symndx,
7332               bfd_vma *toc_addend,
7333               Elf_Internal_Sym **locsymsp,
7334               const Elf_Internal_Rela *rel,
7335               bfd *ibfd)
7336 {
7337   unsigned long r_symndx;
7338   int next_r;
7339   struct elf_link_hash_entry *h;
7340   Elf_Internal_Sym *sym;
7341   asection *sec;
7342   bfd_vma off;
7343
7344   r_symndx = ELF64_R_SYM (rel->r_info);
7345   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7346     return 0;
7347
7348   if ((*tls_maskp != NULL && **tls_maskp != 0)
7349       || sec == NULL
7350       || ppc64_elf_section_data (sec) == NULL
7351       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7352     return 1;
7353
7354   /* Look inside a TOC section too.  */
7355   if (h != NULL)
7356     {
7357       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7358       off = h->root.u.def.value;
7359     }
7360   else
7361     off = sym->st_value;
7362   off += rel->r_addend;
7363   BFD_ASSERT (off % 8 == 0);
7364   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7365   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7366   if (toc_symndx != NULL)
7367     *toc_symndx = r_symndx;
7368   if (toc_addend != NULL)
7369     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7370   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7371     return 0;
7372   if ((h == NULL || is_static_defined (h))
7373       && (next_r == -1 || next_r == -2))
7374     return 1 - next_r;
7375   return 1;
7376 }
7377
7378 /* Find (or create) an entry in the tocsave hash table.  */
7379
7380 static struct tocsave_entry *
7381 tocsave_find (struct ppc_link_hash_table *htab,
7382               enum insert_option insert,
7383               Elf_Internal_Sym **local_syms,
7384               const Elf_Internal_Rela *irela,
7385               bfd *ibfd)
7386 {
7387   unsigned long r_indx;
7388   struct elf_link_hash_entry *h;
7389   Elf_Internal_Sym *sym;
7390   struct tocsave_entry ent, *p;
7391   hashval_t hash;
7392   struct tocsave_entry **slot;
7393
7394   r_indx = ELF64_R_SYM (irela->r_info);
7395   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7396     return NULL;
7397   if (ent.sec == NULL || ent.sec->output_section == NULL)
7398     {
7399       (*_bfd_error_handler)
7400         (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7401       return NULL;
7402     }
7403
7404   if (h != NULL)
7405     ent.offset = h->root.u.def.value;
7406   else
7407     ent.offset = sym->st_value;
7408   ent.offset += irela->r_addend;
7409
7410   hash = tocsave_htab_hash (&ent);
7411   slot = ((struct tocsave_entry **)
7412           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7413   if (slot == NULL)
7414     return NULL;
7415
7416   if (*slot == NULL)
7417     {
7418       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7419       if (p == NULL)
7420         return NULL;
7421       *p = ent;
7422       *slot = p;
7423     }
7424   return *slot;
7425 }
7426
7427 /* Adjust all global syms defined in opd sections.  In gcc generated
7428    code for the old ABI, these will already have been done.  */
7429
7430 static bfd_boolean
7431 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7432 {
7433   struct ppc_link_hash_entry *eh;
7434   asection *sym_sec;
7435   struct _opd_sec_data *opd;
7436
7437   if (h->root.type == bfd_link_hash_indirect)
7438     return TRUE;
7439
7440   if (h->root.type != bfd_link_hash_defined
7441       && h->root.type != bfd_link_hash_defweak)
7442     return TRUE;
7443
7444   eh = (struct ppc_link_hash_entry *) h;
7445   if (eh->adjust_done)
7446     return TRUE;
7447
7448   sym_sec = eh->elf.root.u.def.section;
7449   opd = get_opd_info (sym_sec);
7450   if (opd != NULL && opd->adjust != NULL)
7451     {
7452       long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7453       if (adjust == -1)
7454         {
7455           /* This entry has been deleted.  */
7456           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7457           if (dsec == NULL)
7458             {
7459               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7460                 if (discarded_section (dsec))
7461                   {
7462                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7463                     break;
7464                   }
7465             }
7466           eh->elf.root.u.def.value = 0;
7467           eh->elf.root.u.def.section = dsec;
7468         }
7469       else
7470         eh->elf.root.u.def.value += adjust;
7471       eh->adjust_done = 1;
7472     }
7473   return TRUE;
7474 }
7475
7476 /* Handles decrementing dynamic reloc counts for the reloc specified by
7477    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
7478    have already been determined.  */
7479
7480 static bfd_boolean
7481 dec_dynrel_count (bfd_vma r_info,
7482                   asection *sec,
7483                   struct bfd_link_info *info,
7484                   Elf_Internal_Sym **local_syms,
7485                   struct elf_link_hash_entry *h,
7486                   Elf_Internal_Sym *sym)
7487 {
7488   enum elf_ppc64_reloc_type r_type;
7489   asection *sym_sec = NULL;
7490
7491   /* Can this reloc be dynamic?  This switch, and later tests here
7492      should be kept in sync with the code in check_relocs.  */
7493   r_type = ELF64_R_TYPE (r_info);
7494   switch (r_type)
7495     {
7496     default:
7497       return TRUE;
7498
7499     case R_PPC64_TPREL16:
7500     case R_PPC64_TPREL16_LO:
7501     case R_PPC64_TPREL16_HI:
7502     case R_PPC64_TPREL16_HA:
7503     case R_PPC64_TPREL16_DS:
7504     case R_PPC64_TPREL16_LO_DS:
7505     case R_PPC64_TPREL16_HIGH:
7506     case R_PPC64_TPREL16_HIGHA:
7507     case R_PPC64_TPREL16_HIGHER:
7508     case R_PPC64_TPREL16_HIGHERA:
7509     case R_PPC64_TPREL16_HIGHEST:
7510     case R_PPC64_TPREL16_HIGHESTA:
7511       if (!bfd_link_pic (info))
7512         return TRUE;
7513
7514     case R_PPC64_TPREL64:
7515     case R_PPC64_DTPMOD64:
7516     case R_PPC64_DTPREL64:
7517     case R_PPC64_ADDR64:
7518     case R_PPC64_REL30:
7519     case R_PPC64_REL32:
7520     case R_PPC64_REL64:
7521     case R_PPC64_ADDR14:
7522     case R_PPC64_ADDR14_BRNTAKEN:
7523     case R_PPC64_ADDR14_BRTAKEN:
7524     case R_PPC64_ADDR16:
7525     case R_PPC64_ADDR16_DS:
7526     case R_PPC64_ADDR16_HA:
7527     case R_PPC64_ADDR16_HI:
7528     case R_PPC64_ADDR16_HIGH:
7529     case R_PPC64_ADDR16_HIGHA:
7530     case R_PPC64_ADDR16_HIGHER:
7531     case R_PPC64_ADDR16_HIGHERA:
7532     case R_PPC64_ADDR16_HIGHEST:
7533     case R_PPC64_ADDR16_HIGHESTA:
7534     case R_PPC64_ADDR16_LO:
7535     case R_PPC64_ADDR16_LO_DS:
7536     case R_PPC64_ADDR24:
7537     case R_PPC64_ADDR32:
7538     case R_PPC64_UADDR16:
7539     case R_PPC64_UADDR32:
7540     case R_PPC64_UADDR64:
7541     case R_PPC64_TOC:
7542       break;
7543     }
7544
7545   if (local_syms != NULL)
7546     {
7547       unsigned long r_symndx;
7548       bfd *ibfd = sec->owner;
7549
7550       r_symndx = ELF64_R_SYM (r_info);
7551       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7552         return FALSE;
7553     }
7554
7555   if ((bfd_link_pic (info)
7556        && (must_be_dyn_reloc (info, r_type)
7557            || (h != NULL
7558                && (!SYMBOLIC_BIND (info, h)
7559                    || h->root.type == bfd_link_hash_defweak
7560                    || !h->def_regular))))
7561       || (ELIMINATE_COPY_RELOCS
7562           && !bfd_link_pic (info)
7563           && h != NULL
7564           && (h->root.type == bfd_link_hash_defweak
7565               || !h->def_regular)))
7566     ;
7567   else
7568     return TRUE;
7569
7570   if (h != NULL)
7571     {
7572       struct elf_dyn_relocs *p;
7573       struct elf_dyn_relocs **pp;
7574       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7575
7576       /* elf_gc_sweep may have already removed all dyn relocs associated
7577          with local syms for a given section.  Also, symbol flags are
7578          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
7579          report a dynreloc miscount.  */
7580       if (*pp == NULL && info->gc_sections)
7581         return TRUE;
7582
7583       while ((p = *pp) != NULL)
7584         {
7585           if (p->sec == sec)
7586             {
7587               if (!must_be_dyn_reloc (info, r_type))
7588                 p->pc_count -= 1;
7589               p->count -= 1;
7590               if (p->count == 0)
7591                 *pp = p->next;
7592               return TRUE;
7593             }
7594           pp = &p->next;
7595         }
7596     }
7597   else
7598     {
7599       struct ppc_dyn_relocs *p;
7600       struct ppc_dyn_relocs **pp;
7601       void *vpp;
7602       bfd_boolean is_ifunc;
7603
7604       if (local_syms == NULL)
7605         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7606       if (sym_sec == NULL)
7607         sym_sec = sec;
7608
7609       vpp = &elf_section_data (sym_sec)->local_dynrel;
7610       pp = (struct ppc_dyn_relocs **) vpp;
7611
7612       if (*pp == NULL && info->gc_sections)
7613         return TRUE;
7614
7615       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7616       while ((p = *pp) != NULL)
7617         {
7618           if (p->sec == sec && p->ifunc == is_ifunc)
7619             {
7620               p->count -= 1;
7621               if (p->count == 0)
7622                 *pp = p->next;
7623               return TRUE;
7624             }
7625           pp = &p->next;
7626         }
7627     }
7628
7629   info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7630                           sec->owner, sec);
7631   bfd_set_error (bfd_error_bad_value);
7632   return FALSE;
7633 }
7634
7635 /* Remove unused Official Procedure Descriptor entries.  Currently we
7636    only remove those associated with functions in discarded link-once
7637    sections, or weakly defined functions that have been overridden.  It
7638    would be possible to remove many more entries for statically linked
7639    applications.  */
7640
7641 bfd_boolean
7642 ppc64_elf_edit_opd (struct bfd_link_info *info)
7643 {
7644   bfd *ibfd;
7645   bfd_boolean some_edited = FALSE;
7646   asection *need_pad = NULL;
7647   struct ppc_link_hash_table *htab;
7648
7649   htab = ppc_hash_table (info);
7650   if (htab == NULL)
7651     return FALSE;
7652
7653   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7654     {
7655       asection *sec;
7656       Elf_Internal_Rela *relstart, *rel, *relend;
7657       Elf_Internal_Shdr *symtab_hdr;
7658       Elf_Internal_Sym *local_syms;
7659       struct _opd_sec_data *opd;
7660       bfd_boolean need_edit, add_aux_fields, broken;
7661       bfd_size_type cnt_16b = 0;
7662
7663       if (!is_ppc64_elf (ibfd))
7664         continue;
7665
7666       sec = bfd_get_section_by_name (ibfd, ".opd");
7667       if (sec == NULL || sec->size == 0)
7668         continue;
7669
7670       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7671         continue;
7672
7673       if (sec->output_section == bfd_abs_section_ptr)
7674         continue;
7675
7676       /* Look through the section relocs.  */
7677       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7678         continue;
7679
7680       local_syms = NULL;
7681       symtab_hdr = &elf_symtab_hdr (ibfd);
7682
7683       /* Read the relocations.  */
7684       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7685                                             info->keep_memory);
7686       if (relstart == NULL)
7687         return FALSE;
7688
7689       /* First run through the relocs to check they are sane, and to
7690          determine whether we need to edit this opd section.  */
7691       need_edit = FALSE;
7692       broken = FALSE;
7693       need_pad = sec;
7694       relend = relstart + sec->reloc_count;
7695       for (rel = relstart; rel < relend; )
7696         {
7697           enum elf_ppc64_reloc_type r_type;
7698           unsigned long r_symndx;
7699           asection *sym_sec;
7700           struct elf_link_hash_entry *h;
7701           Elf_Internal_Sym *sym;
7702           bfd_vma offset;
7703
7704           /* .opd contains an array of 16 or 24 byte entries.  We're
7705              only interested in the reloc pointing to a function entry
7706              point.  */
7707           offset = rel->r_offset;
7708           if (rel + 1 == relend
7709               || rel[1].r_offset != offset + 8)
7710             {
7711               /* If someone messes with .opd alignment then after a
7712                  "ld -r" we might have padding in the middle of .opd.
7713                  Also, there's nothing to prevent someone putting
7714                  something silly in .opd with the assembler.  No .opd
7715                  optimization for them!  */
7716             broken_opd:
7717               (*_bfd_error_handler)
7718                 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7719               broken = TRUE;
7720               break;
7721             }
7722
7723           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7724               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7725             {
7726               (*_bfd_error_handler)
7727                 (_("%B: unexpected reloc type %u in .opd section"),
7728                  ibfd, r_type);
7729               broken = TRUE;
7730               break;
7731             }
7732
7733           r_symndx = ELF64_R_SYM (rel->r_info);
7734           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7735                           r_symndx, ibfd))
7736             goto error_ret;
7737
7738           if (sym_sec == NULL || sym_sec->owner == NULL)
7739             {
7740               const char *sym_name;
7741               if (h != NULL)
7742                 sym_name = h->root.root.string;
7743               else
7744                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7745                                              sym_sec);
7746
7747               (*_bfd_error_handler)
7748                 (_("%B: undefined sym `%s' in .opd section"),
7749                  ibfd, sym_name);
7750               broken = TRUE;
7751               break;
7752             }
7753
7754           /* opd entries are always for functions defined in the
7755              current input bfd.  If the symbol isn't defined in the
7756              input bfd, then we won't be using the function in this
7757              bfd;  It must be defined in a linkonce section in another
7758              bfd, or is weak.  It's also possible that we are
7759              discarding the function due to a linker script /DISCARD/,
7760              which we test for via the output_section.  */
7761           if (sym_sec->owner != ibfd
7762               || sym_sec->output_section == bfd_abs_section_ptr)
7763             need_edit = TRUE;
7764
7765           rel += 2;
7766           if (rel + 1 == relend
7767               || (rel + 2 < relend
7768                   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7769             ++rel;
7770
7771           if (rel == relend)
7772             {
7773               if (sec->size == offset + 24)
7774                 {
7775                   need_pad = NULL;
7776                   break;
7777                 }
7778               if (sec->size == offset + 16)
7779                 {
7780                   cnt_16b++;
7781                   break;
7782                 }
7783               goto broken_opd;
7784             }
7785           else if (rel + 1 < relend
7786                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7787                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7788             {
7789               if (rel[0].r_offset == offset + 16)
7790                 cnt_16b++;
7791               else if (rel[0].r_offset != offset + 24)
7792                 goto broken_opd;
7793             }
7794           else
7795             goto broken_opd;
7796         }
7797
7798       add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7799
7800       if (!broken && (need_edit || add_aux_fields))
7801         {
7802           Elf_Internal_Rela *write_rel;
7803           Elf_Internal_Shdr *rel_hdr;
7804           bfd_byte *rptr, *wptr;
7805           bfd_byte *new_contents;
7806           bfd_size_type amt;
7807
7808           new_contents = NULL;
7809           amt = OPD_NDX (sec->size) * sizeof (long);
7810           opd = &ppc64_elf_section_data (sec)->u.opd;
7811           opd->adjust = bfd_zalloc (sec->owner, amt);
7812           if (opd->adjust == NULL)
7813             return FALSE;
7814           ppc64_elf_section_data (sec)->sec_type = sec_opd;
7815
7816           /* This seems a waste of time as input .opd sections are all
7817              zeros as generated by gcc, but I suppose there's no reason
7818              this will always be so.  We might start putting something in
7819              the third word of .opd entries.  */
7820           if ((sec->flags & SEC_IN_MEMORY) == 0)
7821             {
7822               bfd_byte *loc;
7823               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7824                 {
7825                   if (loc != NULL)
7826                     free (loc);
7827                 error_ret:
7828                   if (local_syms != NULL
7829                       && symtab_hdr->contents != (unsigned char *) local_syms)
7830                     free (local_syms);
7831                   if (elf_section_data (sec)->relocs != relstart)
7832                     free (relstart);
7833                   return FALSE;
7834                 }
7835               sec->contents = loc;
7836               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7837             }
7838
7839           elf_section_data (sec)->relocs = relstart;
7840
7841           new_contents = sec->contents;
7842           if (add_aux_fields)
7843             {
7844               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7845               if (new_contents == NULL)
7846                 return FALSE;
7847               need_pad = NULL;
7848             }
7849           wptr = new_contents;
7850           rptr = sec->contents;
7851           write_rel = relstart;
7852           for (rel = relstart; rel < relend; )
7853             {
7854               unsigned long r_symndx;
7855               asection *sym_sec;
7856               struct elf_link_hash_entry *h;
7857               struct ppc_link_hash_entry *fdh = NULL;
7858               Elf_Internal_Sym *sym;
7859               long opd_ent_size;
7860               Elf_Internal_Rela *next_rel;
7861               bfd_boolean skip;
7862
7863               r_symndx = ELF64_R_SYM (rel->r_info);
7864               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7865                               r_symndx, ibfd))
7866                 goto error_ret;
7867
7868               next_rel = rel + 2;
7869               if (next_rel + 1 == relend
7870                   || (next_rel + 2 < relend
7871                       && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7872                 ++next_rel;
7873
7874               /* See if the .opd entry is full 24 byte or
7875                  16 byte (with fd_aux entry overlapped with next
7876                  fd_func).  */
7877               opd_ent_size = 24;
7878               if (next_rel == relend)
7879                 {
7880                   if (sec->size == rel->r_offset + 16)
7881                     opd_ent_size = 16;
7882                 }
7883               else if (next_rel->r_offset == rel->r_offset + 16)
7884                 opd_ent_size = 16;
7885
7886               if (h != NULL
7887                   && h->root.root.string[0] == '.')
7888                 {
7889                   fdh = lookup_fdh ((struct ppc_link_hash_entry *) h, htab);
7890                   if (fdh != NULL
7891                       && fdh->elf.root.type != bfd_link_hash_defined
7892                       && fdh->elf.root.type != bfd_link_hash_defweak)
7893                     fdh = NULL;
7894                 }
7895
7896               skip = (sym_sec->owner != ibfd
7897                       || sym_sec->output_section == bfd_abs_section_ptr);
7898               if (skip)
7899                 {
7900                   if (fdh != NULL && sym_sec->owner == ibfd)
7901                     {
7902                       /* Arrange for the function descriptor sym
7903                          to be dropped.  */
7904                       fdh->elf.root.u.def.value = 0;
7905                       fdh->elf.root.u.def.section = sym_sec;
7906                     }
7907                   opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7908
7909                   if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7910                     rel = next_rel;
7911                   else
7912                     while (1)
7913                       {
7914                         if (!dec_dynrel_count (rel->r_info, sec, info,
7915                                                NULL, h, sym))
7916                           goto error_ret;
7917
7918                         if (++rel == next_rel)
7919                           break;
7920
7921                         r_symndx = ELF64_R_SYM (rel->r_info);
7922                         if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7923                                         r_symndx, ibfd))
7924                           goto error_ret;
7925                       }
7926                 }
7927               else
7928                 {
7929                   /* We'll be keeping this opd entry.  */
7930                   long adjust;
7931
7932                   if (fdh != NULL)
7933                     {
7934                       /* Redefine the function descriptor symbol to
7935                          this location in the opd section.  It is
7936                          necessary to update the value here rather
7937                          than using an array of adjustments as we do
7938                          for local symbols, because various places
7939                          in the generic ELF code use the value
7940                          stored in u.def.value.  */
7941                       fdh->elf.root.u.def.value = wptr - new_contents;
7942                       fdh->adjust_done = 1;
7943                     }
7944
7945                   /* Local syms are a bit tricky.  We could
7946                      tweak them as they can be cached, but
7947                      we'd need to look through the local syms
7948                      for the function descriptor sym which we
7949                      don't have at the moment.  So keep an
7950                      array of adjustments.  */
7951                   adjust = (wptr - new_contents) - (rptr - sec->contents);
7952                   opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7953
7954                   if (wptr != rptr)
7955                     memcpy (wptr, rptr, opd_ent_size);
7956                   wptr += opd_ent_size;
7957                   if (add_aux_fields && opd_ent_size == 16)
7958                     {
7959                       memset (wptr, '\0', 8);
7960                       wptr += 8;
7961                     }
7962
7963                   /* We need to adjust any reloc offsets to point to the
7964                      new opd entries.  */
7965                   for ( ; rel != next_rel; ++rel)
7966                     {
7967                       rel->r_offset += adjust;
7968                       if (write_rel != rel)
7969                         memcpy (write_rel, rel, sizeof (*rel));
7970                       ++write_rel;
7971                     }
7972                 }
7973
7974               rptr += opd_ent_size;
7975             }
7976
7977           sec->size = wptr - new_contents;
7978           sec->reloc_count = write_rel - relstart;
7979           if (add_aux_fields)
7980             {
7981               free (sec->contents);
7982               sec->contents = new_contents;
7983             }
7984
7985           /* Fudge the header size too, as this is used later in
7986              elf_bfd_final_link if we are emitting relocs.  */
7987           rel_hdr = _bfd_elf_single_rel_hdr (sec);
7988           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7989           some_edited = TRUE;
7990         }
7991       else if (elf_section_data (sec)->relocs != relstart)
7992         free (relstart);
7993
7994       if (local_syms != NULL
7995           && symtab_hdr->contents != (unsigned char *) local_syms)
7996         {
7997           if (!info->keep_memory)
7998             free (local_syms);
7999           else
8000             symtab_hdr->contents = (unsigned char *) local_syms;
8001         }
8002     }
8003
8004   if (some_edited)
8005     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8006
8007   /* If we are doing a final link and the last .opd entry is just 16 byte
8008      long, add a 8 byte padding after it.  */
8009   if (need_pad != NULL && !bfd_link_relocatable (info))
8010     {
8011       bfd_byte *p;
8012
8013       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8014         {
8015           BFD_ASSERT (need_pad->size > 0);
8016
8017           p = bfd_malloc (need_pad->size + 8);
8018           if (p == NULL)
8019             return FALSE;
8020
8021           if (! bfd_get_section_contents (need_pad->owner, need_pad,
8022                                           p, 0, need_pad->size))
8023             return FALSE;
8024
8025           need_pad->contents = p;
8026           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8027         }
8028       else
8029         {
8030           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8031           if (p == NULL)
8032             return FALSE;
8033
8034           need_pad->contents = p;
8035         }
8036
8037       memset (need_pad->contents + need_pad->size, 0, 8);
8038       need_pad->size += 8;
8039     }
8040
8041   return TRUE;
8042 }
8043
8044 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
8045
8046 asection *
8047 ppc64_elf_tls_setup (struct bfd_link_info *info)
8048 {
8049   struct ppc_link_hash_table *htab;
8050
8051   htab = ppc_hash_table (info);
8052   if (htab == NULL)
8053     return NULL;
8054
8055   if (abiversion (info->output_bfd) == 1)
8056     htab->opd_abi = 1;
8057
8058   if (htab->params->no_multi_toc)
8059     htab->do_multi_toc = 0;
8060   else if (!htab->do_multi_toc)
8061     htab->params->no_multi_toc = 1;
8062
8063   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8064                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8065                                               FALSE, FALSE, TRUE));
8066   /* Move dynamic linking info to the function descriptor sym.  */
8067   if (htab->tls_get_addr != NULL)
8068     func_desc_adjust (&htab->tls_get_addr->elf, info);
8069   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8070                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8071                                                  FALSE, FALSE, TRUE));
8072   if (!htab->params->no_tls_get_addr_opt)
8073     {
8074       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8075
8076       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8077                                   FALSE, FALSE, TRUE);
8078       if (opt != NULL)
8079         func_desc_adjust (opt, info);
8080       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8081                                      FALSE, FALSE, TRUE);
8082       if (opt_fd != NULL
8083           && (opt_fd->root.type == bfd_link_hash_defined
8084               || opt_fd->root.type == bfd_link_hash_defweak))
8085         {
8086           /* If glibc supports an optimized __tls_get_addr call stub,
8087              signalled by the presence of __tls_get_addr_opt, and we'll
8088              be calling __tls_get_addr via a plt call stub, then
8089              make __tls_get_addr point to __tls_get_addr_opt.  */
8090           tga_fd = &htab->tls_get_addr_fd->elf;
8091           if (htab->elf.dynamic_sections_created
8092               && tga_fd != NULL
8093               && (tga_fd->type == STT_FUNC
8094                   || tga_fd->needs_plt)
8095               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8096                    || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
8097                        && tga_fd->root.type == bfd_link_hash_undefweak)))
8098             {
8099               struct plt_entry *ent;
8100
8101               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8102                 if (ent->plt.refcount > 0)
8103                   break;
8104               if (ent != NULL)
8105                 {
8106                   tga_fd->root.type = bfd_link_hash_indirect;
8107                   tga_fd->root.u.i.link = &opt_fd->root;
8108                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8109                   if (opt_fd->dynindx != -1)
8110                     {
8111                       /* Use __tls_get_addr_opt in dynamic relocations.  */
8112                       opt_fd->dynindx = -1;
8113                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8114                                               opt_fd->dynstr_index);
8115                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8116                         return NULL;
8117                     }
8118                   htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8119                   tga = &htab->tls_get_addr->elf;
8120                   if (opt != NULL && tga != NULL)
8121                     {
8122                       tga->root.type = bfd_link_hash_indirect;
8123                       tga->root.u.i.link = &opt->root;
8124                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
8125                       _bfd_elf_link_hash_hide_symbol (info, opt,
8126                                                       tga->forced_local);
8127                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8128                     }
8129                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8130                   htab->tls_get_addr_fd->is_func_descriptor = 1;
8131                   if (htab->tls_get_addr != NULL)
8132                     {
8133                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8134                       htab->tls_get_addr->is_func = 1;
8135                     }
8136                 }
8137             }
8138         }
8139       else
8140         htab->params->no_tls_get_addr_opt = TRUE;
8141     }
8142   return _bfd_elf_tls_setup (info->output_bfd, info);
8143 }
8144
8145 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8146    HASH1 or HASH2.  */
8147
8148 static bfd_boolean
8149 branch_reloc_hash_match (const bfd *ibfd,
8150                          const Elf_Internal_Rela *rel,
8151                          const struct ppc_link_hash_entry *hash1,
8152                          const struct ppc_link_hash_entry *hash2)
8153 {
8154   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8155   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8156   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8157
8158   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8159     {
8160       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8161       struct elf_link_hash_entry *h;
8162
8163       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8164       h = elf_follow_link (h);
8165       if (h == &hash1->elf || h == &hash2->elf)
8166         return TRUE;
8167     }
8168   return FALSE;
8169 }
8170
8171 /* Run through all the TLS relocs looking for optimization
8172    opportunities.  The linker has been hacked (see ppc64elf.em) to do
8173    a preliminary section layout so that we know the TLS segment
8174    offsets.  We can't optimize earlier because some optimizations need
8175    to know the tp offset, and we need to optimize before allocating
8176    dynamic relocations.  */
8177
8178 bfd_boolean
8179 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8180 {
8181   bfd *ibfd;
8182   asection *sec;
8183   struct ppc_link_hash_table *htab;
8184   unsigned char *toc_ref;
8185   int pass;
8186
8187   if (!bfd_link_executable (info))
8188     return TRUE;
8189
8190   htab = ppc_hash_table (info);
8191   if (htab == NULL)
8192     return FALSE;
8193
8194   /* Make two passes over the relocs.  On the first pass, mark toc
8195      entries involved with tls relocs, and check that tls relocs
8196      involved in setting up a tls_get_addr call are indeed followed by
8197      such a call.  If they are not, we can't do any tls optimization.
8198      On the second pass twiddle tls_mask flags to notify
8199      relocate_section that optimization can be done, and adjust got
8200      and plt refcounts.  */
8201   toc_ref = NULL;
8202   for (pass = 0; pass < 2; ++pass)
8203     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8204       {
8205         Elf_Internal_Sym *locsyms = NULL;
8206         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8207
8208         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8209           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8210             {
8211               Elf_Internal_Rela *relstart, *rel, *relend;
8212               bfd_boolean found_tls_get_addr_arg = 0;
8213
8214               /* Read the relocations.  */
8215               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8216                                                     info->keep_memory);
8217               if (relstart == NULL)
8218                 {
8219                   free (toc_ref);
8220                   return FALSE;
8221                 }
8222
8223               relend = relstart + sec->reloc_count;
8224               for (rel = relstart; rel < relend; rel++)
8225                 {
8226                   enum elf_ppc64_reloc_type r_type;
8227                   unsigned long r_symndx;
8228                   struct elf_link_hash_entry *h;
8229                   Elf_Internal_Sym *sym;
8230                   asection *sym_sec;
8231                   unsigned char *tls_mask;
8232                   unsigned char tls_set, tls_clear, tls_type = 0;
8233                   bfd_vma value;
8234                   bfd_boolean ok_tprel, is_local;
8235                   long toc_ref_index = 0;
8236                   int expecting_tls_get_addr = 0;
8237                   bfd_boolean ret = FALSE;
8238
8239                   r_symndx = ELF64_R_SYM (rel->r_info);
8240                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8241                                   r_symndx, ibfd))
8242                     {
8243                     err_free_rel:
8244                       if (elf_section_data (sec)->relocs != relstart)
8245                         free (relstart);
8246                       if (toc_ref != NULL)
8247                         free (toc_ref);
8248                       if (locsyms != NULL
8249                           && (elf_symtab_hdr (ibfd).contents
8250                               != (unsigned char *) locsyms))
8251                         free (locsyms);
8252                       return ret;
8253                     }
8254
8255                   if (h != NULL)
8256                     {
8257                       if (h->root.type == bfd_link_hash_defined
8258                           || h->root.type == bfd_link_hash_defweak)
8259                         value = h->root.u.def.value;
8260                       else if (h->root.type == bfd_link_hash_undefweak)
8261                         value = 0;
8262                       else
8263                         {
8264                           found_tls_get_addr_arg = 0;
8265                           continue;
8266                         }
8267                     }
8268                   else
8269                     /* Symbols referenced by TLS relocs must be of type
8270                        STT_TLS.  So no need for .opd local sym adjust.  */
8271                     value = sym->st_value;
8272
8273                   ok_tprel = FALSE;
8274                   is_local = FALSE;
8275                   if (h == NULL
8276                       || !h->def_dynamic)
8277                     {
8278                       is_local = TRUE;
8279                       if (h != NULL
8280                           && h->root.type == bfd_link_hash_undefweak)
8281                         ok_tprel = TRUE;
8282                       else
8283                         {
8284                           value += sym_sec->output_offset;
8285                           value += sym_sec->output_section->vma;
8286                           value -= htab->elf.tls_sec->vma;
8287                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8288                                       < (bfd_vma) 1 << 32);
8289                         }
8290                     }
8291
8292                   r_type = ELF64_R_TYPE (rel->r_info);
8293                   /* If this section has old-style __tls_get_addr calls
8294                      without marker relocs, then check that each
8295                      __tls_get_addr call reloc is preceded by a reloc
8296                      that conceivably belongs to the __tls_get_addr arg
8297                      setup insn.  If we don't find matching arg setup
8298                      relocs, don't do any tls optimization.  */
8299                   if (pass == 0
8300                       && sec->has_tls_get_addr_call
8301                       && h != NULL
8302                       && (h == &htab->tls_get_addr->elf
8303                           || h == &htab->tls_get_addr_fd->elf)
8304                       && !found_tls_get_addr_arg
8305                       && is_branch_reloc (r_type))
8306                     {
8307                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8308                                                 "TLS optimization disabled\n"),
8309                                               ibfd, sec, rel->r_offset);
8310                       ret = TRUE;
8311                       goto err_free_rel;
8312                     }
8313
8314                   found_tls_get_addr_arg = 0;
8315                   switch (r_type)
8316                     {
8317                     case R_PPC64_GOT_TLSLD16:
8318                     case R_PPC64_GOT_TLSLD16_LO:
8319                       expecting_tls_get_addr = 1;
8320                       found_tls_get_addr_arg = 1;
8321                       /* Fall thru */
8322
8323                     case R_PPC64_GOT_TLSLD16_HI:
8324                     case R_PPC64_GOT_TLSLD16_HA:
8325                       /* These relocs should never be against a symbol
8326                          defined in a shared lib.  Leave them alone if
8327                          that turns out to be the case.  */
8328                       if (!is_local)
8329                         continue;
8330
8331                       /* LD -> LE */
8332                       tls_set = 0;
8333                       tls_clear = TLS_LD;
8334                       tls_type = TLS_TLS | TLS_LD;
8335                       break;
8336
8337                     case R_PPC64_GOT_TLSGD16:
8338                     case R_PPC64_GOT_TLSGD16_LO:
8339                       expecting_tls_get_addr = 1;
8340                       found_tls_get_addr_arg = 1;
8341                       /* Fall thru */
8342
8343                     case R_PPC64_GOT_TLSGD16_HI:
8344                     case R_PPC64_GOT_TLSGD16_HA:
8345                       if (ok_tprel)
8346                         /* GD -> LE */
8347                         tls_set = 0;
8348                       else
8349                         /* GD -> IE */
8350                         tls_set = TLS_TLS | TLS_TPRELGD;
8351                       tls_clear = TLS_GD;
8352                       tls_type = TLS_TLS | TLS_GD;
8353                       break;
8354
8355                     case R_PPC64_GOT_TPREL16_DS:
8356                     case R_PPC64_GOT_TPREL16_LO_DS:
8357                     case R_PPC64_GOT_TPREL16_HI:
8358                     case R_PPC64_GOT_TPREL16_HA:
8359                       if (ok_tprel)
8360                         {
8361                           /* IE -> LE */
8362                           tls_set = 0;
8363                           tls_clear = TLS_TPREL;
8364                           tls_type = TLS_TLS | TLS_TPREL;
8365                           break;
8366                         }
8367                       continue;
8368
8369                     case R_PPC64_TLSGD:
8370                     case R_PPC64_TLSLD:
8371                       found_tls_get_addr_arg = 1;
8372                       /* Fall thru */
8373
8374                     case R_PPC64_TLS:
8375                     case R_PPC64_TOC16:
8376                     case R_PPC64_TOC16_LO:
8377                       if (sym_sec == NULL || sym_sec != toc)
8378                         continue;
8379
8380                       /* Mark this toc entry as referenced by a TLS
8381                          code sequence.  We can do that now in the
8382                          case of R_PPC64_TLS, and after checking for
8383                          tls_get_addr for the TOC16 relocs.  */
8384                       if (toc_ref == NULL)
8385                         toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8386                       if (toc_ref == NULL)
8387                         goto err_free_rel;
8388
8389                       if (h != NULL)
8390                         value = h->root.u.def.value;
8391                       else
8392                         value = sym->st_value;
8393                       value += rel->r_addend;
8394                       if (value % 8 != 0)
8395                         continue;
8396                       BFD_ASSERT (value < toc->size
8397                                   && toc->output_offset % 8 == 0);
8398                       toc_ref_index = (value + toc->output_offset) / 8;
8399                       if (r_type == R_PPC64_TLS
8400                           || r_type == R_PPC64_TLSGD
8401                           || r_type == R_PPC64_TLSLD)
8402                         {
8403                           toc_ref[toc_ref_index] = 1;
8404                           continue;
8405                         }
8406
8407                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
8408                         continue;
8409
8410                       tls_set = 0;
8411                       tls_clear = 0;
8412                       expecting_tls_get_addr = 2;
8413                       break;
8414
8415                     case R_PPC64_TPREL64:
8416                       if (pass == 0
8417                           || sec != toc
8418                           || toc_ref == NULL
8419                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8420                         continue;
8421                       if (ok_tprel)
8422                         {
8423                           /* IE -> LE */
8424                           tls_set = TLS_EXPLICIT;
8425                           tls_clear = TLS_TPREL;
8426                           break;
8427                         }
8428                       continue;
8429
8430                     case R_PPC64_DTPMOD64:
8431                       if (pass == 0
8432                           || sec != toc
8433                           || toc_ref == NULL
8434                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8435                         continue;
8436                       if (rel + 1 < relend
8437                           && (rel[1].r_info
8438                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8439                           && rel[1].r_offset == rel->r_offset + 8)
8440                         {
8441                           if (ok_tprel)
8442                             /* GD -> LE */
8443                             tls_set = TLS_EXPLICIT | TLS_GD;
8444                           else
8445                             /* GD -> IE */
8446                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8447                           tls_clear = TLS_GD;
8448                         }
8449                       else
8450                         {
8451                           if (!is_local)
8452                             continue;
8453
8454                           /* LD -> LE */
8455                           tls_set = TLS_EXPLICIT;
8456                           tls_clear = TLS_LD;
8457                         }
8458                       break;
8459
8460                     default:
8461                       continue;
8462                     }
8463
8464                   if (pass == 0)
8465                     {
8466                       if (!expecting_tls_get_addr
8467                           || !sec->has_tls_get_addr_call)
8468                         continue;
8469
8470                       if (rel + 1 < relend
8471                           && branch_reloc_hash_match (ibfd, rel + 1,
8472                                                       htab->tls_get_addr,
8473                                                       htab->tls_get_addr_fd))
8474                         {
8475                           if (expecting_tls_get_addr == 2)
8476                             {
8477                               /* Check for toc tls entries.  */
8478                               unsigned char *toc_tls;
8479                               int retval;
8480
8481                               retval = get_tls_mask (&toc_tls, NULL, NULL,
8482                                                      &locsyms,
8483                                                      rel, ibfd);
8484                               if (retval == 0)
8485                                 goto err_free_rel;
8486                               if (toc_tls != NULL)
8487                                 {
8488                                   if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8489                                     found_tls_get_addr_arg = 1;
8490                                   if (retval > 1)
8491                                     toc_ref[toc_ref_index] = 1;
8492                                 }
8493                             }
8494                           continue;
8495                         }
8496
8497                       if (expecting_tls_get_addr != 1)
8498                         continue;
8499
8500                       /* Uh oh, we didn't find the expected call.  We
8501                          could just mark this symbol to exclude it
8502                          from tls optimization but it's safer to skip
8503                          the entire optimization.  */
8504                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8505                                                 "TLS optimization disabled\n"),
8506                                               ibfd, sec, rel->r_offset);
8507                       ret = TRUE;
8508                       goto err_free_rel;
8509                     }
8510
8511                   if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8512                     {
8513                       struct plt_entry *ent;
8514                       for (ent = htab->tls_get_addr->elf.plt.plist;
8515                            ent != NULL;
8516                            ent = ent->next)
8517                         if (ent->addend == 0)
8518                           {
8519                             if (ent->plt.refcount > 0)
8520                               {
8521                                 ent->plt.refcount -= 1;
8522                                 expecting_tls_get_addr = 0;
8523                               }
8524                             break;
8525                           }
8526                     }
8527
8528                   if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8529                     {
8530                       struct plt_entry *ent;
8531                       for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8532                            ent != NULL;
8533                            ent = ent->next)
8534                         if (ent->addend == 0)
8535                           {
8536                             if (ent->plt.refcount > 0)
8537                               ent->plt.refcount -= 1;
8538                             break;
8539                           }
8540                     }
8541
8542                   if (tls_clear == 0)
8543                     continue;
8544
8545                   if ((tls_set & TLS_EXPLICIT) == 0)
8546                     {
8547                       struct got_entry *ent;
8548
8549                       /* Adjust got entry for this reloc.  */
8550                       if (h != NULL)
8551                         ent = h->got.glist;
8552                       else
8553                         ent = elf_local_got_ents (ibfd)[r_symndx];
8554
8555                       for (; ent != NULL; ent = ent->next)
8556                         if (ent->addend == rel->r_addend
8557                             && ent->owner == ibfd
8558                             && ent->tls_type == tls_type)
8559                           break;
8560                       if (ent == NULL)
8561                         abort ();
8562
8563                       if (tls_set == 0)
8564                         {
8565                           /* We managed to get rid of a got entry.  */
8566                           if (ent->got.refcount > 0)
8567                             ent->got.refcount -= 1;
8568                         }
8569                     }
8570                   else
8571                     {
8572                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
8573                          we'll lose one or two dyn relocs.  */
8574                       if (!dec_dynrel_count (rel->r_info, sec, info,
8575                                              NULL, h, sym))
8576                         return FALSE;
8577
8578                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
8579                         {
8580                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8581                                                  NULL, h, sym))
8582                             return FALSE;
8583                         }
8584                     }
8585
8586                   *tls_mask |= tls_set;
8587                   *tls_mask &= ~tls_clear;
8588                 }
8589
8590               if (elf_section_data (sec)->relocs != relstart)
8591                 free (relstart);
8592             }
8593
8594         if (locsyms != NULL
8595             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8596           {
8597             if (!info->keep_memory)
8598               free (locsyms);
8599             else
8600               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8601           }
8602       }
8603
8604   if (toc_ref != NULL)
8605     free (toc_ref);
8606   return TRUE;
8607 }
8608
8609 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8610    the values of any global symbols in a toc section that has been
8611    edited.  Globals in toc sections should be a rarity, so this function
8612    sets a flag if any are found in toc sections other than the one just
8613    edited, so that futher hash table traversals can be avoided.  */
8614
8615 struct adjust_toc_info
8616 {
8617   asection *toc;
8618   unsigned long *skip;
8619   bfd_boolean global_toc_syms;
8620 };
8621
8622 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8623
8624 static bfd_boolean
8625 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8626 {
8627   struct ppc_link_hash_entry *eh;
8628   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8629   unsigned long i;
8630
8631   if (h->root.type != bfd_link_hash_defined
8632       && h->root.type != bfd_link_hash_defweak)
8633     return TRUE;
8634
8635   eh = (struct ppc_link_hash_entry *) h;
8636   if (eh->adjust_done)
8637     return TRUE;
8638
8639   if (eh->elf.root.u.def.section == toc_inf->toc)
8640     {
8641       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8642         i = toc_inf->toc->rawsize >> 3;
8643       else
8644         i = eh->elf.root.u.def.value >> 3;
8645
8646       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8647         {
8648           (*_bfd_error_handler)
8649             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8650           do
8651             ++i;
8652           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8653           eh->elf.root.u.def.value = (bfd_vma) i << 3;
8654         }
8655
8656       eh->elf.root.u.def.value -= toc_inf->skip[i];
8657       eh->adjust_done = 1;
8658     }
8659   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8660     toc_inf->global_toc_syms = TRUE;
8661
8662   return TRUE;
8663 }
8664
8665 /* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc.  */
8666
8667 static bfd_boolean
8668 ok_lo_toc_insn (unsigned int insn)
8669 {
8670   return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8671           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8672           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8673           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8674           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8675           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8676           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8677           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8678           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8679           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8680           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8681           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8682           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8683           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8684           || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8685               && (insn & 3) != 1)
8686           || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8687               && ((insn & 3) == 0 || (insn & 3) == 3))
8688           || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8689 }
8690
8691 /* Examine all relocs referencing .toc sections in order to remove
8692    unused .toc entries.  */
8693
8694 bfd_boolean
8695 ppc64_elf_edit_toc (struct bfd_link_info *info)
8696 {
8697   bfd *ibfd;
8698   struct adjust_toc_info toc_inf;
8699   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8700
8701   htab->do_toc_opt = 1;
8702   toc_inf.global_toc_syms = TRUE;
8703   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8704     {
8705       asection *toc, *sec;
8706       Elf_Internal_Shdr *symtab_hdr;
8707       Elf_Internal_Sym *local_syms;
8708       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8709       unsigned long *skip, *drop;
8710       unsigned char *used;
8711       unsigned char *keep, last, some_unused;
8712
8713       if (!is_ppc64_elf (ibfd))
8714         continue;
8715
8716       toc = bfd_get_section_by_name (ibfd, ".toc");
8717       if (toc == NULL
8718           || toc->size == 0
8719           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8720           || discarded_section (toc))
8721         continue;
8722
8723       toc_relocs = NULL;
8724       local_syms = NULL;
8725       symtab_hdr = &elf_symtab_hdr (ibfd);
8726
8727       /* Look at sections dropped from the final link.  */
8728       skip = NULL;
8729       relstart = NULL;
8730       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8731         {
8732           if (sec->reloc_count == 0
8733               || !discarded_section (sec)
8734               || get_opd_info (sec)
8735               || (sec->flags & SEC_ALLOC) == 0
8736               || (sec->flags & SEC_DEBUGGING) != 0)
8737             continue;
8738
8739           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8740           if (relstart == NULL)
8741             goto error_ret;
8742
8743           /* Run through the relocs to see which toc entries might be
8744              unused.  */
8745           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8746             {
8747               enum elf_ppc64_reloc_type r_type;
8748               unsigned long r_symndx;
8749               asection *sym_sec;
8750               struct elf_link_hash_entry *h;
8751               Elf_Internal_Sym *sym;
8752               bfd_vma val;
8753
8754               r_type = ELF64_R_TYPE (rel->r_info);
8755               switch (r_type)
8756                 {
8757                 default:
8758                   continue;
8759
8760                 case R_PPC64_TOC16:
8761                 case R_PPC64_TOC16_LO:
8762                 case R_PPC64_TOC16_HI:
8763                 case R_PPC64_TOC16_HA:
8764                 case R_PPC64_TOC16_DS:
8765                 case R_PPC64_TOC16_LO_DS:
8766                   break;
8767                 }
8768
8769               r_symndx = ELF64_R_SYM (rel->r_info);
8770               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8771                               r_symndx, ibfd))
8772                 goto error_ret;
8773
8774               if (sym_sec != toc)
8775                 continue;
8776
8777               if (h != NULL)
8778                 val = h->root.u.def.value;
8779               else
8780                 val = sym->st_value;
8781               val += rel->r_addend;
8782
8783               if (val >= toc->size)
8784                 continue;
8785
8786               /* Anything in the toc ought to be aligned to 8 bytes.
8787                  If not, don't mark as unused.  */
8788               if (val & 7)
8789                 continue;
8790
8791               if (skip == NULL)
8792                 {
8793                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8794                   if (skip == NULL)
8795                     goto error_ret;
8796                 }
8797
8798               skip[val >> 3] = ref_from_discarded;
8799             }
8800
8801           if (elf_section_data (sec)->relocs != relstart)
8802             free (relstart);
8803         }
8804
8805       /* For largetoc loads of address constants, we can convert
8806          .  addis rx,2,addr@got@ha
8807          .  ld ry,addr@got@l(rx)
8808          to
8809          .  addis rx,2,addr@toc@ha
8810          .  addi ry,rx,addr@toc@l
8811          when addr is within 2G of the toc pointer.  This then means
8812          that the word storing "addr" in the toc is no longer needed.  */
8813
8814       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8815           && toc->output_section->rawsize < (bfd_vma) 1 << 31
8816           && toc->reloc_count != 0)
8817         {
8818           /* Read toc relocs.  */
8819           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8820                                                   info->keep_memory);
8821           if (toc_relocs == NULL)
8822             goto error_ret;
8823
8824           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8825             {
8826               enum elf_ppc64_reloc_type r_type;
8827               unsigned long r_symndx;
8828               asection *sym_sec;
8829               struct elf_link_hash_entry *h;
8830               Elf_Internal_Sym *sym;
8831               bfd_vma val, addr;
8832
8833               r_type = ELF64_R_TYPE (rel->r_info);
8834               if (r_type != R_PPC64_ADDR64)
8835                 continue;
8836
8837               r_symndx = ELF64_R_SYM (rel->r_info);
8838               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8839                               r_symndx, ibfd))
8840                 goto error_ret;
8841
8842               if (sym_sec == NULL
8843                   || discarded_section (sym_sec))
8844                 continue;
8845
8846               if (!SYMBOL_REFERENCES_LOCAL (info, h))
8847                 continue;
8848
8849               if (h != NULL)
8850                 {
8851                   if (h->type == STT_GNU_IFUNC)
8852                     continue;
8853                   val = h->root.u.def.value;
8854                 }
8855               else
8856                 {
8857                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8858                     continue;
8859                   val = sym->st_value;
8860                 }
8861               val += rel->r_addend;
8862               val += sym_sec->output_section->vma + sym_sec->output_offset;
8863
8864               /* We don't yet know the exact toc pointer value, but we
8865                  know it will be somewhere in the toc section.  Don't
8866                  optimize if the difference from any possible toc
8867                  pointer is outside [ff..f80008000, 7fff7fff].  */
8868               addr = toc->output_section->vma + TOC_BASE_OFF;
8869               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8870                 continue;
8871
8872               addr = toc->output_section->vma + toc->output_section->rawsize;
8873               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8874                 continue;
8875
8876               if (skip == NULL)
8877                 {
8878                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8879                   if (skip == NULL)
8880                     goto error_ret;
8881                 }
8882
8883               skip[rel->r_offset >> 3]
8884                 |= can_optimize | ((rel - toc_relocs) << 2);
8885             }
8886         }
8887
8888       if (skip == NULL)
8889         continue;
8890
8891       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8892       if (used == NULL)
8893         {
8894         error_ret:
8895           if (local_syms != NULL
8896               && symtab_hdr->contents != (unsigned char *) local_syms)
8897             free (local_syms);
8898           if (sec != NULL
8899               && relstart != NULL
8900               && elf_section_data (sec)->relocs != relstart)
8901             free (relstart);
8902           if (toc_relocs != NULL
8903               && elf_section_data (toc)->relocs != toc_relocs)
8904             free (toc_relocs);
8905           if (skip != NULL)
8906             free (skip);
8907           return FALSE;
8908         }
8909
8910       /* Now check all kept sections that might reference the toc.
8911          Check the toc itself last.  */
8912       for (sec = (ibfd->sections == toc && toc->next ? toc->next
8913                   : ibfd->sections);
8914            sec != NULL;
8915            sec = (sec == toc ? NULL
8916                   : sec->next == NULL ? toc
8917                   : sec->next == toc && toc->next ? toc->next
8918                   : sec->next))
8919         {
8920           int repeat;
8921
8922           if (sec->reloc_count == 0
8923               || discarded_section (sec)
8924               || get_opd_info (sec)
8925               || (sec->flags & SEC_ALLOC) == 0
8926               || (sec->flags & SEC_DEBUGGING) != 0)
8927             continue;
8928
8929           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8930                                                 info->keep_memory);
8931           if (relstart == NULL)
8932             {
8933               free (used);
8934               goto error_ret;
8935             }
8936
8937           /* Mark toc entries referenced as used.  */
8938           do
8939             {
8940               repeat = 0;
8941               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8942                 {
8943                   enum elf_ppc64_reloc_type r_type;
8944                   unsigned long r_symndx;
8945                   asection *sym_sec;
8946                   struct elf_link_hash_entry *h;
8947                   Elf_Internal_Sym *sym;
8948                   bfd_vma val;
8949                   enum {no_check, check_lo, check_ha} insn_check;
8950
8951                   r_type = ELF64_R_TYPE (rel->r_info);
8952                   switch (r_type)
8953                     {
8954                     default:
8955                       insn_check = no_check;
8956                       break;
8957
8958                     case R_PPC64_GOT_TLSLD16_HA:
8959                     case R_PPC64_GOT_TLSGD16_HA:
8960                     case R_PPC64_GOT_TPREL16_HA:
8961                     case R_PPC64_GOT_DTPREL16_HA:
8962                     case R_PPC64_GOT16_HA:
8963                     case R_PPC64_TOC16_HA:
8964                       insn_check = check_ha;
8965                       break;
8966
8967                     case R_PPC64_GOT_TLSLD16_LO:
8968                     case R_PPC64_GOT_TLSGD16_LO:
8969                     case R_PPC64_GOT_TPREL16_LO_DS:
8970                     case R_PPC64_GOT_DTPREL16_LO_DS:
8971                     case R_PPC64_GOT16_LO:
8972                     case R_PPC64_GOT16_LO_DS:
8973                     case R_PPC64_TOC16_LO:
8974                     case R_PPC64_TOC16_LO_DS:
8975                       insn_check = check_lo;
8976                       break;
8977                     }
8978
8979                   if (insn_check != no_check)
8980                     {
8981                       bfd_vma off = rel->r_offset & ~3;
8982                       unsigned char buf[4];
8983                       unsigned int insn;
8984
8985                       if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8986                         {
8987                           free (used);
8988                           goto error_ret;
8989                         }
8990                       insn = bfd_get_32 (ibfd, buf);
8991                       if (insn_check == check_lo
8992                           ? !ok_lo_toc_insn (insn)
8993                           : ((insn & ((0x3f << 26) | 0x1f << 16))
8994                              != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8995                         {
8996                           char str[12];
8997
8998                           ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8999                           sprintf (str, "%#08x", insn);
9000                           info->callbacks->einfo
9001                             (_("%P: %H: toc optimization is not supported for"
9002                                " %s instruction.\n"),
9003                              ibfd, sec, rel->r_offset & ~3, str);
9004                         }
9005                     }
9006
9007                   switch (r_type)
9008                     {
9009                     case R_PPC64_TOC16:
9010                     case R_PPC64_TOC16_LO:
9011                     case R_PPC64_TOC16_HI:
9012                     case R_PPC64_TOC16_HA:
9013                     case R_PPC64_TOC16_DS:
9014                     case R_PPC64_TOC16_LO_DS:
9015                       /* In case we're taking addresses of toc entries.  */
9016                     case R_PPC64_ADDR64:
9017                       break;
9018
9019                     default:
9020                       continue;
9021                     }
9022
9023                   r_symndx = ELF64_R_SYM (rel->r_info);
9024                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9025                                   r_symndx, ibfd))
9026                     {
9027                       free (used);
9028                       goto error_ret;
9029                     }
9030
9031                   if (sym_sec != toc)
9032                     continue;
9033
9034                   if (h != NULL)
9035                     val = h->root.u.def.value;
9036                   else
9037                     val = sym->st_value;
9038                   val += rel->r_addend;
9039
9040                   if (val >= toc->size)
9041                     continue;
9042
9043                   if ((skip[val >> 3] & can_optimize) != 0)
9044                     {
9045                       bfd_vma off;
9046                       unsigned char opc;
9047
9048                       switch (r_type)
9049                         {
9050                         case R_PPC64_TOC16_HA:
9051                           break;
9052
9053                         case R_PPC64_TOC16_LO_DS:
9054                           off = rel->r_offset;
9055                           off += (bfd_big_endian (ibfd) ? -2 : 3);
9056                           if (!bfd_get_section_contents (ibfd, sec, &opc,
9057                                                          off, 1))
9058                             {
9059                               free (used);
9060                               goto error_ret;
9061                             }
9062                           if ((opc & (0x3f << 2)) == (58u << 2))
9063                             break;
9064                           /* Fall thru */
9065
9066                         default:
9067                           /* Wrong sort of reloc, or not a ld.  We may
9068                              as well clear ref_from_discarded too.  */
9069                           skip[val >> 3] = 0;
9070                         }
9071                     }
9072
9073                   if (sec != toc)
9074                     used[val >> 3] = 1;
9075                   /* For the toc section, we only mark as used if this
9076                      entry itself isn't unused.  */
9077                   else if ((used[rel->r_offset >> 3]
9078                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9079                            && !used[val >> 3])
9080                     {
9081                       /* Do all the relocs again, to catch reference
9082                          chains.  */
9083                       repeat = 1;
9084                       used[val >> 3] = 1;
9085                     }
9086                 }
9087             }
9088           while (repeat);
9089
9090           if (elf_section_data (sec)->relocs != relstart)
9091             free (relstart);
9092         }
9093
9094       /* Merge the used and skip arrays.  Assume that TOC
9095          doublewords not appearing as either used or unused belong
9096          to to an entry more than one doubleword in size.  */
9097       for (drop = skip, keep = used, last = 0, some_unused = 0;
9098            drop < skip + (toc->size + 7) / 8;
9099            ++drop, ++keep)
9100         {
9101           if (*keep)
9102             {
9103               *drop &= ~ref_from_discarded;
9104               if ((*drop & can_optimize) != 0)
9105                 some_unused = 1;
9106               last = 0;
9107             }
9108           else if ((*drop & ref_from_discarded) != 0)
9109             {
9110               some_unused = 1;
9111               last = ref_from_discarded;
9112             }
9113           else
9114             *drop = last;
9115         }
9116
9117       free (used);
9118
9119       if (some_unused)
9120         {
9121           bfd_byte *contents, *src;
9122           unsigned long off;
9123           Elf_Internal_Sym *sym;
9124           bfd_boolean local_toc_syms = FALSE;
9125
9126           /* Shuffle the toc contents, and at the same time convert the
9127              skip array from booleans into offsets.  */
9128           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9129             goto error_ret;
9130
9131           elf_section_data (toc)->this_hdr.contents = contents;
9132
9133           for (src = contents, off = 0, drop = skip;
9134                src < contents + toc->size;
9135                src += 8, ++drop)
9136             {
9137               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9138                 off += 8;
9139               else if (off != 0)
9140                 {
9141                   *drop = off;
9142                   memcpy (src - off, src, 8);
9143                 }
9144             }
9145           *drop = off;
9146           toc->rawsize = toc->size;
9147           toc->size = src - contents - off;
9148
9149           /* Adjust addends for relocs against the toc section sym,
9150              and optimize any accesses we can.  */
9151           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9152             {
9153               if (sec->reloc_count == 0
9154                   || discarded_section (sec))
9155                 continue;
9156
9157               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9158                                                     info->keep_memory);
9159               if (relstart == NULL)
9160                 goto error_ret;
9161
9162               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9163                 {
9164                   enum elf_ppc64_reloc_type r_type;
9165                   unsigned long r_symndx;
9166                   asection *sym_sec;
9167                   struct elf_link_hash_entry *h;
9168                   bfd_vma val;
9169
9170                   r_type = ELF64_R_TYPE (rel->r_info);
9171                   switch (r_type)
9172                     {
9173                     default:
9174                       continue;
9175
9176                     case R_PPC64_TOC16:
9177                     case R_PPC64_TOC16_LO:
9178                     case R_PPC64_TOC16_HI:
9179                     case R_PPC64_TOC16_HA:
9180                     case R_PPC64_TOC16_DS:
9181                     case R_PPC64_TOC16_LO_DS:
9182                     case R_PPC64_ADDR64:
9183                       break;
9184                     }
9185
9186                   r_symndx = ELF64_R_SYM (rel->r_info);
9187                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9188                                   r_symndx, ibfd))
9189                     goto error_ret;
9190
9191                   if (sym_sec != toc)
9192                     continue;
9193
9194                   if (h != NULL)
9195                     val = h->root.u.def.value;
9196                   else
9197                     {
9198                       val = sym->st_value;
9199                       if (val != 0)
9200                         local_toc_syms = TRUE;
9201                     }
9202
9203                   val += rel->r_addend;
9204
9205                   if (val > toc->rawsize)
9206                     val = toc->rawsize;
9207                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
9208                     continue;
9209                   else if ((skip[val >> 3] & can_optimize) != 0)
9210                     {
9211                       Elf_Internal_Rela *tocrel
9212                         = toc_relocs + (skip[val >> 3] >> 2);
9213                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9214
9215                       switch (r_type)
9216                         {
9217                         case R_PPC64_TOC16_HA:
9218                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9219                           break;
9220
9221                         case R_PPC64_TOC16_LO_DS:
9222                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9223                           break;
9224
9225                         default:
9226                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9227                             ppc_howto_init ();
9228                           info->callbacks->einfo
9229                             (_("%P: %H: %s references "
9230                                "optimized away TOC entry\n"),
9231                              ibfd, sec, rel->r_offset,
9232                              ppc64_elf_howto_table[r_type]->name);
9233                           bfd_set_error (bfd_error_bad_value);
9234                           goto error_ret;
9235                         }
9236                       rel->r_addend = tocrel->r_addend;
9237                       elf_section_data (sec)->relocs = relstart;
9238                       continue;
9239                     }
9240
9241                   if (h != NULL || sym->st_value != 0)
9242                     continue;
9243
9244                   rel->r_addend -= skip[val >> 3];
9245                   elf_section_data (sec)->relocs = relstart;
9246                 }
9247
9248               if (elf_section_data (sec)->relocs != relstart)
9249                 free (relstart);
9250             }
9251
9252           /* We shouldn't have local or global symbols defined in the TOC,
9253              but handle them anyway.  */
9254           if (local_syms != NULL)
9255             for (sym = local_syms;
9256                  sym < local_syms + symtab_hdr->sh_info;
9257                  ++sym)
9258               if (sym->st_value != 0
9259                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9260                 {
9261                   unsigned long i;
9262
9263                   if (sym->st_value > toc->rawsize)
9264                     i = toc->rawsize >> 3;
9265                   else
9266                     i = sym->st_value >> 3;
9267
9268                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9269                     {
9270                       if (local_toc_syms)
9271                         (*_bfd_error_handler)
9272                           (_("%s defined on removed toc entry"),
9273                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9274                       do
9275                         ++i;
9276                       while ((skip[i] & (ref_from_discarded | can_optimize)));
9277                       sym->st_value = (bfd_vma) i << 3;
9278                     }
9279
9280                   sym->st_value -= skip[i];
9281                   symtab_hdr->contents = (unsigned char *) local_syms;
9282                 }
9283
9284           /* Adjust any global syms defined in this toc input section.  */
9285           if (toc_inf.global_toc_syms)
9286             {
9287               toc_inf.toc = toc;
9288               toc_inf.skip = skip;
9289               toc_inf.global_toc_syms = FALSE;
9290               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9291                                       &toc_inf);
9292             }
9293
9294           if (toc->reloc_count != 0)
9295             {
9296               Elf_Internal_Shdr *rel_hdr;
9297               Elf_Internal_Rela *wrel;
9298               bfd_size_type sz;
9299
9300               /* Remove unused toc relocs, and adjust those we keep.  */
9301               if (toc_relocs == NULL)
9302                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9303                                                         info->keep_memory);
9304               if (toc_relocs == NULL)
9305                 goto error_ret;
9306
9307               wrel = toc_relocs;
9308               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9309                 if ((skip[rel->r_offset >> 3]
9310                      & (ref_from_discarded | can_optimize)) == 0)
9311                   {
9312                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9313                     wrel->r_info = rel->r_info;
9314                     wrel->r_addend = rel->r_addend;
9315                     ++wrel;
9316                   }
9317                 else if (!dec_dynrel_count (rel->r_info, toc, info,
9318                                             &local_syms, NULL, NULL))
9319                   goto error_ret;
9320
9321               elf_section_data (toc)->relocs = toc_relocs;
9322               toc->reloc_count = wrel - toc_relocs;
9323               rel_hdr = _bfd_elf_single_rel_hdr (toc);
9324               sz = rel_hdr->sh_entsize;
9325               rel_hdr->sh_size = toc->reloc_count * sz;
9326             }
9327         }
9328       else if (toc_relocs != NULL
9329                && elf_section_data (toc)->relocs != toc_relocs)
9330         free (toc_relocs);
9331
9332       if (local_syms != NULL
9333           && symtab_hdr->contents != (unsigned char *) local_syms)
9334         {
9335           if (!info->keep_memory)
9336             free (local_syms);
9337           else
9338             symtab_hdr->contents = (unsigned char *) local_syms;
9339         }
9340       free (skip);
9341     }
9342
9343   return TRUE;
9344 }
9345
9346 /* Return true iff input section I references the TOC using
9347    instructions limited to +/-32k offsets.  */
9348
9349 bfd_boolean
9350 ppc64_elf_has_small_toc_reloc (asection *i)
9351 {
9352   return (is_ppc64_elf (i->owner)
9353           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9354 }
9355
9356 /* Allocate space for one GOT entry.  */
9357
9358 static void
9359 allocate_got (struct elf_link_hash_entry *h,
9360               struct bfd_link_info *info,
9361               struct got_entry *gent)
9362 {
9363   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9364   bfd_boolean dyn;
9365   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9366   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9367                  ? 16 : 8);
9368   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9369                   ? 2 : 1) * sizeof (Elf64_External_Rela);
9370   asection *got = ppc64_elf_tdata (gent->owner)->got;
9371
9372   gent->got.offset = got->size;
9373   got->size += entsize;
9374
9375   dyn = htab->elf.dynamic_sections_created;
9376   if (h->type == STT_GNU_IFUNC)
9377     {
9378       htab->elf.irelplt->size += rentsize;
9379       htab->got_reli_size += rentsize;
9380     }
9381   else if ((bfd_link_pic (info)
9382             || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9383            && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9384                || h->root.type != bfd_link_hash_undefweak))
9385     {
9386       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9387       relgot->size += rentsize;
9388     }
9389 }
9390
9391 /* This function merges got entries in the same toc group.  */
9392
9393 static void
9394 merge_got_entries (struct got_entry **pent)
9395 {
9396   struct got_entry *ent, *ent2;
9397
9398   for (ent = *pent; ent != NULL; ent = ent->next)
9399     if (!ent->is_indirect)
9400       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9401         if (!ent2->is_indirect
9402             && ent2->addend == ent->addend
9403             && ent2->tls_type == ent->tls_type
9404             && elf_gp (ent2->owner) == elf_gp (ent->owner))
9405           {
9406             ent2->is_indirect = TRUE;
9407             ent2->got.ent = ent;
9408           }
9409 }
9410
9411 /* Allocate space in .plt, .got and associated reloc sections for
9412    dynamic relocs.  */
9413
9414 static bfd_boolean
9415 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9416 {
9417   struct bfd_link_info *info;
9418   struct ppc_link_hash_table *htab;
9419   asection *s;
9420   struct ppc_link_hash_entry *eh;
9421   struct elf_dyn_relocs *p;
9422   struct got_entry **pgent, *gent;
9423
9424   if (h->root.type == bfd_link_hash_indirect)
9425     return TRUE;
9426
9427   info = (struct bfd_link_info *) inf;
9428   htab = ppc_hash_table (info);
9429   if (htab == NULL)
9430     return FALSE;
9431
9432   if ((htab->elf.dynamic_sections_created
9433        && h->dynindx != -1
9434        && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
9435       || h->type == STT_GNU_IFUNC)
9436     {
9437       struct plt_entry *pent;
9438       bfd_boolean doneone = FALSE;
9439       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9440         if (pent->plt.refcount > 0)
9441           {
9442             if (!htab->elf.dynamic_sections_created
9443                 || h->dynindx == -1)
9444               {
9445                 s = htab->elf.iplt;
9446                 pent->plt.offset = s->size;
9447                 s->size += PLT_ENTRY_SIZE (htab);
9448                 s = htab->elf.irelplt;
9449               }
9450             else
9451               {
9452                 /* If this is the first .plt entry, make room for the special
9453                    first entry.  */
9454                 s = htab->elf.splt;
9455                 if (s->size == 0)
9456                   s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9457
9458                 pent->plt.offset = s->size;
9459
9460                 /* Make room for this entry.  */
9461                 s->size += PLT_ENTRY_SIZE (htab);
9462
9463                 /* Make room for the .glink code.  */
9464                 s = htab->glink;
9465                 if (s->size == 0)
9466                   s->size += GLINK_CALL_STUB_SIZE;
9467                 if (htab->opd_abi)
9468                   {
9469                     /* We need bigger stubs past index 32767.  */
9470                     if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9471                       s->size += 4;
9472                     s->size += 2*4;
9473                   }
9474                 else
9475                   s->size += 4;
9476
9477                 /* We also need to make an entry in the .rela.plt section.  */
9478                 s = htab->elf.srelplt;
9479               }
9480             s->size += sizeof (Elf64_External_Rela);
9481             doneone = TRUE;
9482           }
9483         else
9484           pent->plt.offset = (bfd_vma) -1;
9485       if (!doneone)
9486         {
9487           h->plt.plist = NULL;
9488           h->needs_plt = 0;
9489         }
9490     }
9491   else
9492     {
9493       h->plt.plist = NULL;
9494       h->needs_plt = 0;
9495     }
9496
9497   eh = (struct ppc_link_hash_entry *) h;
9498   /* Run through the TLS GD got entries first if we're changing them
9499      to TPREL.  */
9500   if ((eh->tls_mask & TLS_TPRELGD) != 0)
9501     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9502       if (gent->got.refcount > 0
9503           && (gent->tls_type & TLS_GD) != 0)
9504         {
9505           /* This was a GD entry that has been converted to TPREL.  If
9506              there happens to be a TPREL entry we can use that one.  */
9507           struct got_entry *ent;
9508           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9509             if (ent->got.refcount > 0
9510                 && (ent->tls_type & TLS_TPREL) != 0
9511                 && ent->addend == gent->addend
9512                 && ent->owner == gent->owner)
9513               {
9514                 gent->got.refcount = 0;
9515                 break;
9516               }
9517
9518           /* If not, then we'll be using our own TPREL entry.  */
9519           if (gent->got.refcount != 0)
9520             gent->tls_type = TLS_TLS | TLS_TPREL;
9521         }
9522
9523   /* Remove any list entry that won't generate a word in the GOT before
9524      we call merge_got_entries.  Otherwise we risk merging to empty
9525      entries.  */
9526   pgent = &h->got.glist;
9527   while ((gent = *pgent) != NULL)
9528     if (gent->got.refcount > 0)
9529       {
9530         if ((gent->tls_type & TLS_LD) != 0
9531             && !h->def_dynamic)
9532           {
9533             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9534             *pgent = gent->next;
9535           }
9536         else
9537           pgent = &gent->next;
9538       }
9539     else
9540       *pgent = gent->next;
9541
9542   if (!htab->do_multi_toc)
9543     merge_got_entries (&h->got.glist);
9544
9545   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9546     if (!gent->is_indirect)
9547       {
9548         /* Make sure this symbol is output as a dynamic symbol.
9549            Undefined weak syms won't yet be marked as dynamic,
9550            nor will all TLS symbols.  */
9551         if (h->dynindx == -1
9552             && !h->forced_local
9553             && h->type != STT_GNU_IFUNC
9554             && htab->elf.dynamic_sections_created)
9555           {
9556             if (! bfd_elf_link_record_dynamic_symbol (info, h))
9557               return FALSE;
9558           }
9559
9560         if (!is_ppc64_elf (gent->owner))
9561           abort ();
9562
9563         allocate_got (h, info, gent);
9564       }
9565
9566   if (eh->dyn_relocs == NULL
9567       || (!htab->elf.dynamic_sections_created
9568           && h->type != STT_GNU_IFUNC))
9569     return TRUE;
9570
9571   /* In the shared -Bsymbolic case, discard space allocated for
9572      dynamic pc-relative relocs against symbols which turn out to be
9573      defined in regular objects.  For the normal shared case, discard
9574      space for relocs that have become local due to symbol visibility
9575      changes.  */
9576
9577   if (bfd_link_pic (info))
9578     {
9579       /* Relocs that use pc_count are those that appear on a call insn,
9580          or certain REL relocs (see must_be_dyn_reloc) that can be
9581          generated via assembly.  We want calls to protected symbols to
9582          resolve directly to the function rather than going via the plt.
9583          If people want function pointer comparisons to work as expected
9584          then they should avoid writing weird assembly.  */
9585       if (SYMBOL_CALLS_LOCAL (info, h))
9586         {
9587           struct elf_dyn_relocs **pp;
9588
9589           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9590             {
9591               p->count -= p->pc_count;
9592               p->pc_count = 0;
9593               if (p->count == 0)
9594                 *pp = p->next;
9595               else
9596                 pp = &p->next;
9597             }
9598         }
9599
9600       /* Also discard relocs on undefined weak syms with non-default
9601          visibility.  */
9602       if (eh->dyn_relocs != NULL
9603           && h->root.type == bfd_link_hash_undefweak)
9604         {
9605           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9606             eh->dyn_relocs = NULL;
9607
9608           /* Make sure this symbol is output as a dynamic symbol.
9609              Undefined weak syms won't yet be marked as dynamic.  */
9610           else if (h->dynindx == -1
9611                    && !h->forced_local)
9612             {
9613               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9614                 return FALSE;
9615             }
9616         }
9617     }
9618   else if (h->type == STT_GNU_IFUNC)
9619     {
9620       if (!h->non_got_ref)
9621         eh->dyn_relocs = NULL;
9622     }
9623   else if (ELIMINATE_COPY_RELOCS)
9624     {
9625       /* For the non-shared case, discard space for relocs against
9626          symbols which turn out to need copy relocs or are not
9627          dynamic.  */
9628
9629       if (!h->non_got_ref
9630           && !h->def_regular)
9631         {
9632           /* Make sure this symbol is output as a dynamic symbol.
9633              Undefined weak syms won't yet be marked as dynamic.  */
9634           if (h->dynindx == -1
9635               && !h->forced_local)
9636             {
9637               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9638                 return FALSE;
9639             }
9640
9641           /* If that succeeded, we know we'll be keeping all the
9642              relocs.  */
9643           if (h->dynindx != -1)
9644             goto keep;
9645         }
9646
9647       eh->dyn_relocs = NULL;
9648
9649     keep: ;
9650     }
9651
9652   /* Finally, allocate space.  */
9653   for (p = eh->dyn_relocs; p != NULL; p = p->next)
9654     {
9655       asection *sreloc = elf_section_data (p->sec)->sreloc;
9656       if (eh->elf.type == STT_GNU_IFUNC)
9657         sreloc = htab->elf.irelplt;
9658       sreloc->size += p->count * sizeof (Elf64_External_Rela);
9659     }
9660
9661   return TRUE;
9662 }
9663
9664 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9665    to set up space for global entry stubs.  These are put in glink,
9666    after the branch table.  */
9667
9668 static bfd_boolean
9669 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9670 {
9671   struct bfd_link_info *info;
9672   struct ppc_link_hash_table *htab;
9673   struct plt_entry *pent;
9674   asection *s;
9675
9676   if (h->root.type == bfd_link_hash_indirect)
9677     return TRUE;
9678
9679   if (!h->pointer_equality_needed)
9680     return TRUE;
9681
9682   if (h->def_regular)
9683     return TRUE;
9684
9685   info = inf;
9686   htab = ppc_hash_table (info);
9687   if (htab == NULL)
9688     return FALSE;
9689
9690   s = htab->glink;
9691   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9692     if (pent->plt.offset != (bfd_vma) -1
9693         && pent->addend == 0)
9694       {
9695         /* For ELFv2, if this symbol is not defined in a regular file
9696            and we are not generating a shared library or pie, then we
9697            need to define the symbol in the executable on a call stub.
9698            This is to avoid text relocations.  */
9699         s->size = (s->size + 15) & -16;
9700         h->root.u.def.section = s;
9701         h->root.u.def.value = s->size;
9702         s->size += 16;
9703         break;
9704       }
9705   return TRUE;
9706 }
9707
9708 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9709    read-only sections.  */
9710
9711 static bfd_boolean
9712 maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
9713 {
9714   if (h->root.type == bfd_link_hash_indirect)
9715     return TRUE;
9716
9717   if (readonly_dynrelocs (h))
9718     {
9719       ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
9720
9721       /* Not an error, just cut short the traversal.  */
9722       return FALSE;
9723     }
9724   return TRUE;
9725 }
9726
9727 /* Set the sizes of the dynamic sections.  */
9728
9729 static bfd_boolean
9730 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9731                                  struct bfd_link_info *info)
9732 {
9733   struct ppc_link_hash_table *htab;
9734   bfd *dynobj;
9735   asection *s;
9736   bfd_boolean relocs;
9737   bfd *ibfd;
9738   struct got_entry *first_tlsld;
9739
9740   htab = ppc_hash_table (info);
9741   if (htab == NULL)
9742     return FALSE;
9743
9744   dynobj = htab->elf.dynobj;
9745   if (dynobj == NULL)
9746     abort ();
9747
9748   if (htab->elf.dynamic_sections_created)
9749     {
9750       /* Set the contents of the .interp section to the interpreter.  */
9751       if (bfd_link_executable (info))
9752         {
9753           s = bfd_get_linker_section (dynobj, ".interp");
9754           if (s == NULL)
9755             abort ();
9756           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9757           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9758         }
9759     }
9760
9761   /* Set up .got offsets for local syms, and space for local dynamic
9762      relocs.  */
9763   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9764     {
9765       struct got_entry **lgot_ents;
9766       struct got_entry **end_lgot_ents;
9767       struct plt_entry **local_plt;
9768       struct plt_entry **end_local_plt;
9769       unsigned char *lgot_masks;
9770       bfd_size_type locsymcount;
9771       Elf_Internal_Shdr *symtab_hdr;
9772
9773       if (!is_ppc64_elf (ibfd))
9774         continue;
9775
9776       for (s = ibfd->sections; s != NULL; s = s->next)
9777         {
9778           struct ppc_dyn_relocs *p;
9779
9780           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9781             {
9782               if (!bfd_is_abs_section (p->sec)
9783                   && bfd_is_abs_section (p->sec->output_section))
9784                 {
9785                   /* Input section has been discarded, either because
9786                      it is a copy of a linkonce section or due to
9787                      linker script /DISCARD/, so we'll be discarding
9788                      the relocs too.  */
9789                 }
9790               else if (p->count != 0)
9791                 {
9792                   asection *srel = elf_section_data (p->sec)->sreloc;
9793                   if (p->ifunc)
9794                     srel = htab->elf.irelplt;
9795                   srel->size += p->count * sizeof (Elf64_External_Rela);
9796                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9797                     info->flags |= DF_TEXTREL;
9798                 }
9799             }
9800         }
9801
9802       lgot_ents = elf_local_got_ents (ibfd);
9803       if (!lgot_ents)
9804         continue;
9805
9806       symtab_hdr = &elf_symtab_hdr (ibfd);
9807       locsymcount = symtab_hdr->sh_info;
9808       end_lgot_ents = lgot_ents + locsymcount;
9809       local_plt = (struct plt_entry **) end_lgot_ents;
9810       end_local_plt = local_plt + locsymcount;
9811       lgot_masks = (unsigned char *) end_local_plt;
9812       s = ppc64_elf_tdata (ibfd)->got;
9813       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9814         {
9815           struct got_entry **pent, *ent;
9816
9817           pent = lgot_ents;
9818           while ((ent = *pent) != NULL)
9819             if (ent->got.refcount > 0)
9820               {
9821                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9822                   {
9823                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
9824                     *pent = ent->next;
9825                   }
9826                 else
9827                   {
9828                     unsigned int ent_size = 8;
9829                     unsigned int rel_size = sizeof (Elf64_External_Rela);
9830
9831                     ent->got.offset = s->size;
9832                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9833                       {
9834                         ent_size *= 2;
9835                         rel_size *= 2;
9836                       }
9837                     s->size += ent_size;
9838                     if ((*lgot_masks & PLT_IFUNC) != 0)
9839                       {
9840                         htab->elf.irelplt->size += rel_size;
9841                         htab->got_reli_size += rel_size;
9842                       }
9843                     else if (bfd_link_pic (info))
9844                       {
9845                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9846                         srel->size += rel_size;
9847                       }
9848                     pent = &ent->next;
9849                   }
9850               }
9851             else
9852               *pent = ent->next;
9853         }
9854
9855       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
9856       for (; local_plt < end_local_plt; ++local_plt)
9857         {
9858           struct plt_entry *ent;
9859
9860           for (ent = *local_plt; ent != NULL; ent = ent->next)
9861             if (ent->plt.refcount > 0)
9862               {
9863                 s = htab->elf.iplt;
9864                 ent->plt.offset = s->size;
9865                 s->size += PLT_ENTRY_SIZE (htab);
9866
9867                 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9868               }
9869             else
9870               ent->plt.offset = (bfd_vma) -1;
9871         }
9872     }
9873
9874   /* Allocate global sym .plt and .got entries, and space for global
9875      sym dynamic relocs.  */
9876   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9877   /* Stash the end of glink branch table.  */
9878   if (htab->glink != NULL)
9879     htab->glink->rawsize = htab->glink->size;
9880
9881   if (!htab->opd_abi && !bfd_link_pic (info))
9882     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
9883
9884   first_tlsld = NULL;
9885   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9886     {
9887       struct got_entry *ent;
9888
9889       if (!is_ppc64_elf (ibfd))
9890         continue;
9891
9892       ent = ppc64_tlsld_got (ibfd);
9893       if (ent->got.refcount > 0)
9894         {
9895           if (!htab->do_multi_toc && first_tlsld != NULL)
9896             {
9897               ent->is_indirect = TRUE;
9898               ent->got.ent = first_tlsld;
9899             }
9900           else
9901             {
9902               if (first_tlsld == NULL)
9903                 first_tlsld = ent;
9904               s = ppc64_elf_tdata (ibfd)->got;
9905               ent->got.offset = s->size;
9906               ent->owner = ibfd;
9907               s->size += 16;
9908               if (bfd_link_pic (info))
9909                 {
9910                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9911                   srel->size += sizeof (Elf64_External_Rela);
9912                 }
9913             }
9914         }
9915       else
9916         ent->got.offset = (bfd_vma) -1;
9917     }
9918
9919   /* We now have determined the sizes of the various dynamic sections.
9920      Allocate memory for them.  */
9921   relocs = FALSE;
9922   for (s = dynobj->sections; s != NULL; s = s->next)
9923     {
9924       if ((s->flags & SEC_LINKER_CREATED) == 0)
9925         continue;
9926
9927       if (s == htab->brlt || s == htab->relbrlt)
9928         /* These haven't been allocated yet;  don't strip.  */
9929         continue;
9930       else if (s == htab->elf.sgot
9931                || s == htab->elf.splt
9932                || s == htab->elf.iplt
9933                || s == htab->glink
9934                || s == htab->dynbss)
9935         {
9936           /* Strip this section if we don't need it; see the
9937              comment below.  */
9938         }
9939       else if (s == htab->glink_eh_frame)
9940         {
9941           if (!bfd_is_abs_section (s->output_section))
9942             /* Not sized yet.  */
9943             continue;
9944         }
9945       else if (CONST_STRNEQ (s->name, ".rela"))
9946         {
9947           if (s->size != 0)
9948             {
9949               if (s != htab->elf.srelplt)
9950                 relocs = TRUE;
9951
9952               /* We use the reloc_count field as a counter if we need
9953                  to copy relocs into the output file.  */
9954               s->reloc_count = 0;
9955             }
9956         }
9957       else
9958         {
9959           /* It's not one of our sections, so don't allocate space.  */
9960           continue;
9961         }
9962
9963       if (s->size == 0)
9964         {
9965           /* If we don't need this section, strip it from the
9966              output file.  This is mostly to handle .rela.bss and
9967              .rela.plt.  We must create both sections in
9968              create_dynamic_sections, because they must be created
9969              before the linker maps input sections to output
9970              sections.  The linker does that before
9971              adjust_dynamic_symbol is called, and it is that
9972              function which decides whether anything needs to go
9973              into these sections.  */
9974           s->flags |= SEC_EXCLUDE;
9975           continue;
9976         }
9977
9978       if ((s->flags & SEC_HAS_CONTENTS) == 0)
9979         continue;
9980
9981       /* Allocate memory for the section contents.  We use bfd_zalloc
9982          here in case unused entries are not reclaimed before the
9983          section's contents are written out.  This should not happen,
9984          but this way if it does we get a R_PPC64_NONE reloc in .rela
9985          sections instead of garbage.
9986          We also rely on the section contents being zero when writing
9987          the GOT.  */
9988       s->contents = bfd_zalloc (dynobj, s->size);
9989       if (s->contents == NULL)
9990         return FALSE;
9991     }
9992
9993   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9994     {
9995       if (!is_ppc64_elf (ibfd))
9996         continue;
9997
9998       s = ppc64_elf_tdata (ibfd)->got;
9999       if (s != NULL && s != htab->elf.sgot)
10000         {
10001           if (s->size == 0)
10002             s->flags |= SEC_EXCLUDE;
10003           else
10004             {
10005               s->contents = bfd_zalloc (ibfd, s->size);
10006               if (s->contents == NULL)
10007                 return FALSE;
10008             }
10009         }
10010       s = ppc64_elf_tdata (ibfd)->relgot;
10011       if (s != NULL)
10012         {
10013           if (s->size == 0)
10014             s->flags |= SEC_EXCLUDE;
10015           else
10016             {
10017               s->contents = bfd_zalloc (ibfd, s->size);
10018               if (s->contents == NULL)
10019                 return FALSE;
10020               relocs = TRUE;
10021               s->reloc_count = 0;
10022             }
10023         }
10024     }
10025
10026   if (htab->elf.dynamic_sections_created)
10027     {
10028       bfd_boolean tls_opt;
10029
10030       /* Add some entries to the .dynamic section.  We fill in the
10031          values later, in ppc64_elf_finish_dynamic_sections, but we
10032          must add the entries now so that we get the correct size for
10033          the .dynamic section.  The DT_DEBUG entry is filled in by the
10034          dynamic linker and used by the debugger.  */
10035 #define add_dynamic_entry(TAG, VAL) \
10036   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10037
10038       if (bfd_link_executable (info))
10039         {
10040           if (!add_dynamic_entry (DT_DEBUG, 0))
10041             return FALSE;
10042         }
10043
10044       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10045         {
10046           if (!add_dynamic_entry (DT_PLTGOT, 0)
10047               || !add_dynamic_entry (DT_PLTRELSZ, 0)
10048               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10049               || !add_dynamic_entry (DT_JMPREL, 0)
10050               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10051             return FALSE;
10052         }
10053
10054       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10055         {
10056           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10057               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10058             return FALSE;
10059         }
10060
10061       tls_opt = (!htab->params->no_tls_get_addr_opt
10062                  && htab->tls_get_addr_fd != NULL
10063                  && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10064       if (tls_opt || !htab->opd_abi)
10065         {
10066           if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10067             return FALSE;
10068         }
10069
10070       if (relocs)
10071         {
10072           if (!add_dynamic_entry (DT_RELA, 0)
10073               || !add_dynamic_entry (DT_RELASZ, 0)
10074               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10075             return FALSE;
10076
10077           /* If any dynamic relocs apply to a read-only section,
10078              then we need a DT_TEXTREL entry.  */
10079           if ((info->flags & DF_TEXTREL) == 0)
10080             elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10081
10082           if ((info->flags & DF_TEXTREL) != 0)
10083             {
10084               if (!add_dynamic_entry (DT_TEXTREL, 0))
10085                 return FALSE;
10086             }
10087         }
10088     }
10089 #undef add_dynamic_entry
10090
10091   return TRUE;
10092 }
10093
10094 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
10095
10096 static bfd_boolean
10097 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10098 {
10099   if (h->plt.plist != NULL
10100       && !h->def_regular
10101       && !h->pointer_equality_needed)
10102     return FALSE;
10103
10104   return _bfd_elf_hash_symbol (h);
10105 }
10106
10107 /* Determine the type of stub needed, if any, for a call.  */
10108
10109 static inline enum ppc_stub_type
10110 ppc_type_of_stub (asection *input_sec,
10111                   const Elf_Internal_Rela *rel,
10112                   struct ppc_link_hash_entry **hash,
10113                   struct plt_entry **plt_ent,
10114                   bfd_vma destination,
10115                   unsigned long local_off)
10116 {
10117   struct ppc_link_hash_entry *h = *hash;
10118   bfd_vma location;
10119   bfd_vma branch_offset;
10120   bfd_vma max_branch_offset;
10121   enum elf_ppc64_reloc_type r_type;
10122
10123   if (h != NULL)
10124     {
10125       struct plt_entry *ent;
10126       struct ppc_link_hash_entry *fdh = h;
10127       if (h->oh != NULL
10128           && h->oh->is_func_descriptor)
10129         {
10130           fdh = ppc_follow_link (h->oh);
10131           *hash = fdh;
10132         }
10133
10134       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10135         if (ent->addend == rel->r_addend
10136             && ent->plt.offset != (bfd_vma) -1)
10137           {
10138             *plt_ent = ent;
10139             return ppc_stub_plt_call;
10140           }
10141
10142       /* Here, we know we don't have a plt entry.  If we don't have a
10143          either a defined function descriptor or a defined entry symbol
10144          in a regular object file, then it is pointless trying to make
10145          any other type of stub.  */
10146       if (!is_static_defined (&fdh->elf)
10147           && !is_static_defined (&h->elf))
10148         return ppc_stub_none;
10149     }
10150   else if (elf_local_got_ents (input_sec->owner) != NULL)
10151     {
10152       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10153       struct plt_entry **local_plt = (struct plt_entry **)
10154         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10155       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10156
10157       if (local_plt[r_symndx] != NULL)
10158         {
10159           struct plt_entry *ent;
10160
10161           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10162             if (ent->addend == rel->r_addend
10163                 && ent->plt.offset != (bfd_vma) -1)
10164               {
10165                 *plt_ent = ent;
10166                 return ppc_stub_plt_call;
10167               }
10168         }
10169     }
10170
10171   /* Determine where the call point is.  */
10172   location = (input_sec->output_offset
10173               + input_sec->output_section->vma
10174               + rel->r_offset);
10175
10176   branch_offset = destination - location;
10177   r_type = ELF64_R_TYPE (rel->r_info);
10178
10179   /* Determine if a long branch stub is needed.  */
10180   max_branch_offset = 1 << 25;
10181   if (r_type != R_PPC64_REL24)
10182     max_branch_offset = 1 << 15;
10183
10184   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10185     /* We need a stub.  Figure out whether a long_branch or plt_branch
10186        is needed later.  */
10187     return ppc_stub_long_branch;
10188
10189   return ppc_stub_none;
10190 }
10191
10192 /* With power7 weakly ordered memory model, it is possible for ld.so
10193    to update a plt entry in one thread and have another thread see a
10194    stale zero toc entry.  To avoid this we need some sort of acquire
10195    barrier in the call stub.  One solution is to make the load of the
10196    toc word seem to appear to depend on the load of the function entry
10197    word.  Another solution is to test for r2 being zero, and branch to
10198    the appropriate glink entry if so.
10199
10200    .    fake dep barrier        compare
10201    .    ld 12,xxx(2)            ld 12,xxx(2)
10202    .    mtctr 12                mtctr 12
10203    .    xor 11,12,12            ld 2,xxx+8(2)
10204    .    add 2,2,11              cmpldi 2,0
10205    .    ld 2,xxx+8(2)           bnectr+
10206    .    bctr                    b <glink_entry>
10207
10208    The solution involving the compare turns out to be faster, so
10209    that's what we use unless the branch won't reach.  */
10210
10211 #define ALWAYS_USE_FAKE_DEP 0
10212 #define ALWAYS_EMIT_R2SAVE 0
10213
10214 #define PPC_LO(v) ((v) & 0xffff)
10215 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10216 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10217
10218 static inline unsigned int
10219 plt_stub_size (struct ppc_link_hash_table *htab,
10220                struct ppc_stub_hash_entry *stub_entry,
10221                bfd_vma off)
10222 {
10223   unsigned size = 12;
10224
10225   if (ALWAYS_EMIT_R2SAVE
10226       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10227     size += 4;
10228   if (PPC_HA (off) != 0)
10229     size += 4;
10230   if (htab->opd_abi)
10231     {
10232       size += 4;
10233       if (htab->params->plt_static_chain)
10234         size += 4;
10235       if (htab->params->plt_thread_safe
10236           && htab->elf.dynamic_sections_created
10237           && stub_entry->h != NULL
10238           && stub_entry->h->elf.dynindx != -1)
10239         size += 8;
10240       if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10241         size += 4;
10242     }
10243   if (stub_entry->h != NULL
10244       && (stub_entry->h == htab->tls_get_addr_fd
10245           || stub_entry->h == htab->tls_get_addr)
10246       && !htab->params->no_tls_get_addr_opt)
10247     size += 13 * 4;
10248   return size;
10249 }
10250
10251 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10252    then return the padding needed to do so.  */
10253 static inline unsigned int
10254 plt_stub_pad (struct ppc_link_hash_table *htab,
10255               struct ppc_stub_hash_entry *stub_entry,
10256               bfd_vma plt_off)
10257 {
10258   int stub_align = 1 << htab->params->plt_stub_align;
10259   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10260   bfd_vma stub_off = stub_entry->group->stub_sec->size;
10261
10262   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10263       > ((stub_size - 1) & -stub_align))
10264     return stub_align - (stub_off & (stub_align - 1));
10265   return 0;
10266 }
10267
10268 /* Build a .plt call stub.  */
10269
10270 static inline bfd_byte *
10271 build_plt_stub (struct ppc_link_hash_table *htab,
10272                 struct ppc_stub_hash_entry *stub_entry,
10273                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10274 {
10275   bfd *obfd = htab->params->stub_bfd;
10276   bfd_boolean plt_load_toc = htab->opd_abi;
10277   bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10278   bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10279                                  && htab->elf.dynamic_sections_created
10280                                  && stub_entry->h != NULL
10281                                  && stub_entry->h->elf.dynindx != -1);
10282   bfd_boolean use_fake_dep = plt_thread_safe;
10283   bfd_vma cmp_branch_off = 0;
10284
10285   if (!ALWAYS_USE_FAKE_DEP
10286       && plt_load_toc
10287       && plt_thread_safe
10288       && !((stub_entry->h == htab->tls_get_addr_fd
10289             || stub_entry->h == htab->tls_get_addr)
10290            && !htab->params->no_tls_get_addr_opt))
10291     {
10292       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10293       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10294                           / PLT_ENTRY_SIZE (htab));
10295       bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10296       bfd_vma to, from;
10297
10298       if (pltindex > 32768)
10299         glinkoff += (pltindex - 32768) * 4;
10300       to = (glinkoff
10301             + htab->glink->output_offset
10302             + htab->glink->output_section->vma);
10303       from = (p - stub_entry->group->stub_sec->contents
10304               + 4 * (ALWAYS_EMIT_R2SAVE
10305                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10306               + 4 * (PPC_HA (offset) != 0)
10307               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10308                      != PPC_HA (offset))
10309               + 4 * (plt_static_chain != 0)
10310               + 20
10311               + stub_entry->group->stub_sec->output_offset
10312               + stub_entry->group->stub_sec->output_section->vma);
10313       cmp_branch_off = to - from;
10314       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10315     }
10316
10317   if (PPC_HA (offset) != 0)
10318     {
10319       if (r != NULL)
10320         {
10321           if (ALWAYS_EMIT_R2SAVE
10322               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10323             r[0].r_offset += 4;
10324           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10325           r[1].r_offset = r[0].r_offset + 4;
10326           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10327           r[1].r_addend = r[0].r_addend;
10328           if (plt_load_toc)
10329             {
10330               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10331                 {
10332                   r[2].r_offset = r[1].r_offset + 4;
10333                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10334                   r[2].r_addend = r[0].r_addend;
10335                 }
10336               else
10337                 {
10338                   r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10339                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10340                   r[2].r_addend = r[0].r_addend + 8;
10341                   if (plt_static_chain)
10342                     {
10343                       r[3].r_offset = r[2].r_offset + 4;
10344                       r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10345                       r[3].r_addend = r[0].r_addend + 16;
10346                     }
10347                 }
10348             }
10349         }
10350       if (ALWAYS_EMIT_R2SAVE
10351           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10352         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10353       if (plt_load_toc)
10354         {
10355           bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10356           bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),  p += 4;
10357         }
10358       else
10359         {
10360           bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10361           bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),  p += 4;
10362         }
10363       if (plt_load_toc
10364           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10365         {
10366           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10367           offset = 0;
10368         }
10369       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10370       if (plt_load_toc)
10371         {
10372           if (use_fake_dep)
10373             {
10374               bfd_put_32 (obfd, XOR_R2_R12_R12, p),             p += 4;
10375               bfd_put_32 (obfd, ADD_R11_R11_R2, p),             p += 4;
10376             }
10377           bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10378           if (plt_static_chain)
10379             bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10380         }
10381     }
10382   else
10383     {
10384       if (r != NULL)
10385         {
10386           if (ALWAYS_EMIT_R2SAVE
10387               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10388             r[0].r_offset += 4;
10389           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10390           if (plt_load_toc)
10391             {
10392               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10393                 {
10394                   r[1].r_offset = r[0].r_offset + 4;
10395                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10396                   r[1].r_addend = r[0].r_addend;
10397                 }
10398               else
10399                 {
10400                   r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10401                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10402                   r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10403                   if (plt_static_chain)
10404                     {
10405                       r[2].r_offset = r[1].r_offset + 4;
10406                       r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10407                       r[2].r_addend = r[0].r_addend + 8;
10408                     }
10409                 }
10410             }
10411         }
10412       if (ALWAYS_EMIT_R2SAVE
10413           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10414         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10415       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
10416       if (plt_load_toc
10417           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10418         {
10419           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
10420           offset = 0;
10421         }
10422       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10423       if (plt_load_toc)
10424         {
10425           if (use_fake_dep)
10426             {
10427               bfd_put_32 (obfd, XOR_R11_R12_R12, p),            p += 4;
10428               bfd_put_32 (obfd, ADD_R2_R2_R11, p),              p += 4;
10429             }
10430           if (plt_static_chain)
10431             bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10432           bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10433         }
10434     }
10435   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10436     {
10437       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
10438       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
10439       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10440     }
10441   else
10442     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
10443   return p;
10444 }
10445
10446 /* Build a special .plt call stub for __tls_get_addr.  */
10447
10448 #define LD_R11_0R3      0xe9630000
10449 #define LD_R12_0R3      0xe9830000
10450 #define MR_R0_R3        0x7c601b78
10451 #define CMPDI_R11_0     0x2c2b0000
10452 #define ADD_R3_R12_R13  0x7c6c6a14
10453 #define BEQLR           0x4d820020
10454 #define MR_R3_R0        0x7c030378
10455 #define STD_R11_0R1     0xf9610000
10456 #define BCTRL           0x4e800421
10457 #define LD_R11_0R1      0xe9610000
10458 #define MTLR_R11        0x7d6803a6
10459
10460 static inline bfd_byte *
10461 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10462                          struct ppc_stub_hash_entry *stub_entry,
10463                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10464 {
10465   bfd *obfd = htab->params->stub_bfd;
10466
10467   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
10468   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
10469   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
10470   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
10471   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
10472   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
10473   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
10474   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
10475   bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10476
10477   if (r != NULL)
10478     r[0].r_offset += 9 * 4;
10479   p = build_plt_stub (htab, stub_entry, p, offset, r);
10480   bfd_put_32 (obfd, BCTRL, p - 4);
10481
10482   bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p),     p += 4;
10483   bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10484   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
10485   bfd_put_32 (obfd, BLR, p),                    p += 4;
10486
10487   return p;
10488 }
10489
10490 static Elf_Internal_Rela *
10491 get_relocs (asection *sec, int count)
10492 {
10493   Elf_Internal_Rela *relocs;
10494   struct bfd_elf_section_data *elfsec_data;
10495
10496   elfsec_data = elf_section_data (sec);
10497   relocs = elfsec_data->relocs;
10498   if (relocs == NULL)
10499     {
10500       bfd_size_type relsize;
10501       relsize = sec->reloc_count * sizeof (*relocs);
10502       relocs = bfd_alloc (sec->owner, relsize);
10503       if (relocs == NULL)
10504         return NULL;
10505       elfsec_data->relocs = relocs;
10506       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10507                                           sizeof (Elf_Internal_Shdr));
10508       if (elfsec_data->rela.hdr == NULL)
10509         return NULL;
10510       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10511                                         * sizeof (Elf64_External_Rela));
10512       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10513       sec->reloc_count = 0;
10514     }
10515   relocs += sec->reloc_count;
10516   sec->reloc_count += count;
10517   return relocs;
10518 }
10519
10520 static bfd_vma
10521 get_r2off (struct bfd_link_info *info,
10522            struct ppc_stub_hash_entry *stub_entry)
10523 {
10524   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10525   bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10526
10527   if (r2off == 0)
10528     {
10529       /* Support linking -R objects.  Get the toc pointer from the
10530          opd entry.  */
10531       char buf[8];
10532       if (!htab->opd_abi)
10533         return r2off;
10534       asection *opd = stub_entry->h->elf.root.u.def.section;
10535       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10536
10537       if (strcmp (opd->name, ".opd") != 0
10538           || opd->reloc_count != 0)
10539         {
10540           info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10541                                   stub_entry->h->elf.root.root.string);
10542           bfd_set_error (bfd_error_bad_value);
10543           return (bfd_vma) -1;
10544         }
10545       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10546         return (bfd_vma) -1;
10547       r2off = bfd_get_64 (opd->owner, buf);
10548       r2off -= elf_gp (info->output_bfd);
10549     }
10550   r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10551   return r2off;
10552 }
10553
10554 static bfd_boolean
10555 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10556 {
10557   struct ppc_stub_hash_entry *stub_entry;
10558   struct ppc_branch_hash_entry *br_entry;
10559   struct bfd_link_info *info;
10560   struct ppc_link_hash_table *htab;
10561   bfd_byte *loc;
10562   bfd_byte *p;
10563   bfd_vma dest, off;
10564   int size;
10565   Elf_Internal_Rela *r;
10566   asection *plt;
10567
10568   /* Massage our args to the form they really have.  */
10569   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10570   info = in_arg;
10571
10572   htab = ppc_hash_table (info);
10573   if (htab == NULL)
10574     return FALSE;
10575
10576   /* Make a note of the offset within the stubs for this entry.  */
10577   stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10578   loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10579
10580   htab->stub_count[stub_entry->stub_type - 1] += 1;
10581   switch (stub_entry->stub_type)
10582     {
10583     case ppc_stub_long_branch:
10584     case ppc_stub_long_branch_r2off:
10585       /* Branches are relative.  This is where we are going to.  */
10586       dest = (stub_entry->target_value
10587               + stub_entry->target_section->output_offset
10588               + stub_entry->target_section->output_section->vma);
10589       dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10590       off = dest;
10591
10592       /* And this is where we are coming from.  */
10593       off -= (stub_entry->stub_offset
10594               + stub_entry->group->stub_sec->output_offset
10595               + stub_entry->group->stub_sec->output_section->vma);
10596
10597       size = 4;
10598       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10599         {
10600           bfd_vma r2off = get_r2off (info, stub_entry);
10601
10602           if (r2off == (bfd_vma) -1)
10603             {
10604               htab->stub_error = TRUE;
10605               return FALSE;
10606             }
10607           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10608           loc += 4;
10609           size = 8;
10610           if (PPC_HA (r2off) != 0)
10611             {
10612               bfd_put_32 (htab->params->stub_bfd,
10613                           ADDIS_R2_R2 | PPC_HA (r2off), loc);
10614               loc += 4;
10615               size += 4;
10616             }
10617           if (PPC_LO (r2off) != 0)
10618             {
10619               bfd_put_32 (htab->params->stub_bfd,
10620                           ADDI_R2_R2 | PPC_LO (r2off), loc);
10621               loc += 4;
10622               size += 4;
10623             }
10624           off -= size - 4;
10625         }
10626       bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
10627
10628       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10629         {
10630           info->callbacks->einfo
10631             (_("%P: long branch stub `%s' offset overflow\n"),
10632              stub_entry->root.string);
10633           htab->stub_error = TRUE;
10634           return FALSE;
10635         }
10636
10637       if (info->emitrelocations)
10638         {
10639           r = get_relocs (stub_entry->group->stub_sec, 1);
10640           if (r == NULL)
10641             return FALSE;
10642           r->r_offset = loc - stub_entry->group->stub_sec->contents;
10643           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10644           r->r_addend = dest;
10645           if (stub_entry->h != NULL)
10646             {
10647               struct elf_link_hash_entry **hashes;
10648               unsigned long symndx;
10649               struct ppc_link_hash_entry *h;
10650
10651               hashes = elf_sym_hashes (htab->params->stub_bfd);
10652               if (hashes == NULL)
10653                 {
10654                   bfd_size_type hsize;
10655
10656                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10657                   hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10658                   if (hashes == NULL)
10659                     return FALSE;
10660                   elf_sym_hashes (htab->params->stub_bfd) = hashes;
10661                   htab->stub_globals = 1;
10662                 }
10663               symndx = htab->stub_globals++;
10664               h = stub_entry->h;
10665               hashes[symndx] = &h->elf;
10666               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10667               if (h->oh != NULL && h->oh->is_func)
10668                 h = ppc_follow_link (h->oh);
10669               if (h->elf.root.u.def.section != stub_entry->target_section)
10670                 /* H is an opd symbol.  The addend must be zero.  */
10671                 r->r_addend = 0;
10672               else
10673                 {
10674                   off = (h->elf.root.u.def.value
10675                          + h->elf.root.u.def.section->output_offset
10676                          + h->elf.root.u.def.section->output_section->vma);
10677                   r->r_addend -= off;
10678                 }
10679             }
10680         }
10681       break;
10682
10683     case ppc_stub_plt_branch:
10684     case ppc_stub_plt_branch_r2off:
10685       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10686                                          stub_entry->root.string + 9,
10687                                          FALSE, FALSE);
10688       if (br_entry == NULL)
10689         {
10690           info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10691                                   stub_entry->root.string);
10692           htab->stub_error = TRUE;
10693           return FALSE;
10694         }
10695
10696       dest = (stub_entry->target_value
10697               + stub_entry->target_section->output_offset
10698               + stub_entry->target_section->output_section->vma);
10699       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10700         dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10701
10702       bfd_put_64 (htab->brlt->owner, dest,
10703                   htab->brlt->contents + br_entry->offset);
10704
10705       if (br_entry->iter == htab->stub_iteration)
10706         {
10707           br_entry->iter = 0;
10708
10709           if (htab->relbrlt != NULL)
10710             {
10711               /* Create a reloc for the branch lookup table entry.  */
10712               Elf_Internal_Rela rela;
10713               bfd_byte *rl;
10714
10715               rela.r_offset = (br_entry->offset
10716                                + htab->brlt->output_offset
10717                                + htab->brlt->output_section->vma);
10718               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10719               rela.r_addend = dest;
10720
10721               rl = htab->relbrlt->contents;
10722               rl += (htab->relbrlt->reloc_count++
10723                      * sizeof (Elf64_External_Rela));
10724               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10725             }
10726           else if (info->emitrelocations)
10727             {
10728               r = get_relocs (htab->brlt, 1);
10729               if (r == NULL)
10730                 return FALSE;
10731               /* brlt, being SEC_LINKER_CREATED does not go through the
10732                  normal reloc processing.  Symbols and offsets are not
10733                  translated from input file to output file form, so
10734                  set up the offset per the output file.  */
10735               r->r_offset = (br_entry->offset
10736                              + htab->brlt->output_offset
10737                              + htab->brlt->output_section->vma);
10738               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10739               r->r_addend = dest;
10740             }
10741         }
10742
10743       dest = (br_entry->offset
10744               + htab->brlt->output_offset
10745               + htab->brlt->output_section->vma);
10746
10747       off = (dest
10748              - elf_gp (htab->brlt->output_section->owner)
10749              - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10750
10751       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10752         {
10753           info->callbacks->einfo
10754             (_("%P: linkage table error against `%T'\n"),
10755              stub_entry->root.string);
10756           bfd_set_error (bfd_error_bad_value);
10757           htab->stub_error = TRUE;
10758           return FALSE;
10759         }
10760
10761       if (info->emitrelocations)
10762         {
10763           r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
10764           if (r == NULL)
10765             return FALSE;
10766           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10767           if (bfd_big_endian (info->output_bfd))
10768             r[0].r_offset += 2;
10769           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10770             r[0].r_offset += 4;
10771           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10772           r[0].r_addend = dest;
10773           if (PPC_HA (off) != 0)
10774             {
10775               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10776               r[1].r_offset = r[0].r_offset + 4;
10777               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10778               r[1].r_addend = r[0].r_addend;
10779             }
10780         }
10781
10782       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10783         {
10784           if (PPC_HA (off) != 0)
10785             {
10786               size = 16;
10787               bfd_put_32 (htab->params->stub_bfd,
10788                           ADDIS_R12_R2 | PPC_HA (off), loc);
10789               loc += 4;
10790               bfd_put_32 (htab->params->stub_bfd,
10791                           LD_R12_0R12 | PPC_LO (off), loc);
10792             }
10793           else
10794             {
10795               size = 12;
10796               bfd_put_32 (htab->params->stub_bfd,
10797                           LD_R12_0R2 | PPC_LO (off), loc);
10798             }
10799         }
10800       else
10801         {
10802           bfd_vma r2off = get_r2off (info, stub_entry);
10803
10804           if (r2off == (bfd_vma) -1)
10805             {
10806               htab->stub_error = TRUE;
10807               return FALSE;
10808             }
10809
10810           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10811           loc += 4;
10812           size = 16;
10813           if (PPC_HA (off) != 0)
10814             {
10815               size += 4;
10816               bfd_put_32 (htab->params->stub_bfd,
10817                           ADDIS_R12_R2 | PPC_HA (off), loc);
10818               loc += 4;
10819               bfd_put_32 (htab->params->stub_bfd,
10820                           LD_R12_0R12 | PPC_LO (off), loc);
10821             }
10822           else
10823             bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
10824
10825           if (PPC_HA (r2off) != 0)
10826             {
10827               size += 4;
10828               loc += 4;
10829               bfd_put_32 (htab->params->stub_bfd,
10830                           ADDIS_R2_R2 | PPC_HA (r2off), loc);
10831             }
10832           if (PPC_LO (r2off) != 0)
10833             {
10834               size += 4;
10835               loc += 4;
10836               bfd_put_32 (htab->params->stub_bfd,
10837                           ADDI_R2_R2 | PPC_LO (r2off), loc);
10838             }
10839         }
10840       loc += 4;
10841       bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
10842       loc += 4;
10843       bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
10844       break;
10845
10846     case ppc_stub_plt_call:
10847     case ppc_stub_plt_call_r2save:
10848       if (stub_entry->h != NULL
10849           && stub_entry->h->is_func_descriptor
10850           && stub_entry->h->oh != NULL)
10851         {
10852           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10853
10854           /* If the old-ABI "dot-symbol" is undefined make it weak so
10855              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
10856           if (fh->elf.root.type == bfd_link_hash_undefined)
10857             fh->elf.root.type = bfd_link_hash_undefweak;
10858           /* Stop undo_symbol_twiddle changing it back to undefined.  */
10859           fh->was_undefined = 0;
10860         }
10861
10862       /* Now build the stub.  */
10863       dest = stub_entry->plt_ent->plt.offset & ~1;
10864       if (dest >= (bfd_vma) -2)
10865         abort ();
10866
10867       plt = htab->elf.splt;
10868       if (!htab->elf.dynamic_sections_created
10869           || stub_entry->h == NULL
10870           || stub_entry->h->elf.dynindx == -1)
10871         plt = htab->elf.iplt;
10872
10873       dest += plt->output_offset + plt->output_section->vma;
10874
10875       if (stub_entry->h == NULL
10876           && (stub_entry->plt_ent->plt.offset & 1) == 0)
10877         {
10878           Elf_Internal_Rela rela;
10879           bfd_byte *rl;
10880
10881           rela.r_offset = dest;
10882           if (htab->opd_abi)
10883             rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
10884           else
10885             rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
10886           rela.r_addend = (stub_entry->target_value
10887                            + stub_entry->target_section->output_offset
10888                            + stub_entry->target_section->output_section->vma);
10889
10890           rl = (htab->elf.irelplt->contents
10891                 + (htab->elf.irelplt->reloc_count++
10892                    * sizeof (Elf64_External_Rela)));
10893           bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
10894           stub_entry->plt_ent->plt.offset |= 1;
10895         }
10896
10897       off = (dest
10898              - elf_gp (plt->output_section->owner)
10899              - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10900
10901       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10902         {
10903           info->callbacks->einfo
10904             (_("%P: linkage table error against `%T'\n"),
10905              stub_entry->h != NULL
10906              ? stub_entry->h->elf.root.root.string
10907              : "<local sym>");
10908           bfd_set_error (bfd_error_bad_value);
10909           htab->stub_error = TRUE;
10910           return FALSE;
10911         }
10912
10913       if (htab->params->plt_stub_align != 0)
10914         {
10915           unsigned pad = plt_stub_pad (htab, stub_entry, off);
10916
10917           stub_entry->group->stub_sec->size += pad;
10918           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10919           loc += pad;
10920         }
10921
10922       r = NULL;
10923       if (info->emitrelocations)
10924         {
10925           r = get_relocs (stub_entry->group->stub_sec,
10926                           ((PPC_HA (off) != 0)
10927                            + (htab->opd_abi
10928                               ? 2 + (htab->params->plt_static_chain
10929                                      && PPC_HA (off + 16) == PPC_HA (off))
10930                               : 1)));
10931           if (r == NULL)
10932             return FALSE;
10933           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10934           if (bfd_big_endian (info->output_bfd))
10935             r[0].r_offset += 2;
10936           r[0].r_addend = dest;
10937         }
10938       if (stub_entry->h != NULL
10939           && (stub_entry->h == htab->tls_get_addr_fd
10940               || stub_entry->h == htab->tls_get_addr)
10941           && !htab->params->no_tls_get_addr_opt)
10942         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
10943       else
10944         p = build_plt_stub (htab, stub_entry, loc, off, r);
10945       size = p - loc;
10946       break;
10947
10948     default:
10949       BFD_FAIL ();
10950       return FALSE;
10951     }
10952
10953   stub_entry->group->stub_sec->size += size;
10954
10955   if (htab->params->emit_stub_syms)
10956     {
10957       struct elf_link_hash_entry *h;
10958       size_t len1, len2;
10959       char *name;
10960       const char *const stub_str[] = { "long_branch",
10961                                        "long_branch_r2off",
10962                                        "plt_branch",
10963                                        "plt_branch_r2off",
10964                                        "plt_call",
10965                                        "plt_call" };
10966
10967       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10968       len2 = strlen (stub_entry->root.string);
10969       name = bfd_malloc (len1 + len2 + 2);
10970       if (name == NULL)
10971         return FALSE;
10972       memcpy (name, stub_entry->root.string, 9);
10973       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10974       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10975       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
10976       if (h == NULL)
10977         return FALSE;
10978       if (h->root.type == bfd_link_hash_new)
10979         {
10980           h->root.type = bfd_link_hash_defined;
10981           h->root.u.def.section = stub_entry->group->stub_sec;
10982           h->root.u.def.value = stub_entry->stub_offset;
10983           h->ref_regular = 1;
10984           h->def_regular = 1;
10985           h->ref_regular_nonweak = 1;
10986           h->forced_local = 1;
10987           h->non_elf = 0;
10988           h->root.linker_def = 1;
10989         }
10990     }
10991
10992   return TRUE;
10993 }
10994
10995 /* As above, but don't actually build the stub.  Just bump offset so
10996    we know stub section sizes, and select plt_branch stubs where
10997    long_branch stubs won't do.  */
10998
10999 static bfd_boolean
11000 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11001 {
11002   struct ppc_stub_hash_entry *stub_entry;
11003   struct bfd_link_info *info;
11004   struct ppc_link_hash_table *htab;
11005   bfd_vma off;
11006   int size;
11007
11008   /* Massage our args to the form they really have.  */
11009   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11010   info = in_arg;
11011
11012   htab = ppc_hash_table (info);
11013   if (htab == NULL)
11014     return FALSE;
11015
11016   if (stub_entry->stub_type == ppc_stub_plt_call
11017       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11018     {
11019       asection *plt;
11020       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11021       if (off >= (bfd_vma) -2)
11022         abort ();
11023       plt = htab->elf.splt;
11024       if (!htab->elf.dynamic_sections_created
11025           || stub_entry->h == NULL
11026           || stub_entry->h->elf.dynindx == -1)
11027         plt = htab->elf.iplt;
11028       off += (plt->output_offset
11029               + plt->output_section->vma
11030               - elf_gp (plt->output_section->owner)
11031               - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11032
11033       size = plt_stub_size (htab, stub_entry, off);
11034       if (htab->params->plt_stub_align)
11035         size += plt_stub_pad (htab, stub_entry, off);
11036       if (info->emitrelocations)
11037         {
11038           stub_entry->group->stub_sec->reloc_count
11039             += ((PPC_HA (off) != 0)
11040                 + (htab->opd_abi
11041                    ? 2 + (htab->params->plt_static_chain
11042                           && PPC_HA (off + 16) == PPC_HA (off))
11043                    : 1));
11044           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11045         }
11046     }
11047   else
11048     {
11049       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11050          variants.  */
11051       bfd_vma r2off = 0;
11052       bfd_vma local_off = 0;
11053
11054       off = (stub_entry->target_value
11055              + stub_entry->target_section->output_offset
11056              + stub_entry->target_section->output_section->vma);
11057       off -= (stub_entry->group->stub_sec->size
11058               + stub_entry->group->stub_sec->output_offset
11059               + stub_entry->group->stub_sec->output_section->vma);
11060
11061       /* Reset the stub type from the plt variant in case we now
11062          can reach with a shorter stub.  */
11063       if (stub_entry->stub_type >= ppc_stub_plt_branch)
11064         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11065
11066       size = 4;
11067       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11068         {
11069           r2off = get_r2off (info, stub_entry);
11070           if (r2off == (bfd_vma) -1)
11071             {
11072               htab->stub_error = TRUE;
11073               return FALSE;
11074             }
11075           size = 8;
11076           if (PPC_HA (r2off) != 0)
11077             size += 4;
11078           if (PPC_LO (r2off) != 0)
11079             size += 4;
11080           off -= size - 4;
11081         }
11082
11083       local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11084
11085       /* If the branch offset if too big, use a ppc_stub_plt_branch.
11086          Do the same for -R objects without function descriptors.  */
11087       if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11088           || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11089               && r2off == 0
11090               && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11091         {
11092           struct ppc_branch_hash_entry *br_entry;
11093
11094           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11095                                              stub_entry->root.string + 9,
11096                                              TRUE, FALSE);
11097           if (br_entry == NULL)
11098             {
11099               info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
11100                                       stub_entry->root.string);
11101               htab->stub_error = TRUE;
11102               return FALSE;
11103             }
11104
11105           if (br_entry->iter != htab->stub_iteration)
11106             {
11107               br_entry->iter = htab->stub_iteration;
11108               br_entry->offset = htab->brlt->size;
11109               htab->brlt->size += 8;
11110
11111               if (htab->relbrlt != NULL)
11112                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11113               else if (info->emitrelocations)
11114                 {
11115                   htab->brlt->reloc_count += 1;
11116                   htab->brlt->flags |= SEC_RELOC;
11117                 }
11118             }
11119
11120           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11121           off = (br_entry->offset
11122                  + htab->brlt->output_offset
11123                  + htab->brlt->output_section->vma
11124                  - elf_gp (htab->brlt->output_section->owner)
11125                  - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11126
11127           if (info->emitrelocations)
11128             {
11129               stub_entry->group->stub_sec->reloc_count
11130                 += 1 + (PPC_HA (off) != 0);
11131               stub_entry->group->stub_sec->flags |= SEC_RELOC;
11132             }
11133
11134           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11135             {
11136               size = 12;
11137               if (PPC_HA (off) != 0)
11138                 size = 16;
11139             }
11140           else
11141             {
11142               size = 16;
11143               if (PPC_HA (off) != 0)
11144                 size += 4;
11145
11146               if (PPC_HA (r2off) != 0)
11147                 size += 4;
11148               if (PPC_LO (r2off) != 0)
11149                 size += 4;
11150             }
11151         }
11152       else if (info->emitrelocations)
11153         {
11154           stub_entry->group->stub_sec->reloc_count += 1;
11155           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11156         }
11157     }
11158
11159   stub_entry->group->stub_sec->size += size;
11160   return TRUE;
11161 }
11162
11163 /* Set up various things so that we can make a list of input sections
11164    for each output section included in the link.  Returns -1 on error,
11165    0 when no stubs will be needed, and 1 on success.  */
11166
11167 int
11168 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11169 {
11170   unsigned int id;
11171   bfd_size_type amt;
11172   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11173
11174   if (htab == NULL)
11175     return -1;
11176
11177   htab->sec_info_arr_size = bfd_get_next_section_id ();
11178   amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11179   htab->sec_info = bfd_zmalloc (amt);
11180   if (htab->sec_info == NULL)
11181     return -1;
11182
11183   /* Set toc_off for com, und, abs and ind sections.  */
11184   for (id = 0; id < 3; id++)
11185     htab->sec_info[id].toc_off = TOC_BASE_OFF;
11186
11187   return 1;
11188 }
11189
11190 /* Set up for first pass at multitoc partitioning.  */
11191
11192 void
11193 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11194 {
11195   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11196
11197   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11198   htab->toc_bfd = NULL;
11199   htab->toc_first_sec = NULL;
11200 }
11201
11202 /* The linker repeatedly calls this function for each TOC input section
11203    and linker generated GOT section.  Group input bfds such that the toc
11204    within a group is less than 64k in size.  */
11205
11206 bfd_boolean
11207 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11208 {
11209   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11210   bfd_vma addr, off, limit;
11211
11212   if (htab == NULL)
11213     return FALSE;
11214
11215   if (!htab->second_toc_pass)
11216     {
11217       /* Keep track of the first .toc or .got section for this input bfd.  */
11218       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11219
11220       if (new_bfd)
11221         {
11222           htab->toc_bfd = isec->owner;
11223           htab->toc_first_sec = isec;
11224         }
11225
11226       addr = isec->output_offset + isec->output_section->vma;
11227       off = addr - htab->toc_curr;
11228       limit = 0x80008000;
11229       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11230         limit = 0x10000;
11231       if (off + isec->size > limit)
11232         {
11233           addr = (htab->toc_first_sec->output_offset
11234                   + htab->toc_first_sec->output_section->vma);
11235           htab->toc_curr = addr;
11236           htab->toc_curr &= -TOC_BASE_ALIGN;
11237         }
11238
11239       /* toc_curr is the base address of this toc group.  Set elf_gp
11240          for the input section to be the offset relative to the
11241          output toc base plus 0x8000.  Making the input elf_gp an
11242          offset allows us to move the toc as a whole without
11243          recalculating input elf_gp.  */
11244       off = htab->toc_curr - elf_gp (isec->output_section->owner);
11245       off += TOC_BASE_OFF;
11246
11247       /* Die if someone uses a linker script that doesn't keep input
11248          file .toc and .got together.  */
11249       if (new_bfd
11250           && elf_gp (isec->owner) != 0
11251           && elf_gp (isec->owner) != off)
11252         return FALSE;
11253
11254       elf_gp (isec->owner) = off;
11255       return TRUE;
11256     }
11257
11258   /* During the second pass toc_first_sec points to the start of
11259      a toc group, and toc_curr is used to track the old elf_gp.
11260      We use toc_bfd to ensure we only look at each bfd once.  */
11261   if (htab->toc_bfd == isec->owner)
11262     return TRUE;
11263   htab->toc_bfd = isec->owner;
11264
11265   if (htab->toc_first_sec == NULL
11266       || htab->toc_curr != elf_gp (isec->owner))
11267     {
11268       htab->toc_curr = elf_gp (isec->owner);
11269       htab->toc_first_sec = isec;
11270     }
11271   addr = (htab->toc_first_sec->output_offset
11272           + htab->toc_first_sec->output_section->vma);
11273   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11274   elf_gp (isec->owner) = off;
11275
11276   return TRUE;
11277 }
11278
11279 /* Called via elf_link_hash_traverse to merge GOT entries for global
11280    symbol H.  */
11281
11282 static bfd_boolean
11283 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11284 {
11285   if (h->root.type == bfd_link_hash_indirect)
11286     return TRUE;
11287
11288   merge_got_entries (&h->got.glist);
11289
11290   return TRUE;
11291 }
11292
11293 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11294    symbol H.  */
11295
11296 static bfd_boolean
11297 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11298 {
11299   struct got_entry *gent;
11300
11301   if (h->root.type == bfd_link_hash_indirect)
11302     return TRUE;
11303
11304   for (gent = h->got.glist; gent != NULL; gent = gent->next)
11305     if (!gent->is_indirect)
11306       allocate_got (h, (struct bfd_link_info *) inf, gent);
11307   return TRUE;
11308 }
11309
11310 /* Called on the first multitoc pass after the last call to
11311    ppc64_elf_next_toc_section.  This function removes duplicate GOT
11312    entries.  */
11313
11314 bfd_boolean
11315 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11316 {
11317   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11318   struct bfd *ibfd, *ibfd2;
11319   bfd_boolean done_something;
11320
11321   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11322
11323   if (!htab->do_multi_toc)
11324     return FALSE;
11325
11326   /* Merge global sym got entries within a toc group.  */
11327   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11328
11329   /* And tlsld_got.  */
11330   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11331     {
11332       struct got_entry *ent, *ent2;
11333
11334       if (!is_ppc64_elf (ibfd))
11335         continue;
11336
11337       ent = ppc64_tlsld_got (ibfd);
11338       if (!ent->is_indirect
11339           && ent->got.offset != (bfd_vma) -1)
11340         {
11341           for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11342             {
11343               if (!is_ppc64_elf (ibfd2))
11344                 continue;
11345
11346               ent2 = ppc64_tlsld_got (ibfd2);
11347               if (!ent2->is_indirect
11348                   && ent2->got.offset != (bfd_vma) -1
11349                   && elf_gp (ibfd2) == elf_gp (ibfd))
11350                 {
11351                   ent2->is_indirect = TRUE;
11352                   ent2->got.ent = ent;
11353                 }
11354             }
11355         }
11356     }
11357
11358   /* Zap sizes of got sections.  */
11359   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11360   htab->elf.irelplt->size -= htab->got_reli_size;
11361   htab->got_reli_size = 0;
11362
11363   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11364     {
11365       asection *got, *relgot;
11366
11367       if (!is_ppc64_elf (ibfd))
11368         continue;
11369
11370       got = ppc64_elf_tdata (ibfd)->got;
11371       if (got != NULL)
11372         {
11373           got->rawsize = got->size;
11374           got->size = 0;
11375           relgot = ppc64_elf_tdata (ibfd)->relgot;
11376           relgot->rawsize = relgot->size;
11377           relgot->size = 0;
11378         }
11379     }
11380
11381   /* Now reallocate the got, local syms first.  We don't need to
11382      allocate section contents again since we never increase size.  */
11383   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11384     {
11385       struct got_entry **lgot_ents;
11386       struct got_entry **end_lgot_ents;
11387       struct plt_entry **local_plt;
11388       struct plt_entry **end_local_plt;
11389       unsigned char *lgot_masks;
11390       bfd_size_type locsymcount;
11391       Elf_Internal_Shdr *symtab_hdr;
11392       asection *s;
11393
11394       if (!is_ppc64_elf (ibfd))
11395         continue;
11396
11397       lgot_ents = elf_local_got_ents (ibfd);
11398       if (!lgot_ents)
11399         continue;
11400
11401       symtab_hdr = &elf_symtab_hdr (ibfd);
11402       locsymcount = symtab_hdr->sh_info;
11403       end_lgot_ents = lgot_ents + locsymcount;
11404       local_plt = (struct plt_entry **) end_lgot_ents;
11405       end_local_plt = local_plt + locsymcount;
11406       lgot_masks = (unsigned char *) end_local_plt;
11407       s = ppc64_elf_tdata (ibfd)->got;
11408       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11409         {
11410           struct got_entry *ent;
11411
11412           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11413             {
11414               unsigned int ent_size = 8;
11415               unsigned int rel_size = sizeof (Elf64_External_Rela);
11416
11417               ent->got.offset = s->size;
11418               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11419                 {
11420                   ent_size *= 2;
11421                   rel_size *= 2;
11422                 }
11423               s->size += ent_size;
11424               if ((*lgot_masks & PLT_IFUNC) != 0)
11425                 {
11426                   htab->elf.irelplt->size += rel_size;
11427                   htab->got_reli_size += rel_size;
11428                 }
11429               else if (bfd_link_pic (info))
11430                 {
11431                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11432                   srel->size += rel_size;
11433                 }
11434             }
11435         }
11436     }
11437
11438   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11439
11440   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11441     {
11442       struct got_entry *ent;
11443
11444       if (!is_ppc64_elf (ibfd))
11445         continue;
11446
11447       ent = ppc64_tlsld_got (ibfd);
11448       if (!ent->is_indirect
11449           && ent->got.offset != (bfd_vma) -1)
11450         {
11451           asection *s = ppc64_elf_tdata (ibfd)->got;
11452           ent->got.offset = s->size;
11453           s->size += 16;
11454           if (bfd_link_pic (info))
11455             {
11456               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11457               srel->size += sizeof (Elf64_External_Rela);
11458             }
11459         }
11460     }
11461
11462   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11463   if (!done_something)
11464     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11465       {
11466         asection *got;
11467
11468         if (!is_ppc64_elf (ibfd))
11469           continue;
11470
11471         got = ppc64_elf_tdata (ibfd)->got;
11472         if (got != NULL)
11473           {
11474             done_something = got->rawsize != got->size;
11475             if (done_something)
11476               break;
11477           }
11478       }
11479
11480   if (done_something)
11481     (*htab->params->layout_sections_again) ();
11482
11483   /* Set up for second pass over toc sections to recalculate elf_gp
11484      on input sections.  */
11485   htab->toc_bfd = NULL;
11486   htab->toc_first_sec = NULL;
11487   htab->second_toc_pass = TRUE;
11488   return done_something;
11489 }
11490
11491 /* Called after second pass of multitoc partitioning.  */
11492
11493 void
11494 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11495 {
11496   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11497
11498   /* After the second pass, toc_curr tracks the TOC offset used
11499      for code sections below in ppc64_elf_next_input_section.  */
11500   htab->toc_curr = TOC_BASE_OFF;
11501 }
11502
11503 /* No toc references were found in ISEC.  If the code in ISEC makes no
11504    calls, then there's no need to use toc adjusting stubs when branching
11505    into ISEC.  Actually, indirect calls from ISEC are OK as they will
11506    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
11507    needed, and 2 if a cyclical call-graph was found but no other reason
11508    for a stub was detected.  If called from the top level, a return of
11509    2 means the same as a return of 0.  */
11510
11511 static int
11512 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11513 {
11514   int ret;
11515
11516   /* Mark this section as checked.  */
11517   isec->call_check_done = 1;
11518
11519   /* We know none of our code bearing sections will need toc stubs.  */
11520   if ((isec->flags & SEC_LINKER_CREATED) != 0)
11521     return 0;
11522
11523   if (isec->size == 0)
11524     return 0;
11525
11526   if (isec->output_section == NULL)
11527     return 0;
11528
11529   ret = 0;
11530   if (isec->reloc_count != 0)
11531     {
11532       Elf_Internal_Rela *relstart, *rel;
11533       Elf_Internal_Sym *local_syms;
11534       struct ppc_link_hash_table *htab;
11535
11536       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11537                                             info->keep_memory);
11538       if (relstart == NULL)
11539         return -1;
11540
11541       /* Look for branches to outside of this section.  */
11542       local_syms = NULL;
11543       htab = ppc_hash_table (info);
11544       if (htab == NULL)
11545         return -1;
11546
11547       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11548         {
11549           enum elf_ppc64_reloc_type r_type;
11550           unsigned long r_symndx;
11551           struct elf_link_hash_entry *h;
11552           struct ppc_link_hash_entry *eh;
11553           Elf_Internal_Sym *sym;
11554           asection *sym_sec;
11555           struct _opd_sec_data *opd;
11556           bfd_vma sym_value;
11557           bfd_vma dest;
11558
11559           r_type = ELF64_R_TYPE (rel->r_info);
11560           if (r_type != R_PPC64_REL24
11561               && r_type != R_PPC64_REL14
11562               && r_type != R_PPC64_REL14_BRTAKEN
11563               && r_type != R_PPC64_REL14_BRNTAKEN)
11564             continue;
11565
11566           r_symndx = ELF64_R_SYM (rel->r_info);
11567           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11568                           isec->owner))
11569             {
11570               ret = -1;
11571               break;
11572             }
11573
11574           /* Calls to dynamic lib functions go through a plt call stub
11575              that uses r2.  */
11576           eh = (struct ppc_link_hash_entry *) h;
11577           if (eh != NULL
11578               && (eh->elf.plt.plist != NULL
11579                   || (eh->oh != NULL
11580                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11581             {
11582               ret = 1;
11583               break;
11584             }
11585
11586           if (sym_sec == NULL)
11587             /* Ignore other undefined symbols.  */
11588             continue;
11589
11590           /* Assume branches to other sections not included in the
11591              link need stubs too, to cover -R and absolute syms.  */
11592           if (sym_sec->output_section == NULL)
11593             {
11594               ret = 1;
11595               break;
11596             }
11597
11598           if (h == NULL)
11599             sym_value = sym->st_value;
11600           else
11601             {
11602               if (h->root.type != bfd_link_hash_defined
11603                   && h->root.type != bfd_link_hash_defweak)
11604                 abort ();
11605               sym_value = h->root.u.def.value;
11606             }
11607           sym_value += rel->r_addend;
11608
11609           /* If this branch reloc uses an opd sym, find the code section.  */
11610           opd = get_opd_info (sym_sec);
11611           if (opd != NULL)
11612             {
11613               if (h == NULL && opd->adjust != NULL)
11614                 {
11615                   long adjust;
11616
11617                   adjust = opd->adjust[OPD_NDX (sym_value)];
11618                   if (adjust == -1)
11619                     /* Assume deleted functions won't ever be called.  */
11620                     continue;
11621                   sym_value += adjust;
11622                 }
11623
11624               dest = opd_entry_value (sym_sec, sym_value,
11625                                       &sym_sec, NULL, FALSE);
11626               if (dest == (bfd_vma) -1)
11627                 continue;
11628             }
11629           else
11630             dest = (sym_value
11631                     + sym_sec->output_offset
11632                     + sym_sec->output_section->vma);
11633
11634           /* Ignore branch to self.  */
11635           if (sym_sec == isec)
11636             continue;
11637
11638           /* If the called function uses the toc, we need a stub.  */
11639           if (sym_sec->has_toc_reloc
11640               || sym_sec->makes_toc_func_call)
11641             {
11642               ret = 1;
11643               break;
11644             }
11645
11646           /* Assume any branch that needs a long branch stub might in fact
11647              need a plt_branch stub.  A plt_branch stub uses r2.  */
11648           else if (dest - (isec->output_offset
11649                            + isec->output_section->vma
11650                            + rel->r_offset) + (1 << 25)
11651                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11652                                                              ? h->other
11653                                                              : sym->st_other))
11654             {
11655               ret = 1;
11656               break;
11657             }
11658
11659           /* If calling back to a section in the process of being
11660              tested, we can't say for sure that no toc adjusting stubs
11661              are needed, so don't return zero.  */
11662           else if (sym_sec->call_check_in_progress)
11663             ret = 2;
11664
11665           /* Branches to another section that itself doesn't have any TOC
11666              references are OK.  Recursively call ourselves to check.  */
11667           else if (!sym_sec->call_check_done)
11668             {
11669               int recur;
11670
11671               /* Mark current section as indeterminate, so that other
11672                  sections that call back to current won't be marked as
11673                  known.  */
11674               isec->call_check_in_progress = 1;
11675               recur = toc_adjusting_stub_needed (info, sym_sec);
11676               isec->call_check_in_progress = 0;
11677
11678               if (recur != 0)
11679                 {
11680                   ret = recur;
11681                   if (recur != 2)
11682                     break;
11683                 }
11684             }
11685         }
11686
11687       if (local_syms != NULL
11688           && (elf_symtab_hdr (isec->owner).contents
11689               != (unsigned char *) local_syms))
11690         free (local_syms);
11691       if (elf_section_data (isec)->relocs != relstart)
11692         free (relstart);
11693     }
11694
11695   if ((ret & 1) == 0
11696       && isec->map_head.s != NULL
11697       && (strcmp (isec->output_section->name, ".init") == 0
11698           || strcmp (isec->output_section->name, ".fini") == 0))
11699     {
11700       if (isec->map_head.s->has_toc_reloc
11701           || isec->map_head.s->makes_toc_func_call)
11702         ret = 1;
11703       else if (!isec->map_head.s->call_check_done)
11704         {
11705           int recur;
11706           isec->call_check_in_progress = 1;
11707           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11708           isec->call_check_in_progress = 0;
11709           if (recur != 0)
11710             ret = recur;
11711         }
11712     }
11713
11714   if (ret == 1)
11715     isec->makes_toc_func_call = 1;
11716
11717   return ret;
11718 }
11719
11720 /* The linker repeatedly calls this function for each input section,
11721    in the order that input sections are linked into output sections.
11722    Build lists of input sections to determine groupings between which
11723    we may insert linker stubs.  */
11724
11725 bfd_boolean
11726 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11727 {
11728   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11729
11730   if (htab == NULL)
11731     return FALSE;
11732
11733   if ((isec->output_section->flags & SEC_CODE) != 0
11734       && isec->output_section->id < htab->sec_info_arr_size)
11735     {
11736       /* This happens to make the list in reverse order,
11737          which is what we want.  */
11738       htab->sec_info[isec->id].u.list
11739         = htab->sec_info[isec->output_section->id].u.list;
11740       htab->sec_info[isec->output_section->id].u.list = isec;
11741     }
11742
11743   if (htab->multi_toc_needed)
11744     {
11745       /* Analyse sections that aren't already flagged as needing a
11746          valid toc pointer.  Exclude .fixup for the linux kernel.
11747          .fixup contains branches, but only back to the function that
11748          hit an exception.  */
11749       if (!(isec->has_toc_reloc
11750             || (isec->flags & SEC_CODE) == 0
11751             || strcmp (isec->name, ".fixup") == 0
11752             || isec->call_check_done))
11753         {
11754           if (toc_adjusting_stub_needed (info, isec) < 0)
11755             return FALSE;
11756         }
11757       /* Make all sections use the TOC assigned for this object file.
11758          This will be wrong for pasted sections;  We fix that in
11759          check_pasted_section().  */
11760       if (elf_gp (isec->owner) != 0)
11761         htab->toc_curr = elf_gp (isec->owner);
11762     }
11763
11764   htab->sec_info[isec->id].toc_off = htab->toc_curr;
11765   return TRUE;
11766 }
11767
11768 /* Check that all .init and .fini sections use the same toc, if they
11769    have toc relocs.  */
11770
11771 static bfd_boolean
11772 check_pasted_section (struct bfd_link_info *info, const char *name)
11773 {
11774   asection *o = bfd_get_section_by_name (info->output_bfd, name);
11775
11776   if (o != NULL)
11777     {
11778       struct ppc_link_hash_table *htab = ppc_hash_table (info);
11779       bfd_vma toc_off = 0;
11780       asection *i;
11781
11782       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11783         if (i->has_toc_reloc)
11784           {
11785             if (toc_off == 0)
11786               toc_off = htab->sec_info[i->id].toc_off;
11787             else if (toc_off != htab->sec_info[i->id].toc_off)
11788               return FALSE;
11789           }
11790
11791       if (toc_off == 0)
11792         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11793           if (i->makes_toc_func_call)
11794             {
11795               toc_off = htab->sec_info[i->id].toc_off;
11796               break;
11797             }
11798
11799       /* Make sure the whole pasted function uses the same toc offset.  */
11800       if (toc_off != 0)
11801         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11802           htab->sec_info[i->id].toc_off = toc_off;
11803     }
11804   return TRUE;
11805 }
11806
11807 bfd_boolean
11808 ppc64_elf_check_init_fini (struct bfd_link_info *info)
11809 {
11810   return (check_pasted_section (info, ".init")
11811           & check_pasted_section (info, ".fini"));
11812 }
11813
11814 /* See whether we can group stub sections together.  Grouping stub
11815    sections may result in fewer stubs.  More importantly, we need to
11816    put all .init* and .fini* stubs at the beginning of the .init or
11817    .fini output sections respectively, because glibc splits the
11818    _init and _fini functions into multiple parts.  Putting a stub in
11819    the middle of a function is not a good idea.  */
11820
11821 static bfd_boolean
11822 group_sections (struct bfd_link_info *info,
11823                 bfd_size_type stub_group_size,
11824                 bfd_boolean stubs_always_before_branch)
11825 {
11826   struct ppc_link_hash_table *htab;
11827   asection *osec;
11828   bfd_size_type stub14_group_size;
11829   bfd_boolean suppress_size_errors;
11830
11831   htab = ppc_hash_table (info);
11832   if (htab == NULL)
11833     return FALSE;
11834
11835   suppress_size_errors = FALSE;
11836   stub14_group_size = stub_group_size >> 10;
11837   if (stub_group_size == 1)
11838     {
11839       /* Default values.  */
11840       if (stubs_always_before_branch)
11841         {
11842           stub_group_size = 0x1e00000;
11843           stub14_group_size = 0x7800;
11844         }
11845       else
11846         {
11847           stub_group_size = 0x1c00000;
11848           stub14_group_size = 0x7000;
11849         }
11850       suppress_size_errors = TRUE;
11851     }
11852
11853   for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
11854     {
11855       asection *tail;
11856
11857       if (osec->id >= htab->sec_info_arr_size)
11858         continue;
11859
11860       tail = htab->sec_info[osec->id].u.list;
11861       while (tail != NULL)
11862         {
11863           asection *curr;
11864           asection *prev;
11865           bfd_size_type total;
11866           bfd_boolean big_sec;
11867           bfd_vma curr_toc;
11868           struct map_stub *group;
11869
11870           curr = tail;
11871           total = tail->size;
11872           big_sec = total > (ppc64_elf_section_data (tail) != NULL
11873                              && ppc64_elf_section_data (tail)->has_14bit_branch
11874                              ? stub14_group_size : stub_group_size);
11875           if (big_sec && !suppress_size_errors)
11876             (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
11877                                      tail->owner, tail);
11878           curr_toc = htab->sec_info[tail->id].toc_off;
11879
11880           while ((prev = htab->sec_info[curr->id].u.list) != NULL
11881                  && ((total += curr->output_offset - prev->output_offset)
11882                      < (ppc64_elf_section_data (prev) != NULL
11883                         && ppc64_elf_section_data (prev)->has_14bit_branch
11884                         ? stub14_group_size : stub_group_size))
11885                  && htab->sec_info[prev->id].toc_off == curr_toc)
11886             curr = prev;
11887
11888           /* OK, the size from the start of CURR to the end is less
11889              than stub_group_size and thus can be handled by one stub
11890              section.  (or the tail section is itself larger than
11891              stub_group_size, in which case we may be toast.)  We
11892              should really be keeping track of the total size of stubs
11893              added here, as stubs contribute to the final output
11894              section size.  That's a little tricky, and this way will
11895              only break if stubs added make the total size more than
11896              2^25, ie. for the default stub_group_size, if stubs total
11897              more than 2097152 bytes, or nearly 75000 plt call stubs.  */
11898           group = bfd_alloc (curr->owner, sizeof (*group));
11899           if (group == NULL)
11900             return FALSE;
11901           group->link_sec = curr;
11902           group->stub_sec = NULL;
11903           do
11904             {
11905               prev = htab->sec_info[tail->id].u.list;
11906               /* Set up this stub group.  */
11907               htab->sec_info[tail->id].u.group = group;
11908             }
11909           while (tail != curr && (tail = prev) != NULL);
11910
11911           /* But wait, there's more!  Input sections up to stub_group_size
11912              bytes before the stub section can be handled by it too.
11913              Don't do this if we have a really large section after the
11914              stubs, as adding more stubs increases the chance that
11915              branches may not reach into the stub section.  */
11916           if (!stubs_always_before_branch && !big_sec)
11917             {
11918               total = 0;
11919               while (prev != NULL
11920                      && ((total += tail->output_offset - prev->output_offset)
11921                          < (ppc64_elf_section_data (prev) != NULL
11922                             && ppc64_elf_section_data (prev)->has_14bit_branch
11923                             ? stub14_group_size : stub_group_size))
11924                      && htab->sec_info[prev->id].toc_off == curr_toc)
11925                 {
11926                   tail = prev;
11927                   prev = htab->sec_info[tail->id].u.list;
11928                   htab->sec_info[tail->id].u.group = group;
11929                 }
11930             }
11931           tail = prev;
11932         }
11933     }
11934   return TRUE;
11935 }
11936
11937 static const unsigned char glink_eh_frame_cie[] =
11938 {
11939   0, 0, 0, 16,                          /* length.  */
11940   0, 0, 0, 0,                           /* id.  */
11941   1,                                    /* CIE version.  */
11942   'z', 'R', 0,                          /* Augmentation string.  */
11943   4,                                    /* Code alignment.  */
11944   0x78,                                 /* Data alignment.  */
11945   65,                                   /* RA reg.  */
11946   1,                                    /* Augmentation size.  */
11947   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
11948   DW_CFA_def_cfa, 1, 0,                 /* def_cfa: r1 offset 0.  */
11949   0, 0, 0, 0
11950 };
11951
11952 /* Stripping output sections is normally done before dynamic section
11953    symbols have been allocated.  This function is called later, and
11954    handles cases like htab->brlt which is mapped to its own output
11955    section.  */
11956
11957 static void
11958 maybe_strip_output (struct bfd_link_info *info, asection *isec)
11959 {
11960   if (isec->size == 0
11961       && isec->output_section->size == 0
11962       && !(isec->output_section->flags & SEC_KEEP)
11963       && !bfd_section_removed_from_list (info->output_bfd,
11964                                          isec->output_section)
11965       && elf_section_data (isec->output_section)->dynindx == 0)
11966     {
11967       isec->output_section->flags |= SEC_EXCLUDE;
11968       bfd_section_list_remove (info->output_bfd, isec->output_section);
11969       info->output_bfd->section_count--;
11970     }
11971 }
11972
11973 /* Determine and set the size of the stub section for a final link.
11974
11975    The basic idea here is to examine all the relocations looking for
11976    PC-relative calls to a target that is unreachable with a "bl"
11977    instruction.  */
11978
11979 bfd_boolean
11980 ppc64_elf_size_stubs (struct bfd_link_info *info)
11981 {
11982   bfd_size_type stub_group_size;
11983   bfd_boolean stubs_always_before_branch;
11984   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11985
11986   if (htab == NULL)
11987     return FALSE;
11988
11989   if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
11990     htab->params->plt_thread_safe = 1;
11991   if (!htab->opd_abi)
11992     htab->params->plt_thread_safe = 0;
11993   else if (htab->params->plt_thread_safe == -1)
11994     {
11995       static const char *const thread_starter[] =
11996         {
11997           "pthread_create",
11998           /* libstdc++ */
11999           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12000           /* librt */
12001           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12002           "mq_notify", "create_timer",
12003           /* libanl */
12004           "getaddrinfo_a",
12005           /* libgomp */
12006           "GOMP_parallel",
12007           "GOMP_parallel_start",
12008           "GOMP_parallel_loop_static",
12009           "GOMP_parallel_loop_static_start",
12010           "GOMP_parallel_loop_dynamic",
12011           "GOMP_parallel_loop_dynamic_start",
12012           "GOMP_parallel_loop_guided",
12013           "GOMP_parallel_loop_guided_start",
12014           "GOMP_parallel_loop_runtime",
12015           "GOMP_parallel_loop_runtime_start",
12016           "GOMP_parallel_sections",
12017           "GOMP_parallel_sections_start",
12018           /* libgo */
12019           "__go_go",
12020         };
12021       unsigned i;
12022
12023       for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
12024         {
12025           struct elf_link_hash_entry *h;
12026           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12027                                     FALSE, FALSE, TRUE);
12028           htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12029           if (htab->params->plt_thread_safe)
12030             break;
12031         }
12032     }
12033   stubs_always_before_branch = htab->params->group_size < 0;
12034   if (htab->params->group_size < 0)
12035     stub_group_size = -htab->params->group_size;
12036   else
12037     stub_group_size = htab->params->group_size;
12038
12039   if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12040     return FALSE;
12041
12042   while (1)
12043     {
12044       bfd *input_bfd;
12045       unsigned int bfd_indx;
12046       asection *stub_sec;
12047
12048       htab->stub_iteration += 1;
12049
12050       for (input_bfd = info->input_bfds, bfd_indx = 0;
12051            input_bfd != NULL;
12052            input_bfd = input_bfd->link.next, bfd_indx++)
12053         {
12054           Elf_Internal_Shdr *symtab_hdr;
12055           asection *section;
12056           Elf_Internal_Sym *local_syms = NULL;
12057
12058           if (!is_ppc64_elf (input_bfd))
12059             continue;
12060
12061           /* We'll need the symbol table in a second.  */
12062           symtab_hdr = &elf_symtab_hdr (input_bfd);
12063           if (symtab_hdr->sh_info == 0)
12064             continue;
12065
12066           /* Walk over each section attached to the input bfd.  */
12067           for (section = input_bfd->sections;
12068                section != NULL;
12069                section = section->next)
12070             {
12071               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12072
12073               /* If there aren't any relocs, then there's nothing more
12074                  to do.  */
12075               if ((section->flags & SEC_RELOC) == 0
12076                   || (section->flags & SEC_ALLOC) == 0
12077                   || (section->flags & SEC_LOAD) == 0
12078                   || (section->flags & SEC_CODE) == 0
12079                   || section->reloc_count == 0)
12080                 continue;
12081
12082               /* If this section is a link-once section that will be
12083                  discarded, then don't create any stubs.  */
12084               if (section->output_section == NULL
12085                   || section->output_section->owner != info->output_bfd)
12086                 continue;
12087
12088               /* Get the relocs.  */
12089               internal_relocs
12090                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12091                                              info->keep_memory);
12092               if (internal_relocs == NULL)
12093                 goto error_ret_free_local;
12094
12095               /* Now examine each relocation.  */
12096               irela = internal_relocs;
12097               irelaend = irela + section->reloc_count;
12098               for (; irela < irelaend; irela++)
12099                 {
12100                   enum elf_ppc64_reloc_type r_type;
12101                   unsigned int r_indx;
12102                   enum ppc_stub_type stub_type;
12103                   struct ppc_stub_hash_entry *stub_entry;
12104                   asection *sym_sec, *code_sec;
12105                   bfd_vma sym_value, code_value;
12106                   bfd_vma destination;
12107                   unsigned long local_off;
12108                   bfd_boolean ok_dest;
12109                   struct ppc_link_hash_entry *hash;
12110                   struct ppc_link_hash_entry *fdh;
12111                   struct elf_link_hash_entry *h;
12112                   Elf_Internal_Sym *sym;
12113                   char *stub_name;
12114                   const asection *id_sec;
12115                   struct _opd_sec_data *opd;
12116                   struct plt_entry *plt_ent;
12117
12118                   r_type = ELF64_R_TYPE (irela->r_info);
12119                   r_indx = ELF64_R_SYM (irela->r_info);
12120
12121                   if (r_type >= R_PPC64_max)
12122                     {
12123                       bfd_set_error (bfd_error_bad_value);
12124                       goto error_ret_free_internal;
12125                     }
12126
12127                   /* Only look for stubs on branch instructions.  */
12128                   if (r_type != R_PPC64_REL24
12129                       && r_type != R_PPC64_REL14
12130                       && r_type != R_PPC64_REL14_BRTAKEN
12131                       && r_type != R_PPC64_REL14_BRNTAKEN)
12132                     continue;
12133
12134                   /* Now determine the call target, its name, value,
12135                      section.  */
12136                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12137                                   r_indx, input_bfd))
12138                     goto error_ret_free_internal;
12139                   hash = (struct ppc_link_hash_entry *) h;
12140
12141                   ok_dest = FALSE;
12142                   fdh = NULL;
12143                   sym_value = 0;
12144                   if (hash == NULL)
12145                     {
12146                       sym_value = sym->st_value;
12147                       ok_dest = TRUE;
12148                     }
12149                   else if (hash->elf.root.type == bfd_link_hash_defined
12150                            || hash->elf.root.type == bfd_link_hash_defweak)
12151                     {
12152                       sym_value = hash->elf.root.u.def.value;
12153                       if (sym_sec->output_section != NULL)
12154                         ok_dest = TRUE;
12155                     }
12156                   else if (hash->elf.root.type == bfd_link_hash_undefweak
12157                            || hash->elf.root.type == bfd_link_hash_undefined)
12158                     {
12159                       /* Recognise an old ABI func code entry sym, and
12160                          use the func descriptor sym instead if it is
12161                          defined.  */
12162                       if (hash->elf.root.root.string[0] == '.'
12163                           && (fdh = lookup_fdh (hash, htab)) != NULL)
12164                         {
12165                           if (fdh->elf.root.type == bfd_link_hash_defined
12166                               || fdh->elf.root.type == bfd_link_hash_defweak)
12167                             {
12168                               sym_sec = fdh->elf.root.u.def.section;
12169                               sym_value = fdh->elf.root.u.def.value;
12170                               if (sym_sec->output_section != NULL)
12171                                 ok_dest = TRUE;
12172                             }
12173                           else
12174                             fdh = NULL;
12175                         }
12176                     }
12177                   else
12178                     {
12179                       bfd_set_error (bfd_error_bad_value);
12180                       goto error_ret_free_internal;
12181                     }
12182
12183                   destination = 0;
12184                   local_off = 0;
12185                   if (ok_dest)
12186                     {
12187                       sym_value += irela->r_addend;
12188                       destination = (sym_value
12189                                      + sym_sec->output_offset
12190                                      + sym_sec->output_section->vma);
12191                       local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12192                                                             ? hash->elf.other
12193                                                             : sym->st_other);
12194                     }
12195
12196                   code_sec = sym_sec;
12197                   code_value = sym_value;
12198                   opd = get_opd_info (sym_sec);
12199                   if (opd != NULL)
12200                     {
12201                       bfd_vma dest;
12202
12203                       if (hash == NULL && opd->adjust != NULL)
12204                         {
12205                           long adjust = opd->adjust[OPD_NDX (sym_value)];
12206                           if (adjust == -1)
12207                             continue;
12208                           code_value += adjust;
12209                           sym_value += adjust;
12210                         }
12211                       dest = opd_entry_value (sym_sec, sym_value,
12212                                               &code_sec, &code_value, FALSE);
12213                       if (dest != (bfd_vma) -1)
12214                         {
12215                           destination = dest;
12216                           if (fdh != NULL)
12217                             {
12218                               /* Fixup old ABI sym to point at code
12219                                  entry.  */
12220                               hash->elf.root.type = bfd_link_hash_defweak;
12221                               hash->elf.root.u.def.section = code_sec;
12222                               hash->elf.root.u.def.value = code_value;
12223                             }
12224                         }
12225                     }
12226
12227                   /* Determine what (if any) linker stub is needed.  */
12228                   plt_ent = NULL;
12229                   stub_type = ppc_type_of_stub (section, irela, &hash,
12230                                                 &plt_ent, destination,
12231                                                 local_off);
12232
12233                   if (stub_type != ppc_stub_plt_call)
12234                     {
12235                       /* Check whether we need a TOC adjusting stub.
12236                          Since the linker pastes together pieces from
12237                          different object files when creating the
12238                          _init and _fini functions, it may be that a
12239                          call to what looks like a local sym is in
12240                          fact a call needing a TOC adjustment.  */
12241                       if (code_sec != NULL
12242                           && code_sec->output_section != NULL
12243                           && (htab->sec_info[code_sec->id].toc_off
12244                               != htab->sec_info[section->id].toc_off)
12245                           && (code_sec->has_toc_reloc
12246                               || code_sec->makes_toc_func_call))
12247                         stub_type = ppc_stub_long_branch_r2off;
12248                     }
12249
12250                   if (stub_type == ppc_stub_none)
12251                     continue;
12252
12253                   /* __tls_get_addr calls might be eliminated.  */
12254                   if (stub_type != ppc_stub_plt_call
12255                       && hash != NULL
12256                       && (hash == htab->tls_get_addr
12257                           || hash == htab->tls_get_addr_fd)
12258                       && section->has_tls_reloc
12259                       && irela != internal_relocs)
12260                     {
12261                       /* Get tls info.  */
12262                       unsigned char *tls_mask;
12263
12264                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12265                                          irela - 1, input_bfd))
12266                         goto error_ret_free_internal;
12267                       if (*tls_mask != 0)
12268                         continue;
12269                     }
12270
12271                   if (stub_type == ppc_stub_plt_call
12272                       && irela + 1 < irelaend
12273                       && irela[1].r_offset == irela->r_offset + 4
12274                       && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12275                     {
12276                       if (!tocsave_find (htab, INSERT,
12277                                          &local_syms, irela + 1, input_bfd))
12278                         goto error_ret_free_internal;
12279                     }
12280                   else if (stub_type == ppc_stub_plt_call)
12281                     stub_type = ppc_stub_plt_call_r2save;
12282
12283                   /* Support for grouping stub sections.  */
12284                   id_sec = htab->sec_info[section->id].u.group->link_sec;
12285
12286                   /* Get the name of this stub.  */
12287                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12288                   if (!stub_name)
12289                     goto error_ret_free_internal;
12290
12291                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12292                                                      stub_name, FALSE, FALSE);
12293                   if (stub_entry != NULL)
12294                     {
12295                       /* The proper stub has already been created.  */
12296                       free (stub_name);
12297                       if (stub_type == ppc_stub_plt_call_r2save)
12298                         stub_entry->stub_type = stub_type;
12299                       continue;
12300                     }
12301
12302                   stub_entry = ppc_add_stub (stub_name, section, info);
12303                   if (stub_entry == NULL)
12304                     {
12305                       free (stub_name);
12306                     error_ret_free_internal:
12307                       if (elf_section_data (section)->relocs == NULL)
12308                         free (internal_relocs);
12309                     error_ret_free_local:
12310                       if (local_syms != NULL
12311                           && (symtab_hdr->contents
12312                               != (unsigned char *) local_syms))
12313                         free (local_syms);
12314                       return FALSE;
12315                     }
12316
12317                   stub_entry->stub_type = stub_type;
12318                   if (stub_type != ppc_stub_plt_call
12319                       && stub_type != ppc_stub_plt_call_r2save)
12320                     {
12321                       stub_entry->target_value = code_value;
12322                       stub_entry->target_section = code_sec;
12323                     }
12324                   else
12325                     {
12326                       stub_entry->target_value = sym_value;
12327                       stub_entry->target_section = sym_sec;
12328                     }
12329                   stub_entry->h = hash;
12330                   stub_entry->plt_ent = plt_ent;
12331                   stub_entry->other = hash ? hash->elf.other : sym->st_other;
12332
12333                   if (stub_entry->h != NULL)
12334                     htab->stub_globals += 1;
12335                 }
12336
12337               /* We're done with the internal relocs, free them.  */
12338               if (elf_section_data (section)->relocs != internal_relocs)
12339                 free (internal_relocs);
12340             }
12341
12342           if (local_syms != NULL
12343               && symtab_hdr->contents != (unsigned char *) local_syms)
12344             {
12345               if (!info->keep_memory)
12346                 free (local_syms);
12347               else
12348                 symtab_hdr->contents = (unsigned char *) local_syms;
12349             }
12350         }
12351
12352       /* We may have added some stubs.  Find out the new size of the
12353          stub sections.  */
12354       for (stub_sec = htab->params->stub_bfd->sections;
12355            stub_sec != NULL;
12356            stub_sec = stub_sec->next)
12357         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12358           {
12359             stub_sec->rawsize = stub_sec->size;
12360             stub_sec->size = 0;
12361             stub_sec->reloc_count = 0;
12362             stub_sec->flags &= ~SEC_RELOC;
12363           }
12364
12365       htab->brlt->size = 0;
12366       htab->brlt->reloc_count = 0;
12367       htab->brlt->flags &= ~SEC_RELOC;
12368       if (htab->relbrlt != NULL)
12369         htab->relbrlt->size = 0;
12370
12371       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12372
12373       if (info->emitrelocations
12374           && htab->glink != NULL && htab->glink->size != 0)
12375         {
12376           htab->glink->reloc_count = 1;
12377           htab->glink->flags |= SEC_RELOC;
12378         }
12379
12380       if (htab->glink_eh_frame != NULL
12381           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12382           && htab->glink_eh_frame->output_section->size != 0)
12383         {
12384           size_t size = 0, align;
12385
12386           for (stub_sec = htab->params->stub_bfd->sections;
12387                stub_sec != NULL;
12388                stub_sec = stub_sec->next)
12389             if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12390               size += 24;
12391           if (htab->glink != NULL && htab->glink->size != 0)
12392             size += 24;
12393           if (size != 0)
12394             size += sizeof (glink_eh_frame_cie);
12395           align = 1;
12396           align <<= htab->glink_eh_frame->output_section->alignment_power;
12397           align -= 1;
12398           size = (size + align) & ~align;
12399           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12400           htab->glink_eh_frame->size = size;
12401         }
12402
12403       if (htab->params->plt_stub_align != 0)
12404         for (stub_sec = htab->params->stub_bfd->sections;
12405              stub_sec != NULL;
12406              stub_sec = stub_sec->next)
12407           if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12408             stub_sec->size = ((stub_sec->size
12409                                + (1 << htab->params->plt_stub_align) - 1)
12410                               & (-1 << htab->params->plt_stub_align));
12411
12412       for (stub_sec = htab->params->stub_bfd->sections;
12413            stub_sec != NULL;
12414            stub_sec = stub_sec->next)
12415         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12416             && stub_sec->rawsize != stub_sec->size)
12417           break;
12418
12419       /* Exit from this loop when no stubs have been added, and no stubs
12420          have changed size.  */
12421       if (stub_sec == NULL
12422           && (htab->glink_eh_frame == NULL
12423               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12424         break;
12425
12426       /* Ask the linker to do its stuff.  */
12427       (*htab->params->layout_sections_again) ();
12428     }
12429
12430   if (htab->glink_eh_frame != NULL
12431       && htab->glink_eh_frame->size != 0)
12432     {
12433       bfd_vma val;
12434       bfd_byte *p, *last_fde;
12435       size_t last_fde_len, size, align, pad;
12436       asection *stub_sec;
12437
12438       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12439       if (p == NULL)
12440         return FALSE;
12441       htab->glink_eh_frame->contents = p;
12442       last_fde = p;
12443
12444       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12445       /* CIE length (rewrite in case little-endian).  */
12446       last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12447       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12448       p += sizeof (glink_eh_frame_cie);
12449
12450       for (stub_sec = htab->params->stub_bfd->sections;
12451            stub_sec != NULL;
12452            stub_sec = stub_sec->next)
12453         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12454           {
12455             last_fde = p;
12456             last_fde_len = 20;
12457             /* FDE length.  */
12458             bfd_put_32 (htab->elf.dynobj, 20, p);
12459             p += 4;
12460             /* CIE pointer.  */
12461             val = p - htab->glink_eh_frame->contents;
12462             bfd_put_32 (htab->elf.dynobj, val, p);
12463             p += 4;
12464             /* Offset to stub section, written later.  */
12465             p += 4;
12466             /* stub section size.  */
12467             bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
12468             p += 4;
12469             /* Augmentation.  */
12470             p += 1;
12471             /* Pad.  */
12472             p += 7;
12473           }
12474       if (htab->glink != NULL && htab->glink->size != 0)
12475         {
12476           last_fde = p;
12477           last_fde_len = 20;
12478           /* FDE length.  */
12479           bfd_put_32 (htab->elf.dynobj, 20, p);
12480           p += 4;
12481           /* CIE pointer.  */
12482           val = p - htab->glink_eh_frame->contents;
12483           bfd_put_32 (htab->elf.dynobj, val, p);
12484           p += 4;
12485           /* Offset to .glink, written later.  */
12486           p += 4;
12487           /* .glink size.  */
12488           bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12489           p += 4;
12490           /* Augmentation.  */
12491           p += 1;
12492
12493           *p++ = DW_CFA_advance_loc + 1;
12494           *p++ = DW_CFA_register;
12495           *p++ = 65;
12496           *p++ = 12;
12497           *p++ = DW_CFA_advance_loc + 4;
12498           *p++ = DW_CFA_restore_extended;
12499           *p++ = 65;
12500         }
12501       /* Subsume any padding into the last FDE if user .eh_frame
12502          sections are aligned more than glink_eh_frame.  Otherwise any
12503          zero padding will be seen as a terminator.  */
12504       size = p - htab->glink_eh_frame->contents;
12505       align = 1;
12506       align <<= htab->glink_eh_frame->output_section->alignment_power;
12507       align -= 1;
12508       pad = ((size + align) & ~align) - size;
12509       htab->glink_eh_frame->size = size + pad;
12510       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12511     }
12512
12513   maybe_strip_output (info, htab->brlt);
12514   if (htab->glink_eh_frame != NULL)
12515     maybe_strip_output (info, htab->glink_eh_frame);
12516
12517   return TRUE;
12518 }
12519
12520 /* Called after we have determined section placement.  If sections
12521    move, we'll be called again.  Provide a value for TOCstart.  */
12522
12523 bfd_vma
12524 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12525 {
12526   asection *s;
12527   bfd_vma TOCstart, adjust;
12528
12529   if (info != NULL)
12530     {
12531       struct elf_link_hash_entry *h;
12532       struct elf_link_hash_table *htab = elf_hash_table (info);
12533
12534       if (is_elf_hash_table (htab)
12535           && htab->hgot != NULL)
12536         h = htab->hgot;
12537       else
12538         {
12539           h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12540           if (is_elf_hash_table (htab))
12541             htab->hgot = h;
12542         }
12543       if (h != NULL
12544           && h->root.type == bfd_link_hash_defined
12545           && !h->root.linker_def
12546           && (!is_elf_hash_table (htab)
12547               || h->def_regular))
12548         {
12549           TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12550                       + h->root.u.def.section->output_offset
12551                       + h->root.u.def.section->output_section->vma);
12552           _bfd_set_gp_value (obfd, TOCstart);
12553           return TOCstart;
12554         }
12555     }
12556
12557   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12558      order.  The TOC starts where the first of these sections starts.  */
12559   s = bfd_get_section_by_name (obfd, ".got");
12560   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12561     s = bfd_get_section_by_name (obfd, ".toc");
12562   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12563     s = bfd_get_section_by_name (obfd, ".tocbss");
12564   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12565     s = bfd_get_section_by_name (obfd, ".plt");
12566   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12567     {
12568       /* This may happen for
12569          o  references to TOC base (SYM@toc / TOC[tc0]) without a
12570          .toc directive
12571          o  bad linker script
12572          o --gc-sections and empty TOC sections
12573
12574          FIXME: Warn user?  */
12575
12576       /* Look for a likely section.  We probably won't even be
12577          using TOCstart.  */
12578       for (s = obfd->sections; s != NULL; s = s->next)
12579         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12580                          | SEC_EXCLUDE))
12581             == (SEC_ALLOC | SEC_SMALL_DATA))
12582           break;
12583       if (s == NULL)
12584         for (s = obfd->sections; s != NULL; s = s->next)
12585           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12586               == (SEC_ALLOC | SEC_SMALL_DATA))
12587             break;
12588       if (s == NULL)
12589         for (s = obfd->sections; s != NULL; s = s->next)
12590           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12591               == SEC_ALLOC)
12592             break;
12593       if (s == NULL)
12594         for (s = obfd->sections; s != NULL; s = s->next)
12595           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12596             break;
12597     }
12598
12599   TOCstart = 0;
12600   if (s != NULL)
12601     TOCstart = s->output_section->vma + s->output_offset;
12602
12603   /* Force alignment.  */
12604   adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12605   TOCstart -= adjust;
12606   _bfd_set_gp_value (obfd, TOCstart);
12607
12608   if (info != NULL && s != NULL)
12609     {
12610       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12611
12612       if (htab != NULL)
12613         {
12614           if (htab->elf.hgot != NULL)
12615             {
12616               htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
12617               htab->elf.hgot->root.u.def.section = s;
12618             }
12619         }
12620       else
12621         {
12622           struct bfd_link_hash_entry *bh = NULL;
12623           _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
12624                                             s, TOC_BASE_OFF - adjust,
12625                                             NULL, FALSE, FALSE, &bh);
12626         }
12627     }
12628   return TOCstart;
12629 }
12630
12631 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12632    write out any global entry stubs.  */
12633
12634 static bfd_boolean
12635 build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12636 {
12637   struct bfd_link_info *info;
12638   struct ppc_link_hash_table *htab;
12639   struct plt_entry *pent;
12640   asection *s;
12641
12642   if (h->root.type == bfd_link_hash_indirect)
12643     return TRUE;
12644
12645   if (!h->pointer_equality_needed)
12646     return TRUE;
12647
12648   if (h->def_regular)
12649     return TRUE;
12650
12651   info = inf;
12652   htab = ppc_hash_table (info);
12653   if (htab == NULL)
12654     return FALSE;
12655
12656   s = htab->glink;
12657   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12658     if (pent->plt.offset != (bfd_vma) -1
12659         && pent->addend == 0)
12660       {
12661         bfd_byte *p;
12662         asection *plt;
12663         bfd_vma off;
12664
12665         p = s->contents + h->root.u.def.value;
12666         plt = htab->elf.splt;
12667         if (!htab->elf.dynamic_sections_created
12668             || h->dynindx == -1)
12669           plt = htab->elf.iplt;
12670         off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12671         off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12672
12673         if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12674           {
12675             info->callbacks->einfo
12676               (_("%P: linkage table error against `%T'\n"),
12677                h->root.root.string);
12678             bfd_set_error (bfd_error_bad_value);
12679             htab->stub_error = TRUE;
12680           }
12681
12682         htab->stub_count[ppc_stub_global_entry - 1] += 1;
12683         if (htab->params->emit_stub_syms)
12684           {
12685             size_t len = strlen (h->root.root.string);
12686             char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12687
12688             if (name == NULL)
12689               return FALSE;
12690
12691             sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12692             h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12693             if (h == NULL)
12694               return FALSE;
12695             if (h->root.type == bfd_link_hash_new)
12696               {
12697                 h->root.type = bfd_link_hash_defined;
12698                 h->root.u.def.section = s;
12699                 h->root.u.def.value = p - s->contents;
12700                 h->ref_regular = 1;
12701                 h->def_regular = 1;
12702                 h->ref_regular_nonweak = 1;
12703                 h->forced_local = 1;
12704                 h->non_elf = 0;
12705                 h->root.linker_def = 1;
12706               }
12707           }
12708
12709         if (PPC_HA (off) != 0)
12710           {
12711             bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12712             p += 4;
12713           }
12714         bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12715         p += 4;
12716         bfd_put_32 (s->owner, MTCTR_R12, p);
12717         p += 4;
12718         bfd_put_32 (s->owner, BCTR, p);
12719         break;
12720       }
12721   return TRUE;
12722 }
12723
12724 /* Build all the stubs associated with the current output file.
12725    The stubs are kept in a hash table attached to the main linker
12726    hash table.  This function is called via gldelf64ppc_finish.  */
12727
12728 bfd_boolean
12729 ppc64_elf_build_stubs (struct bfd_link_info *info,
12730                        char **stats)
12731 {
12732   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12733   asection *stub_sec;
12734   bfd_byte *p;
12735   int stub_sec_count = 0;
12736
12737   if (htab == NULL)
12738     return FALSE;
12739
12740   /* Allocate memory to hold the linker stubs.  */
12741   for (stub_sec = htab->params->stub_bfd->sections;
12742        stub_sec != NULL;
12743        stub_sec = stub_sec->next)
12744     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12745         && stub_sec->size != 0)
12746       {
12747         stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
12748         if (stub_sec->contents == NULL)
12749           return FALSE;
12750         /* We want to check that built size is the same as calculated
12751            size.  rawsize is a convenient location to use.  */
12752         stub_sec->rawsize = stub_sec->size;
12753         stub_sec->size = 0;
12754       }
12755
12756   if (htab->glink != NULL && htab->glink->size != 0)
12757     {
12758       unsigned int indx;
12759       bfd_vma plt0;
12760
12761       /* Build the .glink plt call stub.  */
12762       if (htab->params->emit_stub_syms)
12763         {
12764           struct elf_link_hash_entry *h;
12765           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12766                                     TRUE, FALSE, FALSE);
12767           if (h == NULL)
12768             return FALSE;
12769           if (h->root.type == bfd_link_hash_new)
12770             {
12771               h->root.type = bfd_link_hash_defined;
12772               h->root.u.def.section = htab->glink;
12773               h->root.u.def.value = 8;
12774               h->ref_regular = 1;
12775               h->def_regular = 1;
12776               h->ref_regular_nonweak = 1;
12777               h->forced_local = 1;
12778               h->non_elf = 0;
12779               h->root.linker_def = 1;
12780             }
12781         }
12782       plt0 = (htab->elf.splt->output_section->vma
12783               + htab->elf.splt->output_offset
12784               - 16);
12785       if (info->emitrelocations)
12786         {
12787           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12788           if (r == NULL)
12789             return FALSE;
12790           r->r_offset = (htab->glink->output_offset
12791                          + htab->glink->output_section->vma);
12792           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12793           r->r_addend = plt0;
12794         }
12795       p = htab->glink->contents;
12796       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
12797       bfd_put_64 (htab->glink->owner, plt0, p);
12798       p += 8;
12799       if (htab->opd_abi)
12800         {
12801           bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12802           p += 4;
12803           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12804           p += 4;
12805           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12806           p += 4;
12807           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12808           p += 4;
12809           bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12810           p += 4;
12811           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12812           p += 4;
12813           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12814           p += 4;
12815           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12816           p += 4;
12817           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12818           p += 4;
12819           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12820           p += 4;
12821         }
12822       else
12823         {
12824           bfd_put_32 (htab->glink->owner, MFLR_R0, p);
12825           p += 4;
12826           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12827           p += 4;
12828           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12829           p += 4;
12830           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12831           p += 4;
12832           bfd_put_32 (htab->glink->owner, MTLR_R0, p);
12833           p += 4;
12834           bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
12835           p += 4;
12836           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12837           p += 4;
12838           bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
12839           p += 4;
12840           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12841           p += 4;
12842           bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
12843           p += 4;
12844           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12845           p += 4;
12846           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
12847           p += 4;
12848         }
12849       bfd_put_32 (htab->glink->owner, BCTR, p);
12850       p += 4;
12851       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
12852         {
12853           bfd_put_32 (htab->glink->owner, NOP, p);
12854           p += 4;
12855         }
12856
12857       /* Build the .glink lazy link call stubs.  */
12858       indx = 0;
12859       while (p < htab->glink->contents + htab->glink->rawsize)
12860         {
12861           if (htab->opd_abi)
12862             {
12863               if (indx < 0x8000)
12864                 {
12865                   bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
12866                   p += 4;
12867                 }
12868               else
12869                 {
12870                   bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
12871                   p += 4;
12872                   bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
12873                               p);
12874                   p += 4;
12875                 }
12876             }
12877           bfd_put_32 (htab->glink->owner,
12878                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
12879           indx++;
12880           p += 4;
12881         }
12882
12883       /* Build .glink global entry stubs.  */
12884       if (htab->glink->size > htab->glink->rawsize)
12885         elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
12886     }
12887
12888   if (htab->brlt != NULL && htab->brlt->size != 0)
12889     {
12890       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
12891                                          htab->brlt->size);
12892       if (htab->brlt->contents == NULL)
12893         return FALSE;
12894     }
12895   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
12896     {
12897       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
12898                                             htab->relbrlt->size);
12899       if (htab->relbrlt->contents == NULL)
12900         return FALSE;
12901     }
12902
12903   /* Build the stubs as directed by the stub hash table.  */
12904   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
12905
12906   if (htab->relbrlt != NULL)
12907     htab->relbrlt->reloc_count = 0;
12908
12909   if (htab->params->plt_stub_align != 0)
12910     for (stub_sec = htab->params->stub_bfd->sections;
12911          stub_sec != NULL;
12912          stub_sec = stub_sec->next)
12913       if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12914         stub_sec->size = ((stub_sec->size
12915                            + (1 << htab->params->plt_stub_align) - 1)
12916                           & (-1 << htab->params->plt_stub_align));
12917
12918   for (stub_sec = htab->params->stub_bfd->sections;
12919        stub_sec != NULL;
12920        stub_sec = stub_sec->next)
12921     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12922       {
12923         stub_sec_count += 1;
12924         if (stub_sec->rawsize != stub_sec->size)
12925           break;
12926       }
12927
12928   /* Note that the glink_eh_frame check here is not only testing that
12929      the generated size matched the calculated size but also that
12930      bfd_elf_discard_info didn't make any changes to the section.  */
12931   if (stub_sec != NULL
12932       || (htab->glink_eh_frame != NULL
12933           && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
12934     {
12935       htab->stub_error = TRUE;
12936       info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
12937     }
12938
12939   if (htab->stub_error)
12940     return FALSE;
12941
12942   if (stats != NULL)
12943     {
12944       *stats = bfd_malloc (500);
12945       if (*stats == NULL)
12946         return FALSE;
12947
12948       sprintf (*stats, _("linker stubs in %u group%s\n"
12949                          "  branch       %lu\n"
12950                          "  toc adjust   %lu\n"
12951                          "  long branch  %lu\n"
12952                          "  long toc adj %lu\n"
12953                          "  plt call     %lu\n"
12954                          "  plt call toc %lu\n"
12955                          "  global entry %lu"),
12956                stub_sec_count,
12957                stub_sec_count == 1 ? "" : "s",
12958                htab->stub_count[ppc_stub_long_branch - 1],
12959                htab->stub_count[ppc_stub_long_branch_r2off - 1],
12960                htab->stub_count[ppc_stub_plt_branch - 1],
12961                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
12962                htab->stub_count[ppc_stub_plt_call - 1],
12963                htab->stub_count[ppc_stub_plt_call_r2save - 1],
12964                htab->stub_count[ppc_stub_global_entry - 1]);
12965     }
12966   return TRUE;
12967 }
12968
12969 /* This function undoes the changes made by add_symbol_adjust.  */
12970
12971 static bfd_boolean
12972 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12973 {
12974   struct ppc_link_hash_entry *eh;
12975
12976   if (h->root.type == bfd_link_hash_indirect)
12977     return TRUE;
12978
12979   eh = (struct ppc_link_hash_entry *) h;
12980   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
12981     return TRUE;
12982
12983   eh->elf.root.type = bfd_link_hash_undefined;
12984   return TRUE;
12985 }
12986
12987 void
12988 ppc64_elf_restore_symbols (struct bfd_link_info *info)
12989 {
12990   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12991
12992   if (htab != NULL)
12993     elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
12994 }
12995
12996 /* What to do when ld finds relocations against symbols defined in
12997    discarded sections.  */
12998
12999 static unsigned int
13000 ppc64_elf_action_discarded (asection *sec)
13001 {
13002   if (strcmp (".opd", sec->name) == 0)
13003     return 0;
13004
13005   if (strcmp (".toc", sec->name) == 0)
13006     return 0;
13007
13008   if (strcmp (".toc1", sec->name) == 0)
13009     return 0;
13010
13011   return _bfd_elf_default_action_discarded (sec);
13012 }
13013
13014 /* The RELOCATE_SECTION function is called by the ELF backend linker
13015    to handle the relocations for a section.
13016
13017    The relocs are always passed as Rela structures; if the section
13018    actually uses Rel structures, the r_addend field will always be
13019    zero.
13020
13021    This function is responsible for adjust the section contents as
13022    necessary, and (if using Rela relocs and generating a
13023    relocatable output file) adjusting the reloc addend as
13024    necessary.
13025
13026    This function does not have to worry about setting the reloc
13027    address or the reloc symbol index.
13028
13029    LOCAL_SYMS is a pointer to the swapped in local symbols.
13030
13031    LOCAL_SECTIONS is an array giving the section in the input file
13032    corresponding to the st_shndx field of each local symbol.
13033
13034    The global hash table entry for the global symbols can be found
13035    via elf_sym_hashes (input_bfd).
13036
13037    When generating relocatable output, this function must handle
13038    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
13039    going to be the section symbol corresponding to the output
13040    section, which means that the addend must be adjusted
13041    accordingly.  */
13042
13043 static bfd_boolean
13044 ppc64_elf_relocate_section (bfd *output_bfd,
13045                             struct bfd_link_info *info,
13046                             bfd *input_bfd,
13047                             asection *input_section,
13048                             bfd_byte *contents,
13049                             Elf_Internal_Rela *relocs,
13050                             Elf_Internal_Sym *local_syms,
13051                             asection **local_sections)
13052 {
13053   struct ppc_link_hash_table *htab;
13054   Elf_Internal_Shdr *symtab_hdr;
13055   struct elf_link_hash_entry **sym_hashes;
13056   Elf_Internal_Rela *rel;
13057   Elf_Internal_Rela *relend;
13058   Elf_Internal_Rela outrel;
13059   bfd_byte *loc;
13060   struct got_entry **local_got_ents;
13061   bfd_vma TOCstart;
13062   bfd_boolean ret = TRUE;
13063   bfd_boolean is_opd;
13064   /* Assume 'at' branch hints.  */
13065   bfd_boolean is_isa_v2 = TRUE;
13066   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
13067
13068   /* Initialize howto table if needed.  */
13069   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13070     ppc_howto_init ();
13071
13072   htab = ppc_hash_table (info);
13073   if (htab == NULL)
13074     return FALSE;
13075
13076   /* Don't relocate stub sections.  */
13077   if (input_section->owner == htab->params->stub_bfd)
13078     return TRUE;
13079
13080   BFD_ASSERT (is_ppc64_elf (input_bfd));
13081
13082   local_got_ents = elf_local_got_ents (input_bfd);
13083   TOCstart = elf_gp (output_bfd);
13084   symtab_hdr = &elf_symtab_hdr (input_bfd);
13085   sym_hashes = elf_sym_hashes (input_bfd);
13086   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13087
13088   rel = relocs;
13089   relend = relocs + input_section->reloc_count;
13090   for (; rel < relend; rel++)
13091     {
13092       enum elf_ppc64_reloc_type r_type;
13093       bfd_vma addend;
13094       bfd_reloc_status_type r;
13095       Elf_Internal_Sym *sym;
13096       asection *sec;
13097       struct elf_link_hash_entry *h_elf;
13098       struct ppc_link_hash_entry *h;
13099       struct ppc_link_hash_entry *fdh;
13100       const char *sym_name;
13101       unsigned long r_symndx, toc_symndx;
13102       bfd_vma toc_addend;
13103       unsigned char tls_mask, tls_gd, tls_type;
13104       unsigned char sym_type;
13105       bfd_vma relocation;
13106       bfd_boolean unresolved_reloc;
13107       bfd_boolean warned;
13108       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13109       unsigned int insn;
13110       unsigned int mask;
13111       struct ppc_stub_hash_entry *stub_entry;
13112       bfd_vma max_br_offset;
13113       bfd_vma from;
13114       const Elf_Internal_Rela orig_rel = *rel;
13115       reloc_howto_type *howto;
13116       struct reloc_howto_struct alt_howto;
13117
13118       r_type = ELF64_R_TYPE (rel->r_info);
13119       r_symndx = ELF64_R_SYM (rel->r_info);
13120
13121       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13122          symbol of the previous ADDR64 reloc.  The symbol gives us the
13123          proper TOC base to use.  */
13124       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13125           && rel != relocs
13126           && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
13127           && is_opd)
13128         r_symndx = ELF64_R_SYM (rel[-1].r_info);
13129
13130       sym = NULL;
13131       sec = NULL;
13132       h_elf = NULL;
13133       sym_name = NULL;
13134       unresolved_reloc = FALSE;
13135       warned = FALSE;
13136
13137       if (r_symndx < symtab_hdr->sh_info)
13138         {
13139           /* It's a local symbol.  */
13140           struct _opd_sec_data *opd;
13141
13142           sym = local_syms + r_symndx;
13143           sec = local_sections[r_symndx];
13144           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13145           sym_type = ELF64_ST_TYPE (sym->st_info);
13146           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13147           opd = get_opd_info (sec);
13148           if (opd != NULL && opd->adjust != NULL)
13149             {
13150               long adjust = opd->adjust[OPD_NDX (sym->st_value
13151                                                  + rel->r_addend)];
13152               if (adjust == -1)
13153                 relocation = 0;
13154               else
13155                 {
13156                   /* If this is a relocation against the opd section sym
13157                      and we have edited .opd, adjust the reloc addend so
13158                      that ld -r and ld --emit-relocs output is correct.
13159                      If it is a reloc against some other .opd symbol,
13160                      then the symbol value will be adjusted later.  */
13161                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13162                     rel->r_addend += adjust;
13163                   else
13164                     relocation += adjust;
13165                 }
13166             }
13167         }
13168       else
13169         {
13170           bfd_boolean ignored;
13171
13172           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13173                                    r_symndx, symtab_hdr, sym_hashes,
13174                                    h_elf, sec, relocation,
13175                                    unresolved_reloc, warned, ignored);
13176           sym_name = h_elf->root.root.string;
13177           sym_type = h_elf->type;
13178           if (sec != NULL
13179               && sec->owner == output_bfd
13180               && strcmp (sec->name, ".opd") == 0)
13181             {
13182               /* This is a symbol defined in a linker script.  All
13183                  such are defined in output sections, even those
13184                  defined by simple assignment from a symbol defined in
13185                  an input section.  Transfer the symbol to an
13186                  appropriate input .opd section, so that a branch to
13187                  this symbol will be mapped to the location specified
13188                  by the opd entry.  */
13189               struct bfd_link_order *lo;
13190               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13191                 if (lo->type == bfd_indirect_link_order)
13192                   {
13193                     asection *isec = lo->u.indirect.section;
13194                     if (h_elf->root.u.def.value >= isec->output_offset
13195                         && h_elf->root.u.def.value < (isec->output_offset
13196                                                       + isec->size))
13197                       {
13198                         h_elf->root.u.def.value -= isec->output_offset;
13199                         h_elf->root.u.def.section = isec;
13200                         sec = isec;
13201                         break;
13202                       }
13203                   }
13204             }
13205         }
13206       h = (struct ppc_link_hash_entry *) h_elf;
13207
13208       if (sec != NULL && discarded_section (sec))
13209         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
13210                                          rel, 1, relend,
13211                                          ppc64_elf_howto_table[r_type], 0,
13212                                          contents);
13213
13214       if (bfd_link_relocatable (info))
13215         continue;
13216
13217       if (h != NULL && &h->elf == htab->elf.hgot)
13218         {
13219           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13220           sec = bfd_abs_section_ptr;
13221           unresolved_reloc = FALSE;
13222         }
13223
13224       /* TLS optimizations.  Replace instruction sequences and relocs
13225          based on information we collected in tls_optimize.  We edit
13226          RELOCS so that --emit-relocs will output something sensible
13227          for the final instruction stream.  */
13228       tls_mask = 0;
13229       tls_gd = 0;
13230       toc_symndx = 0;
13231       if (h != NULL)
13232         tls_mask = h->tls_mask;
13233       else if (local_got_ents != NULL)
13234         {
13235           struct plt_entry **local_plt = (struct plt_entry **)
13236             (local_got_ents + symtab_hdr->sh_info);
13237           unsigned char *lgot_masks = (unsigned char *)
13238             (local_plt + symtab_hdr->sh_info);
13239           tls_mask = lgot_masks[r_symndx];
13240         }
13241       if (tls_mask == 0
13242           && (r_type == R_PPC64_TLS
13243               || r_type == R_PPC64_TLSGD
13244               || r_type == R_PPC64_TLSLD))
13245         {
13246           /* Check for toc tls entries.  */
13247           unsigned char *toc_tls;
13248
13249           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13250                              &local_syms, rel, input_bfd))
13251             return FALSE;
13252
13253           if (toc_tls)
13254             tls_mask = *toc_tls;
13255         }
13256
13257       /* Check that tls relocs are used with tls syms, and non-tls
13258          relocs are used with non-tls syms.  */
13259       if (r_symndx != STN_UNDEF
13260           && r_type != R_PPC64_NONE
13261           && (h == NULL
13262               || h->elf.root.type == bfd_link_hash_defined
13263               || h->elf.root.type == bfd_link_hash_defweak)
13264           && (IS_PPC64_TLS_RELOC (r_type)
13265               != (sym_type == STT_TLS
13266                   || (sym_type == STT_SECTION
13267                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13268         {
13269           if (tls_mask != 0
13270               && (r_type == R_PPC64_TLS
13271                   || r_type == R_PPC64_TLSGD
13272                   || r_type == R_PPC64_TLSLD))
13273             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
13274             ;
13275           else
13276             info->callbacks->einfo
13277               (!IS_PPC64_TLS_RELOC (r_type)
13278                ? _("%P: %H: %s used with TLS symbol `%T'\n")
13279                : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
13280                input_bfd, input_section, rel->r_offset,
13281                ppc64_elf_howto_table[r_type]->name,
13282                sym_name);
13283         }
13284
13285       /* Ensure reloc mapping code below stays sane.  */
13286       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13287           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13288           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
13289           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13290           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13291           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13292           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
13293           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13294           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13295           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13296         abort ();
13297
13298       switch (r_type)
13299         {
13300         default:
13301           break;
13302
13303         case R_PPC64_LO_DS_OPT:
13304           insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
13305           if ((insn & (0x3f << 26)) != 58u << 26)
13306             abort ();
13307           insn += (14u << 26) - (58u << 26);
13308           bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
13309           r_type = R_PPC64_TOC16_LO;
13310           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13311           break;
13312
13313         case R_PPC64_TOC16:
13314         case R_PPC64_TOC16_LO:
13315         case R_PPC64_TOC16_DS:
13316         case R_PPC64_TOC16_LO_DS:
13317           {
13318             /* Check for toc tls entries.  */
13319             unsigned char *toc_tls;
13320             int retval;
13321
13322             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13323                                    &local_syms, rel, input_bfd);
13324             if (retval == 0)
13325               return FALSE;
13326
13327             if (toc_tls)
13328               {
13329                 tls_mask = *toc_tls;
13330                 if (r_type == R_PPC64_TOC16_DS
13331                     || r_type == R_PPC64_TOC16_LO_DS)
13332                   {
13333                     if (tls_mask != 0
13334                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13335                       goto toctprel;
13336                   }
13337                 else
13338                   {
13339                     /* If we found a GD reloc pair, then we might be
13340                        doing a GD->IE transition.  */
13341                     if (retval == 2)
13342                       {
13343                         tls_gd = TLS_TPRELGD;
13344                         if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13345                           goto tls_ldgd_opt;
13346                       }
13347                     else if (retval == 3)
13348                       {
13349                         if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13350                           goto tls_ldgd_opt;
13351                       }
13352                   }
13353               }
13354           }
13355           break;
13356
13357         case R_PPC64_GOT_TPREL16_HI:
13358         case R_PPC64_GOT_TPREL16_HA:
13359           if (tls_mask != 0
13360               && (tls_mask & TLS_TPREL) == 0)
13361             {
13362               rel->r_offset -= d_offset;
13363               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13364               r_type = R_PPC64_NONE;
13365               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13366             }
13367           break;
13368
13369         case R_PPC64_GOT_TPREL16_DS:
13370         case R_PPC64_GOT_TPREL16_LO_DS:
13371           if (tls_mask != 0
13372               && (tls_mask & TLS_TPREL) == 0)
13373             {
13374             toctprel:
13375               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
13376               insn &= 31 << 21;
13377               insn |= 0x3c0d0000;       /* addis 0,13,0 */
13378               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
13379               r_type = R_PPC64_TPREL16_HA;
13380               if (toc_symndx != 0)
13381                 {
13382                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13383                   rel->r_addend = toc_addend;
13384                   /* We changed the symbol.  Start over in order to
13385                      get h, sym, sec etc. right.  */
13386                   rel--;
13387                   continue;
13388                 }
13389               else
13390                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13391             }
13392           break;
13393
13394         case R_PPC64_TLS:
13395           if (tls_mask != 0
13396               && (tls_mask & TLS_TPREL) == 0)
13397             {
13398               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
13399               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13400               if (insn == 0)
13401                 abort ();
13402               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
13403               /* Was PPC64_TLS which sits on insn boundary, now
13404                  PPC64_TPREL16_LO which is at low-order half-word.  */
13405               rel->r_offset += d_offset;
13406               r_type = R_PPC64_TPREL16_LO;
13407               if (toc_symndx != 0)
13408                 {
13409                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13410                   rel->r_addend = toc_addend;
13411                   /* We changed the symbol.  Start over in order to
13412                      get h, sym, sec etc. right.  */
13413                   rel--;
13414                   continue;
13415                 }
13416               else
13417                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13418             }
13419           break;
13420
13421         case R_PPC64_GOT_TLSGD16_HI:
13422         case R_PPC64_GOT_TLSGD16_HA:
13423           tls_gd = TLS_TPRELGD;
13424           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13425             goto tls_gdld_hi;
13426           break;
13427
13428         case R_PPC64_GOT_TLSLD16_HI:
13429         case R_PPC64_GOT_TLSLD16_HA:
13430           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13431             {
13432             tls_gdld_hi:
13433               if ((tls_mask & tls_gd) != 0)
13434                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13435                           + R_PPC64_GOT_TPREL16_DS);
13436               else
13437                 {
13438                   rel->r_offset -= d_offset;
13439                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13440                   r_type = R_PPC64_NONE;
13441                 }
13442               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13443             }
13444           break;
13445
13446         case R_PPC64_GOT_TLSGD16:
13447         case R_PPC64_GOT_TLSGD16_LO:
13448           tls_gd = TLS_TPRELGD;
13449           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13450             goto tls_ldgd_opt;
13451           break;
13452
13453         case R_PPC64_GOT_TLSLD16:
13454         case R_PPC64_GOT_TLSLD16_LO:
13455           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13456             {
13457               unsigned int insn1, insn2, insn3;
13458               bfd_vma offset;
13459
13460             tls_ldgd_opt:
13461               offset = (bfd_vma) -1;
13462               /* If not using the newer R_PPC64_TLSGD/LD to mark
13463                  __tls_get_addr calls, we must trust that the call
13464                  stays with its arg setup insns, ie. that the next
13465                  reloc is the __tls_get_addr call associated with
13466                  the current reloc.  Edit both insns.  */
13467               if (input_section->has_tls_get_addr_call
13468                   && rel + 1 < relend
13469                   && branch_reloc_hash_match (input_bfd, rel + 1,
13470                                               htab->tls_get_addr,
13471                                               htab->tls_get_addr_fd))
13472                 offset = rel[1].r_offset;
13473               /* We read the low GOT_TLS (or TOC16) insn because we
13474                  need to keep the destination reg.  It may be
13475                  something other than the usual r3, and moved to r3
13476                  before the call by intervening code.  */
13477               insn1 = bfd_get_32 (output_bfd,
13478                                   contents + rel->r_offset - d_offset);
13479               if ((tls_mask & tls_gd) != 0)
13480                 {
13481                   /* IE */
13482                   insn1 &= (0x1f << 21) | (0x1f << 16);
13483                   insn1 |= 58 << 26;    /* ld */
13484                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13485                   if (offset != (bfd_vma) -1)
13486                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13487                   if ((tls_mask & TLS_EXPLICIT) == 0)
13488                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13489                               + R_PPC64_GOT_TPREL16_DS);
13490                   else
13491                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13492                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13493                 }
13494               else
13495                 {
13496                   /* LE */
13497                   insn1 &= 0x1f << 21;
13498                   insn1 |= 0x3c0d0000;  /* addis r,13,0 */
13499                   insn2 = 0x38630000;   /* addi 3,3,0 */
13500                   if (tls_gd == 0)
13501                     {
13502                       /* Was an LD reloc.  */
13503                       if (toc_symndx)
13504                         sec = local_sections[toc_symndx];
13505                       for (r_symndx = 0;
13506                            r_symndx < symtab_hdr->sh_info;
13507                            r_symndx++)
13508                         if (local_sections[r_symndx] == sec)
13509                           break;
13510                       if (r_symndx >= symtab_hdr->sh_info)
13511                         r_symndx = STN_UNDEF;
13512                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13513                       if (r_symndx != STN_UNDEF)
13514                         rel->r_addend -= (local_syms[r_symndx].st_value
13515                                           + sec->output_offset
13516                                           + sec->output_section->vma);
13517                     }
13518                   else if (toc_symndx != 0)
13519                     {
13520                       r_symndx = toc_symndx;
13521                       rel->r_addend = toc_addend;
13522                     }
13523                   r_type = R_PPC64_TPREL16_HA;
13524                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13525                   if (offset != (bfd_vma) -1)
13526                     {
13527                       rel[1].r_info = ELF64_R_INFO (r_symndx,
13528                                                     R_PPC64_TPREL16_LO);
13529                       rel[1].r_offset = offset + d_offset;
13530                       rel[1].r_addend = rel->r_addend;
13531                     }
13532                 }
13533               bfd_put_32 (output_bfd, insn1,
13534                           contents + rel->r_offset - d_offset);
13535               if (offset != (bfd_vma) -1)
13536                 {
13537                   insn3 = bfd_get_32 (output_bfd,
13538                                       contents + offset + 4);
13539                   if (insn3 == NOP
13540                       || insn3 == CROR_151515 || insn3 == CROR_313131)
13541                     {
13542                       rel[1].r_offset += 4;
13543                       bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13544                       insn2 = NOP;
13545                     }
13546                   bfd_put_32 (output_bfd, insn2, contents + offset);
13547                 }
13548               if ((tls_mask & tls_gd) == 0
13549                   && (tls_gd == 0 || toc_symndx != 0))
13550                 {
13551                   /* We changed the symbol.  Start over in order
13552                      to get h, sym, sec etc. right.  */
13553                   rel--;
13554                   continue;
13555                 }
13556             }
13557           break;
13558
13559         case R_PPC64_TLSGD:
13560           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13561             {
13562               unsigned int insn2, insn3;
13563               bfd_vma offset = rel->r_offset;
13564
13565               if ((tls_mask & TLS_TPRELGD) != 0)
13566                 {
13567                   /* IE */
13568                   r_type = R_PPC64_NONE;
13569                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13570                 }
13571               else
13572                 {
13573                   /* LE */
13574                   if (toc_symndx != 0)
13575                     {
13576                       r_symndx = toc_symndx;
13577                       rel->r_addend = toc_addend;
13578                     }
13579                   r_type = R_PPC64_TPREL16_LO;
13580                   rel->r_offset = offset + d_offset;
13581                   insn2 = 0x38630000;   /* addi 3,3,0 */
13582                 }
13583               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13584               /* Zap the reloc on the _tls_get_addr call too.  */
13585               BFD_ASSERT (offset == rel[1].r_offset);
13586               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13587               insn3 = bfd_get_32 (output_bfd,
13588                                   contents + offset + 4);
13589               if (insn3 == NOP
13590                   || insn3 == CROR_151515 || insn3 == CROR_313131)
13591                 {
13592                   rel->r_offset += 4;
13593                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13594                   insn2 = NOP;
13595                 }
13596               bfd_put_32 (output_bfd, insn2, contents + offset);
13597               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13598                 {
13599                   rel--;
13600                   continue;
13601                 }
13602             }
13603           break;
13604
13605         case R_PPC64_TLSLD:
13606           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13607             {
13608               unsigned int insn2, insn3;
13609               bfd_vma offset = rel->r_offset;
13610
13611               if (toc_symndx)
13612                 sec = local_sections[toc_symndx];
13613               for (r_symndx = 0;
13614                    r_symndx < symtab_hdr->sh_info;
13615                    r_symndx++)
13616                 if (local_sections[r_symndx] == sec)
13617                   break;
13618               if (r_symndx >= symtab_hdr->sh_info)
13619                 r_symndx = STN_UNDEF;
13620               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13621               if (r_symndx != STN_UNDEF)
13622                 rel->r_addend -= (local_syms[r_symndx].st_value
13623                                   + sec->output_offset
13624                                   + sec->output_section->vma);
13625
13626               r_type = R_PPC64_TPREL16_LO;
13627               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13628               rel->r_offset = offset + d_offset;
13629               /* Zap the reloc on the _tls_get_addr call too.  */
13630               BFD_ASSERT (offset == rel[1].r_offset);
13631               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13632               insn2 = 0x38630000;       /* addi 3,3,0 */
13633               insn3 = bfd_get_32 (output_bfd,
13634                                   contents + offset + 4);
13635               if (insn3 == NOP
13636                   || insn3 == CROR_151515 || insn3 == CROR_313131)
13637                 {
13638                   rel->r_offset += 4;
13639                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13640                   insn2 = NOP;
13641                 }
13642               bfd_put_32 (output_bfd, insn2, contents + offset);
13643               rel--;
13644               continue;
13645             }
13646           break;
13647
13648         case R_PPC64_DTPMOD64:
13649           if (rel + 1 < relend
13650               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13651               && rel[1].r_offset == rel->r_offset + 8)
13652             {
13653               if ((tls_mask & TLS_GD) == 0)
13654                 {
13655                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13656                   if ((tls_mask & TLS_TPRELGD) != 0)
13657                     r_type = R_PPC64_TPREL64;
13658                   else
13659                     {
13660                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13661                       r_type = R_PPC64_NONE;
13662                     }
13663                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13664                 }
13665             }
13666           else
13667             {
13668               if ((tls_mask & TLS_LD) == 0)
13669                 {
13670                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13671                   r_type = R_PPC64_NONE;
13672                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13673                 }
13674             }
13675           break;
13676
13677         case R_PPC64_TPREL64:
13678           if ((tls_mask & TLS_TPREL) == 0)
13679             {
13680               r_type = R_PPC64_NONE;
13681               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13682             }
13683           break;
13684
13685         case R_PPC64_REL16_HA:
13686           /* If we are generating a non-PIC executable, edit
13687              .  0:      addis 2,12,.TOC.-0b@ha
13688              .          addi 2,2,.TOC.-0b@l
13689              used by ELFv2 global entry points to set up r2, to
13690              .          lis 2,.TOC.@ha
13691              .          addi 2,2,.TOC.@l
13692              if .TOC. is in range.  */
13693           if (!bfd_link_pic (info)
13694               && !info->traditional_format
13695               && h != NULL && &h->elf == htab->elf.hgot
13696               && rel + 1 < relend
13697               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
13698               && rel[1].r_offset == rel->r_offset + 4
13699               && rel[1].r_addend == rel->r_addend + 4
13700               && relocation + 0x80008000 <= 0xffffffff)
13701             {
13702               unsigned int insn1, insn2;
13703               bfd_vma offset = rel->r_offset - d_offset;
13704               insn1 = bfd_get_32 (output_bfd, contents + offset);
13705               insn2 = bfd_get_32 (output_bfd, contents + offset + 4);
13706               if ((insn1 & 0xffff0000) == 0x3c4c0000 /* addis 2,12 */
13707                   && (insn2 & 0xffff0000) == 0x38420000 /* addi 2,2 */)
13708                 {
13709                   r_type = R_PPC64_ADDR16_HA;
13710                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13711                   rel->r_addend -= d_offset;
13712                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
13713                   rel[1].r_addend -= d_offset + 4;
13714                   bfd_put_32 (output_bfd, 0x3c400000, contents + offset);
13715                 }
13716             }
13717           break;
13718         }
13719
13720       /* Handle other relocations that tweak non-addend part of insn.  */
13721       insn = 0;
13722       max_br_offset = 1 << 25;
13723       addend = rel->r_addend;
13724       reloc_dest = DEST_NORMAL;
13725       switch (r_type)
13726         {
13727         default:
13728           break;
13729
13730         case R_PPC64_TOCSAVE:
13731           if (relocation + addend == (rel->r_offset
13732                                       + input_section->output_offset
13733                                       + input_section->output_section->vma)
13734               && tocsave_find (htab, NO_INSERT,
13735                                &local_syms, rel, input_bfd))
13736             {
13737               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13738               if (insn == NOP
13739                   || insn == CROR_151515 || insn == CROR_313131)
13740                 bfd_put_32 (input_bfd,
13741                             STD_R2_0R1 + STK_TOC (htab),
13742                             contents + rel->r_offset);
13743             }
13744           break;
13745
13746           /* Branch taken prediction relocations.  */
13747         case R_PPC64_ADDR14_BRTAKEN:
13748         case R_PPC64_REL14_BRTAKEN:
13749           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
13750           /* Fall thru.  */
13751
13752           /* Branch not taken prediction relocations.  */
13753         case R_PPC64_ADDR14_BRNTAKEN:
13754         case R_PPC64_REL14_BRNTAKEN:
13755           insn |= bfd_get_32 (output_bfd,
13756                               contents + rel->r_offset) & ~(0x01 << 21);
13757           /* Fall thru.  */
13758
13759         case R_PPC64_REL14:
13760           max_br_offset = 1 << 15;
13761           /* Fall thru.  */
13762
13763         case R_PPC64_REL24:
13764           /* Calls to functions with a different TOC, such as calls to
13765              shared objects, need to alter the TOC pointer.  This is
13766              done using a linkage stub.  A REL24 branching to these
13767              linkage stubs needs to be followed by a nop, as the nop
13768              will be replaced with an instruction to restore the TOC
13769              base pointer.  */
13770           fdh = h;
13771           if (h != NULL
13772               && h->oh != NULL
13773               && h->oh->is_func_descriptor)
13774             fdh = ppc_follow_link (h->oh);
13775           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13776                                            htab);
13777           if (stub_entry != NULL
13778               && (stub_entry->stub_type == ppc_stub_plt_call
13779                   || stub_entry->stub_type == ppc_stub_plt_call_r2save
13780                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13781                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
13782             {
13783               bfd_boolean can_plt_call = FALSE;
13784
13785               /* All of these stubs will modify r2, so there must be a
13786                  branch and link followed by a nop.  The nop is
13787                  replaced by an insn to restore r2.  */
13788               if (rel->r_offset + 8 <= input_section->size)
13789                 {
13790                   unsigned long br;
13791
13792                   br = bfd_get_32 (input_bfd,
13793                                    contents + rel->r_offset);
13794                   if ((br & 1) != 0)
13795                     {
13796                       unsigned long nop;
13797
13798                       nop = bfd_get_32 (input_bfd,
13799                                         contents + rel->r_offset + 4);
13800                       if (nop == NOP
13801                           || nop == CROR_151515 || nop == CROR_313131)
13802                         {
13803                           if (h != NULL
13804                               && (h == htab->tls_get_addr_fd
13805                                   || h == htab->tls_get_addr)
13806                               && !htab->params->no_tls_get_addr_opt)
13807                             {
13808                               /* Special stub used, leave nop alone.  */
13809                             }
13810                           else
13811                             bfd_put_32 (input_bfd,
13812                                         LD_R2_0R1 + STK_TOC (htab),
13813                                         contents + rel->r_offset + 4);
13814                           can_plt_call = TRUE;
13815                         }
13816                     }
13817                 }
13818
13819               if (!can_plt_call && h != NULL)
13820                 {
13821                   const char *name = h->elf.root.root.string;
13822
13823                   if (*name == '.')
13824                     ++name;
13825
13826                   if (strncmp (name, "__libc_start_main", 17) == 0
13827                       && (name[17] == 0 || name[17] == '@'))
13828                     {
13829                       /* Allow crt1 branch to go via a toc adjusting
13830                          stub.  Other calls that never return could do
13831                          the same, if we could detect such.  */
13832                       can_plt_call = TRUE;
13833                     }
13834                 }
13835
13836               if (!can_plt_call)
13837                 {
13838                   /* g++ as of 20130507 emits self-calls without a
13839                      following nop.  This is arguably wrong since we
13840                      have conflicting information.  On the one hand a
13841                      global symbol and on the other a local call
13842                      sequence, but don't error for this special case.
13843                      It isn't possible to cheaply verify we have
13844                      exactly such a call.  Allow all calls to the same
13845                      section.  */
13846                   asection *code_sec = sec;
13847
13848                   if (get_opd_info (sec) != NULL)
13849                     {
13850                       bfd_vma off = (relocation + addend
13851                                      - sec->output_section->vma
13852                                      - sec->output_offset);
13853
13854                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
13855                     }
13856                   if (code_sec == input_section)
13857                     can_plt_call = TRUE;
13858                 }
13859
13860               if (!can_plt_call)
13861                 {
13862                   if (stub_entry->stub_type == ppc_stub_plt_call
13863                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13864                     info->callbacks->einfo
13865                       (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13866                          "recompile with -fPIC\n"),
13867                        input_bfd, input_section, rel->r_offset, sym_name);
13868                   else
13869                     info->callbacks->einfo
13870                       (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13871                          "(-mcmodel=small toc adjust stub)\n"),
13872                        input_bfd, input_section, rel->r_offset, sym_name);
13873
13874                   bfd_set_error (bfd_error_bad_value);
13875                   ret = FALSE;
13876                 }
13877
13878               if (can_plt_call
13879                   && (stub_entry->stub_type == ppc_stub_plt_call
13880                       || stub_entry->stub_type == ppc_stub_plt_call_r2save))
13881                 unresolved_reloc = FALSE;
13882             }
13883
13884           if ((stub_entry == NULL
13885                || stub_entry->stub_type == ppc_stub_long_branch
13886                || stub_entry->stub_type == ppc_stub_plt_branch)
13887               && get_opd_info (sec) != NULL)
13888             {
13889               /* The branch destination is the value of the opd entry. */
13890               bfd_vma off = (relocation + addend
13891                              - sec->output_section->vma
13892                              - sec->output_offset);
13893               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
13894               if (dest != (bfd_vma) -1)
13895                 {
13896                   relocation = dest;
13897                   addend = 0;
13898                   reloc_dest = DEST_OPD;
13899                 }
13900             }
13901
13902           /* If the branch is out of reach we ought to have a long
13903              branch stub.  */
13904           from = (rel->r_offset
13905                   + input_section->output_offset
13906                   + input_section->output_section->vma);
13907
13908           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
13909                                                   ? fdh->elf.other
13910                                                   : sym->st_other);
13911
13912           if (stub_entry != NULL
13913               && (stub_entry->stub_type == ppc_stub_long_branch
13914                   || stub_entry->stub_type == ppc_stub_plt_branch)
13915               && (r_type == R_PPC64_ADDR14_BRTAKEN
13916                   || r_type == R_PPC64_ADDR14_BRNTAKEN
13917                   || (relocation + addend - from + max_br_offset
13918                       < 2 * max_br_offset)))
13919             /* Don't use the stub if this branch is in range.  */
13920             stub_entry = NULL;
13921
13922           if (stub_entry != NULL)
13923             {
13924               /* Munge up the value and addend so that we call the stub
13925                  rather than the procedure directly.  */
13926               relocation = (stub_entry->stub_offset
13927                             + stub_entry->group->stub_sec->output_offset
13928                             + stub_entry->group->stub_sec->output_section->vma);
13929               addend = 0;
13930               reloc_dest = DEST_STUB;
13931
13932               if ((stub_entry->stub_type == ppc_stub_plt_call
13933                    || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13934                   && (ALWAYS_EMIT_R2SAVE
13935                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13936                   && rel + 1 < relend
13937                   && rel[1].r_offset == rel->r_offset + 4
13938                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
13939                 relocation += 4;
13940             }
13941
13942           if (insn != 0)
13943             {
13944               if (is_isa_v2)
13945                 {
13946                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
13947                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
13948                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
13949                   if ((insn & (0x14 << 21)) == (0x04 << 21))
13950                     insn |= 0x02 << 21;
13951                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
13952                     insn |= 0x08 << 21;
13953                   else
13954                     break;
13955                 }
13956               else
13957                 {
13958                   /* Invert 'y' bit if not the default.  */
13959                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
13960                     insn ^= 0x01 << 21;
13961                 }
13962
13963               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
13964             }
13965
13966           /* NOP out calls to undefined weak functions.
13967              We can thus call a weak function without first
13968              checking whether the function is defined.  */
13969           else if (h != NULL
13970                    && h->elf.root.type == bfd_link_hash_undefweak
13971                    && h->elf.dynindx == -1
13972                    && r_type == R_PPC64_REL24
13973                    && relocation == 0
13974                    && addend == 0)
13975             {
13976               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13977               continue;
13978             }
13979           break;
13980         }
13981
13982       /* Set `addend'.  */
13983       tls_type = 0;
13984       switch (r_type)
13985         {
13986         default:
13987           info->callbacks->einfo
13988             (_("%P: %B: unknown relocation type %d for `%T'\n"),
13989              input_bfd, (int) r_type, sym_name);
13990
13991           bfd_set_error (bfd_error_bad_value);
13992           ret = FALSE;
13993           continue;
13994
13995         case R_PPC64_NONE:
13996         case R_PPC64_TLS:
13997         case R_PPC64_TLSGD:
13998         case R_PPC64_TLSLD:
13999         case R_PPC64_TOCSAVE:
14000         case R_PPC64_GNU_VTINHERIT:
14001         case R_PPC64_GNU_VTENTRY:
14002           continue;
14003
14004           /* GOT16 relocations.  Like an ADDR16 using the symbol's
14005              address in the GOT as relocation value instead of the
14006              symbol's value itself.  Also, create a GOT entry for the
14007              symbol and put the symbol value there.  */
14008         case R_PPC64_GOT_TLSGD16:
14009         case R_PPC64_GOT_TLSGD16_LO:
14010         case R_PPC64_GOT_TLSGD16_HI:
14011         case R_PPC64_GOT_TLSGD16_HA:
14012           tls_type = TLS_TLS | TLS_GD;
14013           goto dogot;
14014
14015         case R_PPC64_GOT_TLSLD16:
14016         case R_PPC64_GOT_TLSLD16_LO:
14017         case R_PPC64_GOT_TLSLD16_HI:
14018         case R_PPC64_GOT_TLSLD16_HA:
14019           tls_type = TLS_TLS | TLS_LD;
14020           goto dogot;
14021
14022         case R_PPC64_GOT_TPREL16_DS:
14023         case R_PPC64_GOT_TPREL16_LO_DS:
14024         case R_PPC64_GOT_TPREL16_HI:
14025         case R_PPC64_GOT_TPREL16_HA:
14026           tls_type = TLS_TLS | TLS_TPREL;
14027           goto dogot;
14028
14029         case R_PPC64_GOT_DTPREL16_DS:
14030         case R_PPC64_GOT_DTPREL16_LO_DS:
14031         case R_PPC64_GOT_DTPREL16_HI:
14032         case R_PPC64_GOT_DTPREL16_HA:
14033           tls_type = TLS_TLS | TLS_DTPREL;
14034           goto dogot;
14035
14036         case R_PPC64_GOT16:
14037         case R_PPC64_GOT16_LO:
14038         case R_PPC64_GOT16_HI:
14039         case R_PPC64_GOT16_HA:
14040         case R_PPC64_GOT16_DS:
14041         case R_PPC64_GOT16_LO_DS:
14042         dogot:
14043           {
14044             /* Relocation is to the entry for this symbol in the global
14045                offset table.  */
14046             asection *got;
14047             bfd_vma *offp;
14048             bfd_vma off;
14049             unsigned long indx = 0;
14050             struct got_entry *ent;
14051
14052             if (tls_type == (TLS_TLS | TLS_LD)
14053                 && (h == NULL
14054                     || !h->elf.def_dynamic))
14055               ent = ppc64_tlsld_got (input_bfd);
14056             else
14057               {
14058
14059                 if (h != NULL)
14060                   {
14061                     bfd_boolean dyn = htab->elf.dynamic_sections_created;
14062                     if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info),
14063                                                           &h->elf)
14064                         || (bfd_link_pic (info)
14065                             && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
14066                       /* This is actually a static link, or it is a
14067                          -Bsymbolic link and the symbol is defined
14068                          locally, or the symbol was forced to be local
14069                          because of a version file.  */
14070                       ;
14071                     else
14072                       {
14073                         BFD_ASSERT (h->elf.dynindx != -1);
14074                         indx = h->elf.dynindx;
14075                         unresolved_reloc = FALSE;
14076                       }
14077                     ent = h->elf.got.glist;
14078                   }
14079                 else
14080                   {
14081                     if (local_got_ents == NULL)
14082                       abort ();
14083                     ent = local_got_ents[r_symndx];
14084                   }
14085
14086                 for (; ent != NULL; ent = ent->next)
14087                   if (ent->addend == orig_rel.r_addend
14088                       && ent->owner == input_bfd
14089                       && ent->tls_type == tls_type)
14090                     break;
14091               }
14092
14093             if (ent == NULL)
14094               abort ();
14095             if (ent->is_indirect)
14096               ent = ent->got.ent;
14097             offp = &ent->got.offset;
14098             got = ppc64_elf_tdata (ent->owner)->got;
14099             if (got == NULL)
14100               abort ();
14101
14102             /* The offset must always be a multiple of 8.  We use the
14103                least significant bit to record whether we have already
14104                processed this entry.  */
14105             off = *offp;
14106             if ((off & 1) != 0)
14107               off &= ~1;
14108             else
14109               {
14110                 /* Generate relocs for the dynamic linker, except in
14111                    the case of TLSLD where we'll use one entry per
14112                    module.  */
14113                 asection *relgot;
14114                 bfd_boolean ifunc;
14115
14116                 *offp = off | 1;
14117                 relgot = NULL;
14118                 ifunc = (h != NULL
14119                          ? h->elf.type == STT_GNU_IFUNC
14120                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14121                 if (ifunc)
14122                   relgot = htab->elf.irelplt;
14123                 else if ((bfd_link_pic (info) || indx != 0)
14124                          && (h == NULL
14125                              || (tls_type == (TLS_TLS | TLS_LD)
14126                                  && !h->elf.def_dynamic)
14127                              || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14128                              || h->elf.root.type != bfd_link_hash_undefweak))
14129                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
14130                 if (relgot != NULL)
14131                   {
14132                     outrel.r_offset = (got->output_section->vma
14133                                        + got->output_offset
14134                                        + off);
14135                     outrel.r_addend = addend;
14136                     if (tls_type & (TLS_LD | TLS_GD))
14137                       {
14138                         outrel.r_addend = 0;
14139                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14140                         if (tls_type == (TLS_TLS | TLS_GD))
14141                           {
14142                             loc = relgot->contents;
14143                             loc += (relgot->reloc_count++
14144                                     * sizeof (Elf64_External_Rela));
14145                             bfd_elf64_swap_reloca_out (output_bfd,
14146                                                        &outrel, loc);
14147                             outrel.r_offset += 8;
14148                             outrel.r_addend = addend;
14149                             outrel.r_info
14150                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14151                           }
14152                       }
14153                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
14154                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14155                     else if (tls_type == (TLS_TLS | TLS_TPREL))
14156                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14157                     else if (indx != 0)
14158                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14159                     else
14160                       {
14161                         if (ifunc)
14162                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14163                         else
14164                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14165
14166                         /* Write the .got section contents for the sake
14167                            of prelink.  */
14168                         loc = got->contents + off;
14169                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14170                                     loc);
14171                       }
14172
14173                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14174                       {
14175                         outrel.r_addend += relocation;
14176                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14177                           {
14178                             if (htab->elf.tls_sec == NULL)
14179                               outrel.r_addend = 0;
14180                             else
14181                               outrel.r_addend -= htab->elf.tls_sec->vma;
14182                           }
14183                       }
14184                     loc = relgot->contents;
14185                     loc += (relgot->reloc_count++
14186                             * sizeof (Elf64_External_Rela));
14187                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14188                   }
14189
14190                 /* Init the .got section contents here if we're not
14191                    emitting a reloc.  */
14192                 else
14193                   {
14194                     relocation += addend;
14195                     if (tls_type == (TLS_TLS | TLS_LD))
14196                       relocation = 1;
14197                     else if (tls_type != 0)
14198                       {
14199                         if (htab->elf.tls_sec == NULL)
14200                           relocation = 0;
14201                         else
14202                           {
14203                             relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14204                             if (tls_type == (TLS_TLS | TLS_TPREL))
14205                               relocation += DTP_OFFSET - TP_OFFSET;
14206                           }
14207
14208                         if (tls_type == (TLS_TLS | TLS_GD))
14209                           {
14210                             bfd_put_64 (output_bfd, relocation,
14211                                         got->contents + off + 8);
14212                             relocation = 1;
14213                           }
14214                       }
14215
14216                     bfd_put_64 (output_bfd, relocation,
14217                                 got->contents + off);
14218                   }
14219               }
14220
14221             if (off >= (bfd_vma) -2)
14222               abort ();
14223
14224             relocation = got->output_section->vma + got->output_offset + off;
14225             addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14226           }
14227           break;
14228
14229         case R_PPC64_PLT16_HA:
14230         case R_PPC64_PLT16_HI:
14231         case R_PPC64_PLT16_LO:
14232         case R_PPC64_PLT32:
14233         case R_PPC64_PLT64:
14234           /* Relocation is to the entry for this symbol in the
14235              procedure linkage table.  */
14236
14237           /* Resolve a PLT reloc against a local symbol directly,
14238              without using the procedure linkage table.  */
14239           if (h == NULL)
14240             break;
14241
14242           /* It's possible that we didn't make a PLT entry for this
14243              symbol.  This happens when statically linking PIC code,
14244              or when using -Bsymbolic.  Go find a match if there is a
14245              PLT entry.  */
14246           if (htab->elf.splt != NULL)
14247             {
14248               struct plt_entry *ent;
14249               for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
14250                 if (ent->plt.offset != (bfd_vma) -1
14251                     && ent->addend == orig_rel.r_addend)
14252                   {
14253                     relocation = (htab->elf.splt->output_section->vma
14254                                   + htab->elf.splt->output_offset
14255                                   + ent->plt.offset);
14256                     unresolved_reloc = FALSE;
14257                     break;
14258                   }
14259             }
14260           break;
14261
14262         case R_PPC64_TOC:
14263           /* Relocation value is TOC base.  */
14264           relocation = TOCstart;
14265           if (r_symndx == STN_UNDEF)
14266             relocation += htab->sec_info[input_section->id].toc_off;
14267           else if (unresolved_reloc)
14268             ;
14269           else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14270             relocation += htab->sec_info[sec->id].toc_off;
14271           else
14272             unresolved_reloc = TRUE;
14273           goto dodyn;
14274
14275           /* TOC16 relocs.  We want the offset relative to the TOC base,
14276              which is the address of the start of the TOC plus 0x8000.
14277              The TOC consists of sections .got, .toc, .tocbss, and .plt,
14278              in this order.  */
14279         case R_PPC64_TOC16:
14280         case R_PPC64_TOC16_LO:
14281         case R_PPC64_TOC16_HI:
14282         case R_PPC64_TOC16_DS:
14283         case R_PPC64_TOC16_LO_DS:
14284         case R_PPC64_TOC16_HA:
14285           addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14286           break;
14287
14288           /* Relocate against the beginning of the section.  */
14289         case R_PPC64_SECTOFF:
14290         case R_PPC64_SECTOFF_LO:
14291         case R_PPC64_SECTOFF_HI:
14292         case R_PPC64_SECTOFF_DS:
14293         case R_PPC64_SECTOFF_LO_DS:
14294         case R_PPC64_SECTOFF_HA:
14295           if (sec != NULL)
14296             addend -= sec->output_section->vma;
14297           break;
14298
14299         case R_PPC64_REL16:
14300         case R_PPC64_REL16_LO:
14301         case R_PPC64_REL16_HI:
14302         case R_PPC64_REL16_HA:
14303           break;
14304
14305         case R_PPC64_REL14:
14306         case R_PPC64_REL14_BRNTAKEN:
14307         case R_PPC64_REL14_BRTAKEN:
14308         case R_PPC64_REL24:
14309           break;
14310
14311         case R_PPC64_TPREL16:
14312         case R_PPC64_TPREL16_LO:
14313         case R_PPC64_TPREL16_HI:
14314         case R_PPC64_TPREL16_HA:
14315         case R_PPC64_TPREL16_DS:
14316         case R_PPC64_TPREL16_LO_DS:
14317         case R_PPC64_TPREL16_HIGH:
14318         case R_PPC64_TPREL16_HIGHA:
14319         case R_PPC64_TPREL16_HIGHER:
14320         case R_PPC64_TPREL16_HIGHERA:
14321         case R_PPC64_TPREL16_HIGHEST:
14322         case R_PPC64_TPREL16_HIGHESTA:
14323           if (h != NULL
14324               && h->elf.root.type == bfd_link_hash_undefweak
14325               && h->elf.dynindx == -1)
14326             {
14327               /* Make this relocation against an undefined weak symbol
14328                  resolve to zero.  This is really just a tweak, since
14329                  code using weak externs ought to check that they are
14330                  defined before using them.  */
14331               bfd_byte *p = contents + rel->r_offset - d_offset;
14332
14333               insn = bfd_get_32 (output_bfd, p);
14334               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14335               if (insn != 0)
14336                 bfd_put_32 (output_bfd, insn, p);
14337               break;
14338             }
14339           if (htab->elf.tls_sec != NULL)
14340             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14341           if (bfd_link_pic (info))
14342             /* The TPREL16 relocs shouldn't really be used in shared
14343                libs as they will result in DT_TEXTREL being set, but
14344                support them anyway.  */
14345             goto dodyn;
14346           break;
14347
14348         case R_PPC64_DTPREL16:
14349         case R_PPC64_DTPREL16_LO:
14350         case R_PPC64_DTPREL16_HI:
14351         case R_PPC64_DTPREL16_HA:
14352         case R_PPC64_DTPREL16_DS:
14353         case R_PPC64_DTPREL16_LO_DS:
14354         case R_PPC64_DTPREL16_HIGH:
14355         case R_PPC64_DTPREL16_HIGHA:
14356         case R_PPC64_DTPREL16_HIGHER:
14357         case R_PPC64_DTPREL16_HIGHERA:
14358         case R_PPC64_DTPREL16_HIGHEST:
14359         case R_PPC64_DTPREL16_HIGHESTA:
14360           if (htab->elf.tls_sec != NULL)
14361             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14362           break;
14363
14364         case R_PPC64_ADDR64_LOCAL:
14365           addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14366                                               ? h->elf.other
14367                                               : sym->st_other);
14368           break;
14369
14370         case R_PPC64_DTPMOD64:
14371           relocation = 1;
14372           addend = 0;
14373           goto dodyn;
14374
14375         case R_PPC64_TPREL64:
14376           if (htab->elf.tls_sec != NULL)
14377             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14378           goto dodyn;
14379
14380         case R_PPC64_DTPREL64:
14381           if (htab->elf.tls_sec != NULL)
14382             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14383           /* Fall thru */
14384
14385           /* Relocations that may need to be propagated if this is a
14386              dynamic object.  */
14387         case R_PPC64_REL30:
14388         case R_PPC64_REL32:
14389         case R_PPC64_REL64:
14390         case R_PPC64_ADDR14:
14391         case R_PPC64_ADDR14_BRNTAKEN:
14392         case R_PPC64_ADDR14_BRTAKEN:
14393         case R_PPC64_ADDR16:
14394         case R_PPC64_ADDR16_DS:
14395         case R_PPC64_ADDR16_HA:
14396         case R_PPC64_ADDR16_HI:
14397         case R_PPC64_ADDR16_HIGH:
14398         case R_PPC64_ADDR16_HIGHA:
14399         case R_PPC64_ADDR16_HIGHER:
14400         case R_PPC64_ADDR16_HIGHERA:
14401         case R_PPC64_ADDR16_HIGHEST:
14402         case R_PPC64_ADDR16_HIGHESTA:
14403         case R_PPC64_ADDR16_LO:
14404         case R_PPC64_ADDR16_LO_DS:
14405         case R_PPC64_ADDR24:
14406         case R_PPC64_ADDR32:
14407         case R_PPC64_ADDR64:
14408         case R_PPC64_UADDR16:
14409         case R_PPC64_UADDR32:
14410         case R_PPC64_UADDR64:
14411         dodyn:
14412           if ((input_section->flags & SEC_ALLOC) == 0)
14413             break;
14414
14415           if (NO_OPD_RELOCS && is_opd)
14416             break;
14417
14418           if ((bfd_link_pic (info)
14419                && (h == NULL
14420                    || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14421                    || h->elf.root.type != bfd_link_hash_undefweak)
14422                && (must_be_dyn_reloc (info, r_type)
14423                    || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
14424               || (ELIMINATE_COPY_RELOCS
14425                   && !bfd_link_pic (info)
14426                   && h != NULL
14427                   && h->elf.dynindx != -1
14428                   && !h->elf.non_got_ref
14429                   && !h->elf.def_regular)
14430               || (!bfd_link_pic (info)
14431                   && (h != NULL
14432                       ? h->elf.type == STT_GNU_IFUNC
14433                       : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
14434             {
14435               bfd_boolean skip, relocate;
14436               asection *sreloc;
14437               bfd_vma out_off;
14438
14439               /* When generating a dynamic object, these relocations
14440                  are copied into the output file to be resolved at run
14441                  time.  */
14442
14443               skip = FALSE;
14444               relocate = FALSE;
14445
14446               out_off = _bfd_elf_section_offset (output_bfd, info,
14447                                                  input_section, rel->r_offset);
14448               if (out_off == (bfd_vma) -1)
14449                 skip = TRUE;
14450               else if (out_off == (bfd_vma) -2)
14451                 skip = TRUE, relocate = TRUE;
14452               out_off += (input_section->output_section->vma
14453                           + input_section->output_offset);
14454               outrel.r_offset = out_off;
14455               outrel.r_addend = rel->r_addend;
14456
14457               /* Optimize unaligned reloc use.  */
14458               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14459                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14460                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14461               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14462                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14463                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14464               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14465                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14466                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14467
14468               if (skip)
14469                 memset (&outrel, 0, sizeof outrel);
14470               else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14471                        && !is_opd
14472                        && r_type != R_PPC64_TOC)
14473                 {
14474                   BFD_ASSERT (h->elf.dynindx != -1);
14475                   outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14476                 }
14477               else
14478                 {
14479                   /* This symbol is local, or marked to become local,
14480                      or this is an opd section reloc which must point
14481                      at a local function.  */
14482                   outrel.r_addend += relocation;
14483                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14484                     {
14485                       if (is_opd && h != NULL)
14486                         {
14487                           /* Lie about opd entries.  This case occurs
14488                              when building shared libraries and we
14489                              reference a function in another shared
14490                              lib.  The same thing happens for a weak
14491                              definition in an application that's
14492                              overridden by a strong definition in a
14493                              shared lib.  (I believe this is a generic
14494                              bug in binutils handling of weak syms.)
14495                              In these cases we won't use the opd
14496                              entry in this lib.  */
14497                           unresolved_reloc = FALSE;
14498                         }
14499                       if (!is_opd
14500                           && r_type == R_PPC64_ADDR64
14501                           && (h != NULL
14502                               ? h->elf.type == STT_GNU_IFUNC
14503                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14504                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14505                       else
14506                         {
14507                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14508
14509                           /* We need to relocate .opd contents for ld.so.
14510                              Prelink also wants simple and consistent rules
14511                              for relocs.  This make all RELATIVE relocs have
14512                              *r_offset equal to r_addend.  */
14513                           relocate = TRUE;
14514                         }
14515                     }
14516                   else
14517                     {
14518                       long indx = 0;
14519
14520                       if (h != NULL
14521                           ? h->elf.type == STT_GNU_IFUNC
14522                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14523                         {
14524                           info->callbacks->einfo
14525                             (_("%P: %H: %s for indirect "
14526                                "function `%T' unsupported\n"),
14527                              input_bfd, input_section, rel->r_offset,
14528                              ppc64_elf_howto_table[r_type]->name,
14529                              sym_name);
14530                           ret = FALSE;
14531                         }
14532                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14533                         ;
14534                       else if (sec == NULL || sec->owner == NULL)
14535                         {
14536                           bfd_set_error (bfd_error_bad_value);
14537                           return FALSE;
14538                         }
14539                       else
14540                         {
14541                           asection *osec;
14542
14543                           osec = sec->output_section;
14544                           indx = elf_section_data (osec)->dynindx;
14545
14546                           if (indx == 0)
14547                             {
14548                               if ((osec->flags & SEC_READONLY) == 0
14549                                   && htab->elf.data_index_section != NULL)
14550                                 osec = htab->elf.data_index_section;
14551                               else
14552                                 osec = htab->elf.text_index_section;
14553                               indx = elf_section_data (osec)->dynindx;
14554                             }
14555                           BFD_ASSERT (indx != 0);
14556
14557                           /* We are turning this relocation into one
14558                              against a section symbol, so subtract out
14559                              the output section's address but not the
14560                              offset of the input section in the output
14561                              section.  */
14562                           outrel.r_addend -= osec->vma;
14563                         }
14564
14565                       outrel.r_info = ELF64_R_INFO (indx, r_type);
14566                     }
14567                 }
14568
14569               sreloc = elf_section_data (input_section)->sreloc;
14570               if (h != NULL
14571                   ? h->elf.type == STT_GNU_IFUNC
14572                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14573                 sreloc = htab->elf.irelplt;
14574               if (sreloc == NULL)
14575                 abort ();
14576
14577               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14578                   >= sreloc->size)
14579                 abort ();
14580               loc = sreloc->contents;
14581               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14582               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14583
14584               /* If this reloc is against an external symbol, it will
14585                  be computed at runtime, so there's no need to do
14586                  anything now.  However, for the sake of prelink ensure
14587                  that the section contents are a known value.  */
14588               if (! relocate)
14589                 {
14590                   unresolved_reloc = FALSE;
14591                   /* The value chosen here is quite arbitrary as ld.so
14592                      ignores section contents except for the special
14593                      case of .opd where the contents might be accessed
14594                      before relocation.  Choose zero, as that won't
14595                      cause reloc overflow.  */
14596                   relocation = 0;
14597                   addend = 0;
14598                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14599                      to improve backward compatibility with older
14600                      versions of ld.  */
14601                   if (r_type == R_PPC64_ADDR64)
14602                     addend = outrel.r_addend;
14603                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
14604                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
14605                     addend = (input_section->output_section->vma
14606                               + input_section->output_offset
14607                               + rel->r_offset);
14608                 }
14609             }
14610           break;
14611
14612         case R_PPC64_COPY:
14613         case R_PPC64_GLOB_DAT:
14614         case R_PPC64_JMP_SLOT:
14615         case R_PPC64_JMP_IREL:
14616         case R_PPC64_RELATIVE:
14617           /* We shouldn't ever see these dynamic relocs in relocatable
14618              files.  */
14619           /* Fall through.  */
14620
14621         case R_PPC64_PLTGOT16:
14622         case R_PPC64_PLTGOT16_DS:
14623         case R_PPC64_PLTGOT16_HA:
14624         case R_PPC64_PLTGOT16_HI:
14625         case R_PPC64_PLTGOT16_LO:
14626         case R_PPC64_PLTGOT16_LO_DS:
14627         case R_PPC64_PLTREL32:
14628         case R_PPC64_PLTREL64:
14629           /* These ones haven't been implemented yet.  */
14630
14631           info->callbacks->einfo
14632             (_("%P: %B: %s is not supported for `%T'\n"),
14633              input_bfd,
14634              ppc64_elf_howto_table[r_type]->name, sym_name);
14635
14636           bfd_set_error (bfd_error_invalid_operation);
14637           ret = FALSE;
14638           continue;
14639         }
14640
14641       /* Multi-instruction sequences that access the TOC can be
14642          optimized, eg. addis ra,r2,0; addi rb,ra,x;
14643          to             nop;           addi rb,r2,x;  */
14644       switch (r_type)
14645         {
14646         default:
14647           break;
14648
14649         case R_PPC64_GOT_TLSLD16_HI:
14650         case R_PPC64_GOT_TLSGD16_HI:
14651         case R_PPC64_GOT_TPREL16_HI:
14652         case R_PPC64_GOT_DTPREL16_HI:
14653         case R_PPC64_GOT16_HI:
14654         case R_PPC64_TOC16_HI:
14655           /* These relocs would only be useful if building up an
14656              offset to later add to r2, perhaps in an indexed
14657              addressing mode instruction.  Don't try to optimize.
14658              Unfortunately, the possibility of someone building up an
14659              offset like this or even with the HA relocs, means that
14660              we need to check the high insn when optimizing the low
14661              insn.  */
14662           break;
14663
14664         case R_PPC64_GOT_TLSLD16_HA:
14665         case R_PPC64_GOT_TLSGD16_HA:
14666         case R_PPC64_GOT_TPREL16_HA:
14667         case R_PPC64_GOT_DTPREL16_HA:
14668         case R_PPC64_GOT16_HA:
14669         case R_PPC64_TOC16_HA:
14670           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14671               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14672             {
14673               bfd_byte *p = contents + (rel->r_offset & ~3);
14674               bfd_put_32 (input_bfd, NOP, p);
14675             }
14676           break;
14677
14678         case R_PPC64_GOT_TLSLD16_LO:
14679         case R_PPC64_GOT_TLSGD16_LO:
14680         case R_PPC64_GOT_TPREL16_LO_DS:
14681         case R_PPC64_GOT_DTPREL16_LO_DS:
14682         case R_PPC64_GOT16_LO:
14683         case R_PPC64_GOT16_LO_DS:
14684         case R_PPC64_TOC16_LO:
14685         case R_PPC64_TOC16_LO_DS:
14686           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14687               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14688             {
14689               bfd_byte *p = contents + (rel->r_offset & ~3);
14690               insn = bfd_get_32 (input_bfd, p);
14691               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14692                 {
14693                   /* Transform addic to addi when we change reg.  */
14694                   insn &= ~((0x3f << 26) | (0x1f << 16));
14695                   insn |= (14u << 26) | (2 << 16);
14696                 }
14697               else
14698                 {
14699                   insn &= ~(0x1f << 16);
14700                   insn |= 2 << 16;
14701                 }
14702               bfd_put_32 (input_bfd, insn, p);
14703             }
14704           break;
14705         }
14706
14707       /* Do any further special processing.  */
14708       howto = ppc64_elf_howto_table[(int) r_type];
14709       switch (r_type)
14710         {
14711         default:
14712           break;
14713
14714         case R_PPC64_REL16_HA:
14715         case R_PPC64_ADDR16_HA:
14716         case R_PPC64_ADDR16_HIGHA:
14717         case R_PPC64_ADDR16_HIGHERA:
14718         case R_PPC64_ADDR16_HIGHESTA:
14719         case R_PPC64_TOC16_HA:
14720         case R_PPC64_SECTOFF_HA:
14721         case R_PPC64_TPREL16_HA:
14722         case R_PPC64_TPREL16_HIGHA:
14723         case R_PPC64_TPREL16_HIGHERA:
14724         case R_PPC64_TPREL16_HIGHESTA:
14725         case R_PPC64_DTPREL16_HA:
14726         case R_PPC64_DTPREL16_HIGHA:
14727         case R_PPC64_DTPREL16_HIGHERA:
14728         case R_PPC64_DTPREL16_HIGHESTA:
14729           /* It's just possible that this symbol is a weak symbol
14730              that's not actually defined anywhere. In that case,
14731              'sec' would be NULL, and we should leave the symbol
14732              alone (it will be set to zero elsewhere in the link).  */
14733           if (sec == NULL)
14734             break;
14735           /* Fall thru */
14736
14737         case R_PPC64_GOT16_HA:
14738         case R_PPC64_PLTGOT16_HA:
14739         case R_PPC64_PLT16_HA:
14740         case R_PPC64_GOT_TLSGD16_HA:
14741         case R_PPC64_GOT_TLSLD16_HA:
14742         case R_PPC64_GOT_TPREL16_HA:
14743         case R_PPC64_GOT_DTPREL16_HA:
14744           /* Add 0x10000 if sign bit in 0:15 is set.
14745              Bits 0:15 are not used.  */
14746           addend += 0x8000;
14747           break;
14748
14749         case R_PPC64_ADDR16_DS:
14750         case R_PPC64_ADDR16_LO_DS:
14751         case R_PPC64_GOT16_DS:
14752         case R_PPC64_GOT16_LO_DS:
14753         case R_PPC64_PLT16_LO_DS:
14754         case R_PPC64_SECTOFF_DS:
14755         case R_PPC64_SECTOFF_LO_DS:
14756         case R_PPC64_TOC16_DS:
14757         case R_PPC64_TOC16_LO_DS:
14758         case R_PPC64_PLTGOT16_DS:
14759         case R_PPC64_PLTGOT16_LO_DS:
14760         case R_PPC64_GOT_TPREL16_DS:
14761         case R_PPC64_GOT_TPREL16_LO_DS:
14762         case R_PPC64_GOT_DTPREL16_DS:
14763         case R_PPC64_GOT_DTPREL16_LO_DS:
14764         case R_PPC64_TPREL16_DS:
14765         case R_PPC64_TPREL16_LO_DS:
14766         case R_PPC64_DTPREL16_DS:
14767         case R_PPC64_DTPREL16_LO_DS:
14768           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14769           mask = 3;
14770           /* If this reloc is against an lq insn, then the value must be
14771              a multiple of 16.  This is somewhat of a hack, but the
14772              "correct" way to do this by defining _DQ forms of all the
14773              _DS relocs bloats all reloc switches in this file.  It
14774              doesn't seem to make much sense to use any of these relocs
14775              in data, so testing the insn should be safe.  */
14776           if ((insn & (0x3f << 26)) == (56u << 26))
14777             mask = 15;
14778           if (((relocation + addend) & mask) != 0)
14779             {
14780               info->callbacks->einfo
14781                 (_("%P: %H: error: %s not a multiple of %u\n"),
14782                  input_bfd, input_section, rel->r_offset,
14783                  howto->name,
14784                  mask + 1);
14785               bfd_set_error (bfd_error_bad_value);
14786               ret = FALSE;
14787               continue;
14788             }
14789           break;
14790         }
14791
14792       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
14793          because such sections are not SEC_ALLOC and thus ld.so will
14794          not process them.  */
14795       if (unresolved_reloc
14796           && !((input_section->flags & SEC_DEBUGGING) != 0
14797                && h->elf.def_dynamic)
14798           && _bfd_elf_section_offset (output_bfd, info, input_section,
14799                                       rel->r_offset) != (bfd_vma) -1)
14800         {
14801           info->callbacks->einfo
14802             (_("%P: %H: unresolvable %s against `%T'\n"),
14803              input_bfd, input_section, rel->r_offset,
14804              howto->name,
14805              h->elf.root.root.string);
14806           ret = FALSE;
14807         }
14808
14809       /* 16-bit fields in insns mostly have signed values, but a
14810          few insns have 16-bit unsigned values.  Really, we should
14811          have different reloc types.  */
14812       if (howto->complain_on_overflow != complain_overflow_dont
14813           && howto->dst_mask == 0xffff
14814           && (input_section->flags & SEC_CODE) != 0)
14815         {
14816           enum complain_overflow complain = complain_overflow_signed;
14817
14818           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14819           if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
14820             complain = complain_overflow_bitfield;
14821           else if (howto->rightshift == 0
14822                    ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
14823                       || (insn & (0x3f << 26)) == 24u << 26 /* ori */
14824                       || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
14825                    : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
14826                       || (insn & (0x3f << 26)) == 25u << 26 /* oris */
14827                       || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
14828             complain = complain_overflow_unsigned;
14829           if (howto->complain_on_overflow != complain)
14830             {
14831               alt_howto = *howto;
14832               alt_howto.complain_on_overflow = complain;
14833               howto = &alt_howto;
14834             }
14835         }
14836
14837       r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
14838                                     rel->r_offset, relocation, addend);
14839
14840       if (r != bfd_reloc_ok)
14841         {
14842           char *more_info = NULL;
14843           const char *reloc_name = howto->name;
14844
14845           if (reloc_dest != DEST_NORMAL)
14846             {
14847               more_info = bfd_malloc (strlen (reloc_name) + 8);
14848               if (more_info != NULL)
14849                 {
14850                   strcpy (more_info, reloc_name);
14851                   strcat (more_info, (reloc_dest == DEST_OPD
14852                                       ? " (OPD)" : " (stub)"));
14853                   reloc_name = more_info;
14854                 }
14855             }
14856
14857           if (r == bfd_reloc_overflow)
14858             {
14859               /* On code like "if (foo) foo();" don't report overflow
14860                  on a branch to zero when foo is undefined.  */
14861               if (!warned
14862                   && (reloc_dest == DEST_STUB
14863                       || !(h != NULL
14864                            && (h->elf.root.type == bfd_link_hash_undefweak
14865                                || h->elf.root.type == bfd_link_hash_undefined)
14866                            && is_branch_reloc (r_type))))
14867                 {
14868                   if (!((*info->callbacks->reloc_overflow)
14869                         (info, &h->elf.root, sym_name,
14870                          reloc_name, orig_rel.r_addend,
14871                          input_bfd, input_section, rel->r_offset)))
14872                     return FALSE;
14873                 }
14874             }
14875           else
14876             {
14877               info->callbacks->einfo
14878                 (_("%P: %H: %s against `%T': error %d\n"),
14879                  input_bfd, input_section, rel->r_offset,
14880                  reloc_name, sym_name, (int) r);
14881               ret = FALSE;
14882             }
14883           if (more_info != NULL)
14884             free (more_info);
14885         }
14886     }
14887
14888   /* If we're emitting relocations, then shortly after this function
14889      returns, reloc offsets and addends for this section will be
14890      adjusted.  Worse, reloc symbol indices will be for the output
14891      file rather than the input.  Save a copy of the relocs for
14892      opd_entry_value.  */
14893   if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
14894     {
14895       bfd_size_type amt;
14896       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
14897       rel = bfd_alloc (input_bfd, amt);
14898       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
14899       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
14900       if (rel == NULL)
14901         return FALSE;
14902       memcpy (rel, relocs, amt);
14903     }
14904   return ret;
14905 }
14906
14907 /* Adjust the value of any local symbols in opd sections.  */
14908
14909 static int
14910 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
14911                               const char *name ATTRIBUTE_UNUSED,
14912                               Elf_Internal_Sym *elfsym,
14913                               asection *input_sec,
14914                               struct elf_link_hash_entry *h)
14915 {
14916   struct _opd_sec_data *opd;
14917   long adjust;
14918   bfd_vma value;
14919
14920   if (h != NULL)
14921     return 1;
14922
14923   opd = get_opd_info (input_sec);
14924   if (opd == NULL || opd->adjust == NULL)
14925     return 1;
14926
14927   value = elfsym->st_value - input_sec->output_offset;
14928   if (!bfd_link_relocatable (info))
14929     value -= input_sec->output_section->vma;
14930
14931   adjust = opd->adjust[OPD_NDX (value)];
14932   if (adjust == -1)
14933     return 2;
14934
14935   elfsym->st_value += adjust;
14936   return 1;
14937 }
14938
14939 /* Finish up dynamic symbol handling.  We set the contents of various
14940    dynamic sections here.  */
14941
14942 static bfd_boolean
14943 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
14944                                  struct bfd_link_info *info,
14945                                  struct elf_link_hash_entry *h,
14946                                  Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
14947 {
14948   struct ppc_link_hash_table *htab;
14949   struct plt_entry *ent;
14950   Elf_Internal_Rela rela;
14951   bfd_byte *loc;
14952
14953   htab = ppc_hash_table (info);
14954   if (htab == NULL)
14955     return FALSE;
14956
14957   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14958     if (ent->plt.offset != (bfd_vma) -1)
14959       {
14960         /* This symbol has an entry in the procedure linkage
14961            table.  Set it up.  */
14962         if (!htab->elf.dynamic_sections_created
14963             || h->dynindx == -1)
14964           {
14965             BFD_ASSERT (h->type == STT_GNU_IFUNC
14966                         && h->def_regular
14967                         && (h->root.type == bfd_link_hash_defined
14968                             || h->root.type == bfd_link_hash_defweak));
14969             rela.r_offset = (htab->elf.iplt->output_section->vma
14970                              + htab->elf.iplt->output_offset
14971                              + ent->plt.offset);
14972             if (htab->opd_abi)
14973               rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14974             else
14975               rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14976             rela.r_addend = (h->root.u.def.value
14977                              + h->root.u.def.section->output_offset
14978                              + h->root.u.def.section->output_section->vma
14979                              + ent->addend);
14980             loc = (htab->elf.irelplt->contents
14981                    + (htab->elf.irelplt->reloc_count++
14982                       * sizeof (Elf64_External_Rela)));
14983           }
14984         else
14985           {
14986             rela.r_offset = (htab->elf.splt->output_section->vma
14987                              + htab->elf.splt->output_offset
14988                              + ent->plt.offset);
14989             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14990             rela.r_addend = ent->addend;
14991             loc = (htab->elf.srelplt->contents
14992                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14993                       / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14994           }
14995         bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
14996
14997         if (!htab->opd_abi)
14998           {
14999             if (!h->def_regular)
15000               {
15001                 /* Mark the symbol as undefined, rather than as
15002                    defined in glink.  Leave the value if there were
15003                    any relocations where pointer equality matters
15004                    (this is a clue for the dynamic linker, to make
15005                    function pointer comparisons work between an
15006                    application and shared library), otherwise set it
15007                    to zero.  */
15008                 sym->st_shndx = SHN_UNDEF;
15009                 if (!h->pointer_equality_needed)
15010                   sym->st_value = 0;
15011                 else if (!h->ref_regular_nonweak)
15012                   {
15013                     /* This breaks function pointer comparisons, but
15014                        that is better than breaking tests for a NULL
15015                        function pointer.  */
15016                     sym->st_value = 0;
15017                   }
15018               }
15019           }
15020       }
15021
15022   if (h->needs_copy)
15023     {
15024       /* This symbol needs a copy reloc.  Set it up.  */
15025
15026       if (h->dynindx == -1
15027           || (h->root.type != bfd_link_hash_defined
15028               && h->root.type != bfd_link_hash_defweak)
15029           || htab->relbss == NULL)
15030         abort ();
15031
15032       rela.r_offset = (h->root.u.def.value
15033                        + h->root.u.def.section->output_section->vma
15034                        + h->root.u.def.section->output_offset);
15035       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15036       rela.r_addend = 0;
15037       loc = htab->relbss->contents;
15038       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
15039       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15040     }
15041
15042   return TRUE;
15043 }
15044
15045 /* Used to decide how to sort relocs in an optimal manner for the
15046    dynamic linker, before writing them out.  */
15047
15048 static enum elf_reloc_type_class
15049 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15050                             const asection *rel_sec,
15051                             const Elf_Internal_Rela *rela)
15052 {
15053   enum elf_ppc64_reloc_type r_type;
15054   struct ppc_link_hash_table *htab = ppc_hash_table (info);
15055
15056   if (rel_sec == htab->elf.irelplt)
15057     return reloc_class_ifunc;
15058
15059   r_type = ELF64_R_TYPE (rela->r_info);
15060   switch (r_type)
15061     {
15062     case R_PPC64_RELATIVE:
15063       return reloc_class_relative;
15064     case R_PPC64_JMP_SLOT:
15065       return reloc_class_plt;
15066     case R_PPC64_COPY:
15067       return reloc_class_copy;
15068     default:
15069       return reloc_class_normal;
15070     }
15071 }
15072
15073 /* Finish up the dynamic sections.  */
15074
15075 static bfd_boolean
15076 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15077                                    struct bfd_link_info *info)
15078 {
15079   struct ppc_link_hash_table *htab;
15080   bfd *dynobj;
15081   asection *sdyn;
15082
15083   htab = ppc_hash_table (info);
15084   if (htab == NULL)
15085     return FALSE;
15086
15087   dynobj = htab->elf.dynobj;
15088   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15089
15090   if (htab->elf.dynamic_sections_created)
15091     {
15092       Elf64_External_Dyn *dyncon, *dynconend;
15093
15094       if (sdyn == NULL || htab->elf.sgot == NULL)
15095         abort ();
15096
15097       dyncon = (Elf64_External_Dyn *) sdyn->contents;
15098       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15099       for (; dyncon < dynconend; dyncon++)
15100         {
15101           Elf_Internal_Dyn dyn;
15102           asection *s;
15103
15104           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15105
15106           switch (dyn.d_tag)
15107             {
15108             default:
15109               continue;
15110
15111             case DT_PPC64_GLINK:
15112               s = htab->glink;
15113               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15114               /* We stupidly defined DT_PPC64_GLINK to be the start
15115                  of glink rather than the first entry point, which is
15116                  what ld.so needs, and now have a bigger stub to
15117                  support automatic multiple TOCs.  */
15118               dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
15119               break;
15120
15121             case DT_PPC64_OPD:
15122               s = bfd_get_section_by_name (output_bfd, ".opd");
15123               if (s == NULL)
15124                 continue;
15125               dyn.d_un.d_ptr = s->vma;
15126               break;
15127
15128             case DT_PPC64_OPT:
15129               if (htab->do_multi_toc && htab->multi_toc_needed)
15130                 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15131               break;
15132
15133             case DT_PPC64_OPDSZ:
15134               s = bfd_get_section_by_name (output_bfd, ".opd");
15135               if (s == NULL)
15136                 continue;
15137               dyn.d_un.d_val = s->size;
15138               break;
15139
15140             case DT_PLTGOT:
15141               s = htab->elf.splt;
15142               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15143               break;
15144
15145             case DT_JMPREL:
15146               s = htab->elf.srelplt;
15147               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15148               break;
15149
15150             case DT_PLTRELSZ:
15151               dyn.d_un.d_val = htab->elf.srelplt->size;
15152               break;
15153
15154             case DT_RELASZ:
15155               /* Don't count procedure linkage table relocs in the
15156                  overall reloc count.  */
15157               s = htab->elf.srelplt;
15158               if (s == NULL)
15159                 continue;
15160               dyn.d_un.d_val -= s->size;
15161               break;
15162
15163             case DT_RELA:
15164               /* We may not be using the standard ELF linker script.
15165                  If .rela.plt is the first .rela section, we adjust
15166                  DT_RELA to not include it.  */
15167               s = htab->elf.srelplt;
15168               if (s == NULL)
15169                 continue;
15170               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
15171                 continue;
15172               dyn.d_un.d_ptr += s->size;
15173               break;
15174             }
15175
15176           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15177         }
15178     }
15179
15180   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0)
15181     {
15182       /* Fill in the first entry in the global offset table.
15183          We use it to hold the link-time TOCbase.  */
15184       bfd_put_64 (output_bfd,
15185                   elf_gp (output_bfd) + TOC_BASE_OFF,
15186                   htab->elf.sgot->contents);
15187
15188       /* Set .got entry size.  */
15189       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
15190     }
15191
15192   if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
15193     {
15194       /* Set .plt entry size.  */
15195       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15196         = PLT_ENTRY_SIZE (htab);
15197     }
15198
15199   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15200      brlt ourselves if emitrelocations.  */
15201   if (htab->brlt != NULL
15202       && htab->brlt->reloc_count != 0
15203       && !_bfd_elf_link_output_relocs (output_bfd,
15204                                        htab->brlt,
15205                                        elf_section_data (htab->brlt)->rela.hdr,
15206                                        elf_section_data (htab->brlt)->relocs,
15207                                        NULL))
15208     return FALSE;
15209
15210   if (htab->glink != NULL
15211       && htab->glink->reloc_count != 0
15212       && !_bfd_elf_link_output_relocs (output_bfd,
15213                                        htab->glink,
15214                                        elf_section_data (htab->glink)->rela.hdr,
15215                                        elf_section_data (htab->glink)->relocs,
15216                                        NULL))
15217     return FALSE;
15218
15219   if (htab->glink_eh_frame != NULL
15220       && htab->glink_eh_frame->size != 0)
15221     {
15222       bfd_vma val;
15223       bfd_byte *p;
15224       asection *stub_sec;
15225
15226       p = htab->glink_eh_frame->contents + sizeof (glink_eh_frame_cie);
15227       for (stub_sec = htab->params->stub_bfd->sections;
15228            stub_sec != NULL;
15229            stub_sec = stub_sec->next)
15230         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
15231           {
15232             /* FDE length.  */
15233             p += 4;
15234             /* CIE pointer.  */
15235             p += 4;
15236             /* Offset to stub section.  */
15237             val = (stub_sec->output_section->vma
15238                    + stub_sec->output_offset);
15239             val -= (htab->glink_eh_frame->output_section->vma
15240                     + htab->glink_eh_frame->output_offset
15241                     + (p - htab->glink_eh_frame->contents));
15242             if (val + 0x80000000 > 0xffffffff)
15243               {
15244                 info->callbacks->einfo
15245                   (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15246                    stub_sec->name);
15247                 return FALSE;
15248               }
15249             bfd_put_32 (dynobj, val, p);
15250             p += 4;
15251             /* stub section size.  */
15252             p += 4;
15253             /* Augmentation.  */
15254             p += 1;
15255             /* Pad.  */
15256             p += 7;
15257           }
15258       if (htab->glink != NULL && htab->glink->size != 0)
15259         {
15260           /* FDE length.  */
15261           p += 4;
15262           /* CIE pointer.  */
15263           p += 4;
15264           /* Offset to .glink.  */
15265           val = (htab->glink->output_section->vma
15266                  + htab->glink->output_offset
15267                  + 8);
15268           val -= (htab->glink_eh_frame->output_section->vma
15269                   + htab->glink_eh_frame->output_offset
15270                   + (p - htab->glink_eh_frame->contents));
15271           if (val + 0x80000000 > 0xffffffff)
15272             {
15273               info->callbacks->einfo
15274                 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15275                  htab->glink->name);
15276               return FALSE;
15277             }
15278           bfd_put_32 (dynobj, val, p);
15279           p += 4;
15280           /* .glink size.  */
15281           p += 4;
15282           /* Augmentation.  */
15283           p += 1;
15284           /* Ops.  */
15285           p += 7;
15286         }
15287
15288       if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15289           && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15290                                                htab->glink_eh_frame,
15291                                                htab->glink_eh_frame->contents))
15292         return FALSE;
15293     }
15294
15295   /* We need to handle writing out multiple GOT sections ourselves,
15296      since we didn't add them to DYNOBJ.  We know dynobj is the first
15297      bfd.  */
15298   while ((dynobj = dynobj->link.next) != NULL)
15299     {
15300       asection *s;
15301
15302       if (!is_ppc64_elf (dynobj))
15303         continue;
15304
15305       s = ppc64_elf_tdata (dynobj)->got;
15306       if (s != NULL
15307           && s->size != 0
15308           && s->output_section != bfd_abs_section_ptr
15309           && !bfd_set_section_contents (output_bfd, s->output_section,
15310                                         s->contents, s->output_offset,
15311                                         s->size))
15312         return FALSE;
15313       s = ppc64_elf_tdata (dynobj)->relgot;
15314       if (s != NULL
15315           && s->size != 0
15316           && s->output_section != bfd_abs_section_ptr
15317           && !bfd_set_section_contents (output_bfd, s->output_section,
15318                                         s->contents, s->output_offset,
15319                                         s->size))
15320         return FALSE;
15321     }
15322
15323   return TRUE;
15324 }
15325
15326 #include "elf64-target.h"
15327
15328 /* FreeBSD support */
15329
15330 #undef  TARGET_LITTLE_SYM
15331 #undef  TARGET_LITTLE_NAME
15332
15333 #undef  TARGET_BIG_SYM
15334 #define TARGET_BIG_SYM  powerpc_elf64_fbsd_vec
15335 #undef  TARGET_BIG_NAME
15336 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15337
15338 #undef  ELF_OSABI
15339 #define ELF_OSABI       ELFOSABI_FREEBSD
15340
15341 #undef  elf64_bed
15342 #define elf64_bed       elf64_powerpc_fbsd_bed
15343
15344 #include "elf64-target.h"
15345