Use dynamic text relocs for protected vars
[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
142 /* Offset of tp and dtp pointers from start of TLS block.  */
143 #define TP_OFFSET       0x7000
144 #define DTP_OFFSET      0x8000
145
146 /* .plt call stub instructions.  The normal stub is like this, but
147    sometimes the .plt entry crosses a 64k boundary and we need to
148    insert an addi to adjust r11.  */
149 #define STD_R2_0R1      0xf8410000      /* std   %r2,0+40(%r1)       */
150 #define ADDIS_R11_R2    0x3d620000      /* addis %r11,%r2,xxx@ha     */
151 #define LD_R12_0R11     0xe98b0000      /* ld    %r12,xxx+0@l(%r11)  */
152 #define MTCTR_R12       0x7d8903a6      /* mtctr %r12                */
153 #define LD_R2_0R11      0xe84b0000      /* ld    %r2,xxx+8@l(%r11)   */
154 #define LD_R11_0R11     0xe96b0000      /* ld    %r11,xxx+16@l(%r11) */
155 #define BCTR            0x4e800420      /* bctr                      */
156
157 #define ADDI_R11_R11    0x396b0000      /* addi %r11,%r11,off@l  */
158 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
159 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
160
161 #define XOR_R2_R12_R12  0x7d826278      /* xor   %r2,%r12,%r12   */
162 #define ADD_R11_R11_R2  0x7d6b1214      /* add   %r11,%r11,%r2   */
163 #define XOR_R11_R12_R12 0x7d8b6278      /* xor   %r11,%r12,%r12  */
164 #define ADD_R2_R2_R11   0x7c425a14      /* add   %r2,%r2,%r11    */
165 #define CMPLDI_R2_0     0x28220000      /* cmpldi %r2,0          */
166 #define BNECTR          0x4ca20420      /* bnectr+               */
167 #define BNECTR_P4       0x4ce20420      /* bnectr+               */
168
169 #define LD_R12_0R2      0xe9820000      /* ld    %r12,xxx+0(%r2) */
170 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
171 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
172
173 #define LD_R2_0R1       0xe8410000      /* ld    %r2,0(%r1)      */
174
175 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha     */
176 #define ADDIS_R12_R12   0x3d8c0000      /* addis %r12,%r12,xxx@ha */
177 #define LD_R12_0R12     0xe98c0000      /* ld    %r12,xxx@l(%r12) */
178
179 /* glink call stub instructions.  We enter with the index in R0.  */
180 #define GLINK_CALL_STUB_SIZE (16*4)
181                                         /* 0:                           */
182                                         /*  .quad plt0-1f               */
183                                         /* __glink:                     */
184 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
185 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
186                                         /* 1:                           */
187 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
188                                         /*  ld %2,(0b-1b)(%11)          */
189 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
190 #define ADD_R11_R2_R11  0x7d625a14      /*  add %11,%2,%11              */
191                                         /*  ld %12,0(%11)               */
192                                         /*  ld %2,8(%11)                */
193                                         /*  mtctr %12                   */
194                                         /*  ld %11,16(%11)              */
195                                         /*  bctr                        */
196 #define MFLR_R0         0x7c0802a6      /*  mflr %r0                    */
197 #define MTLR_R0         0x7c0803a6      /*  mtlr %r0                    */
198 #define SUB_R12_R12_R11 0x7d8b6050      /*  subf %r12,%r11,%r12         */
199 #define ADDI_R0_R12     0x380c0000      /*  addi %r0,%r12,0             */
200 #define SRDI_R0_R0_2    0x7800f082      /*  rldicl %r0,%r0,62,2         */
201
202 /* Pad with this.  */
203 #define NOP             0x60000000
204
205 /* Some other nops.  */
206 #define CROR_151515     0x4def7b82
207 #define CROR_313131     0x4ffffb82
208
209 /* .glink entries for the first 32k functions are two instructions.  */
210 #define LI_R0_0         0x38000000      /* li    %r0,0          */
211 #define B_DOT           0x48000000      /* b     .              */
212
213 /* After that, we need two instructions to load the index, followed by
214    a branch.  */
215 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
216 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
217
218 /* Instructions used by the save and restore reg functions.  */
219 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
220 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
221 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
222 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
223 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
224 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
225 #define LI_R12_0        0x39800000      /* li    %r12,0         */
226 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
227 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
228 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
229 #define BLR             0x4e800020      /* blr                  */
230
231 /* Since .opd is an array of descriptors and each entry will end up
232    with identical R_PPC64_RELATIVE relocs, there is really no need to
233    propagate .opd relocs;  The dynamic linker should be taught to
234    relocate .opd without reloc entries.  */
235 #ifndef NO_OPD_RELOCS
236 #define NO_OPD_RELOCS 0
237 #endif
238
239 static inline int
240 abiversion (bfd *abfd)
241 {
242   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
243 }
244
245 static inline void
246 set_abiversion (bfd *abfd, int ver)
247 {
248   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
249   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
250 }
251 \f
252 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
253
254 /* Relocation HOWTO's.  */
255 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
256
257 static reloc_howto_type ppc64_elf_howto_raw[] = {
258   /* This reloc does nothing.  */
259   HOWTO (R_PPC64_NONE,          /* type */
260          0,                     /* rightshift */
261          3,                     /* size (0 = byte, 1 = short, 2 = long) */
262          0,                     /* bitsize */
263          FALSE,                 /* pc_relative */
264          0,                     /* bitpos */
265          complain_overflow_dont, /* complain_on_overflow */
266          bfd_elf_generic_reloc, /* special_function */
267          "R_PPC64_NONE",        /* name */
268          FALSE,                 /* partial_inplace */
269          0,                     /* src_mask */
270          0,                     /* dst_mask */
271          FALSE),                /* pcrel_offset */
272
273   /* A standard 32 bit relocation.  */
274   HOWTO (R_PPC64_ADDR32,        /* type */
275          0,                     /* rightshift */
276          2,                     /* size (0 = byte, 1 = short, 2 = long) */
277          32,                    /* bitsize */
278          FALSE,                 /* pc_relative */
279          0,                     /* bitpos */
280          complain_overflow_bitfield, /* complain_on_overflow */
281          bfd_elf_generic_reloc, /* special_function */
282          "R_PPC64_ADDR32",      /* name */
283          FALSE,                 /* partial_inplace */
284          0,                     /* src_mask */
285          0xffffffff,            /* dst_mask */
286          FALSE),                /* pcrel_offset */
287
288   /* An absolute 26 bit branch; the lower two bits must be zero.
289      FIXME: we don't check that, we just clear them.  */
290   HOWTO (R_PPC64_ADDR24,        /* type */
291          0,                     /* rightshift */
292          2,                     /* size (0 = byte, 1 = short, 2 = long) */
293          26,                    /* bitsize */
294          FALSE,                 /* pc_relative */
295          0,                     /* bitpos */
296          complain_overflow_bitfield, /* complain_on_overflow */
297          bfd_elf_generic_reloc, /* special_function */
298          "R_PPC64_ADDR24",      /* name */
299          FALSE,                 /* partial_inplace */
300          0,                     /* src_mask */
301          0x03fffffc,            /* dst_mask */
302          FALSE),                /* pcrel_offset */
303
304   /* A standard 16 bit relocation.  */
305   HOWTO (R_PPC64_ADDR16,        /* type */
306          0,                     /* rightshift */
307          1,                     /* size (0 = byte, 1 = short, 2 = long) */
308          16,                    /* bitsize */
309          FALSE,                 /* pc_relative */
310          0,                     /* bitpos */
311          complain_overflow_bitfield, /* complain_on_overflow */
312          bfd_elf_generic_reloc, /* special_function */
313          "R_PPC64_ADDR16",      /* name */
314          FALSE,                 /* partial_inplace */
315          0,                     /* src_mask */
316          0xffff,                /* dst_mask */
317          FALSE),                /* pcrel_offset */
318
319   /* A 16 bit relocation without overflow.  */
320   HOWTO (R_PPC64_ADDR16_LO,     /* type */
321          0,                     /* rightshift */
322          1,                     /* size (0 = byte, 1 = short, 2 = long) */
323          16,                    /* bitsize */
324          FALSE,                 /* pc_relative */
325          0,                     /* bitpos */
326          complain_overflow_dont,/* complain_on_overflow */
327          bfd_elf_generic_reloc, /* special_function */
328          "R_PPC64_ADDR16_LO",   /* name */
329          FALSE,                 /* partial_inplace */
330          0,                     /* src_mask */
331          0xffff,                /* dst_mask */
332          FALSE),                /* pcrel_offset */
333
334   /* Bits 16-31 of an address.  */
335   HOWTO (R_PPC64_ADDR16_HI,     /* type */
336          16,                    /* rightshift */
337          1,                     /* size (0 = byte, 1 = short, 2 = long) */
338          16,                    /* bitsize */
339          FALSE,                 /* pc_relative */
340          0,                     /* bitpos */
341          complain_overflow_signed, /* complain_on_overflow */
342          bfd_elf_generic_reloc, /* special_function */
343          "R_PPC64_ADDR16_HI",   /* name */
344          FALSE,                 /* partial_inplace */
345          0,                     /* src_mask */
346          0xffff,                /* dst_mask */
347          FALSE),                /* pcrel_offset */
348
349   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
350      bits, treated as a signed number, is negative.  */
351   HOWTO (R_PPC64_ADDR16_HA,     /* type */
352          16,                    /* rightshift */
353          1,                     /* size (0 = byte, 1 = short, 2 = long) */
354          16,                    /* bitsize */
355          FALSE,                 /* pc_relative */
356          0,                     /* bitpos */
357          complain_overflow_signed, /* complain_on_overflow */
358          ppc64_elf_ha_reloc,    /* special_function */
359          "R_PPC64_ADDR16_HA",   /* name */
360          FALSE,                 /* partial_inplace */
361          0,                     /* src_mask */
362          0xffff,                /* dst_mask */
363          FALSE),                /* pcrel_offset */
364
365   /* An absolute 16 bit branch; the lower two bits must be zero.
366      FIXME: we don't check that, we just clear them.  */
367   HOWTO (R_PPC64_ADDR14,        /* type */
368          0,                     /* rightshift */
369          2,                     /* size (0 = byte, 1 = short, 2 = long) */
370          16,                    /* bitsize */
371          FALSE,                 /* pc_relative */
372          0,                     /* bitpos */
373          complain_overflow_signed, /* complain_on_overflow */
374          ppc64_elf_branch_reloc, /* special_function */
375          "R_PPC64_ADDR14",      /* name */
376          FALSE,                 /* partial_inplace */
377          0,                     /* src_mask */
378          0x0000fffc,            /* dst_mask */
379          FALSE),                /* pcrel_offset */
380
381   /* An absolute 16 bit branch, for which bit 10 should be set to
382      indicate that the branch is expected to be taken.  The lower two
383      bits must be zero.  */
384   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
385          0,                     /* rightshift */
386          2,                     /* size (0 = byte, 1 = short, 2 = long) */
387          16,                    /* bitsize */
388          FALSE,                 /* pc_relative */
389          0,                     /* bitpos */
390          complain_overflow_signed, /* complain_on_overflow */
391          ppc64_elf_brtaken_reloc, /* special_function */
392          "R_PPC64_ADDR14_BRTAKEN",/* name */
393          FALSE,                 /* partial_inplace */
394          0,                     /* src_mask */
395          0x0000fffc,            /* dst_mask */
396          FALSE),                /* pcrel_offset */
397
398   /* An absolute 16 bit branch, for which bit 10 should be set to
399      indicate that the branch is not expected to be taken.  The lower
400      two bits must be zero.  */
401   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
402          0,                     /* rightshift */
403          2,                     /* size (0 = byte, 1 = short, 2 = long) */
404          16,                    /* bitsize */
405          FALSE,                 /* pc_relative */
406          0,                     /* bitpos */
407          complain_overflow_signed, /* complain_on_overflow */
408          ppc64_elf_brtaken_reloc, /* special_function */
409          "R_PPC64_ADDR14_BRNTAKEN",/* name */
410          FALSE,                 /* partial_inplace */
411          0,                     /* src_mask */
412          0x0000fffc,            /* dst_mask */
413          FALSE),                /* pcrel_offset */
414
415   /* A relative 26 bit branch; the lower two bits must be zero.  */
416   HOWTO (R_PPC64_REL24,         /* type */
417          0,                     /* rightshift */
418          2,                     /* size (0 = byte, 1 = short, 2 = long) */
419          26,                    /* bitsize */
420          TRUE,                  /* pc_relative */
421          0,                     /* bitpos */
422          complain_overflow_signed, /* complain_on_overflow */
423          ppc64_elf_branch_reloc, /* special_function */
424          "R_PPC64_REL24",       /* name */
425          FALSE,                 /* partial_inplace */
426          0,                     /* src_mask */
427          0x03fffffc,            /* dst_mask */
428          TRUE),                 /* pcrel_offset */
429
430   /* A relative 16 bit branch; the lower two bits must be zero.  */
431   HOWTO (R_PPC64_REL14,         /* type */
432          0,                     /* rightshift */
433          2,                     /* size (0 = byte, 1 = short, 2 = long) */
434          16,                    /* bitsize */
435          TRUE,                  /* pc_relative */
436          0,                     /* bitpos */
437          complain_overflow_signed, /* complain_on_overflow */
438          ppc64_elf_branch_reloc, /* special_function */
439          "R_PPC64_REL14",       /* name */
440          FALSE,                 /* partial_inplace */
441          0,                     /* src_mask */
442          0x0000fffc,            /* dst_mask */
443          TRUE),                 /* pcrel_offset */
444
445   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
446      the branch is expected to be taken.  The lower two bits must be
447      zero.  */
448   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
449          0,                     /* rightshift */
450          2,                     /* size (0 = byte, 1 = short, 2 = long) */
451          16,                    /* bitsize */
452          TRUE,                  /* pc_relative */
453          0,                     /* bitpos */
454          complain_overflow_signed, /* complain_on_overflow */
455          ppc64_elf_brtaken_reloc, /* special_function */
456          "R_PPC64_REL14_BRTAKEN", /* name */
457          FALSE,                 /* partial_inplace */
458          0,                     /* src_mask */
459          0x0000fffc,            /* dst_mask */
460          TRUE),                 /* pcrel_offset */
461
462   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
463      the branch is not expected to be taken.  The lower two bits must
464      be zero.  */
465   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
466          0,                     /* rightshift */
467          2,                     /* size (0 = byte, 1 = short, 2 = long) */
468          16,                    /* bitsize */
469          TRUE,                  /* pc_relative */
470          0,                     /* bitpos */
471          complain_overflow_signed, /* complain_on_overflow */
472          ppc64_elf_brtaken_reloc, /* special_function */
473          "R_PPC64_REL14_BRNTAKEN",/* name */
474          FALSE,                 /* partial_inplace */
475          0,                     /* src_mask */
476          0x0000fffc,            /* dst_mask */
477          TRUE),                 /* pcrel_offset */
478
479   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
480      symbol.  */
481   HOWTO (R_PPC64_GOT16,         /* type */
482          0,                     /* rightshift */
483          1,                     /* size (0 = byte, 1 = short, 2 = long) */
484          16,                    /* bitsize */
485          FALSE,                 /* pc_relative */
486          0,                     /* bitpos */
487          complain_overflow_signed, /* complain_on_overflow */
488          ppc64_elf_unhandled_reloc, /* special_function */
489          "R_PPC64_GOT16",       /* name */
490          FALSE,                 /* partial_inplace */
491          0,                     /* src_mask */
492          0xffff,                /* dst_mask */
493          FALSE),                /* pcrel_offset */
494
495   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
496      the symbol.  */
497   HOWTO (R_PPC64_GOT16_LO,      /* type */
498          0,                     /* rightshift */
499          1,                     /* size (0 = byte, 1 = short, 2 = long) */
500          16,                    /* bitsize */
501          FALSE,                 /* pc_relative */
502          0,                     /* bitpos */
503          complain_overflow_dont, /* complain_on_overflow */
504          ppc64_elf_unhandled_reloc, /* special_function */
505          "R_PPC64_GOT16_LO",    /* name */
506          FALSE,                 /* partial_inplace */
507          0,                     /* src_mask */
508          0xffff,                /* dst_mask */
509          FALSE),                /* pcrel_offset */
510
511   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
512      the symbol.  */
513   HOWTO (R_PPC64_GOT16_HI,      /* type */
514          16,                    /* rightshift */
515          1,                     /* size (0 = byte, 1 = short, 2 = long) */
516          16,                    /* bitsize */
517          FALSE,                 /* pc_relative */
518          0,                     /* bitpos */
519          complain_overflow_signed,/* complain_on_overflow */
520          ppc64_elf_unhandled_reloc, /* special_function */
521          "R_PPC64_GOT16_HI",    /* name */
522          FALSE,                 /* partial_inplace */
523          0,                     /* src_mask */
524          0xffff,                /* dst_mask */
525          FALSE),                /* pcrel_offset */
526
527   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
528      the symbol.  */
529   HOWTO (R_PPC64_GOT16_HA,      /* type */
530          16,                    /* rightshift */
531          1,                     /* size (0 = byte, 1 = short, 2 = long) */
532          16,                    /* bitsize */
533          FALSE,                 /* pc_relative */
534          0,                     /* bitpos */
535          complain_overflow_signed,/* complain_on_overflow */
536          ppc64_elf_unhandled_reloc, /* special_function */
537          "R_PPC64_GOT16_HA",    /* name */
538          FALSE,                 /* partial_inplace */
539          0,                     /* src_mask */
540          0xffff,                /* dst_mask */
541          FALSE),                /* pcrel_offset */
542
543   /* This is used only by the dynamic linker.  The symbol should exist
544      both in the object being run and in some shared library.  The
545      dynamic linker copies the data addressed by the symbol from the
546      shared library into the object, because the object being
547      run has to have the data at some particular address.  */
548   HOWTO (R_PPC64_COPY,          /* type */
549          0,                     /* rightshift */
550          0,                     /* this one is variable size */
551          0,                     /* bitsize */
552          FALSE,                 /* pc_relative */
553          0,                     /* bitpos */
554          complain_overflow_dont, /* complain_on_overflow */
555          ppc64_elf_unhandled_reloc, /* special_function */
556          "R_PPC64_COPY",        /* name */
557          FALSE,                 /* partial_inplace */
558          0,                     /* src_mask */
559          0,                     /* dst_mask */
560          FALSE),                /* pcrel_offset */
561
562   /* Like R_PPC64_ADDR64, but used when setting global offset table
563      entries.  */
564   HOWTO (R_PPC64_GLOB_DAT,      /* type */
565          0,                     /* rightshift */
566          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
567          64,                    /* bitsize */
568          FALSE,                 /* pc_relative */
569          0,                     /* bitpos */
570          complain_overflow_dont, /* complain_on_overflow */
571          ppc64_elf_unhandled_reloc,  /* special_function */
572          "R_PPC64_GLOB_DAT",    /* name */
573          FALSE,                 /* partial_inplace */
574          0,                     /* src_mask */
575          ONES (64),             /* dst_mask */
576          FALSE),                /* pcrel_offset */
577
578   /* Created by the link editor.  Marks a procedure linkage table
579      entry for a symbol.  */
580   HOWTO (R_PPC64_JMP_SLOT,      /* type */
581          0,                     /* rightshift */
582          0,                     /* size (0 = byte, 1 = short, 2 = long) */
583          0,                     /* bitsize */
584          FALSE,                 /* pc_relative */
585          0,                     /* bitpos */
586          complain_overflow_dont, /* complain_on_overflow */
587          ppc64_elf_unhandled_reloc, /* special_function */
588          "R_PPC64_JMP_SLOT",    /* name */
589          FALSE,                 /* partial_inplace */
590          0,                     /* src_mask */
591          0,                     /* dst_mask */
592          FALSE),                /* pcrel_offset */
593
594   /* Used only by the dynamic linker.  When the object is run, this
595      doubleword64 is set to the load address of the object, plus the
596      addend.  */
597   HOWTO (R_PPC64_RELATIVE,      /* type */
598          0,                     /* rightshift */
599          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
600          64,                    /* bitsize */
601          FALSE,                 /* pc_relative */
602          0,                     /* bitpos */
603          complain_overflow_dont, /* complain_on_overflow */
604          bfd_elf_generic_reloc, /* special_function */
605          "R_PPC64_RELATIVE",    /* name */
606          FALSE,                 /* partial_inplace */
607          0,                     /* src_mask */
608          ONES (64),             /* dst_mask */
609          FALSE),                /* pcrel_offset */
610
611   /* Like R_PPC64_ADDR32, but may be unaligned.  */
612   HOWTO (R_PPC64_UADDR32,       /* type */
613          0,                     /* rightshift */
614          2,                     /* size (0 = byte, 1 = short, 2 = long) */
615          32,                    /* bitsize */
616          FALSE,                 /* pc_relative */
617          0,                     /* bitpos */
618          complain_overflow_bitfield, /* complain_on_overflow */
619          bfd_elf_generic_reloc, /* special_function */
620          "R_PPC64_UADDR32",     /* name */
621          FALSE,                 /* partial_inplace */
622          0,                     /* src_mask */
623          0xffffffff,            /* dst_mask */
624          FALSE),                /* pcrel_offset */
625
626   /* Like R_PPC64_ADDR16, but may be unaligned.  */
627   HOWTO (R_PPC64_UADDR16,       /* type */
628          0,                     /* rightshift */
629          1,                     /* size (0 = byte, 1 = short, 2 = long) */
630          16,                    /* bitsize */
631          FALSE,                 /* pc_relative */
632          0,                     /* bitpos */
633          complain_overflow_bitfield, /* complain_on_overflow */
634          bfd_elf_generic_reloc, /* special_function */
635          "R_PPC64_UADDR16",     /* name */
636          FALSE,                 /* partial_inplace */
637          0,                     /* src_mask */
638          0xffff,                /* dst_mask */
639          FALSE),                /* pcrel_offset */
640
641   /* 32-bit PC relative.  */
642   HOWTO (R_PPC64_REL32,         /* type */
643          0,                     /* rightshift */
644          2,                     /* size (0 = byte, 1 = short, 2 = long) */
645          32,                    /* bitsize */
646          TRUE,                  /* pc_relative */
647          0,                     /* bitpos */
648          complain_overflow_signed, /* complain_on_overflow */
649          bfd_elf_generic_reloc, /* special_function */
650          "R_PPC64_REL32",       /* name */
651          FALSE,                 /* partial_inplace */
652          0,                     /* src_mask */
653          0xffffffff,            /* dst_mask */
654          TRUE),                 /* pcrel_offset */
655
656   /* 32-bit relocation to the symbol's procedure linkage table.  */
657   HOWTO (R_PPC64_PLT32,         /* type */
658          0,                     /* rightshift */
659          2,                     /* size (0 = byte, 1 = short, 2 = long) */
660          32,                    /* bitsize */
661          FALSE,                 /* pc_relative */
662          0,                     /* bitpos */
663          complain_overflow_bitfield, /* complain_on_overflow */
664          ppc64_elf_unhandled_reloc, /* special_function */
665          "R_PPC64_PLT32",       /* name */
666          FALSE,                 /* partial_inplace */
667          0,                     /* src_mask */
668          0xffffffff,            /* dst_mask */
669          FALSE),                /* pcrel_offset */
670
671   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
672      FIXME: R_PPC64_PLTREL32 not supported.  */
673   HOWTO (R_PPC64_PLTREL32,      /* type */
674          0,                     /* rightshift */
675          2,                     /* size (0 = byte, 1 = short, 2 = long) */
676          32,                    /* bitsize */
677          TRUE,                  /* pc_relative */
678          0,                     /* bitpos */
679          complain_overflow_signed, /* complain_on_overflow */
680          bfd_elf_generic_reloc, /* special_function */
681          "R_PPC64_PLTREL32",    /* name */
682          FALSE,                 /* partial_inplace */
683          0,                     /* src_mask */
684          0xffffffff,            /* dst_mask */
685          TRUE),                 /* pcrel_offset */
686
687   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
688      the symbol.  */
689   HOWTO (R_PPC64_PLT16_LO,      /* type */
690          0,                     /* rightshift */
691          1,                     /* size (0 = byte, 1 = short, 2 = long) */
692          16,                    /* bitsize */
693          FALSE,                 /* pc_relative */
694          0,                     /* bitpos */
695          complain_overflow_dont, /* complain_on_overflow */
696          ppc64_elf_unhandled_reloc, /* special_function */
697          "R_PPC64_PLT16_LO",    /* name */
698          FALSE,                 /* partial_inplace */
699          0,                     /* src_mask */
700          0xffff,                /* dst_mask */
701          FALSE),                /* pcrel_offset */
702
703   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
704      the symbol.  */
705   HOWTO (R_PPC64_PLT16_HI,      /* type */
706          16,                    /* rightshift */
707          1,                     /* size (0 = byte, 1 = short, 2 = long) */
708          16,                    /* bitsize */
709          FALSE,                 /* pc_relative */
710          0,                     /* bitpos */
711          complain_overflow_signed, /* complain_on_overflow */
712          ppc64_elf_unhandled_reloc, /* special_function */
713          "R_PPC64_PLT16_HI",    /* name */
714          FALSE,                 /* partial_inplace */
715          0,                     /* src_mask */
716          0xffff,                /* dst_mask */
717          FALSE),                /* pcrel_offset */
718
719   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
720      the symbol.  */
721   HOWTO (R_PPC64_PLT16_HA,      /* type */
722          16,                    /* rightshift */
723          1,                     /* size (0 = byte, 1 = short, 2 = long) */
724          16,                    /* bitsize */
725          FALSE,                 /* pc_relative */
726          0,                     /* bitpos */
727          complain_overflow_signed, /* complain_on_overflow */
728          ppc64_elf_unhandled_reloc, /* special_function */
729          "R_PPC64_PLT16_HA",    /* name */
730          FALSE,                 /* partial_inplace */
731          0,                     /* src_mask */
732          0xffff,                /* dst_mask */
733          FALSE),                /* pcrel_offset */
734
735   /* 16-bit section relative relocation.  */
736   HOWTO (R_PPC64_SECTOFF,       /* type */
737          0,                     /* rightshift */
738          1,                     /* size (0 = byte, 1 = short, 2 = long) */
739          16,                    /* bitsize */
740          FALSE,                 /* pc_relative */
741          0,                     /* bitpos */
742          complain_overflow_signed, /* complain_on_overflow */
743          ppc64_elf_sectoff_reloc, /* special_function */
744          "R_PPC64_SECTOFF",     /* name */
745          FALSE,                 /* partial_inplace */
746          0,                     /* src_mask */
747          0xffff,                /* dst_mask */
748          FALSE),                /* pcrel_offset */
749
750   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
751   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
752          0,                     /* rightshift */
753          1,                     /* size (0 = byte, 1 = short, 2 = long) */
754          16,                    /* bitsize */
755          FALSE,                 /* pc_relative */
756          0,                     /* bitpos */
757          complain_overflow_dont, /* complain_on_overflow */
758          ppc64_elf_sectoff_reloc, /* special_function */
759          "R_PPC64_SECTOFF_LO",  /* name */
760          FALSE,                 /* partial_inplace */
761          0,                     /* src_mask */
762          0xffff,                /* dst_mask */
763          FALSE),                /* pcrel_offset */
764
765   /* 16-bit upper half section relative relocation.  */
766   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
767          16,                    /* rightshift */
768          1,                     /* size (0 = byte, 1 = short, 2 = long) */
769          16,                    /* bitsize */
770          FALSE,                 /* pc_relative */
771          0,                     /* bitpos */
772          complain_overflow_signed, /* complain_on_overflow */
773          ppc64_elf_sectoff_reloc, /* special_function */
774          "R_PPC64_SECTOFF_HI",  /* name */
775          FALSE,                 /* partial_inplace */
776          0,                     /* src_mask */
777          0xffff,                /* dst_mask */
778          FALSE),                /* pcrel_offset */
779
780   /* 16-bit upper half adjusted section relative relocation.  */
781   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
782          16,                    /* rightshift */
783          1,                     /* size (0 = byte, 1 = short, 2 = long) */
784          16,                    /* bitsize */
785          FALSE,                 /* pc_relative */
786          0,                     /* bitpos */
787          complain_overflow_signed, /* complain_on_overflow */
788          ppc64_elf_sectoff_ha_reloc, /* special_function */
789          "R_PPC64_SECTOFF_HA",  /* name */
790          FALSE,                 /* partial_inplace */
791          0,                     /* src_mask */
792          0xffff,                /* dst_mask */
793          FALSE),                /* pcrel_offset */
794
795   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
796   HOWTO (R_PPC64_REL30,         /* type */
797          2,                     /* rightshift */
798          2,                     /* size (0 = byte, 1 = short, 2 = long) */
799          30,                    /* bitsize */
800          TRUE,                  /* pc_relative */
801          0,                     /* bitpos */
802          complain_overflow_dont, /* complain_on_overflow */
803          bfd_elf_generic_reloc, /* special_function */
804          "R_PPC64_REL30",       /* name */
805          FALSE,                 /* partial_inplace */
806          0,                     /* src_mask */
807          0xfffffffc,            /* dst_mask */
808          TRUE),                 /* pcrel_offset */
809
810   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
811
812   /* A standard 64-bit relocation.  */
813   HOWTO (R_PPC64_ADDR64,        /* type */
814          0,                     /* rightshift */
815          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
816          64,                    /* bitsize */
817          FALSE,                 /* pc_relative */
818          0,                     /* bitpos */
819          complain_overflow_dont, /* complain_on_overflow */
820          bfd_elf_generic_reloc, /* special_function */
821          "R_PPC64_ADDR64",      /* name */
822          FALSE,                 /* partial_inplace */
823          0,                     /* src_mask */
824          ONES (64),             /* dst_mask */
825          FALSE),                /* pcrel_offset */
826
827   /* The bits 32-47 of an address.  */
828   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
829          32,                    /* rightshift */
830          1,                     /* size (0 = byte, 1 = short, 2 = long) */
831          16,                    /* bitsize */
832          FALSE,                 /* pc_relative */
833          0,                     /* bitpos */
834          complain_overflow_dont, /* complain_on_overflow */
835          bfd_elf_generic_reloc, /* special_function */
836          "R_PPC64_ADDR16_HIGHER", /* name */
837          FALSE,                 /* partial_inplace */
838          0,                     /* src_mask */
839          0xffff,                /* dst_mask */
840          FALSE),                /* pcrel_offset */
841
842   /* The bits 32-47 of an address, plus 1 if the contents of the low
843      16 bits, treated as a signed number, is negative.  */
844   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
845          32,                    /* rightshift */
846          1,                     /* size (0 = byte, 1 = short, 2 = long) */
847          16,                    /* bitsize */
848          FALSE,                 /* pc_relative */
849          0,                     /* bitpos */
850          complain_overflow_dont, /* complain_on_overflow */
851          ppc64_elf_ha_reloc,    /* special_function */
852          "R_PPC64_ADDR16_HIGHERA", /* name */
853          FALSE,                 /* partial_inplace */
854          0,                     /* src_mask */
855          0xffff,                /* dst_mask */
856          FALSE),                /* pcrel_offset */
857
858   /* The bits 48-63 of an address.  */
859   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
860          48,                    /* rightshift */
861          1,                     /* size (0 = byte, 1 = short, 2 = long) */
862          16,                    /* bitsize */
863          FALSE,                 /* pc_relative */
864          0,                     /* bitpos */
865          complain_overflow_dont, /* complain_on_overflow */
866          bfd_elf_generic_reloc, /* special_function */
867          "R_PPC64_ADDR16_HIGHEST", /* name */
868          FALSE,                 /* partial_inplace */
869          0,                     /* src_mask */
870          0xffff,                /* dst_mask */
871          FALSE),                /* pcrel_offset */
872
873   /* The bits 48-63 of an address, plus 1 if the contents of the low
874      16 bits, treated as a signed number, is negative.  */
875   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
876          48,                    /* rightshift */
877          1,                     /* size (0 = byte, 1 = short, 2 = long) */
878          16,                    /* bitsize */
879          FALSE,                 /* pc_relative */
880          0,                     /* bitpos */
881          complain_overflow_dont, /* complain_on_overflow */
882          ppc64_elf_ha_reloc,    /* special_function */
883          "R_PPC64_ADDR16_HIGHESTA", /* name */
884          FALSE,                 /* partial_inplace */
885          0,                     /* src_mask */
886          0xffff,                /* dst_mask */
887          FALSE),                /* pcrel_offset */
888
889   /* Like ADDR64, but may be unaligned.  */
890   HOWTO (R_PPC64_UADDR64,       /* type */
891          0,                     /* rightshift */
892          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
893          64,                    /* bitsize */
894          FALSE,                 /* pc_relative */
895          0,                     /* bitpos */
896          complain_overflow_dont, /* complain_on_overflow */
897          bfd_elf_generic_reloc, /* special_function */
898          "R_PPC64_UADDR64",     /* name */
899          FALSE,                 /* partial_inplace */
900          0,                     /* src_mask */
901          ONES (64),             /* dst_mask */
902          FALSE),                /* pcrel_offset */
903
904   /* 64-bit relative relocation.  */
905   HOWTO (R_PPC64_REL64,         /* type */
906          0,                     /* rightshift */
907          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
908          64,                    /* bitsize */
909          TRUE,                  /* pc_relative */
910          0,                     /* bitpos */
911          complain_overflow_dont, /* complain_on_overflow */
912          bfd_elf_generic_reloc, /* special_function */
913          "R_PPC64_REL64",       /* name */
914          FALSE,                 /* partial_inplace */
915          0,                     /* src_mask */
916          ONES (64),             /* dst_mask */
917          TRUE),                 /* pcrel_offset */
918
919   /* 64-bit relocation to the symbol's procedure linkage table.  */
920   HOWTO (R_PPC64_PLT64,         /* type */
921          0,                     /* rightshift */
922          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
923          64,                    /* bitsize */
924          FALSE,                 /* pc_relative */
925          0,                     /* bitpos */
926          complain_overflow_dont, /* complain_on_overflow */
927          ppc64_elf_unhandled_reloc, /* special_function */
928          "R_PPC64_PLT64",       /* name */
929          FALSE,                 /* partial_inplace */
930          0,                     /* src_mask */
931          ONES (64),             /* dst_mask */
932          FALSE),                /* pcrel_offset */
933
934   /* 64-bit PC relative relocation to the symbol's procedure linkage
935      table.  */
936   /* FIXME: R_PPC64_PLTREL64 not supported.  */
937   HOWTO (R_PPC64_PLTREL64,      /* type */
938          0,                     /* rightshift */
939          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
940          64,                    /* bitsize */
941          TRUE,                  /* pc_relative */
942          0,                     /* bitpos */
943          complain_overflow_dont, /* complain_on_overflow */
944          ppc64_elf_unhandled_reloc, /* special_function */
945          "R_PPC64_PLTREL64",    /* name */
946          FALSE,                 /* partial_inplace */
947          0,                     /* src_mask */
948          ONES (64),             /* dst_mask */
949          TRUE),                 /* pcrel_offset */
950
951   /* 16 bit TOC-relative relocation.  */
952
953   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
954   HOWTO (R_PPC64_TOC16,         /* type */
955          0,                     /* rightshift */
956          1,                     /* size (0 = byte, 1 = short, 2 = long) */
957          16,                    /* bitsize */
958          FALSE,                 /* pc_relative */
959          0,                     /* bitpos */
960          complain_overflow_signed, /* complain_on_overflow */
961          ppc64_elf_toc_reloc,   /* special_function */
962          "R_PPC64_TOC16",       /* name */
963          FALSE,                 /* partial_inplace */
964          0,                     /* src_mask */
965          0xffff,                /* dst_mask */
966          FALSE),                /* pcrel_offset */
967
968   /* 16 bit TOC-relative relocation without overflow.  */
969
970   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
971   HOWTO (R_PPC64_TOC16_LO,      /* type */
972          0,                     /* rightshift */
973          1,                     /* size (0 = byte, 1 = short, 2 = long) */
974          16,                    /* bitsize */
975          FALSE,                 /* pc_relative */
976          0,                     /* bitpos */
977          complain_overflow_dont, /* complain_on_overflow */
978          ppc64_elf_toc_reloc,   /* special_function */
979          "R_PPC64_TOC16_LO",    /* name */
980          FALSE,                 /* partial_inplace */
981          0,                     /* src_mask */
982          0xffff,                /* dst_mask */
983          FALSE),                /* pcrel_offset */
984
985   /* 16 bit TOC-relative relocation, high 16 bits.  */
986
987   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
988   HOWTO (R_PPC64_TOC16_HI,      /* type */
989          16,                    /* rightshift */
990          1,                     /* size (0 = byte, 1 = short, 2 = long) */
991          16,                    /* bitsize */
992          FALSE,                 /* pc_relative */
993          0,                     /* bitpos */
994          complain_overflow_signed, /* complain_on_overflow */
995          ppc64_elf_toc_reloc,   /* special_function */
996          "R_PPC64_TOC16_HI",    /* name */
997          FALSE,                 /* partial_inplace */
998          0,                     /* src_mask */
999          0xffff,                /* dst_mask */
1000          FALSE),                /* pcrel_offset */
1001
1002   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1003      contents of the low 16 bits, treated as a signed number, is
1004      negative.  */
1005
1006   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
1007   HOWTO (R_PPC64_TOC16_HA,      /* type */
1008          16,                    /* rightshift */
1009          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1010          16,                    /* bitsize */
1011          FALSE,                 /* pc_relative */
1012          0,                     /* bitpos */
1013          complain_overflow_signed, /* complain_on_overflow */
1014          ppc64_elf_toc_ha_reloc, /* special_function */
1015          "R_PPC64_TOC16_HA",    /* name */
1016          FALSE,                 /* partial_inplace */
1017          0,                     /* src_mask */
1018          0xffff,                /* dst_mask */
1019          FALSE),                /* pcrel_offset */
1020
1021   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
1022
1023   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
1024   HOWTO (R_PPC64_TOC,           /* type */
1025          0,                     /* rightshift */
1026          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1027          64,                    /* bitsize */
1028          FALSE,                 /* pc_relative */
1029          0,                     /* bitpos */
1030          complain_overflow_dont, /* complain_on_overflow */
1031          ppc64_elf_toc64_reloc, /* special_function */
1032          "R_PPC64_TOC",         /* name */
1033          FALSE,                 /* partial_inplace */
1034          0,                     /* src_mask */
1035          ONES (64),             /* dst_mask */
1036          FALSE),                /* pcrel_offset */
1037
1038   /* Like R_PPC64_GOT16, but also informs the link editor that the
1039      value to relocate may (!) refer to a PLT entry which the link
1040      editor (a) may replace with the symbol value.  If the link editor
1041      is unable to fully resolve the symbol, it may (b) create a PLT
1042      entry and store the address to the new PLT entry in the GOT.
1043      This permits lazy resolution of function symbols at run time.
1044      The link editor may also skip all of this and just (c) emit a
1045      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
1046   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
1047     HOWTO (R_PPC64_PLTGOT16,    /* type */
1048          0,                     /* rightshift */
1049          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1050          16,                    /* bitsize */
1051          FALSE,                 /* pc_relative */
1052          0,                     /* bitpos */
1053          complain_overflow_signed, /* complain_on_overflow */
1054          ppc64_elf_unhandled_reloc, /* special_function */
1055          "R_PPC64_PLTGOT16",    /* name */
1056          FALSE,                 /* partial_inplace */
1057          0,                     /* src_mask */
1058          0xffff,                /* dst_mask */
1059          FALSE),                /* pcrel_offset */
1060
1061   /* Like R_PPC64_PLTGOT16, but without overflow.  */
1062   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1063   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1064          0,                     /* rightshift */
1065          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1066          16,                    /* bitsize */
1067          FALSE,                 /* pc_relative */
1068          0,                     /* bitpos */
1069          complain_overflow_dont, /* complain_on_overflow */
1070          ppc64_elf_unhandled_reloc, /* special_function */
1071          "R_PPC64_PLTGOT16_LO", /* name */
1072          FALSE,                 /* partial_inplace */
1073          0,                     /* src_mask */
1074          0xffff,                /* dst_mask */
1075          FALSE),                /* pcrel_offset */
1076
1077   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1078   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1079   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1080          16,                    /* rightshift */
1081          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1082          16,                    /* bitsize */
1083          FALSE,                 /* pc_relative */
1084          0,                     /* bitpos */
1085          complain_overflow_signed, /* complain_on_overflow */
1086          ppc64_elf_unhandled_reloc, /* special_function */
1087          "R_PPC64_PLTGOT16_HI", /* name */
1088          FALSE,                 /* partial_inplace */
1089          0,                     /* src_mask */
1090          0xffff,                /* dst_mask */
1091          FALSE),                /* pcrel_offset */
1092
1093   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1094      1 if the contents of the low 16 bits, treated as a signed number,
1095      is negative.  */
1096   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1097   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1098          16,                    /* rightshift */
1099          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1100          16,                    /* bitsize */
1101          FALSE,                 /* pc_relative */
1102          0,                     /* bitpos */
1103          complain_overflow_signed, /* complain_on_overflow */
1104          ppc64_elf_unhandled_reloc, /* special_function */
1105          "R_PPC64_PLTGOT16_HA", /* name */
1106          FALSE,                 /* partial_inplace */
1107          0,                     /* src_mask */
1108          0xffff,                /* dst_mask */
1109          FALSE),                /* pcrel_offset */
1110
1111   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1112   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1113          0,                     /* rightshift */
1114          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1115          16,                    /* bitsize */
1116          FALSE,                 /* pc_relative */
1117          0,                     /* bitpos */
1118          complain_overflow_signed, /* complain_on_overflow */
1119          bfd_elf_generic_reloc, /* special_function */
1120          "R_PPC64_ADDR16_DS",   /* name */
1121          FALSE,                 /* partial_inplace */
1122          0,                     /* src_mask */
1123          0xfffc,                /* dst_mask */
1124          FALSE),                /* pcrel_offset */
1125
1126   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1127   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1128          0,                     /* rightshift */
1129          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1130          16,                    /* bitsize */
1131          FALSE,                 /* pc_relative */
1132          0,                     /* bitpos */
1133          complain_overflow_dont,/* complain_on_overflow */
1134          bfd_elf_generic_reloc, /* special_function */
1135          "R_PPC64_ADDR16_LO_DS",/* name */
1136          FALSE,                 /* partial_inplace */
1137          0,                     /* src_mask */
1138          0xfffc,                /* dst_mask */
1139          FALSE),                /* pcrel_offset */
1140
1141   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1142   HOWTO (R_PPC64_GOT16_DS,      /* type */
1143          0,                     /* rightshift */
1144          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1145          16,                    /* bitsize */
1146          FALSE,                 /* pc_relative */
1147          0,                     /* bitpos */
1148          complain_overflow_signed, /* complain_on_overflow */
1149          ppc64_elf_unhandled_reloc, /* special_function */
1150          "R_PPC64_GOT16_DS",    /* name */
1151          FALSE,                 /* partial_inplace */
1152          0,                     /* src_mask */
1153          0xfffc,                /* dst_mask */
1154          FALSE),                /* pcrel_offset */
1155
1156   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1157   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1158          0,                     /* rightshift */
1159          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1160          16,                    /* bitsize */
1161          FALSE,                 /* pc_relative */
1162          0,                     /* bitpos */
1163          complain_overflow_dont, /* complain_on_overflow */
1164          ppc64_elf_unhandled_reloc, /* special_function */
1165          "R_PPC64_GOT16_LO_DS", /* name */
1166          FALSE,                 /* partial_inplace */
1167          0,                     /* src_mask */
1168          0xfffc,                /* dst_mask */
1169          FALSE),                /* pcrel_offset */
1170
1171   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1172   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1173          0,                     /* rightshift */
1174          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1175          16,                    /* bitsize */
1176          FALSE,                 /* pc_relative */
1177          0,                     /* bitpos */
1178          complain_overflow_dont, /* complain_on_overflow */
1179          ppc64_elf_unhandled_reloc, /* special_function */
1180          "R_PPC64_PLT16_LO_DS", /* name */
1181          FALSE,                 /* partial_inplace */
1182          0,                     /* src_mask */
1183          0xfffc,                /* dst_mask */
1184          FALSE),                /* pcrel_offset */
1185
1186   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1187   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1188          0,                     /* rightshift */
1189          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1190          16,                    /* bitsize */
1191          FALSE,                 /* pc_relative */
1192          0,                     /* bitpos */
1193          complain_overflow_signed, /* complain_on_overflow */
1194          ppc64_elf_sectoff_reloc, /* special_function */
1195          "R_PPC64_SECTOFF_DS",  /* name */
1196          FALSE,                 /* partial_inplace */
1197          0,                     /* src_mask */
1198          0xfffc,                /* dst_mask */
1199          FALSE),                /* pcrel_offset */
1200
1201   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1202   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1203          0,                     /* rightshift */
1204          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1205          16,                    /* bitsize */
1206          FALSE,                 /* pc_relative */
1207          0,                     /* bitpos */
1208          complain_overflow_dont, /* complain_on_overflow */
1209          ppc64_elf_sectoff_reloc, /* special_function */
1210          "R_PPC64_SECTOFF_LO_DS",/* name */
1211          FALSE,                 /* partial_inplace */
1212          0,                     /* src_mask */
1213          0xfffc,                /* dst_mask */
1214          FALSE),                /* pcrel_offset */
1215
1216   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1217   HOWTO (R_PPC64_TOC16_DS,      /* type */
1218          0,                     /* rightshift */
1219          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1220          16,                    /* bitsize */
1221          FALSE,                 /* pc_relative */
1222          0,                     /* bitpos */
1223          complain_overflow_signed, /* complain_on_overflow */
1224          ppc64_elf_toc_reloc,   /* special_function */
1225          "R_PPC64_TOC16_DS",    /* name */
1226          FALSE,                 /* partial_inplace */
1227          0,                     /* src_mask */
1228          0xfffc,                /* dst_mask */
1229          FALSE),                /* pcrel_offset */
1230
1231   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1232   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1233          0,                     /* rightshift */
1234          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1235          16,                    /* bitsize */
1236          FALSE,                 /* pc_relative */
1237          0,                     /* bitpos */
1238          complain_overflow_dont, /* complain_on_overflow */
1239          ppc64_elf_toc_reloc,   /* special_function */
1240          "R_PPC64_TOC16_LO_DS", /* name */
1241          FALSE,                 /* partial_inplace */
1242          0,                     /* src_mask */
1243          0xfffc,                /* dst_mask */
1244          FALSE),                /* pcrel_offset */
1245
1246   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1247   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1248   HOWTO (R_PPC64_PLTGOT16_DS,   /* type */
1249          0,                     /* rightshift */
1250          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1251          16,                    /* bitsize */
1252          FALSE,                 /* pc_relative */
1253          0,                     /* bitpos */
1254          complain_overflow_signed, /* complain_on_overflow */
1255          ppc64_elf_unhandled_reloc, /* special_function */
1256          "R_PPC64_PLTGOT16_DS", /* name */
1257          FALSE,                 /* partial_inplace */
1258          0,                     /* src_mask */
1259          0xfffc,                /* dst_mask */
1260          FALSE),                /* pcrel_offset */
1261
1262   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1263   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1264   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1265          0,                     /* rightshift */
1266          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1267          16,                    /* bitsize */
1268          FALSE,                 /* pc_relative */
1269          0,                     /* bitpos */
1270          complain_overflow_dont, /* complain_on_overflow */
1271          ppc64_elf_unhandled_reloc, /* special_function */
1272          "R_PPC64_PLTGOT16_LO_DS",/* name */
1273          FALSE,                 /* partial_inplace */
1274          0,                     /* src_mask */
1275          0xfffc,                /* dst_mask */
1276          FALSE),                /* pcrel_offset */
1277
1278   /* Marker relocs for TLS.  */
1279   HOWTO (R_PPC64_TLS,
1280          0,                     /* rightshift */
1281          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1282          32,                    /* bitsize */
1283          FALSE,                 /* pc_relative */
1284          0,                     /* bitpos */
1285          complain_overflow_dont, /* complain_on_overflow */
1286          bfd_elf_generic_reloc, /* special_function */
1287          "R_PPC64_TLS",         /* name */
1288          FALSE,                 /* partial_inplace */
1289          0,                     /* src_mask */
1290          0,                     /* dst_mask */
1291          FALSE),                /* pcrel_offset */
1292
1293   HOWTO (R_PPC64_TLSGD,
1294          0,                     /* rightshift */
1295          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1296          32,                    /* bitsize */
1297          FALSE,                 /* pc_relative */
1298          0,                     /* bitpos */
1299          complain_overflow_dont, /* complain_on_overflow */
1300          bfd_elf_generic_reloc, /* special_function */
1301          "R_PPC64_TLSGD",       /* name */
1302          FALSE,                 /* partial_inplace */
1303          0,                     /* src_mask */
1304          0,                     /* dst_mask */
1305          FALSE),                /* pcrel_offset */
1306
1307   HOWTO (R_PPC64_TLSLD,
1308          0,                     /* rightshift */
1309          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1310          32,                    /* bitsize */
1311          FALSE,                 /* pc_relative */
1312          0,                     /* bitpos */
1313          complain_overflow_dont, /* complain_on_overflow */
1314          bfd_elf_generic_reloc, /* special_function */
1315          "R_PPC64_TLSLD",       /* name */
1316          FALSE,                 /* partial_inplace */
1317          0,                     /* src_mask */
1318          0,                     /* dst_mask */
1319          FALSE),                /* pcrel_offset */
1320
1321   HOWTO (R_PPC64_TOCSAVE,
1322          0,                     /* rightshift */
1323          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1324          32,                    /* bitsize */
1325          FALSE,                 /* pc_relative */
1326          0,                     /* bitpos */
1327          complain_overflow_dont, /* complain_on_overflow */
1328          bfd_elf_generic_reloc, /* special_function */
1329          "R_PPC64_TOCSAVE",     /* name */
1330          FALSE,                 /* partial_inplace */
1331          0,                     /* src_mask */
1332          0,                     /* dst_mask */
1333          FALSE),                /* pcrel_offset */
1334
1335   /* Computes the load module index of the load module that contains the
1336      definition of its TLS sym.  */
1337   HOWTO (R_PPC64_DTPMOD64,
1338          0,                     /* rightshift */
1339          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1340          64,                    /* bitsize */
1341          FALSE,                 /* pc_relative */
1342          0,                     /* bitpos */
1343          complain_overflow_dont, /* complain_on_overflow */
1344          ppc64_elf_unhandled_reloc, /* special_function */
1345          "R_PPC64_DTPMOD64",    /* name */
1346          FALSE,                 /* partial_inplace */
1347          0,                     /* src_mask */
1348          ONES (64),             /* dst_mask */
1349          FALSE),                /* pcrel_offset */
1350
1351   /* Computes a dtv-relative displacement, the difference between the value
1352      of sym+add and the base address of the thread-local storage block that
1353      contains the definition of sym, minus 0x8000.  */
1354   HOWTO (R_PPC64_DTPREL64,
1355          0,                     /* rightshift */
1356          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1357          64,                    /* bitsize */
1358          FALSE,                 /* pc_relative */
1359          0,                     /* bitpos */
1360          complain_overflow_dont, /* complain_on_overflow */
1361          ppc64_elf_unhandled_reloc, /* special_function */
1362          "R_PPC64_DTPREL64",    /* name */
1363          FALSE,                 /* partial_inplace */
1364          0,                     /* src_mask */
1365          ONES (64),             /* dst_mask */
1366          FALSE),                /* pcrel_offset */
1367
1368   /* A 16 bit dtprel reloc.  */
1369   HOWTO (R_PPC64_DTPREL16,
1370          0,                     /* rightshift */
1371          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1372          16,                    /* bitsize */
1373          FALSE,                 /* pc_relative */
1374          0,                     /* bitpos */
1375          complain_overflow_signed, /* complain_on_overflow */
1376          ppc64_elf_unhandled_reloc, /* special_function */
1377          "R_PPC64_DTPREL16",    /* name */
1378          FALSE,                 /* partial_inplace */
1379          0,                     /* src_mask */
1380          0xffff,                /* dst_mask */
1381          FALSE),                /* pcrel_offset */
1382
1383   /* Like DTPREL16, but no overflow.  */
1384   HOWTO (R_PPC64_DTPREL16_LO,
1385          0,                     /* rightshift */
1386          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1387          16,                    /* bitsize */
1388          FALSE,                 /* pc_relative */
1389          0,                     /* bitpos */
1390          complain_overflow_dont, /* complain_on_overflow */
1391          ppc64_elf_unhandled_reloc, /* special_function */
1392          "R_PPC64_DTPREL16_LO", /* name */
1393          FALSE,                 /* partial_inplace */
1394          0,                     /* src_mask */
1395          0xffff,                /* dst_mask */
1396          FALSE),                /* pcrel_offset */
1397
1398   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1399   HOWTO (R_PPC64_DTPREL16_HI,
1400          16,                    /* rightshift */
1401          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1402          16,                    /* bitsize */
1403          FALSE,                 /* pc_relative */
1404          0,                     /* bitpos */
1405          complain_overflow_signed, /* complain_on_overflow */
1406          ppc64_elf_unhandled_reloc, /* special_function */
1407          "R_PPC64_DTPREL16_HI", /* name */
1408          FALSE,                 /* partial_inplace */
1409          0,                     /* src_mask */
1410          0xffff,                /* dst_mask */
1411          FALSE),                /* pcrel_offset */
1412
1413   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1414   HOWTO (R_PPC64_DTPREL16_HA,
1415          16,                    /* rightshift */
1416          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1417          16,                    /* bitsize */
1418          FALSE,                 /* pc_relative */
1419          0,                     /* bitpos */
1420          complain_overflow_signed, /* complain_on_overflow */
1421          ppc64_elf_unhandled_reloc, /* special_function */
1422          "R_PPC64_DTPREL16_HA", /* name */
1423          FALSE,                 /* partial_inplace */
1424          0,                     /* src_mask */
1425          0xffff,                /* dst_mask */
1426          FALSE),                /* pcrel_offset */
1427
1428   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1429   HOWTO (R_PPC64_DTPREL16_HIGHER,
1430          32,                    /* rightshift */
1431          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1432          16,                    /* bitsize */
1433          FALSE,                 /* pc_relative */
1434          0,                     /* bitpos */
1435          complain_overflow_dont, /* complain_on_overflow */
1436          ppc64_elf_unhandled_reloc, /* special_function */
1437          "R_PPC64_DTPREL16_HIGHER", /* name */
1438          FALSE,                 /* partial_inplace */
1439          0,                     /* src_mask */
1440          0xffff,                /* dst_mask */
1441          FALSE),                /* pcrel_offset */
1442
1443   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1444   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1445          32,                    /* rightshift */
1446          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1447          16,                    /* bitsize */
1448          FALSE,                 /* pc_relative */
1449          0,                     /* bitpos */
1450          complain_overflow_dont, /* complain_on_overflow */
1451          ppc64_elf_unhandled_reloc, /* special_function */
1452          "R_PPC64_DTPREL16_HIGHERA", /* name */
1453          FALSE,                 /* partial_inplace */
1454          0,                     /* src_mask */
1455          0xffff,                /* dst_mask */
1456          FALSE),                /* pcrel_offset */
1457
1458   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1459   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1460          48,                    /* rightshift */
1461          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1462          16,                    /* bitsize */
1463          FALSE,                 /* pc_relative */
1464          0,                     /* bitpos */
1465          complain_overflow_dont, /* complain_on_overflow */
1466          ppc64_elf_unhandled_reloc, /* special_function */
1467          "R_PPC64_DTPREL16_HIGHEST", /* name */
1468          FALSE,                 /* partial_inplace */
1469          0,                     /* src_mask */
1470          0xffff,                /* dst_mask */
1471          FALSE),                /* pcrel_offset */
1472
1473   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1474   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1475          48,                    /* rightshift */
1476          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1477          16,                    /* bitsize */
1478          FALSE,                 /* pc_relative */
1479          0,                     /* bitpos */
1480          complain_overflow_dont, /* complain_on_overflow */
1481          ppc64_elf_unhandled_reloc, /* special_function */
1482          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1483          FALSE,                 /* partial_inplace */
1484          0,                     /* src_mask */
1485          0xffff,                /* dst_mask */
1486          FALSE),                /* pcrel_offset */
1487
1488   /* Like DTPREL16, but for insns with a DS field.  */
1489   HOWTO (R_PPC64_DTPREL16_DS,
1490          0,                     /* rightshift */
1491          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1492          16,                    /* bitsize */
1493          FALSE,                 /* pc_relative */
1494          0,                     /* bitpos */
1495          complain_overflow_signed, /* complain_on_overflow */
1496          ppc64_elf_unhandled_reloc, /* special_function */
1497          "R_PPC64_DTPREL16_DS", /* name */
1498          FALSE,                 /* partial_inplace */
1499          0,                     /* src_mask */
1500          0xfffc,                /* dst_mask */
1501          FALSE),                /* pcrel_offset */
1502
1503   /* Like DTPREL16_DS, but no overflow.  */
1504   HOWTO (R_PPC64_DTPREL16_LO_DS,
1505          0,                     /* rightshift */
1506          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1507          16,                    /* bitsize */
1508          FALSE,                 /* pc_relative */
1509          0,                     /* bitpos */
1510          complain_overflow_dont, /* complain_on_overflow */
1511          ppc64_elf_unhandled_reloc, /* special_function */
1512          "R_PPC64_DTPREL16_LO_DS", /* name */
1513          FALSE,                 /* partial_inplace */
1514          0,                     /* src_mask */
1515          0xfffc,                /* dst_mask */
1516          FALSE),                /* pcrel_offset */
1517
1518   /* Computes a tp-relative displacement, the difference between the value of
1519      sym+add and the value of the thread pointer (r13).  */
1520   HOWTO (R_PPC64_TPREL64,
1521          0,                     /* rightshift */
1522          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1523          64,                    /* bitsize */
1524          FALSE,                 /* pc_relative */
1525          0,                     /* bitpos */
1526          complain_overflow_dont, /* complain_on_overflow */
1527          ppc64_elf_unhandled_reloc, /* special_function */
1528          "R_PPC64_TPREL64",     /* name */
1529          FALSE,                 /* partial_inplace */
1530          0,                     /* src_mask */
1531          ONES (64),             /* dst_mask */
1532          FALSE),                /* pcrel_offset */
1533
1534   /* A 16 bit tprel reloc.  */
1535   HOWTO (R_PPC64_TPREL16,
1536          0,                     /* rightshift */
1537          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1538          16,                    /* bitsize */
1539          FALSE,                 /* pc_relative */
1540          0,                     /* bitpos */
1541          complain_overflow_signed, /* complain_on_overflow */
1542          ppc64_elf_unhandled_reloc, /* special_function */
1543          "R_PPC64_TPREL16",     /* name */
1544          FALSE,                 /* partial_inplace */
1545          0,                     /* src_mask */
1546          0xffff,                /* dst_mask */
1547          FALSE),                /* pcrel_offset */
1548
1549   /* Like TPREL16, but no overflow.  */
1550   HOWTO (R_PPC64_TPREL16_LO,
1551          0,                     /* rightshift */
1552          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1553          16,                    /* bitsize */
1554          FALSE,                 /* pc_relative */
1555          0,                     /* bitpos */
1556          complain_overflow_dont, /* complain_on_overflow */
1557          ppc64_elf_unhandled_reloc, /* special_function */
1558          "R_PPC64_TPREL16_LO",  /* name */
1559          FALSE,                 /* partial_inplace */
1560          0,                     /* src_mask */
1561          0xffff,                /* dst_mask */
1562          FALSE),                /* pcrel_offset */
1563
1564   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1565   HOWTO (R_PPC64_TPREL16_HI,
1566          16,                    /* rightshift */
1567          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1568          16,                    /* bitsize */
1569          FALSE,                 /* pc_relative */
1570          0,                     /* bitpos */
1571          complain_overflow_signed, /* complain_on_overflow */
1572          ppc64_elf_unhandled_reloc, /* special_function */
1573          "R_PPC64_TPREL16_HI",  /* name */
1574          FALSE,                 /* partial_inplace */
1575          0,                     /* src_mask */
1576          0xffff,                /* dst_mask */
1577          FALSE),                /* pcrel_offset */
1578
1579   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1580   HOWTO (R_PPC64_TPREL16_HA,
1581          16,                    /* rightshift */
1582          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1583          16,                    /* bitsize */
1584          FALSE,                 /* pc_relative */
1585          0,                     /* bitpos */
1586          complain_overflow_signed, /* complain_on_overflow */
1587          ppc64_elf_unhandled_reloc, /* special_function */
1588          "R_PPC64_TPREL16_HA",  /* name */
1589          FALSE,                 /* partial_inplace */
1590          0,                     /* src_mask */
1591          0xffff,                /* dst_mask */
1592          FALSE),                /* pcrel_offset */
1593
1594   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1595   HOWTO (R_PPC64_TPREL16_HIGHER,
1596          32,                    /* rightshift */
1597          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1598          16,                    /* bitsize */
1599          FALSE,                 /* pc_relative */
1600          0,                     /* bitpos */
1601          complain_overflow_dont, /* complain_on_overflow */
1602          ppc64_elf_unhandled_reloc, /* special_function */
1603          "R_PPC64_TPREL16_HIGHER",      /* name */
1604          FALSE,                 /* partial_inplace */
1605          0,                     /* src_mask */
1606          0xffff,                /* dst_mask */
1607          FALSE),                /* pcrel_offset */
1608
1609   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1610   HOWTO (R_PPC64_TPREL16_HIGHERA,
1611          32,                    /* rightshift */
1612          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1613          16,                    /* bitsize */
1614          FALSE,                 /* pc_relative */
1615          0,                     /* bitpos */
1616          complain_overflow_dont, /* complain_on_overflow */
1617          ppc64_elf_unhandled_reloc, /* special_function */
1618          "R_PPC64_TPREL16_HIGHERA", /* name */
1619          FALSE,                 /* partial_inplace */
1620          0,                     /* src_mask */
1621          0xffff,                /* dst_mask */
1622          FALSE),                /* pcrel_offset */
1623
1624   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1625   HOWTO (R_PPC64_TPREL16_HIGHEST,
1626          48,                    /* rightshift */
1627          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1628          16,                    /* bitsize */
1629          FALSE,                 /* pc_relative */
1630          0,                     /* bitpos */
1631          complain_overflow_dont, /* complain_on_overflow */
1632          ppc64_elf_unhandled_reloc, /* special_function */
1633          "R_PPC64_TPREL16_HIGHEST", /* name */
1634          FALSE,                 /* partial_inplace */
1635          0,                     /* src_mask */
1636          0xffff,                /* dst_mask */
1637          FALSE),                /* pcrel_offset */
1638
1639   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1640   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1641          48,                    /* rightshift */
1642          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1643          16,                    /* bitsize */
1644          FALSE,                 /* pc_relative */
1645          0,                     /* bitpos */
1646          complain_overflow_dont, /* complain_on_overflow */
1647          ppc64_elf_unhandled_reloc, /* special_function */
1648          "R_PPC64_TPREL16_HIGHESTA", /* name */
1649          FALSE,                 /* partial_inplace */
1650          0,                     /* src_mask */
1651          0xffff,                /* dst_mask */
1652          FALSE),                /* pcrel_offset */
1653
1654   /* Like TPREL16, but for insns with a DS field.  */
1655   HOWTO (R_PPC64_TPREL16_DS,
1656          0,                     /* rightshift */
1657          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1658          16,                    /* bitsize */
1659          FALSE,                 /* pc_relative */
1660          0,                     /* bitpos */
1661          complain_overflow_signed, /* complain_on_overflow */
1662          ppc64_elf_unhandled_reloc, /* special_function */
1663          "R_PPC64_TPREL16_DS",  /* name */
1664          FALSE,                 /* partial_inplace */
1665          0,                     /* src_mask */
1666          0xfffc,                /* dst_mask */
1667          FALSE),                /* pcrel_offset */
1668
1669   /* Like TPREL16_DS, but no overflow.  */
1670   HOWTO (R_PPC64_TPREL16_LO_DS,
1671          0,                     /* rightshift */
1672          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1673          16,                    /* bitsize */
1674          FALSE,                 /* pc_relative */
1675          0,                     /* bitpos */
1676          complain_overflow_dont, /* complain_on_overflow */
1677          ppc64_elf_unhandled_reloc, /* special_function */
1678          "R_PPC64_TPREL16_LO_DS", /* name */
1679          FALSE,                 /* partial_inplace */
1680          0,                     /* src_mask */
1681          0xfffc,                /* dst_mask */
1682          FALSE),                /* pcrel_offset */
1683
1684   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1685      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1686      to the first entry relative to the TOC base (r2).  */
1687   HOWTO (R_PPC64_GOT_TLSGD16,
1688          0,                     /* rightshift */
1689          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1690          16,                    /* bitsize */
1691          FALSE,                 /* pc_relative */
1692          0,                     /* bitpos */
1693          complain_overflow_signed, /* complain_on_overflow */
1694          ppc64_elf_unhandled_reloc, /* special_function */
1695          "R_PPC64_GOT_TLSGD16", /* name */
1696          FALSE,                 /* partial_inplace */
1697          0,                     /* src_mask */
1698          0xffff,                /* dst_mask */
1699          FALSE),                /* pcrel_offset */
1700
1701   /* Like GOT_TLSGD16, but no overflow.  */
1702   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1703          0,                     /* rightshift */
1704          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1705          16,                    /* bitsize */
1706          FALSE,                 /* pc_relative */
1707          0,                     /* bitpos */
1708          complain_overflow_dont, /* complain_on_overflow */
1709          ppc64_elf_unhandled_reloc, /* special_function */
1710          "R_PPC64_GOT_TLSGD16_LO", /* name */
1711          FALSE,                 /* partial_inplace */
1712          0,                     /* src_mask */
1713          0xffff,                /* dst_mask */
1714          FALSE),                /* pcrel_offset */
1715
1716   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1717   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1718          16,                    /* rightshift */
1719          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1720          16,                    /* bitsize */
1721          FALSE,                 /* pc_relative */
1722          0,                     /* bitpos */
1723          complain_overflow_signed, /* complain_on_overflow */
1724          ppc64_elf_unhandled_reloc, /* special_function */
1725          "R_PPC64_GOT_TLSGD16_HI", /* name */
1726          FALSE,                 /* partial_inplace */
1727          0,                     /* src_mask */
1728          0xffff,                /* dst_mask */
1729          FALSE),                /* pcrel_offset */
1730
1731   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1732   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1733          16,                    /* rightshift */
1734          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1735          16,                    /* bitsize */
1736          FALSE,                 /* pc_relative */
1737          0,                     /* bitpos */
1738          complain_overflow_signed, /* complain_on_overflow */
1739          ppc64_elf_unhandled_reloc, /* special_function */
1740          "R_PPC64_GOT_TLSGD16_HA", /* name */
1741          FALSE,                 /* partial_inplace */
1742          0,                     /* src_mask */
1743          0xffff,                /* dst_mask */
1744          FALSE),                /* pcrel_offset */
1745
1746   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1747      with values (sym+add)@dtpmod and zero, and computes the offset to the
1748      first entry relative to the TOC base (r2).  */
1749   HOWTO (R_PPC64_GOT_TLSLD16,
1750          0,                     /* rightshift */
1751          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1752          16,                    /* bitsize */
1753          FALSE,                 /* pc_relative */
1754          0,                     /* bitpos */
1755          complain_overflow_signed, /* complain_on_overflow */
1756          ppc64_elf_unhandled_reloc, /* special_function */
1757          "R_PPC64_GOT_TLSLD16", /* name */
1758          FALSE,                 /* partial_inplace */
1759          0,                     /* src_mask */
1760          0xffff,                /* dst_mask */
1761          FALSE),                /* pcrel_offset */
1762
1763   /* Like GOT_TLSLD16, but no overflow.  */
1764   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1765          0,                     /* rightshift */
1766          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1767          16,                    /* bitsize */
1768          FALSE,                 /* pc_relative */
1769          0,                     /* bitpos */
1770          complain_overflow_dont, /* complain_on_overflow */
1771          ppc64_elf_unhandled_reloc, /* special_function */
1772          "R_PPC64_GOT_TLSLD16_LO", /* name */
1773          FALSE,                 /* partial_inplace */
1774          0,                     /* src_mask */
1775          0xffff,                /* dst_mask */
1776          FALSE),                /* pcrel_offset */
1777
1778   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1779   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1780          16,                    /* rightshift */
1781          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1782          16,                    /* bitsize */
1783          FALSE,                 /* pc_relative */
1784          0,                     /* bitpos */
1785          complain_overflow_signed, /* complain_on_overflow */
1786          ppc64_elf_unhandled_reloc, /* special_function */
1787          "R_PPC64_GOT_TLSLD16_HI", /* name */
1788          FALSE,                 /* partial_inplace */
1789          0,                     /* src_mask */
1790          0xffff,                /* dst_mask */
1791          FALSE),                /* pcrel_offset */
1792
1793   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1794   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1795          16,                    /* rightshift */
1796          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1797          16,                    /* bitsize */
1798          FALSE,                 /* pc_relative */
1799          0,                     /* bitpos */
1800          complain_overflow_signed, /* complain_on_overflow */
1801          ppc64_elf_unhandled_reloc, /* special_function */
1802          "R_PPC64_GOT_TLSLD16_HA", /* name */
1803          FALSE,                 /* partial_inplace */
1804          0,                     /* src_mask */
1805          0xffff,                /* dst_mask */
1806          FALSE),                /* pcrel_offset */
1807
1808   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1809      the offset to the entry relative to the TOC base (r2).  */
1810   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1811          0,                     /* rightshift */
1812          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1813          16,                    /* bitsize */
1814          FALSE,                 /* pc_relative */
1815          0,                     /* bitpos */
1816          complain_overflow_signed, /* complain_on_overflow */
1817          ppc64_elf_unhandled_reloc, /* special_function */
1818          "R_PPC64_GOT_DTPREL16_DS", /* name */
1819          FALSE,                 /* partial_inplace */
1820          0,                     /* src_mask */
1821          0xfffc,                /* dst_mask */
1822          FALSE),                /* pcrel_offset */
1823
1824   /* Like GOT_DTPREL16_DS, but no overflow.  */
1825   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1826          0,                     /* rightshift */
1827          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1828          16,                    /* bitsize */
1829          FALSE,                 /* pc_relative */
1830          0,                     /* bitpos */
1831          complain_overflow_dont, /* complain_on_overflow */
1832          ppc64_elf_unhandled_reloc, /* special_function */
1833          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1834          FALSE,                 /* partial_inplace */
1835          0,                     /* src_mask */
1836          0xfffc,                /* dst_mask */
1837          FALSE),                /* pcrel_offset */
1838
1839   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1840   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1841          16,                    /* rightshift */
1842          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1843          16,                    /* bitsize */
1844          FALSE,                 /* pc_relative */
1845          0,                     /* bitpos */
1846          complain_overflow_signed, /* complain_on_overflow */
1847          ppc64_elf_unhandled_reloc, /* special_function */
1848          "R_PPC64_GOT_DTPREL16_HI", /* name */
1849          FALSE,                 /* partial_inplace */
1850          0,                     /* src_mask */
1851          0xffff,                /* dst_mask */
1852          FALSE),                /* pcrel_offset */
1853
1854   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1855   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1856          16,                    /* rightshift */
1857          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1858          16,                    /* bitsize */
1859          FALSE,                 /* pc_relative */
1860          0,                     /* bitpos */
1861          complain_overflow_signed, /* complain_on_overflow */
1862          ppc64_elf_unhandled_reloc, /* special_function */
1863          "R_PPC64_GOT_DTPREL16_HA", /* name */
1864          FALSE,                 /* partial_inplace */
1865          0,                     /* src_mask */
1866          0xffff,                /* dst_mask */
1867          FALSE),                /* pcrel_offset */
1868
1869   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1870      offset to the entry relative to the TOC base (r2).  */
1871   HOWTO (R_PPC64_GOT_TPREL16_DS,
1872          0,                     /* rightshift */
1873          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1874          16,                    /* bitsize */
1875          FALSE,                 /* pc_relative */
1876          0,                     /* bitpos */
1877          complain_overflow_signed, /* complain_on_overflow */
1878          ppc64_elf_unhandled_reloc, /* special_function */
1879          "R_PPC64_GOT_TPREL16_DS", /* name */
1880          FALSE,                 /* partial_inplace */
1881          0,                     /* src_mask */
1882          0xfffc,                /* dst_mask */
1883          FALSE),                /* pcrel_offset */
1884
1885   /* Like GOT_TPREL16_DS, but no overflow.  */
1886   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1887          0,                     /* rightshift */
1888          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1889          16,                    /* bitsize */
1890          FALSE,                 /* pc_relative */
1891          0,                     /* bitpos */
1892          complain_overflow_dont, /* complain_on_overflow */
1893          ppc64_elf_unhandled_reloc, /* special_function */
1894          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1895          FALSE,                 /* partial_inplace */
1896          0,                     /* src_mask */
1897          0xfffc,                /* dst_mask */
1898          FALSE),                /* pcrel_offset */
1899
1900   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1901   HOWTO (R_PPC64_GOT_TPREL16_HI,
1902          16,                    /* rightshift */
1903          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1904          16,                    /* bitsize */
1905          FALSE,                 /* pc_relative */
1906          0,                     /* bitpos */
1907          complain_overflow_signed, /* complain_on_overflow */
1908          ppc64_elf_unhandled_reloc, /* special_function */
1909          "R_PPC64_GOT_TPREL16_HI", /* name */
1910          FALSE,                 /* partial_inplace */
1911          0,                     /* src_mask */
1912          0xffff,                /* dst_mask */
1913          FALSE),                /* pcrel_offset */
1914
1915   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1916   HOWTO (R_PPC64_GOT_TPREL16_HA,
1917          16,                    /* rightshift */
1918          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1919          16,                    /* bitsize */
1920          FALSE,                 /* pc_relative */
1921          0,                     /* bitpos */
1922          complain_overflow_signed, /* complain_on_overflow */
1923          ppc64_elf_unhandled_reloc, /* special_function */
1924          "R_PPC64_GOT_TPREL16_HA", /* name */
1925          FALSE,                 /* partial_inplace */
1926          0,                     /* src_mask */
1927          0xffff,                /* dst_mask */
1928          FALSE),                /* pcrel_offset */
1929
1930   HOWTO (R_PPC64_JMP_IREL,      /* type */
1931          0,                     /* rightshift */
1932          0,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1933          0,                     /* bitsize */
1934          FALSE,                 /* pc_relative */
1935          0,                     /* bitpos */
1936          complain_overflow_dont, /* complain_on_overflow */
1937          ppc64_elf_unhandled_reloc, /* special_function */
1938          "R_PPC64_JMP_IREL",    /* name */
1939          FALSE,                 /* partial_inplace */
1940          0,                     /* src_mask */
1941          0,                     /* dst_mask */
1942          FALSE),                /* pcrel_offset */
1943
1944   HOWTO (R_PPC64_IRELATIVE,     /* type */
1945          0,                     /* rightshift */
1946          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1947          64,                    /* bitsize */
1948          FALSE,                 /* pc_relative */
1949          0,                     /* bitpos */
1950          complain_overflow_dont, /* complain_on_overflow */
1951          bfd_elf_generic_reloc, /* special_function */
1952          "R_PPC64_IRELATIVE",   /* name */
1953          FALSE,                 /* partial_inplace */
1954          0,                     /* src_mask */
1955          ONES (64),             /* dst_mask */
1956          FALSE),                /* pcrel_offset */
1957
1958   /* A 16 bit relative relocation.  */
1959   HOWTO (R_PPC64_REL16,         /* type */
1960          0,                     /* rightshift */
1961          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1962          16,                    /* bitsize */
1963          TRUE,                  /* pc_relative */
1964          0,                     /* bitpos */
1965          complain_overflow_signed, /* complain_on_overflow */
1966          bfd_elf_generic_reloc, /* special_function */
1967          "R_PPC64_REL16",       /* name */
1968          FALSE,                 /* partial_inplace */
1969          0,                     /* src_mask */
1970          0xffff,                /* dst_mask */
1971          TRUE),                 /* pcrel_offset */
1972
1973   /* A 16 bit relative relocation without overflow.  */
1974   HOWTO (R_PPC64_REL16_LO,      /* type */
1975          0,                     /* rightshift */
1976          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1977          16,                    /* bitsize */
1978          TRUE,                  /* pc_relative */
1979          0,                     /* bitpos */
1980          complain_overflow_dont,/* complain_on_overflow */
1981          bfd_elf_generic_reloc, /* special_function */
1982          "R_PPC64_REL16_LO",    /* name */
1983          FALSE,                 /* partial_inplace */
1984          0,                     /* src_mask */
1985          0xffff,                /* dst_mask */
1986          TRUE),                 /* pcrel_offset */
1987
1988   /* The high order 16 bits of a relative address.  */
1989   HOWTO (R_PPC64_REL16_HI,      /* type */
1990          16,                    /* rightshift */
1991          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1992          16,                    /* bitsize */
1993          TRUE,                  /* pc_relative */
1994          0,                     /* bitpos */
1995          complain_overflow_signed, /* complain_on_overflow */
1996          bfd_elf_generic_reloc, /* special_function */
1997          "R_PPC64_REL16_HI",    /* name */
1998          FALSE,                 /* partial_inplace */
1999          0,                     /* src_mask */
2000          0xffff,                /* dst_mask */
2001          TRUE),                 /* pcrel_offset */
2002
2003   /* The high order 16 bits of a relative address, plus 1 if the contents of
2004      the low 16 bits, treated as a signed number, is negative.  */
2005   HOWTO (R_PPC64_REL16_HA,      /* type */
2006          16,                    /* rightshift */
2007          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2008          16,                    /* bitsize */
2009          TRUE,                  /* pc_relative */
2010          0,                     /* bitpos */
2011          complain_overflow_signed, /* complain_on_overflow */
2012          ppc64_elf_ha_reloc,    /* special_function */
2013          "R_PPC64_REL16_HA",    /* name */
2014          FALSE,                 /* partial_inplace */
2015          0,                     /* src_mask */
2016          0xffff,                /* dst_mask */
2017          TRUE),                 /* pcrel_offset */
2018
2019   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
2020   HOWTO (R_PPC64_ADDR16_HIGH,   /* type */
2021          16,                    /* rightshift */
2022          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2023          16,                    /* bitsize */
2024          FALSE,                 /* pc_relative */
2025          0,                     /* bitpos */
2026          complain_overflow_dont, /* complain_on_overflow */
2027          bfd_elf_generic_reloc, /* special_function */
2028          "R_PPC64_ADDR16_HIGH", /* name */
2029          FALSE,                 /* partial_inplace */
2030          0,                     /* src_mask */
2031          0xffff,                /* dst_mask */
2032          FALSE),                /* pcrel_offset */
2033
2034   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
2035   HOWTO (R_PPC64_ADDR16_HIGHA,  /* type */
2036          16,                    /* rightshift */
2037          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2038          16,                    /* bitsize */
2039          FALSE,                 /* pc_relative */
2040          0,                     /* bitpos */
2041          complain_overflow_dont, /* complain_on_overflow */
2042          ppc64_elf_ha_reloc,    /* special_function */
2043          "R_PPC64_ADDR16_HIGHA",        /* name */
2044          FALSE,                 /* partial_inplace */
2045          0,                     /* src_mask */
2046          0xffff,                /* dst_mask */
2047          FALSE),                /* pcrel_offset */
2048
2049   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
2050   HOWTO (R_PPC64_DTPREL16_HIGH,
2051          16,                    /* rightshift */
2052          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2053          16,                    /* bitsize */
2054          FALSE,                 /* pc_relative */
2055          0,                     /* bitpos */
2056          complain_overflow_dont, /* complain_on_overflow */
2057          ppc64_elf_unhandled_reloc, /* special_function */
2058          "R_PPC64_DTPREL16_HIGH", /* name */
2059          FALSE,                 /* partial_inplace */
2060          0,                     /* src_mask */
2061          0xffff,                /* dst_mask */
2062          FALSE),                /* pcrel_offset */
2063
2064   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
2065   HOWTO (R_PPC64_DTPREL16_HIGHA,
2066          16,                    /* rightshift */
2067          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2068          16,                    /* bitsize */
2069          FALSE,                 /* pc_relative */
2070          0,                     /* bitpos */
2071          complain_overflow_dont, /* complain_on_overflow */
2072          ppc64_elf_unhandled_reloc, /* special_function */
2073          "R_PPC64_DTPREL16_HIGHA", /* name */
2074          FALSE,                 /* partial_inplace */
2075          0,                     /* src_mask */
2076          0xffff,                /* dst_mask */
2077          FALSE),                /* pcrel_offset */
2078
2079   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
2080   HOWTO (R_PPC64_TPREL16_HIGH,
2081          16,                    /* rightshift */
2082          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2083          16,                    /* bitsize */
2084          FALSE,                 /* pc_relative */
2085          0,                     /* bitpos */
2086          complain_overflow_dont, /* complain_on_overflow */
2087          ppc64_elf_unhandled_reloc, /* special_function */
2088          "R_PPC64_TPREL16_HIGH",        /* name */
2089          FALSE,                 /* partial_inplace */
2090          0,                     /* src_mask */
2091          0xffff,                /* dst_mask */
2092          FALSE),                /* pcrel_offset */
2093
2094   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
2095   HOWTO (R_PPC64_TPREL16_HIGHA,
2096          16,                    /* rightshift */
2097          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2098          16,                    /* bitsize */
2099          FALSE,                 /* pc_relative */
2100          0,                     /* bitpos */
2101          complain_overflow_dont, /* complain_on_overflow */
2102          ppc64_elf_unhandled_reloc, /* special_function */
2103          "R_PPC64_TPREL16_HIGHA",       /* name */
2104          FALSE,                 /* partial_inplace */
2105          0,                     /* src_mask */
2106          0xffff,                /* dst_mask */
2107          FALSE),                /* pcrel_offset */
2108
2109   /* Like ADDR64, but use local entry point of function.  */
2110   HOWTO (R_PPC64_ADDR64_LOCAL,  /* type */
2111          0,                     /* rightshift */
2112          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2113          64,                    /* bitsize */
2114          FALSE,                 /* pc_relative */
2115          0,                     /* bitpos */
2116          complain_overflow_dont, /* complain_on_overflow */
2117          bfd_elf_generic_reloc, /* special_function */
2118          "R_PPC64_ADDR64_LOCAL", /* name */
2119          FALSE,                 /* partial_inplace */
2120          0,                     /* src_mask */
2121          ONES (64),             /* dst_mask */
2122          FALSE),                /* pcrel_offset */
2123
2124   /* GNU extension to record C++ vtable hierarchy.  */
2125   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2126          0,                     /* rightshift */
2127          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2128          0,                     /* bitsize */
2129          FALSE,                 /* pc_relative */
2130          0,                     /* bitpos */
2131          complain_overflow_dont, /* complain_on_overflow */
2132          NULL,                  /* special_function */
2133          "R_PPC64_GNU_VTINHERIT", /* name */
2134          FALSE,                 /* partial_inplace */
2135          0,                     /* src_mask */
2136          0,                     /* dst_mask */
2137          FALSE),                /* pcrel_offset */
2138
2139   /* GNU extension to record C++ vtable member usage.  */
2140   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
2141          0,                     /* rightshift */
2142          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2143          0,                     /* bitsize */
2144          FALSE,                 /* pc_relative */
2145          0,                     /* bitpos */
2146          complain_overflow_dont, /* complain_on_overflow */
2147          NULL,                  /* special_function */
2148          "R_PPC64_GNU_VTENTRY", /* name */
2149          FALSE,                 /* partial_inplace */
2150          0,                     /* src_mask */
2151          0,                     /* dst_mask */
2152          FALSE),                /* pcrel_offset */
2153 };
2154
2155 \f
2156 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2157    be done.  */
2158
2159 static void
2160 ppc_howto_init (void)
2161 {
2162   unsigned int i, type;
2163
2164   for (i = 0;
2165        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2166        i++)
2167     {
2168       type = ppc64_elf_howto_raw[i].type;
2169       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2170                           / sizeof (ppc64_elf_howto_table[0])));
2171       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2172     }
2173 }
2174
2175 static reloc_howto_type *
2176 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2177                              bfd_reloc_code_real_type code)
2178 {
2179   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2180
2181   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2182     /* Initialize howto table if needed.  */
2183     ppc_howto_init ();
2184
2185   switch (code)
2186     {
2187     default:
2188       return NULL;
2189
2190     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
2191       break;
2192     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
2193       break;
2194     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
2195       break;
2196     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
2197       break;
2198     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
2199       break;
2200     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
2201       break;
2202     case BFD_RELOC_PPC64_ADDR16_HIGH:           r = R_PPC64_ADDR16_HIGH;
2203       break;
2204     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
2205       break;
2206     case BFD_RELOC_PPC64_ADDR16_HIGHA:          r = R_PPC64_ADDR16_HIGHA;
2207       break;
2208     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
2209       break;
2210     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
2211       break;
2212     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
2213       break;
2214     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
2215       break;
2216     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
2217       break;
2218     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
2219       break;
2220     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
2221       break;
2222     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
2223       break;
2224     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
2225       break;
2226     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
2227       break;
2228     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
2229       break;
2230     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
2231       break;
2232     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
2233       break;
2234     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
2235       break;
2236     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
2237       break;
2238     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
2239       break;
2240     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
2241       break;
2242     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
2243       break;
2244     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
2245       break;
2246     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
2247       break;
2248     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
2249       break;
2250     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
2251       break;
2252     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
2253       break;
2254     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
2255       break;
2256     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
2257       break;
2258     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
2259       break;
2260     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
2261       break;
2262     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
2263       break;
2264     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
2265       break;
2266     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
2267       break;
2268     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
2269       break;
2270     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
2271       break;
2272     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
2273       break;
2274     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
2275       break;
2276     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
2277       break;
2278     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
2279       break;
2280     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
2281       break;
2282     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
2283       break;
2284     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
2285       break;
2286     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
2287       break;
2288     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
2289       break;
2290     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
2291       break;
2292     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
2293       break;
2294     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
2295       break;
2296     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
2297       break;
2298     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
2299       break;
2300     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
2301       break;
2302     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2303       break;
2304     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2305       break;
2306     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2307       break;
2308     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2309       break;
2310     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2311       break;
2312     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2313       break;
2314     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
2315       break;
2316     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
2317       break;
2318     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2319       break;
2320     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2321       break;
2322     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2323       break;
2324     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2325       break;
2326     case BFD_RELOC_PPC64_TPREL16_HIGH:          r = R_PPC64_TPREL16_HIGH;
2327       break;
2328     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2329       break;
2330     case BFD_RELOC_PPC64_TPREL16_HIGHA:         r = R_PPC64_TPREL16_HIGHA;
2331       break;
2332     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2333       break;
2334     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2335       break;
2336     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2337       break;
2338     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2339       break;
2340     case BFD_RELOC_PPC64_DTPREL16_HIGH:         r = R_PPC64_DTPREL16_HIGH;
2341       break;
2342     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2343       break;
2344     case BFD_RELOC_PPC64_DTPREL16_HIGHA:        r = R_PPC64_DTPREL16_HIGHA;
2345       break;
2346     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2347       break;
2348     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2349       break;
2350     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2351       break;
2352     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2353       break;
2354     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2355       break;
2356     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2357       break;
2358     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2359       break;
2360     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2361       break;
2362     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2363       break;
2364     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2365       break;
2366     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2367       break;
2368     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2369       break;
2370     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2371       break;
2372     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2373       break;
2374     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2375       break;
2376     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2377       break;
2378     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2379       break;
2380     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2381       break;
2382     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2383       break;
2384     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2385       break;
2386     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2387       break;
2388     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2389       break;
2390     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2391       break;
2392     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2393       break;
2394     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2395       break;
2396     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2397       break;
2398     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2399       break;
2400     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2401       break;
2402     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2403       break;
2404     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
2405       break;
2406     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
2407       break;
2408     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
2409       break;
2410     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
2411       break;
2412     case BFD_RELOC_PPC64_ADDR64_LOCAL:          r = R_PPC64_ADDR64_LOCAL;
2413       break;
2414     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2415       break;
2416     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2417       break;
2418     }
2419
2420   return ppc64_elf_howto_table[r];
2421 };
2422
2423 static reloc_howto_type *
2424 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2425                              const char *r_name)
2426 {
2427   unsigned int i;
2428
2429   for (i = 0;
2430        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2431        i++)
2432     if (ppc64_elf_howto_raw[i].name != NULL
2433         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2434       return &ppc64_elf_howto_raw[i];
2435
2436   return NULL;
2437 }
2438
2439 /* Set the howto pointer for a PowerPC ELF reloc.  */
2440
2441 static void
2442 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2443                          Elf_Internal_Rela *dst)
2444 {
2445   unsigned int type;
2446
2447   /* Initialize howto table if needed.  */
2448   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2449     ppc_howto_init ();
2450
2451   type = ELF64_R_TYPE (dst->r_info);
2452   if (type >= (sizeof (ppc64_elf_howto_table)
2453                / sizeof (ppc64_elf_howto_table[0])))
2454     {
2455       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2456                              abfd, (int) type);
2457       type = R_PPC64_NONE;
2458     }
2459   cache_ptr->howto = ppc64_elf_howto_table[type];
2460 }
2461
2462 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2463
2464 static bfd_reloc_status_type
2465 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2466                     void *data, asection *input_section,
2467                     bfd *output_bfd, char **error_message)
2468 {
2469   /* If this is a relocatable link (output_bfd test tells us), just
2470      call the generic function.  Any adjustment will be done at final
2471      link time.  */
2472   if (output_bfd != NULL)
2473     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2474                                   input_section, output_bfd, error_message);
2475
2476   /* Adjust the addend for sign extension of the low 16 bits.
2477      We won't actually be using the low 16 bits, so trashing them
2478      doesn't matter.  */
2479   reloc_entry->addend += 0x8000;
2480   return bfd_reloc_continue;
2481 }
2482
2483 static bfd_reloc_status_type
2484 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2485                         void *data, asection *input_section,
2486                         bfd *output_bfd, char **error_message)
2487 {
2488   if (output_bfd != NULL)
2489     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2490                                   input_section, output_bfd, error_message);
2491
2492   if (strcmp (symbol->section->name, ".opd") == 0
2493       && (symbol->section->owner->flags & DYNAMIC) == 0)
2494     {
2495       bfd_vma dest = opd_entry_value (symbol->section,
2496                                       symbol->value + reloc_entry->addend,
2497                                       NULL, NULL, FALSE);
2498       if (dest != (bfd_vma) -1)
2499         reloc_entry->addend = dest - (symbol->value
2500                                       + symbol->section->output_section->vma
2501                                       + symbol->section->output_offset);
2502     }
2503   else
2504     {
2505       elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2506
2507       if (symbol->section->owner != abfd
2508           && abiversion (symbol->section->owner) >= 2)
2509         {
2510           unsigned int i;
2511
2512           for (i = 0; i < symbol->section->owner->symcount; ++i)
2513             {
2514               asymbol *symdef = symbol->section->owner->outsymbols[i];
2515
2516               if (strcmp (symdef->name, symbol->name) == 0)
2517                 {
2518                   elfsym = (elf_symbol_type *) symdef;
2519                   break;
2520                 }
2521             }
2522         }
2523       reloc_entry->addend
2524         += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2525     }
2526   return bfd_reloc_continue;
2527 }
2528
2529 static bfd_reloc_status_type
2530 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2531                          void *data, asection *input_section,
2532                          bfd *output_bfd, char **error_message)
2533 {
2534   long insn;
2535   enum elf_ppc64_reloc_type r_type;
2536   bfd_size_type octets;
2537   /* Assume 'at' branch hints.  */
2538   bfd_boolean is_isa_v2 = TRUE;
2539
2540   /* If this is a relocatable link (output_bfd test tells us), just
2541      call the generic function.  Any adjustment will be done at final
2542      link time.  */
2543   if (output_bfd != NULL)
2544     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2545                                   input_section, output_bfd, error_message);
2546
2547   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2548   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2549   insn &= ~(0x01 << 21);
2550   r_type = reloc_entry->howto->type;
2551   if (r_type == R_PPC64_ADDR14_BRTAKEN
2552       || r_type == R_PPC64_REL14_BRTAKEN)
2553     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2554
2555   if (is_isa_v2)
2556     {
2557       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2558          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2559          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2560       if ((insn & (0x14 << 21)) == (0x04 << 21))
2561         insn |= 0x02 << 21;
2562       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2563         insn |= 0x08 << 21;
2564       else
2565         goto out;
2566     }
2567   else
2568     {
2569       bfd_vma target = 0;
2570       bfd_vma from;
2571
2572       if (!bfd_is_com_section (symbol->section))
2573         target = symbol->value;
2574       target += symbol->section->output_section->vma;
2575       target += symbol->section->output_offset;
2576       target += reloc_entry->addend;
2577
2578       from = (reloc_entry->address
2579               + input_section->output_offset
2580               + input_section->output_section->vma);
2581
2582       /* Invert 'y' bit if not the default.  */
2583       if ((bfd_signed_vma) (target - from) < 0)
2584         insn ^= 0x01 << 21;
2585     }
2586   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2587  out:
2588   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2589                                  input_section, output_bfd, error_message);
2590 }
2591
2592 static bfd_reloc_status_type
2593 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2594                          void *data, asection *input_section,
2595                          bfd *output_bfd, char **error_message)
2596 {
2597   /* If this is a relocatable link (output_bfd test tells us), just
2598      call the generic function.  Any adjustment will be done at final
2599      link time.  */
2600   if (output_bfd != NULL)
2601     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2602                                   input_section, output_bfd, error_message);
2603
2604   /* Subtract the symbol section base address.  */
2605   reloc_entry->addend -= symbol->section->output_section->vma;
2606   return bfd_reloc_continue;
2607 }
2608
2609 static bfd_reloc_status_type
2610 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2611                             void *data, asection *input_section,
2612                             bfd *output_bfd, char **error_message)
2613 {
2614   /* If this is a relocatable link (output_bfd test tells us), just
2615      call the generic function.  Any adjustment will be done at final
2616      link time.  */
2617   if (output_bfd != NULL)
2618     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2619                                   input_section, output_bfd, error_message);
2620
2621   /* Subtract the symbol section base address.  */
2622   reloc_entry->addend -= symbol->section->output_section->vma;
2623
2624   /* Adjust the addend for sign extension of the low 16 bits.  */
2625   reloc_entry->addend += 0x8000;
2626   return bfd_reloc_continue;
2627 }
2628
2629 static bfd_reloc_status_type
2630 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2631                      void *data, asection *input_section,
2632                      bfd *output_bfd, char **error_message)
2633 {
2634   bfd_vma TOCstart;
2635
2636   /* If this is a relocatable link (output_bfd test tells us), just
2637      call the generic function.  Any adjustment will be done at final
2638      link time.  */
2639   if (output_bfd != NULL)
2640     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2641                                   input_section, output_bfd, error_message);
2642
2643   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2644   if (TOCstart == 0)
2645     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2646
2647   /* Subtract the TOC base address.  */
2648   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2649   return bfd_reloc_continue;
2650 }
2651
2652 static bfd_reloc_status_type
2653 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2654                         void *data, asection *input_section,
2655                         bfd *output_bfd, char **error_message)
2656 {
2657   bfd_vma TOCstart;
2658
2659   /* If this is a relocatable link (output_bfd test tells us), just
2660      call the generic function.  Any adjustment will be done at final
2661      link time.  */
2662   if (output_bfd != NULL)
2663     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2664                                   input_section, output_bfd, error_message);
2665
2666   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2667   if (TOCstart == 0)
2668     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2669
2670   /* Subtract the TOC base address.  */
2671   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2672
2673   /* Adjust the addend for sign extension of the low 16 bits.  */
2674   reloc_entry->addend += 0x8000;
2675   return bfd_reloc_continue;
2676 }
2677
2678 static bfd_reloc_status_type
2679 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2680                        void *data, asection *input_section,
2681                        bfd *output_bfd, char **error_message)
2682 {
2683   bfd_vma TOCstart;
2684   bfd_size_type octets;
2685
2686   /* If this is a relocatable link (output_bfd test tells us), just
2687      call the generic function.  Any adjustment will be done at final
2688      link time.  */
2689   if (output_bfd != NULL)
2690     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2691                                   input_section, output_bfd, error_message);
2692
2693   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2694   if (TOCstart == 0)
2695     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2696
2697   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2698   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2699   return bfd_reloc_ok;
2700 }
2701
2702 static bfd_reloc_status_type
2703 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2704                            void *data, asection *input_section,
2705                            bfd *output_bfd, char **error_message)
2706 {
2707   /* If this is a relocatable link (output_bfd test tells us), just
2708      call the generic function.  Any adjustment will be done at final
2709      link time.  */
2710   if (output_bfd != NULL)
2711     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2712                                   input_section, output_bfd, error_message);
2713
2714   if (error_message != NULL)
2715     {
2716       static char buf[60];
2717       sprintf (buf, "generic linker can't handle %s",
2718                reloc_entry->howto->name);
2719       *error_message = buf;
2720     }
2721   return bfd_reloc_dangerous;
2722 }
2723
2724 /* Track GOT entries needed for a given symbol.  We might need more
2725    than one got entry per symbol.  */
2726 struct got_entry
2727 {
2728   struct got_entry *next;
2729
2730   /* The symbol addend that we'll be placing in the GOT.  */
2731   bfd_vma addend;
2732
2733   /* Unlike other ELF targets, we use separate GOT entries for the same
2734      symbol referenced from different input files.  This is to support
2735      automatic multiple TOC/GOT sections, where the TOC base can vary
2736      from one input file to another.  After partitioning into TOC groups
2737      we merge entries within the group.
2738
2739      Point to the BFD owning this GOT entry.  */
2740   bfd *owner;
2741
2742   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2743      TLS_TPREL or TLS_DTPREL for tls entries.  */
2744   unsigned char tls_type;
2745
2746   /* Non-zero if got.ent points to real entry.  */
2747   unsigned char is_indirect;
2748
2749   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2750   union
2751     {
2752       bfd_signed_vma refcount;
2753       bfd_vma offset;
2754       struct got_entry *ent;
2755     } got;
2756 };
2757
2758 /* The same for PLT.  */
2759 struct plt_entry
2760 {
2761   struct plt_entry *next;
2762
2763   bfd_vma addend;
2764
2765   union
2766     {
2767       bfd_signed_vma refcount;
2768       bfd_vma offset;
2769     } plt;
2770 };
2771
2772 struct ppc64_elf_obj_tdata
2773 {
2774   struct elf_obj_tdata elf;
2775
2776   /* Shortcuts to dynamic linker sections.  */
2777   asection *got;
2778   asection *relgot;
2779
2780   /* Used during garbage collection.  We attach global symbols defined
2781      on removed .opd entries to this section so that the sym is removed.  */
2782   asection *deleted_section;
2783
2784   /* TLS local dynamic got entry handling.  Support for multiple GOT
2785      sections means we potentially need one of these for each input bfd.  */
2786   struct got_entry tlsld_got;
2787
2788   union {
2789     /* A copy of relocs before they are modified for --emit-relocs.  */
2790     Elf_Internal_Rela *relocs;
2791
2792     /* Section contents.  */
2793     bfd_byte *contents;
2794   } opd;
2795
2796   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2797      the reloc to be in the range -32768 to 32767.  */
2798   unsigned int has_small_toc_reloc : 1;
2799
2800   /* Set if toc/got ha relocs detected not using r2, or lo reloc
2801      instruction not one we handle.  */
2802   unsigned int unexpected_toc_insn : 1;
2803 };
2804
2805 #define ppc64_elf_tdata(bfd) \
2806   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2807
2808 #define ppc64_tlsld_got(bfd) \
2809   (&ppc64_elf_tdata (bfd)->tlsld_got)
2810
2811 #define is_ppc64_elf(bfd) \
2812   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2813    && elf_object_id (bfd) == PPC64_ELF_DATA)
2814
2815 /* Override the generic function because we store some extras.  */
2816
2817 static bfd_boolean
2818 ppc64_elf_mkobject (bfd *abfd)
2819 {
2820   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2821                                   PPC64_ELF_DATA);
2822 }
2823
2824 /* Fix bad default arch selected for a 64 bit input bfd when the
2825    default is 32 bit.  */
2826
2827 static bfd_boolean
2828 ppc64_elf_object_p (bfd *abfd)
2829 {
2830   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2831     {
2832       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2833
2834       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2835         {
2836           /* Relies on arch after 32 bit default being 64 bit default.  */
2837           abfd->arch_info = abfd->arch_info->next;
2838           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2839         }
2840     }
2841   return TRUE;
2842 }
2843
2844 /* Support for core dump NOTE sections.  */
2845
2846 static bfd_boolean
2847 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2848 {
2849   size_t offset, size;
2850
2851   if (note->descsz != 504)
2852     return FALSE;
2853
2854   /* pr_cursig */
2855   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2856
2857   /* pr_pid */
2858   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2859
2860   /* pr_reg */
2861   offset = 112;
2862   size = 384;
2863
2864   /* Make a ".reg/999" section.  */
2865   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2866                                           size, note->descpos + offset);
2867 }
2868
2869 static bfd_boolean
2870 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2871 {
2872   if (note->descsz != 136)
2873     return FALSE;
2874
2875   elf_tdata (abfd)->core->pid
2876     = bfd_get_32 (abfd, note->descdata + 24);
2877   elf_tdata (abfd)->core->program
2878     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2879   elf_tdata (abfd)->core->command
2880     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2881
2882   return TRUE;
2883 }
2884
2885 static char *
2886 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2887                            ...)
2888 {
2889   switch (note_type)
2890     {
2891     default:
2892       return NULL;
2893
2894     case NT_PRPSINFO:
2895       {
2896         char data[136];
2897         va_list ap;
2898
2899         va_start (ap, note_type);
2900         memset (data, 0, sizeof (data));
2901         strncpy (data + 40, va_arg (ap, const char *), 16);
2902         strncpy (data + 56, va_arg (ap, const char *), 80);
2903         va_end (ap);
2904         return elfcore_write_note (abfd, buf, bufsiz,
2905                                    "CORE", note_type, data, sizeof (data));
2906       }
2907
2908     case NT_PRSTATUS:
2909       {
2910         char data[504];
2911         va_list ap;
2912         long pid;
2913         int cursig;
2914         const void *greg;
2915
2916         va_start (ap, note_type);
2917         memset (data, 0, 112);
2918         pid = va_arg (ap, long);
2919         bfd_put_32 (abfd, pid, data + 32);
2920         cursig = va_arg (ap, int);
2921         bfd_put_16 (abfd, cursig, data + 12);
2922         greg = va_arg (ap, const void *);
2923         memcpy (data + 112, greg, 384);
2924         memset (data + 496, 0, 8);
2925         va_end (ap);
2926         return elfcore_write_note (abfd, buf, bufsiz,
2927                                    "CORE", note_type, data, sizeof (data));
2928       }
2929     }
2930 }
2931
2932 /* Add extra PPC sections.  */
2933
2934 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2935 {
2936   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
2937   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2938   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2939   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2940   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2941   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2942   { NULL,                     0,  0, 0,            0 }
2943 };
2944
2945 enum _ppc64_sec_type {
2946   sec_normal = 0,
2947   sec_opd = 1,
2948   sec_toc = 2
2949 };
2950
2951 struct _ppc64_elf_section_data
2952 {
2953   struct bfd_elf_section_data elf;
2954
2955   union
2956   {
2957     /* An array with one entry for each opd function descriptor,
2958        and some spares since opd entries may be either 16 or 24 bytes.  */
2959 #define OPD_NDX(OFF) ((OFF) >> 4)
2960     struct _opd_sec_data
2961     {
2962       /* Points to the function code section for local opd entries.  */
2963       asection **func_sec;
2964
2965       /* After editing .opd, adjust references to opd local syms.  */
2966       long *adjust;
2967     } opd;
2968
2969     /* An array for toc sections, indexed by offset/8.  */
2970     struct _toc_sec_data
2971     {
2972       /* Specifies the relocation symbol index used at a given toc offset.  */
2973       unsigned *symndx;
2974
2975       /* And the relocation addend.  */
2976       bfd_vma *add;
2977     } toc;
2978   } u;
2979
2980   enum _ppc64_sec_type sec_type:2;
2981
2982   /* Flag set when small branches are detected.  Used to
2983      select suitable defaults for the stub group size.  */
2984   unsigned int has_14bit_branch:1;
2985 };
2986
2987 #define ppc64_elf_section_data(sec) \
2988   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2989
2990 static bfd_boolean
2991 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2992 {
2993   if (!sec->used_by_bfd)
2994     {
2995       struct _ppc64_elf_section_data *sdata;
2996       bfd_size_type amt = sizeof (*sdata);
2997
2998       sdata = bfd_zalloc (abfd, amt);
2999       if (sdata == NULL)
3000         return FALSE;
3001       sec->used_by_bfd = sdata;
3002     }
3003
3004   return _bfd_elf_new_section_hook (abfd, sec);
3005 }
3006
3007 static struct _opd_sec_data *
3008 get_opd_info (asection * sec)
3009 {
3010   if (sec != NULL
3011       && ppc64_elf_section_data (sec) != NULL
3012       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3013     return &ppc64_elf_section_data (sec)->u.opd;
3014   return NULL;
3015 }
3016 \f
3017 /* Parameters for the qsort hook.  */
3018 static bfd_boolean synthetic_relocatable;
3019
3020 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
3021
3022 static int
3023 compare_symbols (const void *ap, const void *bp)
3024 {
3025   const asymbol *a = * (const asymbol **) ap;
3026   const asymbol *b = * (const asymbol **) bp;
3027
3028   /* Section symbols first.  */
3029   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3030     return -1;
3031   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3032     return 1;
3033
3034   /* then .opd symbols.  */
3035   if (strcmp (a->section->name, ".opd") == 0
3036       && strcmp (b->section->name, ".opd") != 0)
3037     return -1;
3038   if (strcmp (a->section->name, ".opd") != 0
3039       && strcmp (b->section->name, ".opd") == 0)
3040     return 1;
3041
3042   /* then other code symbols.  */
3043   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3044       == (SEC_CODE | SEC_ALLOC)
3045       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3046          != (SEC_CODE | SEC_ALLOC))
3047     return -1;
3048
3049   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3050       != (SEC_CODE | SEC_ALLOC)
3051       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3052          == (SEC_CODE | SEC_ALLOC))
3053     return 1;
3054
3055   if (synthetic_relocatable)
3056     {
3057       if (a->section->id < b->section->id)
3058         return -1;
3059
3060       if (a->section->id > b->section->id)
3061         return 1;
3062     }
3063
3064   if (a->value + a->section->vma < b->value + b->section->vma)
3065     return -1;
3066
3067   if (a->value + a->section->vma > b->value + b->section->vma)
3068     return 1;
3069
3070   /* For syms with the same value, prefer strong dynamic global function
3071      syms over other syms.  */
3072   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3073     return -1;
3074
3075   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3076     return 1;
3077
3078   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3079     return -1;
3080
3081   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3082     return 1;
3083
3084   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3085     return -1;
3086
3087   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3088     return 1;
3089
3090   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3091     return -1;
3092
3093   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3094     return 1;
3095
3096   return 0;
3097 }
3098
3099 /* Search SYMS for a symbol of the given VALUE.  */
3100
3101 static asymbol *
3102 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
3103 {
3104   long mid;
3105
3106   if (id == -1)
3107     {
3108       while (lo < hi)
3109         {
3110           mid = (lo + hi) >> 1;
3111           if (syms[mid]->value + syms[mid]->section->vma < value)
3112             lo = mid + 1;
3113           else if (syms[mid]->value + syms[mid]->section->vma > value)
3114             hi = mid;
3115           else
3116             return syms[mid];
3117         }
3118     }
3119   else
3120     {
3121       while (lo < hi)
3122         {
3123           mid = (lo + hi) >> 1;
3124           if (syms[mid]->section->id < id)
3125             lo = mid + 1;
3126           else if (syms[mid]->section->id > id)
3127             hi = mid;
3128           else if (syms[mid]->value < value)
3129             lo = mid + 1;
3130           else if (syms[mid]->value > value)
3131             hi = mid;
3132           else
3133             return syms[mid];
3134         }
3135     }
3136   return NULL;
3137 }
3138
3139 static bfd_boolean
3140 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3141 {
3142   bfd_vma vma = *(bfd_vma *) ptr;
3143   return ((section->flags & SEC_ALLOC) != 0
3144           && section->vma <= vma
3145           && vma < section->vma + section->size);
3146 }
3147
3148 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3149    entry syms.  Also generate @plt symbols for the glink branch table.  */
3150
3151 static long
3152 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3153                                 long static_count, asymbol **static_syms,
3154                                 long dyn_count, asymbol **dyn_syms,
3155                                 asymbol **ret)
3156 {
3157   asymbol *s;
3158   long i;
3159   long count;
3160   char *names;
3161   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3162   asection *opd = NULL;
3163   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3164   asymbol **syms;
3165   int abi = abiversion (abfd);
3166
3167   *ret = NULL;
3168
3169   if (abi < 2)
3170     {
3171       opd = bfd_get_section_by_name (abfd, ".opd");
3172       if (opd == NULL && abi == 1)
3173         return 0;
3174     }
3175
3176   symcount = static_count;
3177   if (!relocatable)
3178     symcount += dyn_count;
3179   if (symcount == 0)
3180     return 0;
3181
3182   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3183   if (syms == NULL)
3184     return -1;
3185
3186   if (!relocatable && static_count != 0 && dyn_count != 0)
3187     {
3188       /* Use both symbol tables.  */
3189       memcpy (syms, static_syms, static_count * sizeof (*syms));
3190       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3191     }
3192   else if (!relocatable && static_count == 0)
3193     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3194   else
3195     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3196
3197   synthetic_relocatable = relocatable;
3198   qsort (syms, symcount, sizeof (*syms), compare_symbols);
3199
3200   if (!relocatable && symcount > 1)
3201     {
3202       long j;
3203       /* Trim duplicate syms, since we may have merged the normal and
3204          dynamic symbols.  Actually, we only care about syms that have
3205          different values, so trim any with the same value.  */
3206       for (i = 1, j = 1; i < symcount; ++i)
3207         if (syms[i - 1]->value + syms[i - 1]->section->vma
3208             != syms[i]->value + syms[i]->section->vma)
3209           syms[j++] = syms[i];
3210       symcount = j;
3211     }
3212
3213   i = 0;
3214   if (strcmp (syms[i]->section->name, ".opd") == 0)
3215     ++i;
3216   codesecsym = i;
3217
3218   for (; i < symcount; ++i)
3219     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3220          != (SEC_CODE | SEC_ALLOC))
3221         || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3222       break;
3223   codesecsymend = i;
3224
3225   for (; i < symcount; ++i)
3226     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3227       break;
3228   secsymend = i;
3229
3230   for (; i < symcount; ++i)
3231     if (strcmp (syms[i]->section->name, ".opd") != 0)
3232       break;
3233   opdsymend = i;
3234
3235   for (; i < symcount; ++i)
3236     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3237         != (SEC_CODE | SEC_ALLOC))
3238       break;
3239   symcount = i;
3240
3241   count = 0;
3242
3243   if (relocatable)
3244     {
3245       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3246       arelent *r;
3247       size_t size;
3248       long relcount;
3249
3250       if (opdsymend == secsymend)
3251         goto done;
3252
3253       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3254       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3255       if (relcount == 0)
3256         goto done;
3257
3258       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3259         {
3260           count = -1;
3261           goto done;
3262         }
3263
3264       size = 0;
3265       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3266         {
3267           asymbol *sym;
3268
3269           while (r < opd->relocation + relcount
3270                  && r->address < syms[i]->value + opd->vma)
3271             ++r;
3272
3273           if (r == opd->relocation + relcount)
3274             break;
3275
3276           if (r->address != syms[i]->value + opd->vma)
3277             continue;
3278
3279           if (r->howto->type != R_PPC64_ADDR64)
3280             continue;
3281
3282           sym = *r->sym_ptr_ptr;
3283           if (!sym_exists_at (syms, opdsymend, symcount,
3284                               sym->section->id, sym->value + r->addend))
3285             {
3286               ++count;
3287               size += sizeof (asymbol);
3288               size += strlen (syms[i]->name) + 2;
3289             }
3290         }
3291
3292       s = *ret = bfd_malloc (size);
3293       if (s == NULL)
3294         {
3295           count = -1;
3296           goto done;
3297         }
3298
3299       names = (char *) (s + count);
3300
3301       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3302         {
3303           asymbol *sym;
3304
3305           while (r < opd->relocation + relcount
3306                  && r->address < syms[i]->value + opd->vma)
3307             ++r;
3308
3309           if (r == opd->relocation + relcount)
3310             break;
3311
3312           if (r->address != syms[i]->value + opd->vma)
3313             continue;
3314
3315           if (r->howto->type != R_PPC64_ADDR64)
3316             continue;
3317
3318           sym = *r->sym_ptr_ptr;
3319           if (!sym_exists_at (syms, opdsymend, symcount,
3320                               sym->section->id, sym->value + r->addend))
3321             {
3322               size_t len;
3323
3324               *s = *syms[i];
3325               s->flags |= BSF_SYNTHETIC;
3326               s->section = sym->section;
3327               s->value = sym->value + r->addend;
3328               s->name = names;
3329               *names++ = '.';
3330               len = strlen (syms[i]->name);
3331               memcpy (names, syms[i]->name, len + 1);
3332               names += len + 1;
3333               /* Have udata.p point back to the original symbol this
3334                  synthetic symbol was derived from.  */
3335               s->udata.p = syms[i];
3336               s++;
3337             }
3338         }
3339     }
3340   else
3341     {
3342       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3343       bfd_byte *contents = NULL;
3344       size_t size;
3345       long plt_count = 0;
3346       bfd_vma glink_vma = 0, resolv_vma = 0;
3347       asection *dynamic, *glink = NULL, *relplt = NULL;
3348       arelent *p;
3349
3350       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3351         {
3352         free_contents_and_exit:
3353           if (contents)
3354             free (contents);
3355           count = -1;
3356           goto done;
3357         }
3358
3359       size = 0;
3360       for (i = secsymend; i < opdsymend; ++i)
3361         {
3362           bfd_vma ent;
3363
3364           /* Ignore bogus symbols.  */
3365           if (syms[i]->value > opd->size - 8)
3366             continue;
3367
3368           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3369           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3370             {
3371               ++count;
3372               size += sizeof (asymbol);
3373               size += strlen (syms[i]->name) + 2;
3374             }
3375         }
3376
3377       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
3378       if (dyn_count != 0
3379           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3380         {
3381           bfd_byte *dynbuf, *extdyn, *extdynend;
3382           size_t extdynsize;
3383           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3384
3385           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3386             goto free_contents_and_exit;
3387
3388           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3389           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3390
3391           extdyn = dynbuf;
3392           extdynend = extdyn + dynamic->size;
3393           for (; extdyn < extdynend; extdyn += extdynsize)
3394             {
3395               Elf_Internal_Dyn dyn;
3396               (*swap_dyn_in) (abfd, extdyn, &dyn);
3397
3398               if (dyn.d_tag == DT_NULL)
3399                 break;
3400
3401               if (dyn.d_tag == DT_PPC64_GLINK)
3402                 {
3403                   /* The first glink stub starts at offset 32; see
3404                      comment in ppc64_elf_finish_dynamic_sections. */
3405                   glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
3406                   /* The .glink section usually does not survive the final
3407                      link; search for the section (usually .text) where the
3408                      glink stubs now reside.  */
3409                   glink = bfd_sections_find_if (abfd, section_covers_vma,
3410                                                 &glink_vma);
3411                   break;
3412                 }
3413             }
3414
3415           free (dynbuf);
3416         }
3417
3418       if (glink != NULL)
3419         {
3420           /* Determine __glink trampoline by reading the relative branch
3421              from the first glink stub.  */
3422           bfd_byte buf[4];
3423           unsigned int off = 0;
3424
3425           while (bfd_get_section_contents (abfd, glink, buf,
3426                                            glink_vma + off - glink->vma, 4))
3427             {
3428               unsigned int insn = bfd_get_32 (abfd, buf);
3429               insn ^= B_DOT;
3430               if ((insn & ~0x3fffffc) == 0)
3431                 {
3432                   resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3433                   break;
3434                 }
3435               off += 4;
3436               if (off > 4)
3437                 break;
3438             }
3439
3440           if (resolv_vma)
3441             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3442
3443           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3444           if (relplt != NULL)
3445             {
3446               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3447               if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3448                 goto free_contents_and_exit;
3449
3450               plt_count = relplt->size / sizeof (Elf64_External_Rela);
3451               size += plt_count * sizeof (asymbol);
3452
3453               p = relplt->relocation;
3454               for (i = 0; i < plt_count; i++, p++)
3455                 {
3456                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3457                   if (p->addend != 0)
3458                     size += sizeof ("+0x") - 1 + 16;
3459                 }
3460             }
3461         }
3462
3463       s = *ret = bfd_malloc (size);
3464       if (s == NULL)
3465         goto free_contents_and_exit;
3466
3467       names = (char *) (s + count + plt_count + (resolv_vma != 0));
3468
3469       for (i = secsymend; i < opdsymend; ++i)
3470         {
3471           bfd_vma ent;
3472
3473           if (syms[i]->value > opd->size - 8)
3474             continue;
3475
3476           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3477           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3478             {
3479               long lo, hi;
3480               size_t len;
3481               asection *sec = abfd->sections;
3482
3483               *s = *syms[i];
3484               lo = codesecsym;
3485               hi = codesecsymend;
3486               while (lo < hi)
3487                 {
3488                   long mid = (lo + hi) >> 1;
3489                   if (syms[mid]->section->vma < ent)
3490                     lo = mid + 1;
3491                   else if (syms[mid]->section->vma > ent)
3492                     hi = mid;
3493                   else
3494                     {
3495                       sec = syms[mid]->section;
3496                       break;
3497                     }
3498                 }
3499
3500               if (lo >= hi && lo > codesecsym)
3501                 sec = syms[lo - 1]->section;
3502
3503               for (; sec != NULL; sec = sec->next)
3504                 {
3505                   if (sec->vma > ent)
3506                     break;
3507                   /* SEC_LOAD may not be set if SEC is from a separate debug
3508                      info file.  */
3509                   if ((sec->flags & SEC_ALLOC) == 0)
3510                     break;
3511                   if ((sec->flags & SEC_CODE) != 0)
3512                     s->section = sec;
3513                 }
3514               s->flags |= BSF_SYNTHETIC;
3515               s->value = ent - s->section->vma;
3516               s->name = names;
3517               *names++ = '.';
3518               len = strlen (syms[i]->name);
3519               memcpy (names, syms[i]->name, len + 1);
3520               names += len + 1;
3521               /* Have udata.p point back to the original symbol this
3522                  synthetic symbol was derived from.  */
3523               s->udata.p = syms[i];
3524               s++;
3525             }
3526         }
3527       free (contents);
3528
3529       if (glink != NULL && relplt != NULL)
3530         {
3531           if (resolv_vma)
3532             {
3533               /* Add a symbol for the main glink trampoline.  */
3534               memset (s, 0, sizeof *s);
3535               s->the_bfd = abfd;
3536               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3537               s->section = glink;
3538               s->value = resolv_vma - glink->vma;
3539               s->name = names;
3540               memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3541               names += sizeof ("__glink_PLTresolve");
3542               s++;
3543               count++;
3544             }
3545
3546           /* FIXME: It would be very much nicer to put sym@plt on the
3547              stub rather than on the glink branch table entry.  The
3548              objdump disassembler would then use a sensible symbol
3549              name on plt calls.  The difficulty in doing so is
3550              a) finding the stubs, and,
3551              b) matching stubs against plt entries, and,
3552              c) there can be multiple stubs for a given plt entry.
3553
3554              Solving (a) could be done by code scanning, but older
3555              ppc64 binaries used different stubs to current code.
3556              (b) is the tricky one since you need to known the toc
3557              pointer for at least one function that uses a pic stub to
3558              be able to calculate the plt address referenced.
3559              (c) means gdb would need to set multiple breakpoints (or
3560              find the glink branch itself) when setting breakpoints
3561              for pending shared library loads.  */
3562           p = relplt->relocation;
3563           for (i = 0; i < plt_count; i++, p++)
3564             {
3565               size_t len;
3566
3567               *s = **p->sym_ptr_ptr;
3568               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
3569                  we are defining a symbol, ensure one of them is set.  */
3570               if ((s->flags & BSF_LOCAL) == 0)
3571                 s->flags |= BSF_GLOBAL;
3572               s->flags |= BSF_SYNTHETIC;
3573               s->section = glink;
3574               s->value = glink_vma - glink->vma;
3575               s->name = names;
3576               s->udata.p = NULL;
3577               len = strlen ((*p->sym_ptr_ptr)->name);
3578               memcpy (names, (*p->sym_ptr_ptr)->name, len);
3579               names += len;
3580               if (p->addend != 0)
3581                 {
3582                   memcpy (names, "+0x", sizeof ("+0x") - 1);
3583                   names += sizeof ("+0x") - 1;
3584                   bfd_sprintf_vma (abfd, names, p->addend);
3585                   names += strlen (names);
3586                 }
3587               memcpy (names, "@plt", sizeof ("@plt"));
3588               names += sizeof ("@plt");
3589               s++;
3590               if (abi < 2)
3591                 {
3592                   glink_vma += 8;
3593                   if (i >= 0x8000)
3594                     glink_vma += 4;
3595                 }
3596               else
3597                 glink_vma += 4;
3598             }
3599           count += plt_count;
3600         }
3601     }
3602
3603  done:
3604   free (syms);
3605   return count;
3606 }
3607 \f
3608 /* The following functions are specific to the ELF linker, while
3609    functions above are used generally.  Those named ppc64_elf_* are
3610    called by the main ELF linker code.  They appear in this file more
3611    or less in the order in which they are called.  eg.
3612    ppc64_elf_check_relocs is called early in the link process,
3613    ppc64_elf_finish_dynamic_sections is one of the last functions
3614    called.
3615
3616    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3617    functions have both a function code symbol and a function descriptor
3618    symbol.  A call to foo in a relocatable object file looks like:
3619
3620    .            .text
3621    .    x:
3622    .            bl      .foo
3623    .            nop
3624
3625    The function definition in another object file might be:
3626
3627    .            .section .opd
3628    .    foo:    .quad   .foo
3629    .            .quad   .TOC.@tocbase
3630    .            .quad   0
3631    .
3632    .            .text
3633    .    .foo:   blr
3634
3635    When the linker resolves the call during a static link, the branch
3636    unsurprisingly just goes to .foo and the .opd information is unused.
3637    If the function definition is in a shared library, things are a little
3638    different:  The call goes via a plt call stub, the opd information gets
3639    copied to the plt, and the linker patches the nop.
3640
3641    .    x:
3642    .            bl      .foo_stub
3643    .            ld      2,40(1)
3644    .
3645    .
3646    .    .foo_stub:
3647    .            std     2,40(1)                 # in practice, the call stub
3648    .            addis   11,2,Lfoo@toc@ha        # is slightly optimized, but
3649    .            addi    11,11,Lfoo@toc@l        # this is the general idea
3650    .            ld      12,0(11)
3651    .            ld      2,8(11)
3652    .            mtctr   12
3653    .            ld      11,16(11)
3654    .            bctr
3655    .
3656    .            .section .plt
3657    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
3658
3659    The "reloc ()" notation is supposed to indicate that the linker emits
3660    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3661    copying.
3662
3663    What are the difficulties here?  Well, firstly, the relocations
3664    examined by the linker in check_relocs are against the function code
3665    sym .foo, while the dynamic relocation in the plt is emitted against
3666    the function descriptor symbol, foo.  Somewhere along the line, we need
3667    to carefully copy dynamic link information from one symbol to the other.
3668    Secondly, the generic part of the elf linker will make .foo a dynamic
3669    symbol as is normal for most other backends.  We need foo dynamic
3670    instead, at least for an application final link.  However, when
3671    creating a shared library containing foo, we need to have both symbols
3672    dynamic so that references to .foo are satisfied during the early
3673    stages of linking.  Otherwise the linker might decide to pull in a
3674    definition from some other object, eg. a static library.
3675
3676    Update: As of August 2004, we support a new convention.  Function
3677    calls may use the function descriptor symbol, ie. "bl foo".  This
3678    behaves exactly as "bl .foo".  */
3679
3680 /* Of those relocs that might be copied as dynamic relocs, this function
3681    selects those that must be copied when linking a shared library,
3682    even when the symbol is local.  */
3683
3684 static int
3685 must_be_dyn_reloc (struct bfd_link_info *info,
3686                    enum elf_ppc64_reloc_type r_type)
3687 {
3688   switch (r_type)
3689     {
3690     default:
3691       return 1;
3692
3693     case R_PPC64_REL32:
3694     case R_PPC64_REL64:
3695     case R_PPC64_REL30:
3696       return 0;
3697
3698     case R_PPC64_TPREL16:
3699     case R_PPC64_TPREL16_LO:
3700     case R_PPC64_TPREL16_HI:
3701     case R_PPC64_TPREL16_HA:
3702     case R_PPC64_TPREL16_DS:
3703     case R_PPC64_TPREL16_LO_DS:
3704     case R_PPC64_TPREL16_HIGH:
3705     case R_PPC64_TPREL16_HIGHA:
3706     case R_PPC64_TPREL16_HIGHER:
3707     case R_PPC64_TPREL16_HIGHERA:
3708     case R_PPC64_TPREL16_HIGHEST:
3709     case R_PPC64_TPREL16_HIGHESTA:
3710     case R_PPC64_TPREL64:
3711       return !info->executable;
3712     }
3713 }
3714
3715 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3716    copying dynamic variables from a shared lib into an app's dynbss
3717    section, and instead use a dynamic relocation to point into the
3718    shared lib.  With code that gcc generates, it's vital that this be
3719    enabled;  In the PowerPC64 ABI, the address of a function is actually
3720    the address of a function descriptor, which resides in the .opd
3721    section.  gcc uses the descriptor directly rather than going via the
3722    GOT as some other ABI's do, which means that initialized function
3723    pointers must reference the descriptor.  Thus, a function pointer
3724    initialized to the address of a function in a shared library will
3725    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3726    redefines the function descriptor symbol to point to the copy.  This
3727    presents a problem as a plt entry for that function is also
3728    initialized from the function descriptor symbol and the copy reloc
3729    may not be initialized first.  */
3730 #define ELIMINATE_COPY_RELOCS 1
3731
3732 /* Section name for stubs is the associated section name plus this
3733    string.  */
3734 #define STUB_SUFFIX ".stub"
3735
3736 /* Linker stubs.
3737    ppc_stub_long_branch:
3738    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3739    destination, but a 24 bit branch in a stub section will reach.
3740    .    b       dest
3741
3742    ppc_stub_plt_branch:
3743    Similar to the above, but a 24 bit branch in the stub section won't
3744    reach its destination.
3745    .    addis   %r11,%r2,xxx@toc@ha
3746    .    ld      %r12,xxx@toc@l(%r11)
3747    .    mtctr   %r12
3748    .    bctr
3749
3750    ppc_stub_plt_call:
3751    Used to call a function in a shared library.  If it so happens that
3752    the plt entry referenced crosses a 64k boundary, then an extra
3753    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3754    .    std     %r2,40(%r1)
3755    .    addis   %r11,%r2,xxx@toc@ha
3756    .    ld      %r12,xxx+0@toc@l(%r11)
3757    .    mtctr   %r12
3758    .    ld      %r2,xxx+8@toc@l(%r11)
3759    .    ld      %r11,xxx+16@toc@l(%r11)
3760    .    bctr
3761
3762    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3763    code to adjust the value and save r2 to support multiple toc sections.
3764    A ppc_stub_long_branch with an r2 offset looks like:
3765    .    std     %r2,40(%r1)
3766    .    addis   %r2,%r2,off@ha
3767    .    addi    %r2,%r2,off@l
3768    .    b       dest
3769
3770    A ppc_stub_plt_branch with an r2 offset looks like:
3771    .    std     %r2,40(%r1)
3772    .    addis   %r11,%r2,xxx@toc@ha
3773    .    ld      %r12,xxx@toc@l(%r11)
3774    .    addis   %r2,%r2,off@ha
3775    .    addi    %r2,%r2,off@l
3776    .    mtctr   %r12
3777    .    bctr
3778
3779    In cases where the "addis" instruction would add zero, the "addis" is
3780    omitted and following instructions modified slightly in some cases.
3781 */
3782
3783 enum ppc_stub_type {
3784   ppc_stub_none,
3785   ppc_stub_long_branch,
3786   ppc_stub_long_branch_r2off,
3787   ppc_stub_plt_branch,
3788   ppc_stub_plt_branch_r2off,
3789   ppc_stub_plt_call,
3790   ppc_stub_plt_call_r2save,
3791   ppc_stub_global_entry
3792 };
3793
3794 struct ppc_stub_hash_entry {
3795
3796   /* Base hash table entry structure.  */
3797   struct bfd_hash_entry root;
3798
3799   enum ppc_stub_type stub_type;
3800
3801   /* The stub section.  */
3802   asection *stub_sec;
3803
3804   /* Offset within stub_sec of the beginning of this stub.  */
3805   bfd_vma stub_offset;
3806
3807   /* Given the symbol's value and its section we can determine its final
3808      value when building the stubs (so the stub knows where to jump.  */
3809   bfd_vma target_value;
3810   asection *target_section;
3811
3812   /* The symbol table entry, if any, that this was derived from.  */
3813   struct ppc_link_hash_entry *h;
3814   struct plt_entry *plt_ent;
3815
3816   /* Where this stub is being called from, or, in the case of combined
3817      stub sections, the first input section in the group.  */
3818   asection *id_sec;
3819
3820   /* Symbol st_other.  */
3821   unsigned char other;
3822 };
3823
3824 struct ppc_branch_hash_entry {
3825
3826   /* Base hash table entry structure.  */
3827   struct bfd_hash_entry root;
3828
3829   /* Offset within branch lookup table.  */
3830   unsigned int offset;
3831
3832   /* Generation marker.  */
3833   unsigned int iter;
3834 };
3835
3836 /* Used to track dynamic relocations for local symbols.  */
3837 struct ppc_dyn_relocs
3838 {
3839   struct ppc_dyn_relocs *next;
3840
3841   /* The input section of the reloc.  */
3842   asection *sec;
3843
3844   /* Total number of relocs copied for the input section.  */
3845   unsigned int count : 31;
3846
3847   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
3848   unsigned int ifunc : 1;
3849 };
3850
3851 struct ppc_link_hash_entry
3852 {
3853   struct elf_link_hash_entry elf;
3854
3855   union {
3856     /* A pointer to the most recently used stub hash entry against this
3857        symbol.  */
3858     struct ppc_stub_hash_entry *stub_cache;
3859
3860     /* A pointer to the next symbol starting with a '.'  */
3861     struct ppc_link_hash_entry *next_dot_sym;
3862   } u;
3863
3864   /* Track dynamic relocs copied for this symbol.  */
3865   struct elf_dyn_relocs *dyn_relocs;
3866
3867   /* Link between function code and descriptor symbols.  */
3868   struct ppc_link_hash_entry *oh;
3869
3870   /* Flag function code and descriptor symbols.  */
3871   unsigned int is_func:1;
3872   unsigned int is_func_descriptor:1;
3873   unsigned int fake:1;
3874
3875   /* Whether global opd/toc sym has been adjusted or not.
3876      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3877      should be set for all globals defined in any opd/toc section.  */
3878   unsigned int adjust_done:1;
3879
3880   /* Set if we twiddled this symbol to weak at some stage.  */
3881   unsigned int was_undefined:1;
3882
3883   /* Contexts in which symbol is used in the GOT (or TOC).
3884      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3885      corresponding relocs are encountered during check_relocs.
3886      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3887      indicate the corresponding GOT entry type is not needed.
3888      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3889      a TPREL one.  We use a separate flag rather than setting TPREL
3890      just for convenience in distinguishing the two cases.  */
3891 #define TLS_GD           1      /* GD reloc. */
3892 #define TLS_LD           2      /* LD reloc. */
3893 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
3894 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
3895 #define TLS_TLS         16      /* Any TLS reloc.  */
3896 #define TLS_EXPLICIT    32      /* Marks TOC section TLS relocs. */
3897 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
3898 #define PLT_IFUNC      128      /* STT_GNU_IFUNC.  */
3899   unsigned char tls_mask;
3900 };
3901
3902 /* ppc64 ELF linker hash table.  */
3903
3904 struct ppc_link_hash_table
3905 {
3906   struct elf_link_hash_table elf;
3907
3908   /* The stub hash table.  */
3909   struct bfd_hash_table stub_hash_table;
3910
3911   /* Another hash table for plt_branch stubs.  */
3912   struct bfd_hash_table branch_hash_table;
3913
3914   /* Hash table for function prologue tocsave.  */
3915   htab_t tocsave_htab;
3916
3917   /* Various options and other info passed from the linker.  */
3918   struct ppc64_elf_params *params;
3919
3920   /* Array to keep track of which stub sections have been created, and
3921      information on stub grouping.  */
3922   struct map_stub {
3923     /* This is the section to which stubs in the group will be attached.  */
3924     asection *link_sec;
3925     /* The stub section.  */
3926     asection *stub_sec;
3927     /* Along with elf_gp, specifies the TOC pointer used in this group.  */
3928     bfd_vma toc_off;
3929   } *stub_group;
3930
3931   /* Temp used when calculating TOC pointers.  */
3932   bfd_vma toc_curr;
3933   bfd *toc_bfd;
3934   asection *toc_first_sec;
3935
3936   /* Highest input section id.  */
3937   int top_id;
3938
3939   /* Highest output section index.  */
3940   int top_index;
3941
3942   /* Used when adding symbols.  */
3943   struct ppc_link_hash_entry *dot_syms;
3944
3945   /* List of input sections for each output section.  */
3946   asection **input_list;
3947
3948   /* Shortcuts to get to dynamic linker sections.  */
3949   asection *dynbss;
3950   asection *relbss;
3951   asection *glink;
3952   asection *sfpr;
3953   asection *brlt;
3954   asection *relbrlt;
3955   asection *glink_eh_frame;
3956
3957   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
3958   struct ppc_link_hash_entry *tls_get_addr;
3959   struct ppc_link_hash_entry *tls_get_addr_fd;
3960
3961   /* The size of reliplt used by got entry relocs.  */
3962   bfd_size_type got_reli_size;
3963
3964   /* Statistics.  */
3965   unsigned long stub_count[ppc_stub_global_entry];
3966
3967   /* Number of stubs against global syms.  */
3968   unsigned long stub_globals;
3969
3970   /* Set if we're linking code with function descriptors.  */
3971   unsigned int opd_abi:1;
3972
3973   /* Support for multiple toc sections.  */
3974   unsigned int do_multi_toc:1;
3975   unsigned int multi_toc_needed:1;
3976   unsigned int second_toc_pass:1;
3977   unsigned int do_toc_opt:1;
3978
3979   /* Set on error.  */
3980   unsigned int stub_error:1;
3981
3982   /* Temp used by ppc64_elf_before_check_relocs.  */
3983   unsigned int twiddled_syms:1;
3984
3985   /* Incremented every time we size stubs.  */
3986   unsigned int stub_iteration;
3987
3988   /* Small local sym cache.  */
3989   struct sym_cache sym_cache;
3990 };
3991
3992 /* Rename some of the generic section flags to better document how they
3993    are used here.  */
3994
3995 /* Nonzero if this section has TLS related relocations.  */
3996 #define has_tls_reloc sec_flg0
3997
3998 /* Nonzero if this section has a call to __tls_get_addr.  */
3999 #define has_tls_get_addr_call sec_flg1
4000
4001 /* Nonzero if this section has any toc or got relocs.  */
4002 #define has_toc_reloc sec_flg2
4003
4004 /* Nonzero if this section has a call to another section that uses
4005    the toc or got.  */
4006 #define makes_toc_func_call sec_flg3
4007
4008 /* Recursion protection when determining above flag.  */
4009 #define call_check_in_progress sec_flg4
4010 #define call_check_done sec_flg5
4011
4012 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
4013
4014 #define ppc_hash_table(p) \
4015   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4016   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4017
4018 #define ppc_stub_hash_lookup(table, string, create, copy) \
4019   ((struct ppc_stub_hash_entry *) \
4020    bfd_hash_lookup ((table), (string), (create), (copy)))
4021
4022 #define ppc_branch_hash_lookup(table, string, create, copy) \
4023   ((struct ppc_branch_hash_entry *) \
4024    bfd_hash_lookup ((table), (string), (create), (copy)))
4025
4026 /* Create an entry in the stub hash table.  */
4027
4028 static struct bfd_hash_entry *
4029 stub_hash_newfunc (struct bfd_hash_entry *entry,
4030                    struct bfd_hash_table *table,
4031                    const char *string)
4032 {
4033   /* Allocate the structure if it has not already been allocated by a
4034      subclass.  */
4035   if (entry == NULL)
4036     {
4037       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4038       if (entry == NULL)
4039         return entry;
4040     }
4041
4042   /* Call the allocation method of the superclass.  */
4043   entry = bfd_hash_newfunc (entry, table, string);
4044   if (entry != NULL)
4045     {
4046       struct ppc_stub_hash_entry *eh;
4047
4048       /* Initialize the local fields.  */
4049       eh = (struct ppc_stub_hash_entry *) entry;
4050       eh->stub_type = ppc_stub_none;
4051       eh->stub_sec = NULL;
4052       eh->stub_offset = 0;
4053       eh->target_value = 0;
4054       eh->target_section = NULL;
4055       eh->h = NULL;
4056       eh->plt_ent = NULL;
4057       eh->id_sec = NULL;
4058       eh->other = 0;
4059     }
4060
4061   return entry;
4062 }
4063
4064 /* Create an entry in the branch hash table.  */
4065
4066 static struct bfd_hash_entry *
4067 branch_hash_newfunc (struct bfd_hash_entry *entry,
4068                      struct bfd_hash_table *table,
4069                      const char *string)
4070 {
4071   /* Allocate the structure if it has not already been allocated by a
4072      subclass.  */
4073   if (entry == NULL)
4074     {
4075       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4076       if (entry == NULL)
4077         return entry;
4078     }
4079
4080   /* Call the allocation method of the superclass.  */
4081   entry = bfd_hash_newfunc (entry, table, string);
4082   if (entry != NULL)
4083     {
4084       struct ppc_branch_hash_entry *eh;
4085
4086       /* Initialize the local fields.  */
4087       eh = (struct ppc_branch_hash_entry *) entry;
4088       eh->offset = 0;
4089       eh->iter = 0;
4090     }
4091
4092   return entry;
4093 }
4094
4095 /* Create an entry in a ppc64 ELF linker hash table.  */
4096
4097 static struct bfd_hash_entry *
4098 link_hash_newfunc (struct bfd_hash_entry *entry,
4099                    struct bfd_hash_table *table,
4100                    const char *string)
4101 {
4102   /* Allocate the structure if it has not already been allocated by a
4103      subclass.  */
4104   if (entry == NULL)
4105     {
4106       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4107       if (entry == NULL)
4108         return entry;
4109     }
4110
4111   /* Call the allocation method of the superclass.  */
4112   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4113   if (entry != NULL)
4114     {
4115       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4116
4117       memset (&eh->u.stub_cache, 0,
4118               (sizeof (struct ppc_link_hash_entry)
4119                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4120
4121       /* When making function calls, old ABI code references function entry
4122          points (dot symbols), while new ABI code references the function
4123          descriptor symbol.  We need to make any combination of reference and
4124          definition work together, without breaking archive linking.
4125
4126          For a defined function "foo" and an undefined call to "bar":
4127          An old object defines "foo" and ".foo", references ".bar" (possibly
4128          "bar" too).
4129          A new object defines "foo" and references "bar".
4130
4131          A new object thus has no problem with its undefined symbols being
4132          satisfied by definitions in an old object.  On the other hand, the
4133          old object won't have ".bar" satisfied by a new object.
4134
4135          Keep a list of newly added dot-symbols.  */
4136
4137       if (string[0] == '.')
4138         {
4139           struct ppc_link_hash_table *htab;
4140
4141           htab = (struct ppc_link_hash_table *) table;
4142           eh->u.next_dot_sym = htab->dot_syms;
4143           htab->dot_syms = eh;
4144         }
4145     }
4146
4147   return entry;
4148 }
4149
4150 struct tocsave_entry {
4151   asection *sec;
4152   bfd_vma offset;
4153 };
4154
4155 static hashval_t
4156 tocsave_htab_hash (const void *p)
4157 {
4158   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4159   return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4160 }
4161
4162 static int
4163 tocsave_htab_eq (const void *p1, const void *p2)
4164 {
4165   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4166   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4167   return e1->sec == e2->sec && e1->offset == e2->offset;
4168 }
4169
4170 /* Destroy a ppc64 ELF linker hash table.  */
4171
4172 static void
4173 ppc64_elf_link_hash_table_free (bfd *obfd)
4174 {
4175   struct ppc_link_hash_table *htab;
4176
4177   htab = (struct ppc_link_hash_table *) obfd->link.hash;
4178   if (htab->tocsave_htab)
4179     htab_delete (htab->tocsave_htab);
4180   bfd_hash_table_free (&htab->branch_hash_table);
4181   bfd_hash_table_free (&htab->stub_hash_table);
4182   _bfd_elf_link_hash_table_free (obfd);
4183 }
4184
4185 /* Create a ppc64 ELF linker hash table.  */
4186
4187 static struct bfd_link_hash_table *
4188 ppc64_elf_link_hash_table_create (bfd *abfd)
4189 {
4190   struct ppc_link_hash_table *htab;
4191   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4192
4193   htab = bfd_zmalloc (amt);
4194   if (htab == NULL)
4195     return NULL;
4196
4197   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4198                                       sizeof (struct ppc_link_hash_entry),
4199                                       PPC64_ELF_DATA))
4200     {
4201       free (htab);
4202       return NULL;
4203     }
4204
4205   /* Init the stub hash table too.  */
4206   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4207                             sizeof (struct ppc_stub_hash_entry)))
4208     {
4209       _bfd_elf_link_hash_table_free (abfd);
4210       return NULL;
4211     }
4212
4213   /* And the branch hash table.  */
4214   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4215                             sizeof (struct ppc_branch_hash_entry)))
4216     {
4217       bfd_hash_table_free (&htab->stub_hash_table);
4218       _bfd_elf_link_hash_table_free (abfd);
4219       return NULL;
4220     }
4221
4222   htab->tocsave_htab = htab_try_create (1024,
4223                                         tocsave_htab_hash,
4224                                         tocsave_htab_eq,
4225                                         NULL);
4226   if (htab->tocsave_htab == NULL)
4227     {
4228       ppc64_elf_link_hash_table_free (abfd);
4229       return NULL;
4230     }
4231   htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4232
4233   /* Initializing two fields of the union is just cosmetic.  We really
4234      only care about glist, but when compiled on a 32-bit host the
4235      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
4236      debugger inspection of these fields look nicer.  */
4237   htab->elf.init_got_refcount.refcount = 0;
4238   htab->elf.init_got_refcount.glist = NULL;
4239   htab->elf.init_plt_refcount.refcount = 0;
4240   htab->elf.init_plt_refcount.glist = NULL;
4241   htab->elf.init_got_offset.offset = 0;
4242   htab->elf.init_got_offset.glist = NULL;
4243   htab->elf.init_plt_offset.offset = 0;
4244   htab->elf.init_plt_offset.glist = NULL;
4245
4246   return &htab->elf.root;
4247 }
4248
4249 /* Create sections for linker generated code.  */
4250
4251 static bfd_boolean
4252 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4253 {
4254   struct ppc_link_hash_table *htab;
4255   flagword flags;
4256
4257   htab = ppc_hash_table (info);
4258
4259   /* Create .sfpr for code to save and restore fp regs.  */
4260   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4261            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4262   htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4263                                                    flags);
4264   if (htab->sfpr == NULL
4265       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4266     return FALSE;
4267
4268   /* Create .glink for lazy dynamic linking support.  */
4269   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4270                                                     flags);
4271   if (htab->glink == NULL
4272       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4273     return FALSE;
4274
4275   if (!info->no_ld_generated_unwind_info)
4276     {
4277       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4278                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4279       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4280                                                                  ".eh_frame",
4281                                                                  flags);
4282       if (htab->glink_eh_frame == NULL
4283           || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4284         return FALSE;
4285     }
4286
4287   flags = SEC_ALLOC | SEC_LINKER_CREATED;
4288   htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4289   if (htab->elf.iplt == NULL
4290       || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4291     return FALSE;
4292
4293   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4294            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4295   htab->elf.irelplt
4296     = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4297   if (htab->elf.irelplt == NULL
4298       || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4299     return FALSE;
4300
4301   /* Create branch lookup table for plt_branch stubs.  */
4302   flags = (SEC_ALLOC | SEC_LOAD
4303            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4304   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4305                                                    flags);
4306   if (htab->brlt == NULL
4307       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4308     return FALSE;
4309
4310   if (!info->shared)
4311     return TRUE;
4312
4313   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4314            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4315   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4316                                                       ".rela.branch_lt",
4317                                                       flags);
4318   if (htab->relbrlt == NULL
4319       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4320     return FALSE;
4321
4322   return TRUE;
4323 }
4324
4325 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
4326
4327 bfd_boolean
4328 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4329                          struct ppc64_elf_params *params)
4330 {
4331   struct ppc_link_hash_table *htab;
4332
4333   elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4334
4335 /* Always hook our dynamic sections into the first bfd, which is the
4336    linker created stub bfd.  This ensures that the GOT header is at
4337    the start of the output TOC section.  */
4338   htab = ppc_hash_table (info);
4339   if (htab == NULL)
4340     return FALSE;
4341   htab->elf.dynobj = params->stub_bfd;
4342   htab->params = params;
4343
4344   if (info->relocatable)
4345     return TRUE;
4346
4347   return create_linkage_sections (htab->elf.dynobj, info);
4348 }
4349
4350 /* Build a name for an entry in the stub hash table.  */
4351
4352 static char *
4353 ppc_stub_name (const asection *input_section,
4354                const asection *sym_sec,
4355                const struct ppc_link_hash_entry *h,
4356                const Elf_Internal_Rela *rel)
4357 {
4358   char *stub_name;
4359   ssize_t len;
4360
4361   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4362      offsets from a sym as a branch target?  In fact, we could
4363      probably assume the addend is always zero.  */
4364   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4365
4366   if (h)
4367     {
4368       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4369       stub_name = bfd_malloc (len);
4370       if (stub_name == NULL)
4371         return stub_name;
4372
4373       len = sprintf (stub_name, "%08x.%s+%x",
4374                      input_section->id & 0xffffffff,
4375                      h->elf.root.root.string,
4376                      (int) rel->r_addend & 0xffffffff);
4377     }
4378   else
4379     {
4380       len = 8 + 1 + 8 + 1 + 8 + 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.%x:%x+%x",
4386                      input_section->id & 0xffffffff,
4387                      sym_sec->id & 0xffffffff,
4388                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4389                      (int) rel->r_addend & 0xffffffff);
4390     }
4391   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4392     stub_name[len - 2] = 0;
4393   return stub_name;
4394 }
4395
4396 /* Look up an entry in the stub hash.  Stub entries are cached because
4397    creating the stub name takes a bit of time.  */
4398
4399 static struct ppc_stub_hash_entry *
4400 ppc_get_stub_entry (const asection *input_section,
4401                     const asection *sym_sec,
4402                     struct ppc_link_hash_entry *h,
4403                     const Elf_Internal_Rela *rel,
4404                     struct ppc_link_hash_table *htab)
4405 {
4406   struct ppc_stub_hash_entry *stub_entry;
4407   const asection *id_sec;
4408
4409   /* If this input section is part of a group of sections sharing one
4410      stub section, then use the id of the first section in the group.
4411      Stub names need to include a section id, as there may well be
4412      more than one stub used to reach say, printf, and we need to
4413      distinguish between them.  */
4414   id_sec = htab->stub_group[input_section->id].link_sec;
4415
4416   if (h != NULL && h->u.stub_cache != NULL
4417       && h->u.stub_cache->h == h
4418       && h->u.stub_cache->id_sec == id_sec)
4419     {
4420       stub_entry = h->u.stub_cache;
4421     }
4422   else
4423     {
4424       char *stub_name;
4425
4426       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4427       if (stub_name == NULL)
4428         return NULL;
4429
4430       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4431                                          stub_name, FALSE, FALSE);
4432       if (h != NULL)
4433         h->u.stub_cache = stub_entry;
4434
4435       free (stub_name);
4436     }
4437
4438   return stub_entry;
4439 }
4440
4441 /* Add a new stub entry to the stub hash.  Not all fields of the new
4442    stub entry are initialised.  */
4443
4444 static struct ppc_stub_hash_entry *
4445 ppc_add_stub (const char *stub_name,
4446               asection *section,
4447               struct bfd_link_info *info)
4448 {
4449   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4450   asection *link_sec;
4451   asection *stub_sec;
4452   struct ppc_stub_hash_entry *stub_entry;
4453
4454   link_sec = htab->stub_group[section->id].link_sec;
4455   stub_sec = htab->stub_group[section->id].stub_sec;
4456   if (stub_sec == NULL)
4457     {
4458       stub_sec = htab->stub_group[link_sec->id].stub_sec;
4459       if (stub_sec == NULL)
4460         {
4461           size_t namelen;
4462           bfd_size_type len;
4463           char *s_name;
4464
4465           namelen = strlen (link_sec->name);
4466           len = namelen + sizeof (STUB_SUFFIX);
4467           s_name = bfd_alloc (htab->params->stub_bfd, len);
4468           if (s_name == NULL)
4469             return NULL;
4470
4471           memcpy (s_name, link_sec->name, namelen);
4472           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4473           stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4474           if (stub_sec == NULL)
4475             return NULL;
4476           htab->stub_group[link_sec->id].stub_sec = stub_sec;
4477         }
4478       htab->stub_group[section->id].stub_sec = stub_sec;
4479     }
4480
4481   /* Enter this entry into the linker stub hash table.  */
4482   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4483                                      TRUE, FALSE);
4484   if (stub_entry == NULL)
4485     {
4486       info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4487                               section->owner, stub_name);
4488       return NULL;
4489     }
4490
4491   stub_entry->stub_sec = stub_sec;
4492   stub_entry->stub_offset = 0;
4493   stub_entry->id_sec = link_sec;
4494   return stub_entry;
4495 }
4496
4497 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4498    not already done.  */
4499
4500 static bfd_boolean
4501 create_got_section (bfd *abfd, struct bfd_link_info *info)
4502 {
4503   asection *got, *relgot;
4504   flagword flags;
4505   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4506
4507   if (!is_ppc64_elf (abfd))
4508     return FALSE;
4509   if (htab == NULL)
4510     return FALSE;
4511
4512   if (!htab->elf.sgot
4513       && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4514     return FALSE;
4515
4516   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4517            | SEC_LINKER_CREATED);
4518
4519   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4520   if (!got
4521       || !bfd_set_section_alignment (abfd, got, 3))
4522     return FALSE;
4523
4524   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4525                                                flags | SEC_READONLY);
4526   if (!relgot
4527       || ! bfd_set_section_alignment (abfd, relgot, 3))
4528     return FALSE;
4529
4530   ppc64_elf_tdata (abfd)->got = got;
4531   ppc64_elf_tdata (abfd)->relgot = relgot;
4532   return TRUE;
4533 }
4534
4535 /* Create the dynamic sections, and set up shortcuts.  */
4536
4537 static bfd_boolean
4538 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4539 {
4540   struct ppc_link_hash_table *htab;
4541
4542   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4543     return FALSE;
4544
4545   htab = ppc_hash_table (info);
4546   if (htab == NULL)
4547     return FALSE;
4548
4549   htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
4550   if (!info->shared)
4551     htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
4552
4553   if (!htab->elf.sgot || !htab->elf.splt || !htab->elf.srelplt || !htab->dynbss
4554       || (!info->shared && !htab->relbss))
4555     abort ();
4556
4557   return TRUE;
4558 }
4559
4560 /* Follow indirect and warning symbol links.  */
4561
4562 static inline struct bfd_link_hash_entry *
4563 follow_link (struct bfd_link_hash_entry *h)
4564 {
4565   while (h->type == bfd_link_hash_indirect
4566          || h->type == bfd_link_hash_warning)
4567     h = h->u.i.link;
4568   return h;
4569 }
4570
4571 static inline struct elf_link_hash_entry *
4572 elf_follow_link (struct elf_link_hash_entry *h)
4573 {
4574   return (struct elf_link_hash_entry *) follow_link (&h->root);
4575 }
4576
4577 static inline struct ppc_link_hash_entry *
4578 ppc_follow_link (struct ppc_link_hash_entry *h)
4579 {
4580   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4581 }
4582
4583 /* Merge PLT info on FROM with that on TO.  */
4584
4585 static void
4586 move_plt_plist (struct ppc_link_hash_entry *from,
4587                 struct ppc_link_hash_entry *to)
4588 {
4589   if (from->elf.plt.plist != NULL)
4590     {
4591       if (to->elf.plt.plist != NULL)
4592         {
4593           struct plt_entry **entp;
4594           struct plt_entry *ent;
4595
4596           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4597             {
4598               struct plt_entry *dent;
4599
4600               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4601                 if (dent->addend == ent->addend)
4602                   {
4603                     dent->plt.refcount += ent->plt.refcount;
4604                     *entp = ent->next;
4605                     break;
4606                   }
4607               if (dent == NULL)
4608                 entp = &ent->next;
4609             }
4610           *entp = to->elf.plt.plist;
4611         }
4612
4613       to->elf.plt.plist = from->elf.plt.plist;
4614       from->elf.plt.plist = NULL;
4615     }
4616 }
4617
4618 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
4619
4620 static void
4621 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4622                                 struct elf_link_hash_entry *dir,
4623                                 struct elf_link_hash_entry *ind)
4624 {
4625   struct ppc_link_hash_entry *edir, *eind;
4626
4627   edir = (struct ppc_link_hash_entry *) dir;
4628   eind = (struct ppc_link_hash_entry *) ind;
4629
4630   edir->is_func |= eind->is_func;
4631   edir->is_func_descriptor |= eind->is_func_descriptor;
4632   edir->tls_mask |= eind->tls_mask;
4633   if (eind->oh != NULL)
4634     edir->oh = ppc_follow_link (eind->oh);
4635
4636   /* If called to transfer flags for a weakdef during processing
4637      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4638      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
4639   if (!(ELIMINATE_COPY_RELOCS
4640         && eind->elf.root.type != bfd_link_hash_indirect
4641         && edir->elf.dynamic_adjusted))
4642     edir->elf.non_got_ref |= eind->elf.non_got_ref;
4643
4644   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4645   edir->elf.ref_regular |= eind->elf.ref_regular;
4646   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4647   edir->elf.needs_plt |= eind->elf.needs_plt;
4648   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4649
4650   /* Copy over any dynamic relocs we may have on the indirect sym.  */
4651   if (eind->dyn_relocs != NULL)
4652     {
4653       if (edir->dyn_relocs != NULL)
4654         {
4655           struct elf_dyn_relocs **pp;
4656           struct elf_dyn_relocs *p;
4657
4658           /* Add reloc counts against the indirect sym to the direct sym
4659              list.  Merge any entries against the same section.  */
4660           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4661             {
4662               struct elf_dyn_relocs *q;
4663
4664               for (q = edir->dyn_relocs; q != NULL; q = q->next)
4665                 if (q->sec == p->sec)
4666                   {
4667                     q->pc_count += p->pc_count;
4668                     q->count += p->count;
4669                     *pp = p->next;
4670                     break;
4671                   }
4672               if (q == NULL)
4673                 pp = &p->next;
4674             }
4675           *pp = edir->dyn_relocs;
4676         }
4677
4678       edir->dyn_relocs = eind->dyn_relocs;
4679       eind->dyn_relocs = NULL;
4680     }
4681
4682   /* If we were called to copy over info for a weak sym, that's all.
4683      You might think dyn_relocs need not be copied over;  After all,
4684      both syms will be dynamic or both non-dynamic so we're just
4685      moving reloc accounting around.  However, ELIMINATE_COPY_RELOCS
4686      code in ppc64_elf_adjust_dynamic_symbol needs to check for
4687      dyn_relocs in read-only sections, and it does so on what is the
4688      DIR sym here.  */
4689   if (eind->elf.root.type != bfd_link_hash_indirect)
4690     return;
4691
4692   /* Copy over got entries that we may have already seen to the
4693      symbol which just became indirect.  */
4694   if (eind->elf.got.glist != NULL)
4695     {
4696       if (edir->elf.got.glist != NULL)
4697         {
4698           struct got_entry **entp;
4699           struct got_entry *ent;
4700
4701           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4702             {
4703               struct got_entry *dent;
4704
4705               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4706                 if (dent->addend == ent->addend
4707                     && dent->owner == ent->owner
4708                     && dent->tls_type == ent->tls_type)
4709                   {
4710                     dent->got.refcount += ent->got.refcount;
4711                     *entp = ent->next;
4712                     break;
4713                   }
4714               if (dent == NULL)
4715                 entp = &ent->next;
4716             }
4717           *entp = edir->elf.got.glist;
4718         }
4719
4720       edir->elf.got.glist = eind->elf.got.glist;
4721       eind->elf.got.glist = NULL;
4722     }
4723
4724   /* And plt entries.  */
4725   move_plt_plist (eind, edir);
4726
4727   if (eind->elf.dynindx != -1)
4728     {
4729       if (edir->elf.dynindx != -1)
4730         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4731                                 edir->elf.dynstr_index);
4732       edir->elf.dynindx = eind->elf.dynindx;
4733       edir->elf.dynstr_index = eind->elf.dynstr_index;
4734       eind->elf.dynindx = -1;
4735       eind->elf.dynstr_index = 0;
4736     }
4737 }
4738
4739 /* Find the function descriptor hash entry from the given function code
4740    hash entry FH.  Link the entries via their OH fields.  */
4741
4742 static struct ppc_link_hash_entry *
4743 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4744 {
4745   struct ppc_link_hash_entry *fdh = fh->oh;
4746
4747   if (fdh == NULL)
4748     {
4749       const char *fd_name = fh->elf.root.root.string + 1;
4750
4751       fdh = (struct ppc_link_hash_entry *)
4752         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4753       if (fdh == NULL)
4754         return fdh;
4755
4756       fdh->is_func_descriptor = 1;
4757       fdh->oh = fh;
4758       fh->is_func = 1;
4759       fh->oh = fdh;
4760     }
4761
4762   return ppc_follow_link (fdh);
4763 }
4764
4765 /* Make a fake function descriptor sym for the code sym FH.  */
4766
4767 static struct ppc_link_hash_entry *
4768 make_fdh (struct bfd_link_info *info,
4769           struct ppc_link_hash_entry *fh)
4770 {
4771   bfd *abfd;
4772   asymbol *newsym;
4773   struct bfd_link_hash_entry *bh;
4774   struct ppc_link_hash_entry *fdh;
4775
4776   abfd = fh->elf.root.u.undef.abfd;
4777   newsym = bfd_make_empty_symbol (abfd);
4778   newsym->name = fh->elf.root.root.string + 1;
4779   newsym->section = bfd_und_section_ptr;
4780   newsym->value = 0;
4781   newsym->flags = BSF_WEAK;
4782
4783   bh = NULL;
4784   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4785                                          newsym->flags, newsym->section,
4786                                          newsym->value, NULL, FALSE, FALSE,
4787                                          &bh))
4788     return NULL;
4789
4790   fdh = (struct ppc_link_hash_entry *) bh;
4791   fdh->elf.non_elf = 0;
4792   fdh->fake = 1;
4793   fdh->is_func_descriptor = 1;
4794   fdh->oh = fh;
4795   fh->is_func = 1;
4796   fh->oh = fdh;
4797   return fdh;
4798 }
4799
4800 /* Fix function descriptor symbols defined in .opd sections to be
4801    function type.  */
4802
4803 static bfd_boolean
4804 ppc64_elf_add_symbol_hook (bfd *ibfd,
4805                            struct bfd_link_info *info,
4806                            Elf_Internal_Sym *isym,
4807                            const char **name,
4808                            flagword *flags ATTRIBUTE_UNUSED,
4809                            asection **sec,
4810                            bfd_vma *value)
4811 {
4812   if ((ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4813        || ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4814       && (ibfd->flags & DYNAMIC) == 0
4815       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4816     elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4817
4818   if (*sec != NULL
4819       && strcmp ((*sec)->name, ".opd") == 0)
4820     {
4821       asection *code_sec;
4822
4823       if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4824             || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4825         isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4826
4827       /* If the symbol is a function defined in .opd, and the function
4828          code is in a discarded group, let it appear to be undefined.  */
4829       if (!info->relocatable
4830           && (*sec)->reloc_count != 0
4831           && opd_entry_value (*sec, *value, &code_sec, NULL,
4832                               FALSE) != (bfd_vma) -1
4833           && discarded_section (code_sec))
4834         {
4835           *sec = bfd_und_section_ptr;
4836           isym->st_shndx = SHN_UNDEF;
4837         }
4838     }
4839   else if (*sec != NULL
4840            && strcmp ((*sec)->name, ".toc") == 0
4841            && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4842     {
4843       struct ppc_link_hash_table *htab = ppc_hash_table (info);
4844       if (htab != NULL)
4845         htab->params->object_in_toc = 1;
4846     }
4847
4848   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4849     {
4850       if (abiversion (ibfd) == 0)
4851         set_abiversion (ibfd, 2);
4852       else if (abiversion (ibfd) == 1)
4853         {
4854           info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4855                                     " for ABI version 1\n"), name);
4856           bfd_set_error (bfd_error_bad_value);
4857           return FALSE;
4858         }
4859     }
4860
4861   return TRUE;
4862 }
4863
4864 /* Merge non-visibility st_other attributes: local entry point.  */
4865
4866 static void
4867 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4868                                   const Elf_Internal_Sym *isym,
4869                                   bfd_boolean definition,
4870                                   bfd_boolean dynamic)
4871 {
4872   if (definition && !dynamic)
4873     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4874                 | ELF_ST_VISIBILITY (h->other));
4875 }
4876
4877 /* This function makes an old ABI object reference to ".bar" cause the
4878    inclusion of a new ABI object archive that defines "bar".
4879    NAME is a symbol defined in an archive.  Return a symbol in the hash
4880    table that might be satisfied by the archive symbols.  */
4881
4882 static struct elf_link_hash_entry *
4883 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4884                                  struct bfd_link_info *info,
4885                                  const char *name)
4886 {
4887   struct elf_link_hash_entry *h;
4888   char *dot_name;
4889   size_t len;
4890
4891   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4892   if (h != NULL
4893       /* Don't return this sym if it is a fake function descriptor
4894          created by add_symbol_adjust.  */
4895       && !(h->root.type == bfd_link_hash_undefweak
4896            && ((struct ppc_link_hash_entry *) h)->fake))
4897     return h;
4898
4899   if (name[0] == '.')
4900     return h;
4901
4902   len = strlen (name);
4903   dot_name = bfd_alloc (abfd, len + 2);
4904   if (dot_name == NULL)
4905     return (struct elf_link_hash_entry *) 0 - 1;
4906   dot_name[0] = '.';
4907   memcpy (dot_name + 1, name, len + 1);
4908   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4909   bfd_release (abfd, dot_name);
4910   return h;
4911 }
4912
4913 /* This function satisfies all old ABI object references to ".bar" if a
4914    new ABI object defines "bar".  Well, at least, undefined dot symbols
4915    are made weak.  This stops later archive searches from including an
4916    object if we already have a function descriptor definition.  It also
4917    prevents the linker complaining about undefined symbols.
4918    We also check and correct mismatched symbol visibility here.  The
4919    most restrictive visibility of the function descriptor and the
4920    function entry symbol is used.  */
4921
4922 static bfd_boolean
4923 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4924 {
4925   struct ppc_link_hash_table *htab;
4926   struct ppc_link_hash_entry *fdh;
4927
4928   if (eh->elf.root.type == bfd_link_hash_indirect)
4929     return TRUE;
4930
4931   if (eh->elf.root.type == bfd_link_hash_warning)
4932     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4933
4934   if (eh->elf.root.root.string[0] != '.')
4935     abort ();
4936
4937   htab = ppc_hash_table (info);
4938   if (htab == NULL)
4939     return FALSE;
4940
4941   fdh = lookup_fdh (eh, htab);
4942   if (fdh == NULL)
4943     {
4944       if (!info->relocatable
4945           && (eh->elf.root.type == bfd_link_hash_undefined
4946               || eh->elf.root.type == bfd_link_hash_undefweak)
4947           && eh->elf.ref_regular)
4948         {
4949           /* Make an undefweak function descriptor sym, which is enough to
4950              pull in an --as-needed shared lib, but won't cause link
4951              errors.  Archives are handled elsewhere.  */
4952           fdh = make_fdh (info, eh);
4953           if (fdh == NULL)
4954             return FALSE;
4955           fdh->elf.ref_regular = 1;
4956         }
4957     }
4958   else
4959     {
4960       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4961       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4962       if (entry_vis < descr_vis)
4963         fdh->elf.other += entry_vis - descr_vis;
4964       else if (entry_vis > descr_vis)
4965         eh->elf.other += descr_vis - entry_vis;
4966
4967       if ((fdh->elf.root.type == bfd_link_hash_defined
4968            || fdh->elf.root.type == bfd_link_hash_defweak)
4969           && eh->elf.root.type == bfd_link_hash_undefined)
4970         {
4971           eh->elf.root.type = bfd_link_hash_undefweak;
4972           eh->was_undefined = 1;
4973           htab->twiddled_syms = 1;
4974         }
4975     }
4976
4977   return TRUE;
4978 }
4979
4980 /* Set up opd section info and abiversion for IBFD, and process list
4981    of dot-symbols we made in link_hash_newfunc.  */
4982
4983 static bfd_boolean
4984 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4985 {
4986   struct ppc_link_hash_table *htab;
4987   struct ppc_link_hash_entry **p, *eh;
4988   asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4989
4990   if (opd != NULL && opd->size != 0)
4991     {
4992       if (abiversion (ibfd) == 0)
4993         set_abiversion (ibfd, 1);
4994       else if (abiversion (ibfd) == 2)
4995         {
4996           info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
4997                                     " version %d\n"),
4998                                   ibfd, abiversion (ibfd));
4999           bfd_set_error (bfd_error_bad_value);
5000           return FALSE;
5001         }
5002
5003       if ((ibfd->flags & DYNAMIC) == 0
5004           && (opd->flags & SEC_RELOC) != 0
5005           && opd->reloc_count != 0
5006           && !bfd_is_abs_section (opd->output_section))
5007         {
5008           /* Garbage collection needs some extra help with .opd sections.
5009              We don't want to necessarily keep everything referenced by
5010              relocs in .opd, as that would keep all functions.  Instead,
5011              if we reference an .opd symbol (a function descriptor), we
5012              want to keep the function code symbol's section.  This is
5013              easy for global symbols, but for local syms we need to keep
5014              information about the associated function section.  */
5015           bfd_size_type amt;
5016           asection **opd_sym_map;
5017
5018           amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5019           opd_sym_map = bfd_zalloc (ibfd, amt);
5020           if (opd_sym_map == NULL)
5021             return FALSE;
5022           ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5023           BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5024           ppc64_elf_section_data (opd)->sec_type = sec_opd;
5025         }
5026     }
5027
5028   if (!is_ppc64_elf (info->output_bfd))
5029     return TRUE;
5030   htab = ppc_hash_table (info);
5031   if (htab == NULL)
5032     return FALSE;
5033
5034   /* For input files without an explicit abiversion in e_flags
5035      we should have flagged any with symbol st_other bits set
5036      as ELFv1 and above flagged those with .opd as ELFv2.
5037      Set the output abiversion if not yet set, and for any input
5038      still ambiguous, take its abiversion from the output.
5039      Differences in ABI are reported later.  */
5040   if (abiversion (info->output_bfd) == 0)
5041     set_abiversion (info->output_bfd, abiversion (ibfd));
5042   else if (abiversion (ibfd) == 0)
5043     set_abiversion (ibfd, abiversion (info->output_bfd));
5044
5045   p = &htab->dot_syms;
5046   while ((eh = *p) != NULL)
5047     {
5048       *p = NULL;
5049       if (&eh->elf == htab->elf.hgot)
5050         ;
5051       else if (htab->elf.hgot == NULL
5052                && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5053         htab->elf.hgot = &eh->elf;
5054       else if (!add_symbol_adjust (eh, info))
5055         return FALSE;
5056       p = &eh->u.next_dot_sym;
5057     }
5058
5059   /* Clear the list for non-ppc64 input files.  */
5060   p = &htab->dot_syms;
5061   while ((eh = *p) != NULL)
5062     {
5063       *p = NULL;
5064       p = &eh->u.next_dot_sym;
5065     }
5066
5067   /* We need to fix the undefs list for any syms we have twiddled to
5068      undef_weak.  */
5069   if (htab->twiddled_syms)
5070     {
5071       bfd_link_repair_undef_list (&htab->elf.root);
5072       htab->twiddled_syms = 0;
5073     }
5074   return TRUE;
5075 }
5076
5077 /* Undo hash table changes when an --as-needed input file is determined
5078    not to be needed.  */
5079
5080 static bfd_boolean
5081 ppc64_elf_notice_as_needed (bfd *ibfd,
5082                             struct bfd_link_info *info,
5083                             enum notice_asneeded_action act)
5084 {
5085   if (act == notice_not_needed)
5086     {
5087       struct ppc_link_hash_table *htab = ppc_hash_table (info);
5088
5089       if (htab == NULL)
5090         return FALSE;
5091
5092       htab->dot_syms = NULL;
5093     }
5094   return _bfd_elf_notice_as_needed (ibfd, info, act);
5095 }
5096
5097 /* If --just-symbols against a final linked binary, then assume we need
5098    toc adjusting stubs when calling functions defined there.  */
5099
5100 static void
5101 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5102 {
5103   if ((sec->flags & SEC_CODE) != 0
5104       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5105       && is_ppc64_elf (sec->owner))
5106     {
5107       if (abiversion (sec->owner) >= 2
5108           || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5109         sec->has_toc_reloc = 1;
5110     }
5111   _bfd_elf_link_just_syms (sec, info);
5112 }
5113
5114 static struct plt_entry **
5115 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5116                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5117 {
5118   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5119   struct plt_entry **local_plt;
5120   unsigned char *local_got_tls_masks;
5121
5122   if (local_got_ents == NULL)
5123     {
5124       bfd_size_type size = symtab_hdr->sh_info;
5125
5126       size *= (sizeof (*local_got_ents)
5127                + sizeof (*local_plt)
5128                + sizeof (*local_got_tls_masks));
5129       local_got_ents = bfd_zalloc (abfd, size);
5130       if (local_got_ents == NULL)
5131         return NULL;
5132       elf_local_got_ents (abfd) = local_got_ents;
5133     }
5134
5135   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5136     {
5137       struct got_entry *ent;
5138
5139       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5140         if (ent->addend == r_addend
5141             && ent->owner == abfd
5142             && ent->tls_type == tls_type)
5143           break;
5144       if (ent == NULL)
5145         {
5146           bfd_size_type amt = sizeof (*ent);
5147           ent = bfd_alloc (abfd, amt);
5148           if (ent == NULL)
5149             return FALSE;
5150           ent->next = local_got_ents[r_symndx];
5151           ent->addend = r_addend;
5152           ent->owner = abfd;
5153           ent->tls_type = tls_type;
5154           ent->is_indirect = FALSE;
5155           ent->got.refcount = 0;
5156           local_got_ents[r_symndx] = ent;
5157         }
5158       ent->got.refcount += 1;
5159     }
5160
5161   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5162   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5163   local_got_tls_masks[r_symndx] |= tls_type;
5164
5165   return local_plt + r_symndx;
5166 }
5167
5168 static bfd_boolean
5169 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5170 {
5171   struct plt_entry *ent;
5172
5173   for (ent = *plist; ent != NULL; ent = ent->next)
5174     if (ent->addend == addend)
5175       break;
5176   if (ent == NULL)
5177     {
5178       bfd_size_type amt = sizeof (*ent);
5179       ent = bfd_alloc (abfd, amt);
5180       if (ent == NULL)
5181         return FALSE;
5182       ent->next = *plist;
5183       ent->addend = addend;
5184       ent->plt.refcount = 0;
5185       *plist = ent;
5186     }
5187   ent->plt.refcount += 1;
5188   return TRUE;
5189 }
5190
5191 static bfd_boolean
5192 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5193 {
5194   return (r_type == R_PPC64_REL24
5195           || r_type == R_PPC64_REL14
5196           || r_type == R_PPC64_REL14_BRTAKEN
5197           || r_type == R_PPC64_REL14_BRNTAKEN
5198           || r_type == R_PPC64_ADDR24
5199           || r_type == R_PPC64_ADDR14
5200           || r_type == R_PPC64_ADDR14_BRTAKEN
5201           || r_type == R_PPC64_ADDR14_BRNTAKEN);
5202 }
5203
5204 /* Look through the relocs for a section during the first phase, and
5205    calculate needed space in the global offset table, procedure
5206    linkage table, and dynamic reloc sections.  */
5207
5208 static bfd_boolean
5209 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5210                         asection *sec, const Elf_Internal_Rela *relocs)
5211 {
5212   struct ppc_link_hash_table *htab;
5213   Elf_Internal_Shdr *symtab_hdr;
5214   struct elf_link_hash_entry **sym_hashes;
5215   const Elf_Internal_Rela *rel;
5216   const Elf_Internal_Rela *rel_end;
5217   asection *sreloc;
5218   asection **opd_sym_map;
5219   struct elf_link_hash_entry *tga, *dottga;
5220
5221   if (info->relocatable)
5222     return TRUE;
5223
5224   /* Don't do anything special with non-loaded, non-alloced sections.
5225      In particular, any relocs in such sections should not affect GOT
5226      and PLT reference counting (ie. we don't allow them to create GOT
5227      or PLT entries), there's no possibility or desire to optimize TLS
5228      relocs, and there's not much point in propagating relocs to shared
5229      libs that the dynamic linker won't relocate.  */
5230   if ((sec->flags & SEC_ALLOC) == 0)
5231     return TRUE;
5232
5233   BFD_ASSERT (is_ppc64_elf (abfd));
5234
5235   htab = ppc_hash_table (info);
5236   if (htab == NULL)
5237     return FALSE;
5238
5239   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5240                               FALSE, FALSE, TRUE);
5241   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5242                                  FALSE, FALSE, TRUE);
5243   symtab_hdr = &elf_symtab_hdr (abfd);
5244   sym_hashes = elf_sym_hashes (abfd);
5245   sreloc = NULL;
5246   opd_sym_map = NULL;
5247   if (ppc64_elf_section_data (sec) != NULL
5248       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5249     opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5250
5251   rel_end = relocs + sec->reloc_count;
5252   for (rel = relocs; rel < rel_end; rel++)
5253     {
5254       unsigned long r_symndx;
5255       struct elf_link_hash_entry *h;
5256       enum elf_ppc64_reloc_type r_type;
5257       int tls_type;
5258       struct _ppc64_elf_section_data *ppc64_sec;
5259       struct plt_entry **ifunc;
5260
5261       r_symndx = ELF64_R_SYM (rel->r_info);
5262       if (r_symndx < symtab_hdr->sh_info)
5263         h = NULL;
5264       else
5265         {
5266           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5267           h = elf_follow_link (h);
5268
5269           /* PR15323, ref flags aren't set for references in the same
5270              object.  */
5271           h->root.non_ir_ref = 1;
5272
5273           if (h == htab->elf.hgot)
5274             sec->has_toc_reloc = 1;
5275         }
5276
5277       tls_type = 0;
5278       ifunc = NULL;
5279       if (h != NULL)
5280         {
5281           if (h->type == STT_GNU_IFUNC)
5282             {
5283               h->needs_plt = 1;
5284               ifunc = &h->plt.plist;
5285             }
5286         }
5287       else
5288         {
5289           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5290                                                           abfd, r_symndx);
5291           if (isym == NULL)
5292             return FALSE;
5293
5294           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5295             {
5296               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5297                                              rel->r_addend, PLT_IFUNC);
5298               if (ifunc == NULL)
5299                 return FALSE;
5300             }
5301         }
5302       r_type = ELF64_R_TYPE (rel->r_info);
5303       if (is_branch_reloc (r_type))
5304         {
5305           if (h != NULL && (h == tga || h == dottga))
5306             {
5307               if (rel != relocs
5308                   && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5309                       || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5310                 /* We have a new-style __tls_get_addr call with a marker
5311                    reloc.  */
5312                 ;
5313               else
5314                 /* Mark this section as having an old-style call.  */
5315                 sec->has_tls_get_addr_call = 1;
5316             }
5317
5318           /* STT_GNU_IFUNC symbols must have a PLT entry.  */
5319           if (ifunc != NULL
5320               && !update_plt_info (abfd, ifunc, rel->r_addend))
5321             return FALSE;
5322         }
5323
5324       switch (r_type)
5325         {
5326         case R_PPC64_TLSGD:
5327         case R_PPC64_TLSLD:
5328           /* These special tls relocs tie a call to __tls_get_addr with
5329              its parameter symbol.  */
5330           break;
5331
5332         case R_PPC64_GOT_TLSLD16:
5333         case R_PPC64_GOT_TLSLD16_LO:
5334         case R_PPC64_GOT_TLSLD16_HI:
5335         case R_PPC64_GOT_TLSLD16_HA:
5336           tls_type = TLS_TLS | TLS_LD;
5337           goto dogottls;
5338
5339         case R_PPC64_GOT_TLSGD16:
5340         case R_PPC64_GOT_TLSGD16_LO:
5341         case R_PPC64_GOT_TLSGD16_HI:
5342         case R_PPC64_GOT_TLSGD16_HA:
5343           tls_type = TLS_TLS | TLS_GD;
5344           goto dogottls;
5345
5346         case R_PPC64_GOT_TPREL16_DS:
5347         case R_PPC64_GOT_TPREL16_LO_DS:
5348         case R_PPC64_GOT_TPREL16_HI:
5349         case R_PPC64_GOT_TPREL16_HA:
5350           if (info->shared)
5351             info->flags |= DF_STATIC_TLS;
5352           tls_type = TLS_TLS | TLS_TPREL;
5353           goto dogottls;
5354
5355         case R_PPC64_GOT_DTPREL16_DS:
5356         case R_PPC64_GOT_DTPREL16_LO_DS:
5357         case R_PPC64_GOT_DTPREL16_HI:
5358         case R_PPC64_GOT_DTPREL16_HA:
5359           tls_type = TLS_TLS | TLS_DTPREL;
5360         dogottls:
5361           sec->has_tls_reloc = 1;
5362           /* Fall thru */
5363
5364         case R_PPC64_GOT16:
5365         case R_PPC64_GOT16_DS:
5366         case R_PPC64_GOT16_HA:
5367         case R_PPC64_GOT16_HI:
5368         case R_PPC64_GOT16_LO:
5369         case R_PPC64_GOT16_LO_DS:
5370           /* This symbol requires a global offset table entry.  */
5371           sec->has_toc_reloc = 1;
5372           if (r_type == R_PPC64_GOT_TLSLD16
5373               || r_type == R_PPC64_GOT_TLSGD16
5374               || r_type == R_PPC64_GOT_TPREL16_DS
5375               || r_type == R_PPC64_GOT_DTPREL16_DS
5376               || r_type == R_PPC64_GOT16
5377               || r_type == R_PPC64_GOT16_DS)
5378             {
5379               htab->do_multi_toc = 1;
5380               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5381             }
5382
5383           if (ppc64_elf_tdata (abfd)->got == NULL
5384               && !create_got_section (abfd, info))
5385             return FALSE;
5386
5387           if (h != NULL)
5388             {
5389               struct ppc_link_hash_entry *eh;
5390               struct got_entry *ent;
5391
5392               eh = (struct ppc_link_hash_entry *) h;
5393               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5394                 if (ent->addend == rel->r_addend
5395                     && ent->owner == abfd
5396                     && ent->tls_type == tls_type)
5397                   break;
5398               if (ent == NULL)
5399                 {
5400                   bfd_size_type amt = sizeof (*ent);
5401                   ent = bfd_alloc (abfd, amt);
5402                   if (ent == NULL)
5403                     return FALSE;
5404                   ent->next = eh->elf.got.glist;
5405                   ent->addend = rel->r_addend;
5406                   ent->owner = abfd;
5407                   ent->tls_type = tls_type;
5408                   ent->is_indirect = FALSE;
5409                   ent->got.refcount = 0;
5410                   eh->elf.got.glist = ent;
5411                 }
5412               ent->got.refcount += 1;
5413               eh->tls_mask |= tls_type;
5414             }
5415           else
5416             /* This is a global offset table entry for a local symbol.  */
5417             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5418                                         rel->r_addend, tls_type))
5419               return FALSE;
5420
5421           /* We may also need a plt entry if the symbol turns out to be
5422              an ifunc.  */
5423           if (h != NULL && !info->shared && abiversion (abfd) != 1)
5424             {
5425               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5426                 return FALSE;
5427             }
5428           break;
5429
5430         case R_PPC64_PLT16_HA:
5431         case R_PPC64_PLT16_HI:
5432         case R_PPC64_PLT16_LO:
5433         case R_PPC64_PLT32:
5434         case R_PPC64_PLT64:
5435           /* This symbol requires a procedure linkage table entry.  We
5436              actually build the entry in adjust_dynamic_symbol,
5437              because this might be a case of linking PIC code without
5438              linking in any dynamic objects, in which case we don't
5439              need to generate a procedure linkage table after all.  */
5440           if (h == NULL)
5441             {
5442               /* It does not make sense to have a procedure linkage
5443                  table entry for a local symbol.  */
5444               bfd_set_error (bfd_error_bad_value);
5445               return FALSE;
5446             }
5447           else
5448             {
5449               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5450                 return FALSE;
5451               h->needs_plt = 1;
5452               if (h->root.root.string[0] == '.'
5453                   && h->root.root.string[1] != '\0')
5454                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5455             }
5456           break;
5457
5458           /* The following relocations don't need to propagate the
5459              relocation if linking a shared object since they are
5460              section relative.  */
5461         case R_PPC64_SECTOFF:
5462         case R_PPC64_SECTOFF_LO:
5463         case R_PPC64_SECTOFF_HI:
5464         case R_PPC64_SECTOFF_HA:
5465         case R_PPC64_SECTOFF_DS:
5466         case R_PPC64_SECTOFF_LO_DS:
5467         case R_PPC64_DTPREL16:
5468         case R_PPC64_DTPREL16_LO:
5469         case R_PPC64_DTPREL16_HI:
5470         case R_PPC64_DTPREL16_HA:
5471         case R_PPC64_DTPREL16_DS:
5472         case R_PPC64_DTPREL16_LO_DS:
5473         case R_PPC64_DTPREL16_HIGH:
5474         case R_PPC64_DTPREL16_HIGHA:
5475         case R_PPC64_DTPREL16_HIGHER:
5476         case R_PPC64_DTPREL16_HIGHERA:
5477         case R_PPC64_DTPREL16_HIGHEST:
5478         case R_PPC64_DTPREL16_HIGHESTA:
5479           break;
5480
5481           /* Nor do these.  */
5482         case R_PPC64_REL16:
5483         case R_PPC64_REL16_LO:
5484         case R_PPC64_REL16_HI:
5485         case R_PPC64_REL16_HA:
5486           break;
5487
5488           /* Not supported as a dynamic relocation.  */
5489         case R_PPC64_ADDR64_LOCAL:
5490           if (info->shared)
5491             {
5492               if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5493                 ppc_howto_init ();
5494               info->callbacks->einfo (_("%P: %H: %s reloc unsupported "
5495                                         "in shared libraries and PIEs.\n"),
5496                                       abfd, sec, rel->r_offset,
5497                                       ppc64_elf_howto_table[r_type]->name);
5498               bfd_set_error (bfd_error_bad_value);
5499               return FALSE;
5500             }
5501           break;
5502
5503         case R_PPC64_TOC16:
5504         case R_PPC64_TOC16_DS:
5505           htab->do_multi_toc = 1;
5506           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5507         case R_PPC64_TOC16_LO:
5508         case R_PPC64_TOC16_HI:
5509         case R_PPC64_TOC16_HA:
5510         case R_PPC64_TOC16_LO_DS:
5511           sec->has_toc_reloc = 1;
5512           break;
5513
5514           /* This relocation describes the C++ object vtable hierarchy.
5515              Reconstruct it for later use during GC.  */
5516         case R_PPC64_GNU_VTINHERIT:
5517           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5518             return FALSE;
5519           break;
5520
5521           /* This relocation describes which C++ vtable entries are actually
5522              used.  Record for later use during GC.  */
5523         case R_PPC64_GNU_VTENTRY:
5524           BFD_ASSERT (h != NULL);
5525           if (h != NULL
5526               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5527             return FALSE;
5528           break;
5529
5530         case R_PPC64_REL14:
5531         case R_PPC64_REL14_BRTAKEN:
5532         case R_PPC64_REL14_BRNTAKEN:
5533           {
5534             asection *dest = NULL;
5535
5536             /* Heuristic: If jumping outside our section, chances are
5537                we are going to need a stub.  */
5538             if (h != NULL)
5539               {
5540                 /* If the sym is weak it may be overridden later, so
5541                    don't assume we know where a weak sym lives.  */
5542                 if (h->root.type == bfd_link_hash_defined)
5543                   dest = h->root.u.def.section;
5544               }
5545             else
5546               {
5547                 Elf_Internal_Sym *isym;
5548
5549                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5550                                               abfd, r_symndx);
5551                 if (isym == NULL)
5552                   return FALSE;
5553
5554                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5555               }
5556
5557             if (dest != sec)
5558               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5559           }
5560           /* Fall through.  */
5561
5562         case R_PPC64_REL24:
5563           if (h != NULL && ifunc == NULL)
5564             {
5565               /* We may need a .plt entry if the function this reloc
5566                  refers to is in a shared lib.  */
5567               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5568                 return FALSE;
5569               h->needs_plt = 1;
5570               if (h->root.root.string[0] == '.'
5571                   && h->root.root.string[1] != '\0')
5572                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5573               if (h == tga || h == dottga)
5574                 sec->has_tls_reloc = 1;
5575             }
5576           break;
5577
5578         case R_PPC64_TPREL64:
5579           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5580           if (info->shared)
5581             info->flags |= DF_STATIC_TLS;
5582           goto dotlstoc;
5583
5584         case R_PPC64_DTPMOD64:
5585           if (rel + 1 < rel_end
5586               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5587               && rel[1].r_offset == rel->r_offset + 8)
5588             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5589           else
5590             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5591           goto dotlstoc;
5592
5593         case R_PPC64_DTPREL64:
5594           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5595           if (rel != relocs
5596               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5597               && rel[-1].r_offset == rel->r_offset - 8)
5598             /* This is the second reloc of a dtpmod, dtprel pair.
5599                Don't mark with TLS_DTPREL.  */
5600             goto dodyn;
5601
5602         dotlstoc:
5603           sec->has_tls_reloc = 1;
5604           if (h != NULL)
5605             {
5606               struct ppc_link_hash_entry *eh;
5607               eh = (struct ppc_link_hash_entry *) h;
5608               eh->tls_mask |= tls_type;
5609             }
5610           else
5611             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5612                                         rel->r_addend, tls_type))
5613               return FALSE;
5614
5615           ppc64_sec = ppc64_elf_section_data (sec);
5616           if (ppc64_sec->sec_type != sec_toc)
5617             {
5618               bfd_size_type amt;
5619
5620               /* One extra to simplify get_tls_mask.  */
5621               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5622               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5623               if (ppc64_sec->u.toc.symndx == NULL)
5624                 return FALSE;
5625               amt = sec->size * sizeof (bfd_vma) / 8;
5626               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5627               if (ppc64_sec->u.toc.add == NULL)
5628                 return FALSE;
5629               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5630               ppc64_sec->sec_type = sec_toc;
5631             }
5632           BFD_ASSERT (rel->r_offset % 8 == 0);
5633           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5634           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5635
5636           /* Mark the second slot of a GD or LD entry.
5637              -1 to indicate GD and -2 to indicate LD.  */
5638           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5639             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5640           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5641             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5642           goto dodyn;
5643
5644         case R_PPC64_TPREL16:
5645         case R_PPC64_TPREL16_LO:
5646         case R_PPC64_TPREL16_HI:
5647         case R_PPC64_TPREL16_HA:
5648         case R_PPC64_TPREL16_DS:
5649         case R_PPC64_TPREL16_LO_DS:
5650         case R_PPC64_TPREL16_HIGH:
5651         case R_PPC64_TPREL16_HIGHA:
5652         case R_PPC64_TPREL16_HIGHER:
5653         case R_PPC64_TPREL16_HIGHERA:
5654         case R_PPC64_TPREL16_HIGHEST:
5655         case R_PPC64_TPREL16_HIGHESTA:
5656           if (info->shared)
5657             {
5658               info->flags |= DF_STATIC_TLS;
5659               goto dodyn;
5660             }
5661           break;
5662
5663         case R_PPC64_ADDR64:
5664           if (opd_sym_map != NULL
5665               && rel + 1 < rel_end
5666               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5667             {
5668               if (h != NULL)
5669                 {
5670                   if (h->root.root.string[0] == '.'
5671                       && h->root.root.string[1] != 0
5672                       && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5673                     ;
5674                   else
5675                     ((struct ppc_link_hash_entry *) h)->is_func = 1;
5676                 }
5677               else
5678                 {
5679                   asection *s;
5680                   Elf_Internal_Sym *isym;
5681
5682                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5683                                                 abfd, r_symndx);
5684                   if (isym == NULL)
5685                     return FALSE;
5686
5687                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5688                   if (s != NULL && s != sec)
5689                     opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5690                 }
5691             }
5692           /* Fall through.  */
5693
5694         case R_PPC64_ADDR16:
5695         case R_PPC64_ADDR16_DS:
5696         case R_PPC64_ADDR16_HA:
5697         case R_PPC64_ADDR16_HI:
5698         case R_PPC64_ADDR16_HIGH:
5699         case R_PPC64_ADDR16_HIGHA:
5700         case R_PPC64_ADDR16_HIGHER:
5701         case R_PPC64_ADDR16_HIGHERA:
5702         case R_PPC64_ADDR16_HIGHEST:
5703         case R_PPC64_ADDR16_HIGHESTA:
5704         case R_PPC64_ADDR16_LO:
5705         case R_PPC64_ADDR16_LO_DS:
5706           if (h != NULL && !info->shared && abiversion (abfd) != 1
5707               && rel->r_addend == 0)
5708             {
5709               /* We may need a .plt entry if this reloc refers to a
5710                  function in a shared lib.  */
5711               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5712                 return FALSE;
5713               h->pointer_equality_needed = 1;
5714             }
5715           /* Fall through.  */
5716
5717         case R_PPC64_REL30:
5718         case R_PPC64_REL32:
5719         case R_PPC64_REL64:
5720         case R_PPC64_ADDR14:
5721         case R_PPC64_ADDR14_BRNTAKEN:
5722         case R_PPC64_ADDR14_BRTAKEN:
5723         case R_PPC64_ADDR24:
5724         case R_PPC64_ADDR32:
5725         case R_PPC64_UADDR16:
5726         case R_PPC64_UADDR32:
5727         case R_PPC64_UADDR64:
5728         case R_PPC64_TOC:
5729           if (h != NULL && !info->shared)
5730             /* We may need a copy reloc.  */
5731             h->non_got_ref = 1;
5732
5733           /* Don't propagate .opd relocs.  */
5734           if (NO_OPD_RELOCS && opd_sym_map != NULL)
5735             break;
5736
5737           /* If we are creating a shared library, and this is a reloc
5738              against a global symbol, or a non PC relative reloc
5739              against a local symbol, then we need to copy the reloc
5740              into the shared library.  However, if we are linking with
5741              -Bsymbolic, we do not need to copy a reloc against a
5742              global symbol which is defined in an object we are
5743              including in the link (i.e., DEF_REGULAR is set).  At
5744              this point we have not seen all the input files, so it is
5745              possible that DEF_REGULAR is not set now but will be set
5746              later (it is never cleared).  In case of a weak definition,
5747              DEF_REGULAR may be cleared later by a strong definition in
5748              a shared library.  We account for that possibility below by
5749              storing information in the dyn_relocs field of the hash
5750              table entry.  A similar situation occurs when creating
5751              shared libraries and symbol visibility changes render the
5752              symbol local.
5753
5754              If on the other hand, we are creating an executable, we
5755              may need to keep relocations for symbols satisfied by a
5756              dynamic library if we manage to avoid copy relocs for the
5757              symbol.  */
5758         dodyn:
5759           if ((info->shared
5760                && (must_be_dyn_reloc (info, r_type)
5761                    || (h != NULL
5762                        && (!SYMBOLIC_BIND (info, h)
5763                            || h->root.type == bfd_link_hash_defweak
5764                            || !h->def_regular))))
5765               || (ELIMINATE_COPY_RELOCS
5766                   && !info->shared
5767                   && h != NULL
5768                   && (h->root.type == bfd_link_hash_defweak
5769                       || !h->def_regular))
5770               || (!info->shared
5771                   && ifunc != NULL))
5772             {
5773               /* We must copy these reloc types into the output file.
5774                  Create a reloc section in dynobj and make room for
5775                  this reloc.  */
5776               if (sreloc == NULL)
5777                 {
5778                   sreloc = _bfd_elf_make_dynamic_reloc_section
5779                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5780
5781                   if (sreloc == NULL)
5782                     return FALSE;
5783                 }
5784
5785               /* If this is a global symbol, we count the number of
5786                  relocations we need for this symbol.  */
5787               if (h != NULL)
5788                 {
5789                   struct elf_dyn_relocs *p;
5790                   struct elf_dyn_relocs **head;
5791
5792                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5793                   p = *head;
5794                   if (p == NULL || p->sec != sec)
5795                     {
5796                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5797                       if (p == NULL)
5798                         return FALSE;
5799                       p->next = *head;
5800                       *head = p;
5801                       p->sec = sec;
5802                       p->count = 0;
5803                       p->pc_count = 0;
5804                     }
5805                   p->count += 1;
5806                   if (!must_be_dyn_reloc (info, r_type))
5807                     p->pc_count += 1;
5808                 }
5809               else
5810                 {
5811                   /* Track dynamic relocs needed for local syms too.
5812                      We really need local syms available to do this
5813                      easily.  Oh well.  */
5814                   struct ppc_dyn_relocs *p;
5815                   struct ppc_dyn_relocs **head;
5816                   bfd_boolean is_ifunc;
5817                   asection *s;
5818                   void *vpp;
5819                   Elf_Internal_Sym *isym;
5820
5821                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5822                                                 abfd, r_symndx);
5823                   if (isym == NULL)
5824                     return FALSE;
5825
5826                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5827                   if (s == NULL)
5828                     s = sec;
5829
5830                   vpp = &elf_section_data (s)->local_dynrel;
5831                   head = (struct ppc_dyn_relocs **) vpp;
5832                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5833                   p = *head;
5834                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5835                     p = p->next;
5836                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5837                     {
5838                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5839                       if (p == NULL)
5840                         return FALSE;
5841                       p->next = *head;
5842                       *head = p;
5843                       p->sec = sec;
5844                       p->ifunc = is_ifunc;
5845                       p->count = 0;
5846                     }
5847                   p->count += 1;
5848                 }
5849             }
5850           break;
5851
5852         default:
5853           break;
5854         }
5855     }
5856
5857   return TRUE;
5858 }
5859
5860 /* Merge backend specific data from an object file to the output
5861    object file when linking.  */
5862
5863 static bfd_boolean
5864 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5865 {
5866   unsigned long iflags, oflags;
5867
5868   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5869     return TRUE;
5870
5871   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5872     return TRUE;
5873
5874   if (!_bfd_generic_verify_endian_match (ibfd, obfd))
5875     return FALSE;
5876
5877   iflags = elf_elfheader (ibfd)->e_flags;
5878   oflags = elf_elfheader (obfd)->e_flags;
5879
5880   if (iflags & ~EF_PPC64_ABI)
5881     {
5882       (*_bfd_error_handler)
5883         (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
5884       bfd_set_error (bfd_error_bad_value);
5885       return FALSE;
5886     }
5887   else if (iflags != oflags && iflags != 0)
5888     {
5889       (*_bfd_error_handler)
5890         (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
5891          ibfd, iflags, oflags);
5892       bfd_set_error (bfd_error_bad_value);
5893       return FALSE;
5894     }
5895
5896   /* Merge Tag_compatibility attributes and any common GNU ones.  */
5897   _bfd_elf_merge_object_attributes (ibfd, obfd);
5898
5899   return TRUE;
5900 }
5901
5902 static bfd_boolean
5903 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5904 {
5905   /* Print normal ELF private data.  */
5906   _bfd_elf_print_private_bfd_data (abfd, ptr);
5907
5908   if (elf_elfheader (abfd)->e_flags != 0)
5909     {
5910       FILE *file = ptr;
5911
5912       /* xgettext:c-format */
5913       fprintf (file, _("private flags = 0x%lx:"),
5914                elf_elfheader (abfd)->e_flags);
5915
5916       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5917         fprintf (file, _(" [abiv%ld]"),
5918                  elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5919       fputc ('\n', file);
5920     }
5921
5922   return TRUE;
5923 }
5924
5925 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
5926    of the code entry point, and its section, which must be in the same
5927    object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
5928
5929 static bfd_vma
5930 opd_entry_value (asection *opd_sec,
5931                  bfd_vma offset,
5932                  asection **code_sec,
5933                  bfd_vma *code_off,
5934                  bfd_boolean in_code_sec)
5935 {
5936   bfd *opd_bfd = opd_sec->owner;
5937   Elf_Internal_Rela *relocs;
5938   Elf_Internal_Rela *lo, *hi, *look;
5939   bfd_vma val;
5940
5941   /* No relocs implies we are linking a --just-symbols object, or looking
5942      at a final linked executable with addr2line or somesuch.  */
5943   if (opd_sec->reloc_count == 0)
5944     {
5945       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5946
5947       if (contents == NULL)
5948         {
5949           if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5950             return (bfd_vma) -1;
5951           ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5952         }
5953
5954       /* PR 17512: file: 64b9dfbb.  */
5955       if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5956         return (bfd_vma) -1;
5957
5958       val = bfd_get_64 (opd_bfd, contents + offset);
5959       if (code_sec != NULL)
5960         {
5961           asection *sec, *likely = NULL;
5962
5963           if (in_code_sec)
5964             {
5965               sec = *code_sec;
5966               if (sec->vma <= val
5967                   && val < sec->vma + sec->size)
5968                 likely = sec;
5969               else
5970                 val = -1;
5971             }
5972           else
5973             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5974               if (sec->vma <= val
5975                   && (sec->flags & SEC_LOAD) != 0
5976                   && (sec->flags & SEC_ALLOC) != 0)
5977                 likely = sec;
5978           if (likely != NULL)
5979             {
5980               *code_sec = likely;
5981               if (code_off != NULL)
5982                 *code_off = val - likely->vma;
5983             }
5984         }
5985       return val;
5986     }
5987
5988   BFD_ASSERT (is_ppc64_elf (opd_bfd));
5989
5990   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5991   if (relocs == NULL)
5992     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5993   /* PR 17512: file: df8e1fd6.  */
5994   if (relocs == NULL)
5995     return (bfd_vma) -1;
5996
5997   /* Go find the opd reloc at the sym address.  */
5998   lo = relocs;
5999   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6000   val = (bfd_vma) -1;
6001   while (lo < hi)
6002     {
6003       look = lo + (hi - lo) / 2;
6004       if (look->r_offset < offset)
6005         lo = look + 1;
6006       else if (look->r_offset > offset)
6007         hi = look;
6008       else
6009         {
6010           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6011
6012           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6013               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6014             {
6015               unsigned long symndx = ELF64_R_SYM (look->r_info);
6016               asection *sec = NULL;
6017
6018               if (symndx >= symtab_hdr->sh_info
6019                   && elf_sym_hashes (opd_bfd) != NULL)
6020                 {
6021                   struct elf_link_hash_entry **sym_hashes;
6022                   struct elf_link_hash_entry *rh;
6023
6024                   sym_hashes = elf_sym_hashes (opd_bfd);
6025                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
6026                   if (rh != NULL)
6027                     {
6028                       rh = elf_follow_link (rh);
6029                       BFD_ASSERT (rh->root.type == bfd_link_hash_defined
6030                                   || rh->root.type == bfd_link_hash_defweak);
6031                       val = rh->root.u.def.value;
6032                       sec = rh->root.u.def.section;
6033                       if (sec->owner != opd_bfd)
6034                         {
6035                           sec = NULL;
6036                           val = (bfd_vma) -1;
6037                         }
6038                     }
6039                 }
6040
6041               if (sec == NULL)
6042                 {
6043                   Elf_Internal_Sym *sym;
6044
6045                   if (symndx < symtab_hdr->sh_info)
6046                     {
6047                       sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6048                       if (sym == NULL)
6049                         {
6050                           size_t symcnt = symtab_hdr->sh_info;
6051                           sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6052                                                       symcnt, 0,
6053                                                       NULL, NULL, NULL);
6054                           if (sym == NULL)
6055                             break;
6056                           symtab_hdr->contents = (bfd_byte *) sym;
6057                         }
6058                       sym += symndx;
6059                     }
6060                   else
6061                     {
6062                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6063                                                   1, symndx,
6064                                                   NULL, NULL, NULL);
6065                       if (sym == NULL)
6066                         break;
6067                     }
6068                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6069                   if (sec == NULL)
6070                     break;
6071                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6072                   val = sym->st_value;
6073                 }
6074
6075               val += look->r_addend;
6076               if (code_off != NULL)
6077                 *code_off = val;
6078               if (code_sec != NULL)
6079                 {
6080                   if (in_code_sec && *code_sec != sec)
6081                     return -1;
6082                   else
6083                     *code_sec = sec;
6084                 }
6085               if (sec->output_section != NULL)
6086                 val += sec->output_section->vma + sec->output_offset;
6087             }
6088           break;
6089         }
6090     }
6091
6092   return val;
6093 }
6094
6095 /* If the ELF symbol SYM might be a function in SEC, return the
6096    function size and set *CODE_OFF to the function's entry point,
6097    otherwise return zero.  */
6098
6099 static bfd_size_type
6100 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6101                               bfd_vma *code_off)
6102 {
6103   bfd_size_type size;
6104
6105   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6106                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6107     return 0;
6108
6109   size = 0;
6110   if (!(sym->flags & BSF_SYNTHETIC))
6111     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6112
6113   if (strcmp (sym->section->name, ".opd") == 0)
6114     {
6115       if (opd_entry_value (sym->section, sym->value,
6116                            &sec, code_off, TRUE) == (bfd_vma) -1)
6117         return 0;
6118       /* An old ABI binary with dot-syms has a size of 24 on the .opd
6119          symbol.  This size has nothing to do with the code size of the
6120          function, which is what we're supposed to return, but the
6121          code size isn't available without looking up the dot-sym.
6122          However, doing that would be a waste of time particularly
6123          since elf_find_function will look at the dot-sym anyway.
6124          Now, elf_find_function will keep the largest size of any
6125          function sym found at the code address of interest, so return
6126          1 here to avoid it incorrectly caching a larger function size
6127          for a small function.  This does mean we return the wrong
6128          size for a new-ABI function of size 24, but all that does is
6129          disable caching for such functions.  */
6130       if (size == 24)
6131         size = 1;
6132     }
6133   else
6134     {
6135       if (sym->section != sec)
6136         return 0;
6137       *code_off = sym->value;
6138     }
6139   if (size == 0)
6140     size = 1;
6141   return size;
6142 }
6143
6144 /* Return true if symbol is defined in a regular object file.  */
6145
6146 static bfd_boolean
6147 is_static_defined (struct elf_link_hash_entry *h)
6148 {
6149   return ((h->root.type == bfd_link_hash_defined
6150            || h->root.type == bfd_link_hash_defweak)
6151           && h->root.u.def.section != NULL
6152           && h->root.u.def.section->output_section != NULL);
6153 }
6154
6155 /* If FDH is a function descriptor symbol, return the associated code
6156    entry symbol if it is defined.  Return NULL otherwise.  */
6157
6158 static struct ppc_link_hash_entry *
6159 defined_code_entry (struct ppc_link_hash_entry *fdh)
6160 {
6161   if (fdh->is_func_descriptor)
6162     {
6163       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6164       if (fh->elf.root.type == bfd_link_hash_defined
6165           || fh->elf.root.type == bfd_link_hash_defweak)
6166         return fh;
6167     }
6168   return NULL;
6169 }
6170
6171 /* If FH is a function code entry symbol, return the associated
6172    function descriptor symbol if it is defined.  Return NULL otherwise.  */
6173
6174 static struct ppc_link_hash_entry *
6175 defined_func_desc (struct ppc_link_hash_entry *fh)
6176 {
6177   if (fh->oh != NULL
6178       && fh->oh->is_func_descriptor)
6179     {
6180       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6181       if (fdh->elf.root.type == bfd_link_hash_defined
6182           || fdh->elf.root.type == bfd_link_hash_defweak)
6183         return fdh;
6184     }
6185   return NULL;
6186 }
6187
6188 /* Mark all our entry sym sections, both opd and code section.  */
6189
6190 static void
6191 ppc64_elf_gc_keep (struct bfd_link_info *info)
6192 {
6193   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6194   struct bfd_sym_chain *sym;
6195
6196   if (htab == NULL)
6197     return;
6198
6199   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6200     {
6201       struct ppc_link_hash_entry *eh, *fh;
6202       asection *sec;
6203
6204       eh = (struct ppc_link_hash_entry *)
6205         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6206       if (eh == NULL)
6207         continue;
6208       if (eh->elf.root.type != bfd_link_hash_defined
6209           && eh->elf.root.type != bfd_link_hash_defweak)
6210         continue;
6211
6212       fh = defined_code_entry (eh);
6213       if (fh != NULL)
6214         {
6215           sec = fh->elf.root.u.def.section;
6216           sec->flags |= SEC_KEEP;
6217         }
6218       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6219                && opd_entry_value (eh->elf.root.u.def.section,
6220                                    eh->elf.root.u.def.value,
6221                                    &sec, NULL, FALSE) != (bfd_vma) -1)
6222         sec->flags |= SEC_KEEP;
6223
6224       sec = eh->elf.root.u.def.section;
6225       sec->flags |= SEC_KEEP;
6226     }
6227 }
6228
6229 /* Mark sections containing dynamically referenced symbols.  When
6230    building shared libraries, we must assume that any visible symbol is
6231    referenced.  */
6232
6233 static bfd_boolean
6234 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6235 {
6236   struct bfd_link_info *info = (struct bfd_link_info *) inf;
6237   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6238   struct ppc_link_hash_entry *fdh;
6239   struct bfd_elf_dynamic_list *d = info->dynamic_list;
6240
6241   /* Dynamic linking info is on the func descriptor sym.  */
6242   fdh = defined_func_desc (eh);
6243   if (fdh != NULL)
6244     eh = fdh;
6245
6246   if ((eh->elf.root.type == bfd_link_hash_defined
6247        || eh->elf.root.type == bfd_link_hash_defweak)
6248       && (eh->elf.ref_dynamic
6249           || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6250               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6251               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6252               && (!info->executable
6253                   || info->export_dynamic
6254                   || (eh->elf.dynamic
6255                       && d != NULL
6256                       && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6257               && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6258                   || !bfd_hide_sym_by_version (info->version_info,
6259                                                eh->elf.root.root.string)))))
6260     {
6261       asection *code_sec;
6262       struct ppc_link_hash_entry *fh;
6263
6264       eh->elf.root.u.def.section->flags |= SEC_KEEP;
6265
6266       /* Function descriptor syms cause the associated
6267          function code sym section to be marked.  */
6268       fh = defined_code_entry (eh);
6269       if (fh != NULL)
6270         {
6271           code_sec = fh->elf.root.u.def.section;
6272           code_sec->flags |= SEC_KEEP;
6273         }
6274       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6275                && opd_entry_value (eh->elf.root.u.def.section,
6276                                    eh->elf.root.u.def.value,
6277                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
6278         code_sec->flags |= SEC_KEEP;
6279     }
6280
6281   return TRUE;
6282 }
6283
6284 /* Return the section that should be marked against GC for a given
6285    relocation.  */
6286
6287 static asection *
6288 ppc64_elf_gc_mark_hook (asection *sec,
6289                         struct bfd_link_info *info,
6290                         Elf_Internal_Rela *rel,
6291                         struct elf_link_hash_entry *h,
6292                         Elf_Internal_Sym *sym)
6293 {
6294   asection *rsec;
6295
6296   /* Syms return NULL if we're marking .opd, so we avoid marking all
6297      function sections, as all functions are referenced in .opd.  */
6298   rsec = NULL;
6299   if (get_opd_info (sec) != NULL)
6300     return rsec;
6301
6302   if (h != NULL)
6303     {
6304       enum elf_ppc64_reloc_type r_type;
6305       struct ppc_link_hash_entry *eh, *fh, *fdh;
6306
6307       r_type = ELF64_R_TYPE (rel->r_info);
6308       switch (r_type)
6309         {
6310         case R_PPC64_GNU_VTINHERIT:
6311         case R_PPC64_GNU_VTENTRY:
6312           break;
6313
6314         default:
6315           switch (h->root.type)
6316             {
6317             case bfd_link_hash_defined:
6318             case bfd_link_hash_defweak:
6319               eh = (struct ppc_link_hash_entry *) h;
6320               fdh = defined_func_desc (eh);
6321               if (fdh != NULL)
6322                 eh = fdh;
6323
6324               /* Function descriptor syms cause the associated
6325                  function code sym section to be marked.  */
6326               fh = defined_code_entry (eh);
6327               if (fh != NULL)
6328                 {
6329                   /* They also mark their opd section.  */
6330                   eh->elf.root.u.def.section->gc_mark = 1;
6331
6332                   rsec = fh->elf.root.u.def.section;
6333                 }
6334               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6335                        && opd_entry_value (eh->elf.root.u.def.section,
6336                                            eh->elf.root.u.def.value,
6337                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
6338                 eh->elf.root.u.def.section->gc_mark = 1;
6339               else
6340                 rsec = h->root.u.def.section;
6341               break;
6342
6343             case bfd_link_hash_common:
6344               rsec = h->root.u.c.p->section;
6345               break;
6346
6347             default:
6348               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6349             }
6350         }
6351     }
6352   else
6353     {
6354       struct _opd_sec_data *opd;
6355
6356       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6357       opd = get_opd_info (rsec);
6358       if (opd != NULL && opd->func_sec != NULL)
6359         {
6360           rsec->gc_mark = 1;
6361
6362           rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6363         }
6364     }
6365
6366   return rsec;
6367 }
6368
6369 /* Update the .got, .plt. and dynamic reloc reference counts for the
6370    section being removed.  */
6371
6372 static bfd_boolean
6373 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6374                          asection *sec, const Elf_Internal_Rela *relocs)
6375 {
6376   struct ppc_link_hash_table *htab;
6377   Elf_Internal_Shdr *symtab_hdr;
6378   struct elf_link_hash_entry **sym_hashes;
6379   struct got_entry **local_got_ents;
6380   const Elf_Internal_Rela *rel, *relend;
6381
6382   if (info->relocatable)
6383     return TRUE;
6384
6385   if ((sec->flags & SEC_ALLOC) == 0)
6386     return TRUE;
6387
6388   elf_section_data (sec)->local_dynrel = NULL;
6389
6390   htab = ppc_hash_table (info);
6391   if (htab == NULL)
6392     return FALSE;
6393
6394   symtab_hdr = &elf_symtab_hdr (abfd);
6395   sym_hashes = elf_sym_hashes (abfd);
6396   local_got_ents = elf_local_got_ents (abfd);
6397
6398   relend = relocs + sec->reloc_count;
6399   for (rel = relocs; rel < relend; rel++)
6400     {
6401       unsigned long r_symndx;
6402       enum elf_ppc64_reloc_type r_type;
6403       struct elf_link_hash_entry *h = NULL;
6404       unsigned char tls_type = 0;
6405
6406       r_symndx = ELF64_R_SYM (rel->r_info);
6407       r_type = ELF64_R_TYPE (rel->r_info);
6408       if (r_symndx >= symtab_hdr->sh_info)
6409         {
6410           struct ppc_link_hash_entry *eh;
6411           struct elf_dyn_relocs **pp;
6412           struct elf_dyn_relocs *p;
6413
6414           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6415           h = elf_follow_link (h);
6416           eh = (struct ppc_link_hash_entry *) h;
6417
6418           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6419             if (p->sec == sec)
6420               {
6421                 /* Everything must go for SEC.  */
6422                 *pp = p->next;
6423                 break;
6424               }
6425         }
6426
6427       if (is_branch_reloc (r_type))
6428         {
6429           struct plt_entry **ifunc = NULL;
6430           if (h != NULL)
6431             {
6432               if (h->type == STT_GNU_IFUNC)
6433                 ifunc = &h->plt.plist;
6434             }
6435           else if (local_got_ents != NULL)
6436             {
6437               struct plt_entry **local_plt = (struct plt_entry **)
6438                 (local_got_ents + symtab_hdr->sh_info);
6439               unsigned char *local_got_tls_masks = (unsigned char *)
6440                 (local_plt + symtab_hdr->sh_info);
6441               if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6442                 ifunc = local_plt + r_symndx;
6443             }
6444           if (ifunc != NULL)
6445             {
6446               struct plt_entry *ent;
6447
6448               for (ent = *ifunc; ent != NULL; ent = ent->next)
6449                 if (ent->addend == rel->r_addend)
6450                   break;
6451               if (ent == NULL)
6452                 abort ();
6453               if (ent->plt.refcount > 0)
6454                 ent->plt.refcount -= 1;
6455               continue;
6456             }
6457         }
6458
6459       switch (r_type)
6460         {
6461         case R_PPC64_GOT_TLSLD16:
6462         case R_PPC64_GOT_TLSLD16_LO:
6463         case R_PPC64_GOT_TLSLD16_HI:
6464         case R_PPC64_GOT_TLSLD16_HA:
6465           tls_type = TLS_TLS | TLS_LD;
6466           goto dogot;
6467
6468         case R_PPC64_GOT_TLSGD16:
6469         case R_PPC64_GOT_TLSGD16_LO:
6470         case R_PPC64_GOT_TLSGD16_HI:
6471         case R_PPC64_GOT_TLSGD16_HA:
6472           tls_type = TLS_TLS | TLS_GD;
6473           goto dogot;
6474
6475         case R_PPC64_GOT_TPREL16_DS:
6476         case R_PPC64_GOT_TPREL16_LO_DS:
6477         case R_PPC64_GOT_TPREL16_HI:
6478         case R_PPC64_GOT_TPREL16_HA:
6479           tls_type = TLS_TLS | TLS_TPREL;
6480           goto dogot;
6481
6482         case R_PPC64_GOT_DTPREL16_DS:
6483         case R_PPC64_GOT_DTPREL16_LO_DS:
6484         case R_PPC64_GOT_DTPREL16_HI:
6485         case R_PPC64_GOT_DTPREL16_HA:
6486           tls_type = TLS_TLS | TLS_DTPREL;
6487           goto dogot;
6488
6489         case R_PPC64_GOT16:
6490         case R_PPC64_GOT16_DS:
6491         case R_PPC64_GOT16_HA:
6492         case R_PPC64_GOT16_HI:
6493         case R_PPC64_GOT16_LO:
6494         case R_PPC64_GOT16_LO_DS:
6495         dogot:
6496           {
6497             struct got_entry *ent;
6498
6499             if (h != NULL)
6500               ent = h->got.glist;
6501             else
6502               ent = local_got_ents[r_symndx];
6503
6504             for (; ent != NULL; ent = ent->next)
6505               if (ent->addend == rel->r_addend
6506                   && ent->owner == abfd
6507                   && ent->tls_type == tls_type)
6508                 break;
6509             if (ent == NULL)
6510               abort ();
6511             if (ent->got.refcount > 0)
6512               ent->got.refcount -= 1;
6513           }
6514           break;
6515
6516         case R_PPC64_PLT16_HA:
6517         case R_PPC64_PLT16_HI:
6518         case R_PPC64_PLT16_LO:
6519         case R_PPC64_PLT32:
6520         case R_PPC64_PLT64:
6521         case R_PPC64_REL14:
6522         case R_PPC64_REL14_BRNTAKEN:
6523         case R_PPC64_REL14_BRTAKEN:
6524         case R_PPC64_REL24:
6525           if (h != NULL)
6526             {
6527               struct plt_entry *ent;
6528
6529               for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6530                 if (ent->addend == rel->r_addend)
6531                   break;
6532               if (ent != NULL && ent->plt.refcount > 0)
6533                 ent->plt.refcount -= 1;
6534             }
6535           break;
6536
6537         default:
6538           break;
6539         }
6540     }
6541   return TRUE;
6542 }
6543
6544 /* The maximum size of .sfpr.  */
6545 #define SFPR_MAX (218*4)
6546
6547 struct sfpr_def_parms
6548 {
6549   const char name[12];
6550   unsigned char lo, hi;
6551   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6552   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6553 };
6554
6555 /* Auto-generate _save*, _rest* functions in .sfpr.  */
6556
6557 static bfd_boolean
6558 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6559 {
6560   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6561   unsigned int i;
6562   size_t len = strlen (parm->name);
6563   bfd_boolean writing = FALSE;
6564   char sym[16];
6565
6566   if (htab == NULL)
6567     return FALSE;
6568
6569   memcpy (sym, parm->name, len);
6570   sym[len + 2] = 0;
6571
6572   for (i = parm->lo; i <= parm->hi; i++)
6573     {
6574       struct elf_link_hash_entry *h;
6575
6576       sym[len + 0] = i / 10 + '0';
6577       sym[len + 1] = i % 10 + '0';
6578       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6579       if (h != NULL
6580           && !h->def_regular)
6581         {
6582           h->root.type = bfd_link_hash_defined;
6583           h->root.u.def.section = htab->sfpr;
6584           h->root.u.def.value = htab->sfpr->size;
6585           h->type = STT_FUNC;
6586           h->def_regular = 1;
6587           _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6588           writing = TRUE;
6589           if (htab->sfpr->contents == NULL)
6590             {
6591               htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6592               if (htab->sfpr->contents == NULL)
6593                 return FALSE;
6594             }
6595         }
6596       if (writing)
6597         {
6598           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6599           if (i != parm->hi)
6600             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6601           else
6602             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6603           htab->sfpr->size = p - htab->sfpr->contents;
6604         }
6605     }
6606
6607   return TRUE;
6608 }
6609
6610 static bfd_byte *
6611 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6612 {
6613   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6614   return p + 4;
6615 }
6616
6617 static bfd_byte *
6618 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6619 {
6620   p = savegpr0 (abfd, p, r);
6621   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6622   p = p + 4;
6623   bfd_put_32 (abfd, BLR, p);
6624   return p + 4;
6625 }
6626
6627 static bfd_byte *
6628 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6629 {
6630   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6631   return p + 4;
6632 }
6633
6634 static bfd_byte *
6635 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6636 {
6637   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6638   p = p + 4;
6639   p = restgpr0 (abfd, p, r);
6640   bfd_put_32 (abfd, MTLR_R0, p);
6641   p = p + 4;
6642   if (r == 29)
6643     {
6644       p = restgpr0 (abfd, p, 30);
6645       p = restgpr0 (abfd, p, 31);
6646     }
6647   bfd_put_32 (abfd, BLR, p);
6648   return p + 4;
6649 }
6650
6651 static bfd_byte *
6652 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6653 {
6654   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6655   return p + 4;
6656 }
6657
6658 static bfd_byte *
6659 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6660 {
6661   p = savegpr1 (abfd, p, r);
6662   bfd_put_32 (abfd, BLR, p);
6663   return p + 4;
6664 }
6665
6666 static bfd_byte *
6667 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6668 {
6669   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6670   return p + 4;
6671 }
6672
6673 static bfd_byte *
6674 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6675 {
6676   p = restgpr1 (abfd, p, r);
6677   bfd_put_32 (abfd, BLR, p);
6678   return p + 4;
6679 }
6680
6681 static bfd_byte *
6682 savefpr (bfd *abfd, bfd_byte *p, int r)
6683 {
6684   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6685   return p + 4;
6686 }
6687
6688 static bfd_byte *
6689 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6690 {
6691   p = savefpr (abfd, p, r);
6692   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6693   p = p + 4;
6694   bfd_put_32 (abfd, BLR, p);
6695   return p + 4;
6696 }
6697
6698 static bfd_byte *
6699 restfpr (bfd *abfd, bfd_byte *p, int r)
6700 {
6701   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6702   return p + 4;
6703 }
6704
6705 static bfd_byte *
6706 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6707 {
6708   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6709   p = p + 4;
6710   p = restfpr (abfd, p, r);
6711   bfd_put_32 (abfd, MTLR_R0, p);
6712   p = p + 4;
6713   if (r == 29)
6714     {
6715       p = restfpr (abfd, p, 30);
6716       p = restfpr (abfd, p, 31);
6717     }
6718   bfd_put_32 (abfd, BLR, p);
6719   return p + 4;
6720 }
6721
6722 static bfd_byte *
6723 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6724 {
6725   p = savefpr (abfd, p, r);
6726   bfd_put_32 (abfd, BLR, p);
6727   return p + 4;
6728 }
6729
6730 static bfd_byte *
6731 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6732 {
6733   p = restfpr (abfd, p, r);
6734   bfd_put_32 (abfd, BLR, p);
6735   return p + 4;
6736 }
6737
6738 static bfd_byte *
6739 savevr (bfd *abfd, bfd_byte *p, int r)
6740 {
6741   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6742   p = p + 4;
6743   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6744   return p + 4;
6745 }
6746
6747 static bfd_byte *
6748 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6749 {
6750   p = savevr (abfd, p, r);
6751   bfd_put_32 (abfd, BLR, p);
6752   return p + 4;
6753 }
6754
6755 static bfd_byte *
6756 restvr (bfd *abfd, bfd_byte *p, int r)
6757 {
6758   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6759   p = p + 4;
6760   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6761   return p + 4;
6762 }
6763
6764 static bfd_byte *
6765 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6766 {
6767   p = restvr (abfd, p, r);
6768   bfd_put_32 (abfd, BLR, p);
6769   return p + 4;
6770 }
6771
6772 /* Called via elf_link_hash_traverse to transfer dynamic linking
6773    information on function code symbol entries to their corresponding
6774    function descriptor symbol entries.  */
6775
6776 static bfd_boolean
6777 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6778 {
6779   struct bfd_link_info *info;
6780   struct ppc_link_hash_table *htab;
6781   struct plt_entry *ent;
6782   struct ppc_link_hash_entry *fh;
6783   struct ppc_link_hash_entry *fdh;
6784   bfd_boolean force_local;
6785
6786   fh = (struct ppc_link_hash_entry *) h;
6787   if (fh->elf.root.type == bfd_link_hash_indirect)
6788     return TRUE;
6789
6790   info = inf;
6791   htab = ppc_hash_table (info);
6792   if (htab == NULL)
6793     return FALSE;
6794
6795   /* Resolve undefined references to dot-symbols as the value
6796      in the function descriptor, if we have one in a regular object.
6797      This is to satisfy cases like ".quad .foo".  Calls to functions
6798      in dynamic objects are handled elsewhere.  */
6799   if (fh->elf.root.type == bfd_link_hash_undefweak
6800       && fh->was_undefined
6801       && (fdh = defined_func_desc (fh)) != NULL
6802       && get_opd_info (fdh->elf.root.u.def.section) != NULL
6803       && opd_entry_value (fdh->elf.root.u.def.section,
6804                           fdh->elf.root.u.def.value,
6805                           &fh->elf.root.u.def.section,
6806                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6807     {
6808       fh->elf.root.type = fdh->elf.root.type;
6809       fh->elf.forced_local = 1;
6810       fh->elf.def_regular = fdh->elf.def_regular;
6811       fh->elf.def_dynamic = fdh->elf.def_dynamic;
6812     }
6813
6814   /* If this is a function code symbol, transfer dynamic linking
6815      information to the function descriptor symbol.  */
6816   if (!fh->is_func)
6817     return TRUE;
6818
6819   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6820     if (ent->plt.refcount > 0)
6821       break;
6822   if (ent == NULL
6823       || fh->elf.root.root.string[0] != '.'
6824       || fh->elf.root.root.string[1] == '\0')
6825     return TRUE;
6826
6827   /* Find the corresponding function descriptor symbol.  Create it
6828      as undefined if necessary.  */
6829
6830   fdh = lookup_fdh (fh, htab);
6831   if (fdh == NULL
6832       && !info->executable
6833       && (fh->elf.root.type == bfd_link_hash_undefined
6834           || fh->elf.root.type == bfd_link_hash_undefweak))
6835     {
6836       fdh = make_fdh (info, fh);
6837       if (fdh == NULL)
6838         return FALSE;
6839     }
6840
6841   /* Fake function descriptors are made undefweak.  If the function
6842      code symbol is strong undefined, make the fake sym the same.
6843      If the function code symbol is defined, then force the fake
6844      descriptor local;  We can't support overriding of symbols in a
6845      shared library on a fake descriptor.  */
6846
6847   if (fdh != NULL
6848       && fdh->fake
6849       && fdh->elf.root.type == bfd_link_hash_undefweak)
6850     {
6851       if (fh->elf.root.type == bfd_link_hash_undefined)
6852         {
6853           fdh->elf.root.type = bfd_link_hash_undefined;
6854           bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6855         }
6856       else if (fh->elf.root.type == bfd_link_hash_defined
6857                || fh->elf.root.type == bfd_link_hash_defweak)
6858         {
6859           _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6860         }
6861     }
6862
6863   if (fdh != NULL
6864       && !fdh->elf.forced_local
6865       && (!info->executable
6866           || fdh->elf.def_dynamic
6867           || fdh->elf.ref_dynamic
6868           || (fdh->elf.root.type == bfd_link_hash_undefweak
6869               && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6870     {
6871       if (fdh->elf.dynindx == -1)
6872         if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6873           return FALSE;
6874       fdh->elf.ref_regular |= fh->elf.ref_regular;
6875       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6876       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6877       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6878       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6879         {
6880           move_plt_plist (fh, fdh);
6881           fdh->elf.needs_plt = 1;
6882         }
6883       fdh->is_func_descriptor = 1;
6884       fdh->oh = fh;
6885       fh->oh = fdh;
6886     }
6887
6888   /* Now that the info is on the function descriptor, clear the
6889      function code sym info.  Any function code syms for which we
6890      don't have a definition in a regular file, we force local.
6891      This prevents a shared library from exporting syms that have
6892      been imported from another library.  Function code syms that
6893      are really in the library we must leave global to prevent the
6894      linker dragging in a definition from a static library.  */
6895   force_local = (!fh->elf.def_regular
6896                  || fdh == NULL
6897                  || !fdh->elf.def_regular
6898                  || fdh->elf.forced_local);
6899   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6900
6901   return TRUE;
6902 }
6903
6904 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
6905    this hook to a) provide some gcc support functions, and b) transfer
6906    dynamic linking information gathered so far on function code symbol
6907    entries, to their corresponding function descriptor symbol entries.  */
6908
6909 static bfd_boolean
6910 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6911                             struct bfd_link_info *info)
6912 {
6913   struct ppc_link_hash_table *htab;
6914   unsigned int i;
6915   static const struct sfpr_def_parms funcs[] =
6916     {
6917       { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6918       { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6919       { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6920       { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6921       { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6922       { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6923       { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6924       { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6925       { "._savef", 14, 31, savefpr, savefpr1_tail },
6926       { "._restf", 14, 31, restfpr, restfpr1_tail },
6927       { "_savevr_", 20, 31, savevr, savevr_tail },
6928       { "_restvr_", 20, 31, restvr, restvr_tail }
6929     };
6930
6931   htab = ppc_hash_table (info);
6932   if (htab == NULL)
6933     return FALSE;
6934
6935   if (!info->relocatable
6936       && htab->elf.hgot != NULL)
6937     {
6938       _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6939       /* Make .TOC. defined so as to prevent it being made dynamic.
6940          The wrong value here is fixed later in ppc64_elf_set_toc.  */
6941       htab->elf.hgot->type = STT_OBJECT;
6942       htab->elf.hgot->root.type = bfd_link_hash_defined;
6943       htab->elf.hgot->root.u.def.value = 0;
6944       htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6945       htab->elf.hgot->def_regular = 1;
6946       htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
6947                                | STV_HIDDEN);
6948     }
6949
6950   if (htab->sfpr == NULL)
6951     /* We don't have any relocs.  */
6952     return TRUE;
6953
6954   /* Provide any missing _save* and _rest* functions.  */
6955   htab->sfpr->size = 0;
6956   if (htab->params->save_restore_funcs)
6957     for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6958       if (!sfpr_define (info, &funcs[i]))
6959         return FALSE;
6960
6961   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6962
6963   if (htab->sfpr->size == 0)
6964     htab->sfpr->flags |= SEC_EXCLUDE;
6965
6966   return TRUE;
6967 }
6968
6969 /* Return true if we have dynamic relocs that apply to read-only sections.  */
6970
6971 static bfd_boolean
6972 readonly_dynrelocs (struct elf_link_hash_entry *h)
6973 {
6974   struct ppc_link_hash_entry *eh;
6975   struct elf_dyn_relocs *p;
6976
6977   eh = (struct ppc_link_hash_entry *) h;
6978   for (p = eh->dyn_relocs; p != NULL; p = p->next)
6979     {
6980       asection *s = p->sec->output_section;
6981
6982       if (s != NULL && (s->flags & SEC_READONLY) != 0)
6983         return TRUE;
6984     }
6985   return FALSE;
6986 }
6987
6988 /* Adjust a symbol defined by a dynamic object and referenced by a
6989    regular object.  The current definition is in some section of the
6990    dynamic object, but we're not including those sections.  We have to
6991    change the definition to something the rest of the link can
6992    understand.  */
6993
6994 static bfd_boolean
6995 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6996                                  struct elf_link_hash_entry *h)
6997 {
6998   struct ppc_link_hash_table *htab;
6999   asection *s;
7000
7001   htab = ppc_hash_table (info);
7002   if (htab == NULL)
7003     return FALSE;
7004
7005   /* Deal with function syms.  */
7006   if (h->type == STT_FUNC
7007       || h->type == STT_GNU_IFUNC
7008       || h->needs_plt)
7009     {
7010       /* Clear procedure linkage table information for any symbol that
7011          won't need a .plt entry.  */
7012       struct plt_entry *ent;
7013       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7014         if (ent->plt.refcount > 0)
7015           break;
7016       if (ent == NULL
7017           || (h->type != STT_GNU_IFUNC
7018               && (SYMBOL_CALLS_LOCAL (info, h)
7019                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7020                       && h->root.type == bfd_link_hash_undefweak))))
7021         {
7022           h->plt.plist = NULL;
7023           h->needs_plt = 0;
7024           h->pointer_equality_needed = 0;
7025         }
7026       else if (abiversion (info->output_bfd) == 2)
7027         {
7028           /* Taking a function's address in a read/write section
7029              doesn't require us to define the function symbol in the
7030              executable on a global entry stub.  A dynamic reloc can
7031              be used instead.  */
7032           if (h->pointer_equality_needed
7033               && h->type != STT_GNU_IFUNC
7034               && !readonly_dynrelocs (h))
7035             {
7036               h->pointer_equality_needed = 0;
7037               h->non_got_ref = 0;
7038             }
7039
7040           /* After adjust_dynamic_symbol, non_got_ref set in the
7041              non-shared case means that we have allocated space in
7042              .dynbss for the symbol and thus dyn_relocs for this
7043              symbol should be discarded.
7044              If we get here we know we are making a PLT entry for this
7045              symbol, and in an executable we'd normally resolve
7046              relocations against this symbol to the PLT entry.  Allow
7047              dynamic relocs if the reference is weak, and the dynamic
7048              relocs will not cause text relocation.  */
7049           else if (!h->ref_regular_nonweak
7050                    && h->non_got_ref
7051                    && h->type != STT_GNU_IFUNC
7052                    && !readonly_dynrelocs (h))
7053             h->non_got_ref = 0;
7054
7055           /* If making a plt entry, then we don't need copy relocs.  */
7056           return TRUE;
7057         }
7058     }
7059   else
7060     h->plt.plist = NULL;
7061
7062   /* If this is a weak symbol, and there is a real definition, the
7063      processor independent code will have arranged for us to see the
7064      real definition first, and we can just use the same value.  */
7065   if (h->u.weakdef != NULL)
7066     {
7067       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7068                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
7069       h->root.u.def.section = h->u.weakdef->root.u.def.section;
7070       h->root.u.def.value = h->u.weakdef->root.u.def.value;
7071       if (ELIMINATE_COPY_RELOCS)
7072         h->non_got_ref = h->u.weakdef->non_got_ref;
7073       return TRUE;
7074     }
7075
7076   /* If we are creating a shared library, we must presume that the
7077      only references to the symbol are via the global offset table.
7078      For such cases we need not do anything here; the relocations will
7079      be handled correctly by relocate_section.  */
7080   if (info->shared)
7081     return TRUE;
7082
7083   /* If there are no references to this symbol that do not use the
7084      GOT, we don't need to generate a copy reloc.  */
7085   if (!h->non_got_ref)
7086     return TRUE;
7087
7088   /* Don't generate a copy reloc for symbols defined in the executable.  */
7089   if (!h->def_dynamic || !h->ref_regular || h->def_regular)
7090     return TRUE;
7091
7092   /* If -z nocopyreloc was given, don't generate them either.  */
7093   if (info->nocopyreloc)
7094     {
7095       h->non_got_ref = 0;
7096       return TRUE;
7097     }
7098
7099   /* If we didn't find any dynamic relocs in read-only sections, then
7100      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
7101   if (ELIMINATE_COPY_RELOCS && !readonly_dynrelocs (h))
7102     {
7103       h->non_got_ref = 0;
7104       return TRUE;
7105     }
7106
7107   /* Protected variables do not work with .dynbss.  The copy in
7108      .dynbss won't be used by the shared library with the protected
7109      definition for the variable.  Text relocations are preferable
7110      to an incorrect program.  */
7111   if (h->protected_def)
7112     {
7113       h->non_got_ref = 0;
7114       return TRUE;
7115     }
7116
7117   if (h->plt.plist != NULL)
7118     {
7119       /* We should never get here, but unfortunately there are versions
7120          of gcc out there that improperly (for this ABI) put initialized
7121          function pointers, vtable refs and suchlike in read-only
7122          sections.  Allow them to proceed, but warn that this might
7123          break at runtime.  */
7124       info->callbacks->einfo
7125         (_("%P: copy reloc against `%T' requires lazy plt linking; "
7126            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7127          h->root.root.string);
7128     }
7129
7130   /* This is a reference to a symbol defined by a dynamic object which
7131      is not a function.  */
7132
7133   /* We must allocate the symbol in our .dynbss section, which will
7134      become part of the .bss section of the executable.  There will be
7135      an entry for this symbol in the .dynsym section.  The dynamic
7136      object will contain position independent code, so all references
7137      from the dynamic object to this symbol will go through the global
7138      offset table.  The dynamic linker will use the .dynsym entry to
7139      determine the address it must put in the global offset table, so
7140      both the dynamic object and the regular object will refer to the
7141      same memory location for the variable.  */
7142
7143   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7144      to copy the initial value out of the dynamic object and into the
7145      runtime process image.  We need to remember the offset into the
7146      .rela.bss section we are going to use.  */
7147   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7148     {
7149       htab->relbss->size += sizeof (Elf64_External_Rela);
7150       h->needs_copy = 1;
7151     }
7152
7153   s = htab->dynbss;
7154
7155   return _bfd_elf_adjust_dynamic_copy (info, h, s);
7156 }
7157
7158 /* If given a function descriptor symbol, hide both the function code
7159    sym and the descriptor.  */
7160 static void
7161 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7162                        struct elf_link_hash_entry *h,
7163                        bfd_boolean force_local)
7164 {
7165   struct ppc_link_hash_entry *eh;
7166   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7167
7168   eh = (struct ppc_link_hash_entry *) h;
7169   if (eh->is_func_descriptor)
7170     {
7171       struct ppc_link_hash_entry *fh = eh->oh;
7172
7173       if (fh == NULL)
7174         {
7175           const char *p, *q;
7176           struct ppc_link_hash_table *htab;
7177           char save;
7178
7179           /* We aren't supposed to use alloca in BFD because on
7180              systems which do not have alloca the version in libiberty
7181              calls xmalloc, which might cause the program to crash
7182              when it runs out of memory.  This function doesn't have a
7183              return status, so there's no way to gracefully return an
7184              error.  So cheat.  We know that string[-1] can be safely
7185              accessed;  It's either a string in an ELF string table,
7186              or allocated in an objalloc structure.  */
7187
7188           p = eh->elf.root.root.string - 1;
7189           save = *p;
7190           *(char *) p = '.';
7191           htab = ppc_hash_table (info);
7192           if (htab == NULL)
7193             return;
7194
7195           fh = (struct ppc_link_hash_entry *)
7196             elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7197           *(char *) p = save;
7198
7199           /* Unfortunately, if it so happens that the string we were
7200              looking for was allocated immediately before this string,
7201              then we overwrote the string terminator.  That's the only
7202              reason the lookup should fail.  */
7203           if (fh == NULL)
7204             {
7205               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7206               while (q >= eh->elf.root.root.string && *q == *p)
7207                 --q, --p;
7208               if (q < eh->elf.root.root.string && *p == '.')
7209                 fh = (struct ppc_link_hash_entry *)
7210                   elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7211             }
7212           if (fh != NULL)
7213             {
7214               eh->oh = fh;
7215               fh->oh = eh;
7216             }
7217         }
7218       if (fh != NULL)
7219         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7220     }
7221 }
7222
7223 static bfd_boolean
7224 get_sym_h (struct elf_link_hash_entry **hp,
7225            Elf_Internal_Sym **symp,
7226            asection **symsecp,
7227            unsigned char **tls_maskp,
7228            Elf_Internal_Sym **locsymsp,
7229            unsigned long r_symndx,
7230            bfd *ibfd)
7231 {
7232   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7233
7234   if (r_symndx >= symtab_hdr->sh_info)
7235     {
7236       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7237       struct elf_link_hash_entry *h;
7238
7239       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7240       h = elf_follow_link (h);
7241
7242       if (hp != NULL)
7243         *hp = h;
7244
7245       if (symp != NULL)
7246         *symp = NULL;
7247
7248       if (symsecp != NULL)
7249         {
7250           asection *symsec = NULL;
7251           if (h->root.type == bfd_link_hash_defined
7252               || h->root.type == bfd_link_hash_defweak)
7253             symsec = h->root.u.def.section;
7254           *symsecp = symsec;
7255         }
7256
7257       if (tls_maskp != NULL)
7258         {
7259           struct ppc_link_hash_entry *eh;
7260
7261           eh = (struct ppc_link_hash_entry *) h;
7262           *tls_maskp = &eh->tls_mask;
7263         }
7264     }
7265   else
7266     {
7267       Elf_Internal_Sym *sym;
7268       Elf_Internal_Sym *locsyms = *locsymsp;
7269
7270       if (locsyms == NULL)
7271         {
7272           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7273           if (locsyms == NULL)
7274             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7275                                             symtab_hdr->sh_info,
7276                                             0, NULL, NULL, NULL);
7277           if (locsyms == NULL)
7278             return FALSE;
7279           *locsymsp = locsyms;
7280         }
7281       sym = locsyms + r_symndx;
7282
7283       if (hp != NULL)
7284         *hp = NULL;
7285
7286       if (symp != NULL)
7287         *symp = sym;
7288
7289       if (symsecp != NULL)
7290         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7291
7292       if (tls_maskp != NULL)
7293         {
7294           struct got_entry **lgot_ents;
7295           unsigned char *tls_mask;
7296
7297           tls_mask = NULL;
7298           lgot_ents = elf_local_got_ents (ibfd);
7299           if (lgot_ents != NULL)
7300             {
7301               struct plt_entry **local_plt = (struct plt_entry **)
7302                 (lgot_ents + symtab_hdr->sh_info);
7303               unsigned char *lgot_masks = (unsigned char *)
7304                 (local_plt + symtab_hdr->sh_info);
7305               tls_mask = &lgot_masks[r_symndx];
7306             }
7307           *tls_maskp = tls_mask;
7308         }
7309     }
7310   return TRUE;
7311 }
7312
7313 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
7314    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7315    type suitable for optimization, and 1 otherwise.  */
7316
7317 static int
7318 get_tls_mask (unsigned char **tls_maskp,
7319               unsigned long *toc_symndx,
7320               bfd_vma *toc_addend,
7321               Elf_Internal_Sym **locsymsp,
7322               const Elf_Internal_Rela *rel,
7323               bfd *ibfd)
7324 {
7325   unsigned long r_symndx;
7326   int next_r;
7327   struct elf_link_hash_entry *h;
7328   Elf_Internal_Sym *sym;
7329   asection *sec;
7330   bfd_vma off;
7331
7332   r_symndx = ELF64_R_SYM (rel->r_info);
7333   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7334     return 0;
7335
7336   if ((*tls_maskp != NULL && **tls_maskp != 0)
7337       || sec == NULL
7338       || ppc64_elf_section_data (sec) == NULL
7339       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7340     return 1;
7341
7342   /* Look inside a TOC section too.  */
7343   if (h != NULL)
7344     {
7345       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7346       off = h->root.u.def.value;
7347     }
7348   else
7349     off = sym->st_value;
7350   off += rel->r_addend;
7351   BFD_ASSERT (off % 8 == 0);
7352   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7353   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7354   if (toc_symndx != NULL)
7355     *toc_symndx = r_symndx;
7356   if (toc_addend != NULL)
7357     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7358   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7359     return 0;
7360   if ((h == NULL || is_static_defined (h))
7361       && (next_r == -1 || next_r == -2))
7362     return 1 - next_r;
7363   return 1;
7364 }
7365
7366 /* Find (or create) an entry in the tocsave hash table.  */
7367
7368 static struct tocsave_entry *
7369 tocsave_find (struct ppc_link_hash_table *htab,
7370               enum insert_option insert,
7371               Elf_Internal_Sym **local_syms,
7372               const Elf_Internal_Rela *irela,
7373               bfd *ibfd)
7374 {
7375   unsigned long r_indx;
7376   struct elf_link_hash_entry *h;
7377   Elf_Internal_Sym *sym;
7378   struct tocsave_entry ent, *p;
7379   hashval_t hash;
7380   struct tocsave_entry **slot;
7381
7382   r_indx = ELF64_R_SYM (irela->r_info);
7383   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7384     return NULL;
7385   if (ent.sec == NULL || ent.sec->output_section == NULL)
7386     {
7387       (*_bfd_error_handler)
7388         (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7389       return NULL;
7390     }
7391
7392   if (h != NULL)
7393     ent.offset = h->root.u.def.value;
7394   else
7395     ent.offset = sym->st_value;
7396   ent.offset += irela->r_addend;
7397
7398   hash = tocsave_htab_hash (&ent);
7399   slot = ((struct tocsave_entry **)
7400           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7401   if (slot == NULL)
7402     return NULL;
7403
7404   if (*slot == NULL)
7405     {
7406       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7407       if (p == NULL)
7408         return NULL;
7409       *p = ent;
7410       *slot = p;
7411     }
7412   return *slot;
7413 }
7414
7415 /* Adjust all global syms defined in opd sections.  In gcc generated
7416    code for the old ABI, these will already have been done.  */
7417
7418 static bfd_boolean
7419 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7420 {
7421   struct ppc_link_hash_entry *eh;
7422   asection *sym_sec;
7423   struct _opd_sec_data *opd;
7424
7425   if (h->root.type == bfd_link_hash_indirect)
7426     return TRUE;
7427
7428   if (h->root.type != bfd_link_hash_defined
7429       && h->root.type != bfd_link_hash_defweak)
7430     return TRUE;
7431
7432   eh = (struct ppc_link_hash_entry *) h;
7433   if (eh->adjust_done)
7434     return TRUE;
7435
7436   sym_sec = eh->elf.root.u.def.section;
7437   opd = get_opd_info (sym_sec);
7438   if (opd != NULL && opd->adjust != NULL)
7439     {
7440       long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7441       if (adjust == -1)
7442         {
7443           /* This entry has been deleted.  */
7444           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7445           if (dsec == NULL)
7446             {
7447               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7448                 if (discarded_section (dsec))
7449                   {
7450                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7451                     break;
7452                   }
7453             }
7454           eh->elf.root.u.def.value = 0;
7455           eh->elf.root.u.def.section = dsec;
7456         }
7457       else
7458         eh->elf.root.u.def.value += adjust;
7459       eh->adjust_done = 1;
7460     }
7461   return TRUE;
7462 }
7463
7464 /* Handles decrementing dynamic reloc counts for the reloc specified by
7465    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
7466    have already been determined.  */
7467
7468 static bfd_boolean
7469 dec_dynrel_count (bfd_vma r_info,
7470                   asection *sec,
7471                   struct bfd_link_info *info,
7472                   Elf_Internal_Sym **local_syms,
7473                   struct elf_link_hash_entry *h,
7474                   Elf_Internal_Sym *sym)
7475 {
7476   enum elf_ppc64_reloc_type r_type;
7477   asection *sym_sec = NULL;
7478
7479   /* Can this reloc be dynamic?  This switch, and later tests here
7480      should be kept in sync with the code in check_relocs.  */
7481   r_type = ELF64_R_TYPE (r_info);
7482   switch (r_type)
7483     {
7484     default:
7485       return TRUE;
7486
7487     case R_PPC64_TPREL16:
7488     case R_PPC64_TPREL16_LO:
7489     case R_PPC64_TPREL16_HI:
7490     case R_PPC64_TPREL16_HA:
7491     case R_PPC64_TPREL16_DS:
7492     case R_PPC64_TPREL16_LO_DS:
7493     case R_PPC64_TPREL16_HIGH:
7494     case R_PPC64_TPREL16_HIGHA:
7495     case R_PPC64_TPREL16_HIGHER:
7496     case R_PPC64_TPREL16_HIGHERA:
7497     case R_PPC64_TPREL16_HIGHEST:
7498     case R_PPC64_TPREL16_HIGHESTA:
7499       if (!info->shared)
7500         return TRUE;
7501
7502     case R_PPC64_TPREL64:
7503     case R_PPC64_DTPMOD64:
7504     case R_PPC64_DTPREL64:
7505     case R_PPC64_ADDR64:
7506     case R_PPC64_REL30:
7507     case R_PPC64_REL32:
7508     case R_PPC64_REL64:
7509     case R_PPC64_ADDR14:
7510     case R_PPC64_ADDR14_BRNTAKEN:
7511     case R_PPC64_ADDR14_BRTAKEN:
7512     case R_PPC64_ADDR16:
7513     case R_PPC64_ADDR16_DS:
7514     case R_PPC64_ADDR16_HA:
7515     case R_PPC64_ADDR16_HI:
7516     case R_PPC64_ADDR16_HIGH:
7517     case R_PPC64_ADDR16_HIGHA:
7518     case R_PPC64_ADDR16_HIGHER:
7519     case R_PPC64_ADDR16_HIGHERA:
7520     case R_PPC64_ADDR16_HIGHEST:
7521     case R_PPC64_ADDR16_HIGHESTA:
7522     case R_PPC64_ADDR16_LO:
7523     case R_PPC64_ADDR16_LO_DS:
7524     case R_PPC64_ADDR24:
7525     case R_PPC64_ADDR32:
7526     case R_PPC64_UADDR16:
7527     case R_PPC64_UADDR32:
7528     case R_PPC64_UADDR64:
7529     case R_PPC64_TOC:
7530       break;
7531     }
7532
7533   if (local_syms != NULL)
7534     {
7535       unsigned long r_symndx;
7536       bfd *ibfd = sec->owner;
7537
7538       r_symndx = ELF64_R_SYM (r_info);
7539       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7540         return FALSE;
7541     }
7542
7543   if ((info->shared
7544        && (must_be_dyn_reloc (info, r_type)
7545            || (h != NULL
7546                && (!SYMBOLIC_BIND (info, h)
7547                    || h->root.type == bfd_link_hash_defweak
7548                    || !h->def_regular))))
7549       || (ELIMINATE_COPY_RELOCS
7550           && !info->shared
7551           && h != NULL
7552           && (h->root.type == bfd_link_hash_defweak
7553               || !h->def_regular)))
7554     ;
7555   else
7556     return TRUE;
7557
7558   if (h != NULL)
7559     {
7560       struct elf_dyn_relocs *p;
7561       struct elf_dyn_relocs **pp;
7562       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7563
7564       /* elf_gc_sweep may have already removed all dyn relocs associated
7565          with local syms for a given section.  Also, symbol flags are
7566          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
7567          report a dynreloc miscount.  */
7568       if (*pp == NULL && info->gc_sections)
7569         return TRUE;
7570
7571       while ((p = *pp) != NULL)
7572         {
7573           if (p->sec == sec)
7574             {
7575               if (!must_be_dyn_reloc (info, r_type))
7576                 p->pc_count -= 1;
7577               p->count -= 1;
7578               if (p->count == 0)
7579                 *pp = p->next;
7580               return TRUE;
7581             }
7582           pp = &p->next;
7583         }
7584     }
7585   else
7586     {
7587       struct ppc_dyn_relocs *p;
7588       struct ppc_dyn_relocs **pp;
7589       void *vpp;
7590       bfd_boolean is_ifunc;
7591
7592       if (local_syms == NULL)
7593         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7594       if (sym_sec == NULL)
7595         sym_sec = sec;
7596
7597       vpp = &elf_section_data (sym_sec)->local_dynrel;
7598       pp = (struct ppc_dyn_relocs **) vpp;
7599
7600       if (*pp == NULL && info->gc_sections)
7601         return TRUE;
7602
7603       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7604       while ((p = *pp) != NULL)
7605         {
7606           if (p->sec == sec && p->ifunc == is_ifunc)
7607             {
7608               p->count -= 1;
7609               if (p->count == 0)
7610                 *pp = p->next;
7611               return TRUE;
7612             }
7613           pp = &p->next;
7614         }
7615     }
7616
7617   info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7618                           sec->owner, sec);
7619   bfd_set_error (bfd_error_bad_value);
7620   return FALSE;
7621 }
7622
7623 /* Remove unused Official Procedure Descriptor entries.  Currently we
7624    only remove those associated with functions in discarded link-once
7625    sections, or weakly defined functions that have been overridden.  It
7626    would be possible to remove many more entries for statically linked
7627    applications.  */
7628
7629 bfd_boolean
7630 ppc64_elf_edit_opd (struct bfd_link_info *info)
7631 {
7632   bfd *ibfd;
7633   bfd_boolean some_edited = FALSE;
7634   asection *need_pad = NULL;
7635   struct ppc_link_hash_table *htab;
7636
7637   htab = ppc_hash_table (info);
7638   if (htab == NULL)
7639     return FALSE;
7640
7641   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7642     {
7643       asection *sec;
7644       Elf_Internal_Rela *relstart, *rel, *relend;
7645       Elf_Internal_Shdr *symtab_hdr;
7646       Elf_Internal_Sym *local_syms;
7647       struct _opd_sec_data *opd;
7648       bfd_boolean need_edit, add_aux_fields, broken;
7649       bfd_size_type cnt_16b = 0;
7650
7651       if (!is_ppc64_elf (ibfd))
7652         continue;
7653
7654       sec = bfd_get_section_by_name (ibfd, ".opd");
7655       if (sec == NULL || sec->size == 0)
7656         continue;
7657
7658       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7659         continue;
7660
7661       if (sec->output_section == bfd_abs_section_ptr)
7662         continue;
7663
7664       /* Look through the section relocs.  */
7665       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7666         continue;
7667
7668       local_syms = NULL;
7669       symtab_hdr = &elf_symtab_hdr (ibfd);
7670
7671       /* Read the relocations.  */
7672       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7673                                             info->keep_memory);
7674       if (relstart == NULL)
7675         return FALSE;
7676
7677       /* First run through the relocs to check they are sane, and to
7678          determine whether we need to edit this opd section.  */
7679       need_edit = FALSE;
7680       broken = FALSE;
7681       need_pad = sec;
7682       relend = relstart + sec->reloc_count;
7683       for (rel = relstart; rel < relend; )
7684         {
7685           enum elf_ppc64_reloc_type r_type;
7686           unsigned long r_symndx;
7687           asection *sym_sec;
7688           struct elf_link_hash_entry *h;
7689           Elf_Internal_Sym *sym;
7690           bfd_vma offset;
7691
7692           /* .opd contains an array of 16 or 24 byte entries.  We're
7693              only interested in the reloc pointing to a function entry
7694              point.  */
7695           offset = rel->r_offset;
7696           if (rel + 1 == relend
7697               || rel[1].r_offset != offset + 8)
7698             {
7699               /* If someone messes with .opd alignment then after a
7700                  "ld -r" we might have padding in the middle of .opd.
7701                  Also, there's nothing to prevent someone putting
7702                  something silly in .opd with the assembler.  No .opd
7703                  optimization for them!  */
7704             broken_opd:
7705               (*_bfd_error_handler)
7706                 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7707               broken = TRUE;
7708               break;
7709             }
7710
7711           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7712               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7713             {
7714               (*_bfd_error_handler)
7715                 (_("%B: unexpected reloc type %u in .opd section"),
7716                  ibfd, r_type);
7717               broken = TRUE;
7718               break;
7719             }
7720
7721           r_symndx = ELF64_R_SYM (rel->r_info);
7722           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7723                           r_symndx, ibfd))
7724             goto error_ret;
7725
7726           if (sym_sec == NULL || sym_sec->owner == NULL)
7727             {
7728               const char *sym_name;
7729               if (h != NULL)
7730                 sym_name = h->root.root.string;
7731               else
7732                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7733                                              sym_sec);
7734
7735               (*_bfd_error_handler)
7736                 (_("%B: undefined sym `%s' in .opd section"),
7737                  ibfd, sym_name);
7738               broken = TRUE;
7739               break;
7740             }
7741
7742           /* opd entries are always for functions defined in the
7743              current input bfd.  If the symbol isn't defined in the
7744              input bfd, then we won't be using the function in this
7745              bfd;  It must be defined in a linkonce section in another
7746              bfd, or is weak.  It's also possible that we are
7747              discarding the function due to a linker script /DISCARD/,
7748              which we test for via the output_section.  */
7749           if (sym_sec->owner != ibfd
7750               || sym_sec->output_section == bfd_abs_section_ptr)
7751             need_edit = TRUE;
7752
7753           rel += 2;
7754           if (rel + 1 == relend
7755               || (rel + 2 < relend
7756                   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7757             ++rel;
7758
7759           if (rel == relend)
7760             {
7761               if (sec->size == offset + 24)
7762                 {
7763                   need_pad = NULL;
7764                   break;
7765                 }
7766               if (sec->size == offset + 16)
7767                 {
7768                   cnt_16b++;
7769                   break;
7770                 }
7771               goto broken_opd;
7772             }
7773           else if (rel + 1 < relend
7774                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7775                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7776             {
7777               if (rel[0].r_offset == offset + 16)
7778                 cnt_16b++;
7779               else if (rel[0].r_offset != offset + 24)
7780                 goto broken_opd;
7781             }
7782           else
7783             goto broken_opd;
7784         }
7785
7786       add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7787
7788       if (!broken && (need_edit || add_aux_fields))
7789         {
7790           Elf_Internal_Rela *write_rel;
7791           Elf_Internal_Shdr *rel_hdr;
7792           bfd_byte *rptr, *wptr;
7793           bfd_byte *new_contents;
7794           bfd_size_type amt;
7795
7796           new_contents = NULL;
7797           amt = OPD_NDX (sec->size) * sizeof (long);
7798           opd = &ppc64_elf_section_data (sec)->u.opd;
7799           opd->adjust = bfd_zalloc (sec->owner, amt);
7800           if (opd->adjust == NULL)
7801             return FALSE;
7802           ppc64_elf_section_data (sec)->sec_type = sec_opd;
7803
7804           /* This seems a waste of time as input .opd sections are all
7805              zeros as generated by gcc, but I suppose there's no reason
7806              this will always be so.  We might start putting something in
7807              the third word of .opd entries.  */
7808           if ((sec->flags & SEC_IN_MEMORY) == 0)
7809             {
7810               bfd_byte *loc;
7811               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7812                 {
7813                   if (loc != NULL)
7814                     free (loc);
7815                 error_ret:
7816                   if (local_syms != NULL
7817                       && symtab_hdr->contents != (unsigned char *) local_syms)
7818                     free (local_syms);
7819                   if (elf_section_data (sec)->relocs != relstart)
7820                     free (relstart);
7821                   return FALSE;
7822                 }
7823               sec->contents = loc;
7824               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7825             }
7826
7827           elf_section_data (sec)->relocs = relstart;
7828
7829           new_contents = sec->contents;
7830           if (add_aux_fields)
7831             {
7832               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7833               if (new_contents == NULL)
7834                 return FALSE;
7835               need_pad = NULL;
7836             }
7837           wptr = new_contents;
7838           rptr = sec->contents;
7839           write_rel = relstart;
7840           for (rel = relstart; rel < relend; )
7841             {
7842               unsigned long r_symndx;
7843               asection *sym_sec;
7844               struct elf_link_hash_entry *h;
7845               struct ppc_link_hash_entry *fdh = NULL;
7846               Elf_Internal_Sym *sym;
7847               long opd_ent_size;
7848               Elf_Internal_Rela *next_rel;
7849               bfd_boolean skip;
7850
7851               r_symndx = ELF64_R_SYM (rel->r_info);
7852               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7853                               r_symndx, ibfd))
7854                 goto error_ret;
7855
7856               next_rel = rel + 2;
7857               if (next_rel + 1 == relend
7858                   || (next_rel + 2 < relend
7859                       && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7860                 ++next_rel;
7861
7862               /* See if the .opd entry is full 24 byte or
7863                  16 byte (with fd_aux entry overlapped with next
7864                  fd_func).  */
7865               opd_ent_size = 24;
7866               if (next_rel == relend)
7867                 {
7868                   if (sec->size == rel->r_offset + 16)
7869                     opd_ent_size = 16;
7870                 }
7871               else if (next_rel->r_offset == rel->r_offset + 16)
7872                 opd_ent_size = 16;
7873
7874               if (h != NULL
7875                   && h->root.root.string[0] == '.')
7876                 {
7877                   fdh = lookup_fdh ((struct ppc_link_hash_entry *) h, htab);
7878                   if (fdh != NULL
7879                       && fdh->elf.root.type != bfd_link_hash_defined
7880                       && fdh->elf.root.type != bfd_link_hash_defweak)
7881                     fdh = NULL;
7882                 }
7883
7884               skip = (sym_sec->owner != ibfd
7885                       || sym_sec->output_section == bfd_abs_section_ptr);
7886               if (skip)
7887                 {
7888                   if (fdh != NULL && sym_sec->owner == ibfd)
7889                     {
7890                       /* Arrange for the function descriptor sym
7891                          to be dropped.  */
7892                       fdh->elf.root.u.def.value = 0;
7893                       fdh->elf.root.u.def.section = sym_sec;
7894                     }
7895                   opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7896
7897                   if (NO_OPD_RELOCS || info->relocatable)
7898                     rel = next_rel;
7899                   else
7900                     while (1)
7901                       {
7902                         if (!dec_dynrel_count (rel->r_info, sec, info,
7903                                                NULL, h, sym))
7904                           goto error_ret;
7905
7906                         if (++rel == next_rel)
7907                           break;
7908
7909                         r_symndx = ELF64_R_SYM (rel->r_info);
7910                         if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7911                                         r_symndx, ibfd))
7912                           goto error_ret;
7913                       }
7914                 }
7915               else
7916                 {
7917                   /* We'll be keeping this opd entry.  */
7918                   long adjust;
7919
7920                   if (fdh != NULL)
7921                     {
7922                       /* Redefine the function descriptor symbol to
7923                          this location in the opd section.  It is
7924                          necessary to update the value here rather
7925                          than using an array of adjustments as we do
7926                          for local symbols, because various places
7927                          in the generic ELF code use the value
7928                          stored in u.def.value.  */
7929                       fdh->elf.root.u.def.value = wptr - new_contents;
7930                       fdh->adjust_done = 1;
7931                     }
7932
7933                   /* Local syms are a bit tricky.  We could
7934                      tweak them as they can be cached, but
7935                      we'd need to look through the local syms
7936                      for the function descriptor sym which we
7937                      don't have at the moment.  So keep an
7938                      array of adjustments.  */
7939                   adjust = (wptr - new_contents) - (rptr - sec->contents);
7940                   opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7941
7942                   if (wptr != rptr)
7943                     memcpy (wptr, rptr, opd_ent_size);
7944                   wptr += opd_ent_size;
7945                   if (add_aux_fields && opd_ent_size == 16)
7946                     {
7947                       memset (wptr, '\0', 8);
7948                       wptr += 8;
7949                     }
7950
7951                   /* We need to adjust any reloc offsets to point to the
7952                      new opd entries.  */
7953                   for ( ; rel != next_rel; ++rel)
7954                     {
7955                       rel->r_offset += adjust;
7956                       if (write_rel != rel)
7957                         memcpy (write_rel, rel, sizeof (*rel));
7958                       ++write_rel;
7959                     }
7960                 }
7961
7962               rptr += opd_ent_size;
7963             }
7964
7965           sec->size = wptr - new_contents;
7966           sec->reloc_count = write_rel - relstart;
7967           if (add_aux_fields)
7968             {
7969               free (sec->contents);
7970               sec->contents = new_contents;
7971             }
7972
7973           /* Fudge the header size too, as this is used later in
7974              elf_bfd_final_link if we are emitting relocs.  */
7975           rel_hdr = _bfd_elf_single_rel_hdr (sec);
7976           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7977           some_edited = TRUE;
7978         }
7979       else if (elf_section_data (sec)->relocs != relstart)
7980         free (relstart);
7981
7982       if (local_syms != NULL
7983           && symtab_hdr->contents != (unsigned char *) local_syms)
7984         {
7985           if (!info->keep_memory)
7986             free (local_syms);
7987           else
7988             symtab_hdr->contents = (unsigned char *) local_syms;
7989         }
7990     }
7991
7992   if (some_edited)
7993     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7994
7995   /* If we are doing a final link and the last .opd entry is just 16 byte
7996      long, add a 8 byte padding after it.  */
7997   if (need_pad != NULL && !info->relocatable)
7998     {
7999       bfd_byte *p;
8000
8001       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8002         {
8003           BFD_ASSERT (need_pad->size > 0);
8004
8005           p = bfd_malloc (need_pad->size + 8);
8006           if (p == NULL)
8007             return FALSE;
8008
8009           if (! bfd_get_section_contents (need_pad->owner, need_pad,
8010                                           p, 0, need_pad->size))
8011             return FALSE;
8012
8013           need_pad->contents = p;
8014           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8015         }
8016       else
8017         {
8018           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8019           if (p == NULL)
8020             return FALSE;
8021
8022           need_pad->contents = p;
8023         }
8024
8025       memset (need_pad->contents + need_pad->size, 0, 8);
8026       need_pad->size += 8;
8027     }
8028
8029   return TRUE;
8030 }
8031
8032 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
8033
8034 asection *
8035 ppc64_elf_tls_setup (struct bfd_link_info *info)
8036 {
8037   struct ppc_link_hash_table *htab;
8038
8039   htab = ppc_hash_table (info);
8040   if (htab == NULL)
8041     return NULL;
8042
8043   if (abiversion (info->output_bfd) == 1)
8044     htab->opd_abi = 1;
8045
8046   if (htab->params->no_multi_toc)
8047     htab->do_multi_toc = 0;
8048   else if (!htab->do_multi_toc)
8049     htab->params->no_multi_toc = 1;
8050
8051   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8052                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8053                                               FALSE, FALSE, TRUE));
8054   /* Move dynamic linking info to the function descriptor sym.  */
8055   if (htab->tls_get_addr != NULL)
8056     func_desc_adjust (&htab->tls_get_addr->elf, info);
8057   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8058                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8059                                                  FALSE, FALSE, TRUE));
8060   if (!htab->params->no_tls_get_addr_opt)
8061     {
8062       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8063
8064       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8065                                   FALSE, FALSE, TRUE);
8066       if (opt != NULL)
8067         func_desc_adjust (opt, info);
8068       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8069                                      FALSE, FALSE, TRUE);
8070       if (opt_fd != NULL
8071           && (opt_fd->root.type == bfd_link_hash_defined
8072               || opt_fd->root.type == bfd_link_hash_defweak))
8073         {
8074           /* If glibc supports an optimized __tls_get_addr call stub,
8075              signalled by the presence of __tls_get_addr_opt, and we'll
8076              be calling __tls_get_addr via a plt call stub, then
8077              make __tls_get_addr point to __tls_get_addr_opt.  */
8078           tga_fd = &htab->tls_get_addr_fd->elf;
8079           if (htab->elf.dynamic_sections_created
8080               && tga_fd != NULL
8081               && (tga_fd->type == STT_FUNC
8082                   || tga_fd->needs_plt)
8083               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8084                    || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
8085                        && tga_fd->root.type == bfd_link_hash_undefweak)))
8086             {
8087               struct plt_entry *ent;
8088
8089               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8090                 if (ent->plt.refcount > 0)
8091                   break;
8092               if (ent != NULL)
8093                 {
8094                   tga_fd->root.type = bfd_link_hash_indirect;
8095                   tga_fd->root.u.i.link = &opt_fd->root;
8096                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8097                   if (opt_fd->dynindx != -1)
8098                     {
8099                       /* Use __tls_get_addr_opt in dynamic relocations.  */
8100                       opt_fd->dynindx = -1;
8101                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8102                                               opt_fd->dynstr_index);
8103                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8104                         return NULL;
8105                     }
8106                   htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8107                   tga = &htab->tls_get_addr->elf;
8108                   if (opt != NULL && tga != NULL)
8109                     {
8110                       tga->root.type = bfd_link_hash_indirect;
8111                       tga->root.u.i.link = &opt->root;
8112                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
8113                       _bfd_elf_link_hash_hide_symbol (info, opt,
8114                                                       tga->forced_local);
8115                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8116                     }
8117                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8118                   htab->tls_get_addr_fd->is_func_descriptor = 1;
8119                   if (htab->tls_get_addr != NULL)
8120                     {
8121                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8122                       htab->tls_get_addr->is_func = 1;
8123                     }
8124                 }
8125             }
8126         }
8127       else
8128         htab->params->no_tls_get_addr_opt = TRUE;
8129     }
8130   return _bfd_elf_tls_setup (info->output_bfd, info);
8131 }
8132
8133 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8134    HASH1 or HASH2.  */
8135
8136 static bfd_boolean
8137 branch_reloc_hash_match (const bfd *ibfd,
8138                          const Elf_Internal_Rela *rel,
8139                          const struct ppc_link_hash_entry *hash1,
8140                          const struct ppc_link_hash_entry *hash2)
8141 {
8142   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8143   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8144   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8145
8146   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8147     {
8148       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8149       struct elf_link_hash_entry *h;
8150
8151       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8152       h = elf_follow_link (h);
8153       if (h == &hash1->elf || h == &hash2->elf)
8154         return TRUE;
8155     }
8156   return FALSE;
8157 }
8158
8159 /* Run through all the TLS relocs looking for optimization
8160    opportunities.  The linker has been hacked (see ppc64elf.em) to do
8161    a preliminary section layout so that we know the TLS segment
8162    offsets.  We can't optimize earlier because some optimizations need
8163    to know the tp offset, and we need to optimize before allocating
8164    dynamic relocations.  */
8165
8166 bfd_boolean
8167 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8168 {
8169   bfd *ibfd;
8170   asection *sec;
8171   struct ppc_link_hash_table *htab;
8172   unsigned char *toc_ref;
8173   int pass;
8174
8175   if (info->relocatable || !info->executable)
8176     return TRUE;
8177
8178   htab = ppc_hash_table (info);
8179   if (htab == NULL)
8180     return FALSE;
8181
8182   /* Make two passes over the relocs.  On the first pass, mark toc
8183      entries involved with tls relocs, and check that tls relocs
8184      involved in setting up a tls_get_addr call are indeed followed by
8185      such a call.  If they are not, we can't do any tls optimization.
8186      On the second pass twiddle tls_mask flags to notify
8187      relocate_section that optimization can be done, and adjust got
8188      and plt refcounts.  */
8189   toc_ref = NULL;
8190   for (pass = 0; pass < 2; ++pass)
8191     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8192       {
8193         Elf_Internal_Sym *locsyms = NULL;
8194         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8195
8196         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8197           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8198             {
8199               Elf_Internal_Rela *relstart, *rel, *relend;
8200               bfd_boolean found_tls_get_addr_arg = 0;
8201
8202               /* Read the relocations.  */
8203               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8204                                                     info->keep_memory);
8205               if (relstart == NULL)
8206                 {
8207                   free (toc_ref);
8208                   return FALSE;
8209                 }
8210
8211               relend = relstart + sec->reloc_count;
8212               for (rel = relstart; rel < relend; rel++)
8213                 {
8214                   enum elf_ppc64_reloc_type r_type;
8215                   unsigned long r_symndx;
8216                   struct elf_link_hash_entry *h;
8217                   Elf_Internal_Sym *sym;
8218                   asection *sym_sec;
8219                   unsigned char *tls_mask;
8220                   unsigned char tls_set, tls_clear, tls_type = 0;
8221                   bfd_vma value;
8222                   bfd_boolean ok_tprel, is_local;
8223                   long toc_ref_index = 0;
8224                   int expecting_tls_get_addr = 0;
8225                   bfd_boolean ret = FALSE;
8226
8227                   r_symndx = ELF64_R_SYM (rel->r_info);
8228                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8229                                   r_symndx, ibfd))
8230                     {
8231                     err_free_rel:
8232                       if (elf_section_data (sec)->relocs != relstart)
8233                         free (relstart);
8234                       if (toc_ref != NULL)
8235                         free (toc_ref);
8236                       if (locsyms != NULL
8237                           && (elf_symtab_hdr (ibfd).contents
8238                               != (unsigned char *) locsyms))
8239                         free (locsyms);
8240                       return ret;
8241                     }
8242
8243                   if (h != NULL)
8244                     {
8245                       if (h->root.type == bfd_link_hash_defined
8246                           || h->root.type == bfd_link_hash_defweak)
8247                         value = h->root.u.def.value;
8248                       else if (h->root.type == bfd_link_hash_undefweak)
8249                         value = 0;
8250                       else
8251                         {
8252                           found_tls_get_addr_arg = 0;
8253                           continue;
8254                         }
8255                     }
8256                   else
8257                     /* Symbols referenced by TLS relocs must be of type
8258                        STT_TLS.  So no need for .opd local sym adjust.  */
8259                     value = sym->st_value;
8260
8261                   ok_tprel = FALSE;
8262                   is_local = FALSE;
8263                   if (h == NULL
8264                       || !h->def_dynamic)
8265                     {
8266                       is_local = TRUE;
8267                       if (h != NULL
8268                           && h->root.type == bfd_link_hash_undefweak)
8269                         ok_tprel = TRUE;
8270                       else
8271                         {
8272                           value += sym_sec->output_offset;
8273                           value += sym_sec->output_section->vma;
8274                           value -= htab->elf.tls_sec->vma;
8275                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8276                                       < (bfd_vma) 1 << 32);
8277                         }
8278                     }
8279
8280                   r_type = ELF64_R_TYPE (rel->r_info);
8281                   /* If this section has old-style __tls_get_addr calls
8282                      without marker relocs, then check that each
8283                      __tls_get_addr call reloc is preceded by a reloc
8284                      that conceivably belongs to the __tls_get_addr arg
8285                      setup insn.  If we don't find matching arg setup
8286                      relocs, don't do any tls optimization.  */
8287                   if (pass == 0
8288                       && sec->has_tls_get_addr_call
8289                       && h != NULL
8290                       && (h == &htab->tls_get_addr->elf
8291                           || h == &htab->tls_get_addr_fd->elf)
8292                       && !found_tls_get_addr_arg
8293                       && is_branch_reloc (r_type))
8294                     {
8295                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8296                                                 "TLS optimization disabled\n"),
8297                                               ibfd, sec, rel->r_offset);
8298                       ret = TRUE;
8299                       goto err_free_rel;
8300                     }
8301
8302                   found_tls_get_addr_arg = 0;
8303                   switch (r_type)
8304                     {
8305                     case R_PPC64_GOT_TLSLD16:
8306                     case R_PPC64_GOT_TLSLD16_LO:
8307                       expecting_tls_get_addr = 1;
8308                       found_tls_get_addr_arg = 1;
8309                       /* Fall thru */
8310
8311                     case R_PPC64_GOT_TLSLD16_HI:
8312                     case R_PPC64_GOT_TLSLD16_HA:
8313                       /* These relocs should never be against a symbol
8314                          defined in a shared lib.  Leave them alone if
8315                          that turns out to be the case.  */
8316                       if (!is_local)
8317                         continue;
8318
8319                       /* LD -> LE */
8320                       tls_set = 0;
8321                       tls_clear = TLS_LD;
8322                       tls_type = TLS_TLS | TLS_LD;
8323                       break;
8324
8325                     case R_PPC64_GOT_TLSGD16:
8326                     case R_PPC64_GOT_TLSGD16_LO:
8327                       expecting_tls_get_addr = 1;
8328                       found_tls_get_addr_arg = 1;
8329                       /* Fall thru */
8330
8331                     case R_PPC64_GOT_TLSGD16_HI:
8332                     case R_PPC64_GOT_TLSGD16_HA:
8333                       if (ok_tprel)
8334                         /* GD -> LE */
8335                         tls_set = 0;
8336                       else
8337                         /* GD -> IE */
8338                         tls_set = TLS_TLS | TLS_TPRELGD;
8339                       tls_clear = TLS_GD;
8340                       tls_type = TLS_TLS | TLS_GD;
8341                       break;
8342
8343                     case R_PPC64_GOT_TPREL16_DS:
8344                     case R_PPC64_GOT_TPREL16_LO_DS:
8345                     case R_PPC64_GOT_TPREL16_HI:
8346                     case R_PPC64_GOT_TPREL16_HA:
8347                       if (ok_tprel)
8348                         {
8349                           /* IE -> LE */
8350                           tls_set = 0;
8351                           tls_clear = TLS_TPREL;
8352                           tls_type = TLS_TLS | TLS_TPREL;
8353                           break;
8354                         }
8355                       continue;
8356
8357                     case R_PPC64_TLSGD:
8358                     case R_PPC64_TLSLD:
8359                       found_tls_get_addr_arg = 1;
8360                       /* Fall thru */
8361
8362                     case R_PPC64_TLS:
8363                     case R_PPC64_TOC16:
8364                     case R_PPC64_TOC16_LO:
8365                       if (sym_sec == NULL || sym_sec != toc)
8366                         continue;
8367
8368                       /* Mark this toc entry as referenced by a TLS
8369                          code sequence.  We can do that now in the
8370                          case of R_PPC64_TLS, and after checking for
8371                          tls_get_addr for the TOC16 relocs.  */
8372                       if (toc_ref == NULL)
8373                         toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8374                       if (toc_ref == NULL)
8375                         goto err_free_rel;
8376
8377                       if (h != NULL)
8378                         value = h->root.u.def.value;
8379                       else
8380                         value = sym->st_value;
8381                       value += rel->r_addend;
8382                       if (value % 8 != 0)
8383                         continue;
8384                       BFD_ASSERT (value < toc->size
8385                                   && toc->output_offset % 8 == 0);
8386                       toc_ref_index = (value + toc->output_offset) / 8;
8387                       if (r_type == R_PPC64_TLS
8388                           || r_type == R_PPC64_TLSGD
8389                           || r_type == R_PPC64_TLSLD)
8390                         {
8391                           toc_ref[toc_ref_index] = 1;
8392                           continue;
8393                         }
8394
8395                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
8396                         continue;
8397
8398                       tls_set = 0;
8399                       tls_clear = 0;
8400                       expecting_tls_get_addr = 2;
8401                       break;
8402
8403                     case R_PPC64_TPREL64:
8404                       if (pass == 0
8405                           || sec != toc
8406                           || toc_ref == NULL
8407                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8408                         continue;
8409                       if (ok_tprel)
8410                         {
8411                           /* IE -> LE */
8412                           tls_set = TLS_EXPLICIT;
8413                           tls_clear = TLS_TPREL;
8414                           break;
8415                         }
8416                       continue;
8417
8418                     case R_PPC64_DTPMOD64:
8419                       if (pass == 0
8420                           || sec != toc
8421                           || toc_ref == NULL
8422                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8423                         continue;
8424                       if (rel + 1 < relend
8425                           && (rel[1].r_info
8426                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8427                           && rel[1].r_offset == rel->r_offset + 8)
8428                         {
8429                           if (ok_tprel)
8430                             /* GD -> LE */
8431                             tls_set = TLS_EXPLICIT | TLS_GD;
8432                           else
8433                             /* GD -> IE */
8434                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8435                           tls_clear = TLS_GD;
8436                         }
8437                       else
8438                         {
8439                           if (!is_local)
8440                             continue;
8441
8442                           /* LD -> LE */
8443                           tls_set = TLS_EXPLICIT;
8444                           tls_clear = TLS_LD;
8445                         }
8446                       break;
8447
8448                     default:
8449                       continue;
8450                     }
8451
8452                   if (pass == 0)
8453                     {
8454                       if (!expecting_tls_get_addr
8455                           || !sec->has_tls_get_addr_call)
8456                         continue;
8457
8458                       if (rel + 1 < relend
8459                           && branch_reloc_hash_match (ibfd, rel + 1,
8460                                                       htab->tls_get_addr,
8461                                                       htab->tls_get_addr_fd))
8462                         {
8463                           if (expecting_tls_get_addr == 2)
8464                             {
8465                               /* Check for toc tls entries.  */
8466                               unsigned char *toc_tls;
8467                               int retval;
8468
8469                               retval = get_tls_mask (&toc_tls, NULL, NULL,
8470                                                      &locsyms,
8471                                                      rel, ibfd);
8472                               if (retval == 0)
8473                                 goto err_free_rel;
8474                               if (toc_tls != NULL)
8475                                 {
8476                                   if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8477                                     found_tls_get_addr_arg = 1;
8478                                   if (retval > 1)
8479                                     toc_ref[toc_ref_index] = 1;
8480                                 }
8481                             }
8482                           continue;
8483                         }
8484
8485                       if (expecting_tls_get_addr != 1)
8486                         continue;
8487
8488                       /* Uh oh, we didn't find the expected call.  We
8489                          could just mark this symbol to exclude it
8490                          from tls optimization but it's safer to skip
8491                          the entire optimization.  */
8492                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8493                                                 "TLS optimization disabled\n"),
8494                                               ibfd, sec, rel->r_offset);
8495                       ret = TRUE;
8496                       goto err_free_rel;
8497                     }
8498
8499                   if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8500                     {
8501                       struct plt_entry *ent;
8502                       for (ent = htab->tls_get_addr->elf.plt.plist;
8503                            ent != NULL;
8504                            ent = ent->next)
8505                         if (ent->addend == 0)
8506                           {
8507                             if (ent->plt.refcount > 0)
8508                               {
8509                                 ent->plt.refcount -= 1;
8510                                 expecting_tls_get_addr = 0;
8511                               }
8512                             break;
8513                           }
8514                     }
8515
8516                   if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8517                     {
8518                       struct plt_entry *ent;
8519                       for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8520                            ent != NULL;
8521                            ent = ent->next)
8522                         if (ent->addend == 0)
8523                           {
8524                             if (ent->plt.refcount > 0)
8525                               ent->plt.refcount -= 1;
8526                             break;
8527                           }
8528                     }
8529
8530                   if (tls_clear == 0)
8531                     continue;
8532
8533                   if ((tls_set & TLS_EXPLICIT) == 0)
8534                     {
8535                       struct got_entry *ent;
8536
8537                       /* Adjust got entry for this reloc.  */
8538                       if (h != NULL)
8539                         ent = h->got.glist;
8540                       else
8541                         ent = elf_local_got_ents (ibfd)[r_symndx];
8542
8543                       for (; ent != NULL; ent = ent->next)
8544                         if (ent->addend == rel->r_addend
8545                             && ent->owner == ibfd
8546                             && ent->tls_type == tls_type)
8547                           break;
8548                       if (ent == NULL)
8549                         abort ();
8550
8551                       if (tls_set == 0)
8552                         {
8553                           /* We managed to get rid of a got entry.  */
8554                           if (ent->got.refcount > 0)
8555                             ent->got.refcount -= 1;
8556                         }
8557                     }
8558                   else
8559                     {
8560                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
8561                          we'll lose one or two dyn relocs.  */
8562                       if (!dec_dynrel_count (rel->r_info, sec, info,
8563                                              NULL, h, sym))
8564                         return FALSE;
8565
8566                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
8567                         {
8568                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8569                                                  NULL, h, sym))
8570                             return FALSE;
8571                         }
8572                     }
8573
8574                   *tls_mask |= tls_set;
8575                   *tls_mask &= ~tls_clear;
8576                 }
8577
8578               if (elf_section_data (sec)->relocs != relstart)
8579                 free (relstart);
8580             }
8581
8582         if (locsyms != NULL
8583             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8584           {
8585             if (!info->keep_memory)
8586               free (locsyms);
8587             else
8588               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8589           }
8590       }
8591
8592   if (toc_ref != NULL)
8593     free (toc_ref);
8594   return TRUE;
8595 }
8596
8597 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8598    the values of any global symbols in a toc section that has been
8599    edited.  Globals in toc sections should be a rarity, so this function
8600    sets a flag if any are found in toc sections other than the one just
8601    edited, so that futher hash table traversals can be avoided.  */
8602
8603 struct adjust_toc_info
8604 {
8605   asection *toc;
8606   unsigned long *skip;
8607   bfd_boolean global_toc_syms;
8608 };
8609
8610 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8611
8612 static bfd_boolean
8613 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8614 {
8615   struct ppc_link_hash_entry *eh;
8616   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8617   unsigned long i;
8618
8619   if (h->root.type != bfd_link_hash_defined
8620       && h->root.type != bfd_link_hash_defweak)
8621     return TRUE;
8622
8623   eh = (struct ppc_link_hash_entry *) h;
8624   if (eh->adjust_done)
8625     return TRUE;
8626
8627   if (eh->elf.root.u.def.section == toc_inf->toc)
8628     {
8629       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8630         i = toc_inf->toc->rawsize >> 3;
8631       else
8632         i = eh->elf.root.u.def.value >> 3;
8633
8634       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8635         {
8636           (*_bfd_error_handler)
8637             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8638           do
8639             ++i;
8640           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8641           eh->elf.root.u.def.value = (bfd_vma) i << 3;
8642         }
8643
8644       eh->elf.root.u.def.value -= toc_inf->skip[i];
8645       eh->adjust_done = 1;
8646     }
8647   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8648     toc_inf->global_toc_syms = TRUE;
8649
8650   return TRUE;
8651 }
8652
8653 /* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc.  */
8654
8655 static bfd_boolean
8656 ok_lo_toc_insn (unsigned int insn)
8657 {
8658   return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8659           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8660           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8661           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8662           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8663           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8664           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8665           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8666           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8667           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8668           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8669           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8670           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8671           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8672           || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8673               && (insn & 3) != 1)
8674           || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8675               && ((insn & 3) == 0 || (insn & 3) == 3))
8676           || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8677 }
8678
8679 /* Examine all relocs referencing .toc sections in order to remove
8680    unused .toc entries.  */
8681
8682 bfd_boolean
8683 ppc64_elf_edit_toc (struct bfd_link_info *info)
8684 {
8685   bfd *ibfd;
8686   struct adjust_toc_info toc_inf;
8687   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8688
8689   htab->do_toc_opt = 1;
8690   toc_inf.global_toc_syms = TRUE;
8691   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8692     {
8693       asection *toc, *sec;
8694       Elf_Internal_Shdr *symtab_hdr;
8695       Elf_Internal_Sym *local_syms;
8696       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8697       unsigned long *skip, *drop;
8698       unsigned char *used;
8699       unsigned char *keep, last, some_unused;
8700
8701       if (!is_ppc64_elf (ibfd))
8702         continue;
8703
8704       toc = bfd_get_section_by_name (ibfd, ".toc");
8705       if (toc == NULL
8706           || toc->size == 0
8707           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8708           || discarded_section (toc))
8709         continue;
8710
8711       toc_relocs = NULL;
8712       local_syms = NULL;
8713       symtab_hdr = &elf_symtab_hdr (ibfd);
8714
8715       /* Look at sections dropped from the final link.  */
8716       skip = NULL;
8717       relstart = NULL;
8718       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8719         {
8720           if (sec->reloc_count == 0
8721               || !discarded_section (sec)
8722               || get_opd_info (sec)
8723               || (sec->flags & SEC_ALLOC) == 0
8724               || (sec->flags & SEC_DEBUGGING) != 0)
8725             continue;
8726
8727           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8728           if (relstart == NULL)
8729             goto error_ret;
8730
8731           /* Run through the relocs to see which toc entries might be
8732              unused.  */
8733           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8734             {
8735               enum elf_ppc64_reloc_type r_type;
8736               unsigned long r_symndx;
8737               asection *sym_sec;
8738               struct elf_link_hash_entry *h;
8739               Elf_Internal_Sym *sym;
8740               bfd_vma val;
8741
8742               r_type = ELF64_R_TYPE (rel->r_info);
8743               switch (r_type)
8744                 {
8745                 default:
8746                   continue;
8747
8748                 case R_PPC64_TOC16:
8749                 case R_PPC64_TOC16_LO:
8750                 case R_PPC64_TOC16_HI:
8751                 case R_PPC64_TOC16_HA:
8752                 case R_PPC64_TOC16_DS:
8753                 case R_PPC64_TOC16_LO_DS:
8754                   break;
8755                 }
8756
8757               r_symndx = ELF64_R_SYM (rel->r_info);
8758               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8759                               r_symndx, ibfd))
8760                 goto error_ret;
8761
8762               if (sym_sec != toc)
8763                 continue;
8764
8765               if (h != NULL)
8766                 val = h->root.u.def.value;
8767               else
8768                 val = sym->st_value;
8769               val += rel->r_addend;
8770
8771               if (val >= toc->size)
8772                 continue;
8773
8774               /* Anything in the toc ought to be aligned to 8 bytes.
8775                  If not, don't mark as unused.  */
8776               if (val & 7)
8777                 continue;
8778
8779               if (skip == NULL)
8780                 {
8781                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8782                   if (skip == NULL)
8783                     goto error_ret;
8784                 }
8785
8786               skip[val >> 3] = ref_from_discarded;
8787             }
8788
8789           if (elf_section_data (sec)->relocs != relstart)
8790             free (relstart);
8791         }
8792
8793       /* For largetoc loads of address constants, we can convert
8794          .  addis rx,2,addr@got@ha
8795          .  ld ry,addr@got@l(rx)
8796          to
8797          .  addis rx,2,addr@toc@ha
8798          .  addi ry,rx,addr@toc@l
8799          when addr is within 2G of the toc pointer.  This then means
8800          that the word storing "addr" in the toc is no longer needed.  */
8801
8802       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8803           && toc->output_section->rawsize < (bfd_vma) 1 << 31
8804           && toc->reloc_count != 0)
8805         {
8806           /* Read toc relocs.  */
8807           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8808                                                   info->keep_memory);
8809           if (toc_relocs == NULL)
8810             goto error_ret;
8811
8812           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8813             {
8814               enum elf_ppc64_reloc_type r_type;
8815               unsigned long r_symndx;
8816               asection *sym_sec;
8817               struct elf_link_hash_entry *h;
8818               Elf_Internal_Sym *sym;
8819               bfd_vma val, addr;
8820
8821               r_type = ELF64_R_TYPE (rel->r_info);
8822               if (r_type != R_PPC64_ADDR64)
8823                 continue;
8824
8825               r_symndx = ELF64_R_SYM (rel->r_info);
8826               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8827                               r_symndx, ibfd))
8828                 goto error_ret;
8829
8830               if (sym_sec == NULL
8831                   || discarded_section (sym_sec))
8832                 continue;
8833
8834               if (!SYMBOL_REFERENCES_LOCAL (info, h))
8835                 continue;
8836
8837               if (h != NULL)
8838                 {
8839                   if (h->type == STT_GNU_IFUNC)
8840                     continue;
8841                   val = h->root.u.def.value;
8842                 }
8843               else
8844                 {
8845                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8846                     continue;
8847                   val = sym->st_value;
8848                 }
8849               val += rel->r_addend;
8850               val += sym_sec->output_section->vma + sym_sec->output_offset;
8851
8852               /* We don't yet know the exact toc pointer value, but we
8853                  know it will be somewhere in the toc section.  Don't
8854                  optimize if the difference from any possible toc
8855                  pointer is outside [ff..f80008000, 7fff7fff].  */
8856               addr = toc->output_section->vma + TOC_BASE_OFF;
8857               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8858                 continue;
8859
8860               addr = toc->output_section->vma + toc->output_section->rawsize;
8861               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8862                 continue;
8863
8864               if (skip == NULL)
8865                 {
8866                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8867                   if (skip == NULL)
8868                     goto error_ret;
8869                 }
8870
8871               skip[rel->r_offset >> 3]
8872                 |= can_optimize | ((rel - toc_relocs) << 2);
8873             }
8874         }
8875
8876       if (skip == NULL)
8877         continue;
8878
8879       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8880       if (used == NULL)
8881         {
8882         error_ret:
8883           if (local_syms != NULL
8884               && symtab_hdr->contents != (unsigned char *) local_syms)
8885             free (local_syms);
8886           if (sec != NULL
8887               && relstart != NULL
8888               && elf_section_data (sec)->relocs != relstart)
8889             free (relstart);
8890           if (toc_relocs != NULL
8891               && elf_section_data (toc)->relocs != toc_relocs)
8892             free (toc_relocs);
8893           if (skip != NULL)
8894             free (skip);
8895           return FALSE;
8896         }
8897
8898       /* Now check all kept sections that might reference the toc.
8899          Check the toc itself last.  */
8900       for (sec = (ibfd->sections == toc && toc->next ? toc->next
8901                   : ibfd->sections);
8902            sec != NULL;
8903            sec = (sec == toc ? NULL
8904                   : sec->next == NULL ? toc
8905                   : sec->next == toc && toc->next ? toc->next
8906                   : sec->next))
8907         {
8908           int repeat;
8909
8910           if (sec->reloc_count == 0
8911               || discarded_section (sec)
8912               || get_opd_info (sec)
8913               || (sec->flags & SEC_ALLOC) == 0
8914               || (sec->flags & SEC_DEBUGGING) != 0)
8915             continue;
8916
8917           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8918                                                 info->keep_memory);
8919           if (relstart == NULL)
8920             {
8921               free (used);
8922               goto error_ret;
8923             }
8924
8925           /* Mark toc entries referenced as used.  */
8926           do
8927             {
8928               repeat = 0;
8929               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8930                 {
8931                   enum elf_ppc64_reloc_type r_type;
8932                   unsigned long r_symndx;
8933                   asection *sym_sec;
8934                   struct elf_link_hash_entry *h;
8935                   Elf_Internal_Sym *sym;
8936                   bfd_vma val;
8937                   enum {no_check, check_lo, check_ha} insn_check;
8938
8939                   r_type = ELF64_R_TYPE (rel->r_info);
8940                   switch (r_type)
8941                     {
8942                     default:
8943                       insn_check = no_check;
8944                       break;
8945
8946                     case R_PPC64_GOT_TLSLD16_HA:
8947                     case R_PPC64_GOT_TLSGD16_HA:
8948                     case R_PPC64_GOT_TPREL16_HA:
8949                     case R_PPC64_GOT_DTPREL16_HA:
8950                     case R_PPC64_GOT16_HA:
8951                     case R_PPC64_TOC16_HA:
8952                       insn_check = check_ha;
8953                       break;
8954
8955                     case R_PPC64_GOT_TLSLD16_LO:
8956                     case R_PPC64_GOT_TLSGD16_LO:
8957                     case R_PPC64_GOT_TPREL16_LO_DS:
8958                     case R_PPC64_GOT_DTPREL16_LO_DS:
8959                     case R_PPC64_GOT16_LO:
8960                     case R_PPC64_GOT16_LO_DS:
8961                     case R_PPC64_TOC16_LO:
8962                     case R_PPC64_TOC16_LO_DS:
8963                       insn_check = check_lo;
8964                       break;
8965                     }
8966
8967                   if (insn_check != no_check)
8968                     {
8969                       bfd_vma off = rel->r_offset & ~3;
8970                       unsigned char buf[4];
8971                       unsigned int insn;
8972
8973                       if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8974                         {
8975                           free (used);
8976                           goto error_ret;
8977                         }
8978                       insn = bfd_get_32 (ibfd, buf);
8979                       if (insn_check == check_lo
8980                           ? !ok_lo_toc_insn (insn)
8981                           : ((insn & ((0x3f << 26) | 0x1f << 16))
8982                              != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8983                         {
8984                           char str[12];
8985
8986                           ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8987                           sprintf (str, "%#08x", insn);
8988                           info->callbacks->einfo
8989                             (_("%P: %H: toc optimization is not supported for"
8990                                " %s instruction.\n"),
8991                              ibfd, sec, rel->r_offset & ~3, str);
8992                         }
8993                     }
8994
8995                   switch (r_type)
8996                     {
8997                     case R_PPC64_TOC16:
8998                     case R_PPC64_TOC16_LO:
8999                     case R_PPC64_TOC16_HI:
9000                     case R_PPC64_TOC16_HA:
9001                     case R_PPC64_TOC16_DS:
9002                     case R_PPC64_TOC16_LO_DS:
9003                       /* In case we're taking addresses of toc entries.  */
9004                     case R_PPC64_ADDR64:
9005                       break;
9006
9007                     default:
9008                       continue;
9009                     }
9010
9011                   r_symndx = ELF64_R_SYM (rel->r_info);
9012                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9013                                   r_symndx, ibfd))
9014                     {
9015                       free (used);
9016                       goto error_ret;
9017                     }
9018
9019                   if (sym_sec != toc)
9020                     continue;
9021
9022                   if (h != NULL)
9023                     val = h->root.u.def.value;
9024                   else
9025                     val = sym->st_value;
9026                   val += rel->r_addend;
9027
9028                   if (val >= toc->size)
9029                     continue;
9030
9031                   if ((skip[val >> 3] & can_optimize) != 0)
9032                     {
9033                       bfd_vma off;
9034                       unsigned char opc;
9035
9036                       switch (r_type)
9037                         {
9038                         case R_PPC64_TOC16_HA:
9039                           break;
9040
9041                         case R_PPC64_TOC16_LO_DS:
9042                           off = rel->r_offset;
9043                           off += (bfd_big_endian (ibfd) ? -2 : 3);
9044                           if (!bfd_get_section_contents (ibfd, sec, &opc,
9045                                                          off, 1))
9046                             {
9047                               free (used);
9048                               goto error_ret;
9049                             }
9050                           if ((opc & (0x3f << 2)) == (58u << 2))
9051                             break;
9052                           /* Fall thru */
9053
9054                         default:
9055                           /* Wrong sort of reloc, or not a ld.  We may
9056                              as well clear ref_from_discarded too.  */
9057                           skip[val >> 3] = 0;
9058                         }
9059                     }
9060
9061                   if (sec != toc)
9062                     used[val >> 3] = 1;
9063                   /* For the toc section, we only mark as used if this
9064                      entry itself isn't unused.  */
9065                   else if ((used[rel->r_offset >> 3]
9066                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9067                            && !used[val >> 3])
9068                     {
9069                       /* Do all the relocs again, to catch reference
9070                          chains.  */
9071                       repeat = 1;
9072                       used[val >> 3] = 1;
9073                     }
9074                 }
9075             }
9076           while (repeat);
9077
9078           if (elf_section_data (sec)->relocs != relstart)
9079             free (relstart);
9080         }
9081
9082       /* Merge the used and skip arrays.  Assume that TOC
9083          doublewords not appearing as either used or unused belong
9084          to to an entry more than one doubleword in size.  */
9085       for (drop = skip, keep = used, last = 0, some_unused = 0;
9086            drop < skip + (toc->size + 7) / 8;
9087            ++drop, ++keep)
9088         {
9089           if (*keep)
9090             {
9091               *drop &= ~ref_from_discarded;
9092               if ((*drop & can_optimize) != 0)
9093                 some_unused = 1;
9094               last = 0;
9095             }
9096           else if ((*drop & ref_from_discarded) != 0)
9097             {
9098               some_unused = 1;
9099               last = ref_from_discarded;
9100             }
9101           else
9102             *drop = last;
9103         }
9104
9105       free (used);
9106
9107       if (some_unused)
9108         {
9109           bfd_byte *contents, *src;
9110           unsigned long off;
9111           Elf_Internal_Sym *sym;
9112           bfd_boolean local_toc_syms = FALSE;
9113
9114           /* Shuffle the toc contents, and at the same time convert the
9115              skip array from booleans into offsets.  */
9116           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9117             goto error_ret;
9118
9119           elf_section_data (toc)->this_hdr.contents = contents;
9120
9121           for (src = contents, off = 0, drop = skip;
9122                src < contents + toc->size;
9123                src += 8, ++drop)
9124             {
9125               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9126                 off += 8;
9127               else if (off != 0)
9128                 {
9129                   *drop = off;
9130                   memcpy (src - off, src, 8);
9131                 }
9132             }
9133           *drop = off;
9134           toc->rawsize = toc->size;
9135           toc->size = src - contents - off;
9136
9137           /* Adjust addends for relocs against the toc section sym,
9138              and optimize any accesses we can.  */
9139           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9140             {
9141               if (sec->reloc_count == 0
9142                   || discarded_section (sec))
9143                 continue;
9144
9145               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9146                                                     info->keep_memory);
9147               if (relstart == NULL)
9148                 goto error_ret;
9149
9150               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9151                 {
9152                   enum elf_ppc64_reloc_type r_type;
9153                   unsigned long r_symndx;
9154                   asection *sym_sec;
9155                   struct elf_link_hash_entry *h;
9156                   bfd_vma val;
9157
9158                   r_type = ELF64_R_TYPE (rel->r_info);
9159                   switch (r_type)
9160                     {
9161                     default:
9162                       continue;
9163
9164                     case R_PPC64_TOC16:
9165                     case R_PPC64_TOC16_LO:
9166                     case R_PPC64_TOC16_HI:
9167                     case R_PPC64_TOC16_HA:
9168                     case R_PPC64_TOC16_DS:
9169                     case R_PPC64_TOC16_LO_DS:
9170                     case R_PPC64_ADDR64:
9171                       break;
9172                     }
9173
9174                   r_symndx = ELF64_R_SYM (rel->r_info);
9175                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9176                                   r_symndx, ibfd))
9177                     goto error_ret;
9178
9179                   if (sym_sec != toc)
9180                     continue;
9181
9182                   if (h != NULL)
9183                     val = h->root.u.def.value;
9184                   else
9185                     {
9186                       val = sym->st_value;
9187                       if (val != 0)
9188                         local_toc_syms = TRUE;
9189                     }
9190
9191                   val += rel->r_addend;
9192
9193                   if (val > toc->rawsize)
9194                     val = toc->rawsize;
9195                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
9196                     continue;
9197                   else if ((skip[val >> 3] & can_optimize) != 0)
9198                     {
9199                       Elf_Internal_Rela *tocrel
9200                         = toc_relocs + (skip[val >> 3] >> 2);
9201                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9202
9203                       switch (r_type)
9204                         {
9205                         case R_PPC64_TOC16_HA:
9206                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9207                           break;
9208
9209                         case R_PPC64_TOC16_LO_DS:
9210                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9211                           break;
9212
9213                         default:
9214                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9215                             ppc_howto_init ();
9216                           info->callbacks->einfo
9217                             (_("%P: %H: %s references "
9218                                "optimized away TOC entry\n"),
9219                              ibfd, sec, rel->r_offset,
9220                              ppc64_elf_howto_table[r_type]->name);
9221                           bfd_set_error (bfd_error_bad_value);
9222                           goto error_ret;
9223                         }
9224                       rel->r_addend = tocrel->r_addend;
9225                       elf_section_data (sec)->relocs = relstart;
9226                       continue;
9227                     }
9228
9229                   if (h != NULL || sym->st_value != 0)
9230                     continue;
9231
9232                   rel->r_addend -= skip[val >> 3];
9233                   elf_section_data (sec)->relocs = relstart;
9234                 }
9235
9236               if (elf_section_data (sec)->relocs != relstart)
9237                 free (relstart);
9238             }
9239
9240           /* We shouldn't have local or global symbols defined in the TOC,
9241              but handle them anyway.  */
9242           if (local_syms != NULL)
9243             for (sym = local_syms;
9244                  sym < local_syms + symtab_hdr->sh_info;
9245                  ++sym)
9246               if (sym->st_value != 0
9247                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9248                 {
9249                   unsigned long i;
9250
9251                   if (sym->st_value > toc->rawsize)
9252                     i = toc->rawsize >> 3;
9253                   else
9254                     i = sym->st_value >> 3;
9255
9256                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9257                     {
9258                       if (local_toc_syms)
9259                         (*_bfd_error_handler)
9260                           (_("%s defined on removed toc entry"),
9261                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9262                       do
9263                         ++i;
9264                       while ((skip[i] & (ref_from_discarded | can_optimize)));
9265                       sym->st_value = (bfd_vma) i << 3;
9266                     }
9267
9268                   sym->st_value -= skip[i];
9269                   symtab_hdr->contents = (unsigned char *) local_syms;
9270                 }
9271
9272           /* Adjust any global syms defined in this toc input section.  */
9273           if (toc_inf.global_toc_syms)
9274             {
9275               toc_inf.toc = toc;
9276               toc_inf.skip = skip;
9277               toc_inf.global_toc_syms = FALSE;
9278               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9279                                       &toc_inf);
9280             }
9281
9282           if (toc->reloc_count != 0)
9283             {
9284               Elf_Internal_Shdr *rel_hdr;
9285               Elf_Internal_Rela *wrel;
9286               bfd_size_type sz;
9287
9288               /* Remove unused toc relocs, and adjust those we keep.  */
9289               if (toc_relocs == NULL)
9290                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9291                                                         info->keep_memory);
9292               if (toc_relocs == NULL)
9293                 goto error_ret;
9294
9295               wrel = toc_relocs;
9296               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9297                 if ((skip[rel->r_offset >> 3]
9298                      & (ref_from_discarded | can_optimize)) == 0)
9299                   {
9300                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9301                     wrel->r_info = rel->r_info;
9302                     wrel->r_addend = rel->r_addend;
9303                     ++wrel;
9304                   }
9305                 else if (!dec_dynrel_count (rel->r_info, toc, info,
9306                                             &local_syms, NULL, NULL))
9307                   goto error_ret;
9308
9309               elf_section_data (toc)->relocs = toc_relocs;
9310               toc->reloc_count = wrel - toc_relocs;
9311               rel_hdr = _bfd_elf_single_rel_hdr (toc);
9312               sz = rel_hdr->sh_entsize;
9313               rel_hdr->sh_size = toc->reloc_count * sz;
9314             }
9315         }
9316       else if (toc_relocs != NULL
9317                && elf_section_data (toc)->relocs != toc_relocs)
9318         free (toc_relocs);
9319
9320       if (local_syms != NULL
9321           && symtab_hdr->contents != (unsigned char *) local_syms)
9322         {
9323           if (!info->keep_memory)
9324             free (local_syms);
9325           else
9326             symtab_hdr->contents = (unsigned char *) local_syms;
9327         }
9328       free (skip);
9329     }
9330
9331   return TRUE;
9332 }
9333
9334 /* Return true iff input section I references the TOC using
9335    instructions limited to +/-32k offsets.  */
9336
9337 bfd_boolean
9338 ppc64_elf_has_small_toc_reloc (asection *i)
9339 {
9340   return (is_ppc64_elf (i->owner)
9341           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9342 }
9343
9344 /* Allocate space for one GOT entry.  */
9345
9346 static void
9347 allocate_got (struct elf_link_hash_entry *h,
9348               struct bfd_link_info *info,
9349               struct got_entry *gent)
9350 {
9351   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9352   bfd_boolean dyn;
9353   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9354   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9355                  ? 16 : 8);
9356   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9357                   ? 2 : 1) * sizeof (Elf64_External_Rela);
9358   asection *got = ppc64_elf_tdata (gent->owner)->got;
9359
9360   gent->got.offset = got->size;
9361   got->size += entsize;
9362
9363   dyn = htab->elf.dynamic_sections_created;
9364   if (h->type == STT_GNU_IFUNC)
9365     {
9366       htab->elf.irelplt->size += rentsize;
9367       htab->got_reli_size += rentsize;
9368     }
9369   else if ((info->shared
9370             || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9371            && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9372                || h->root.type != bfd_link_hash_undefweak))
9373     {
9374       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9375       relgot->size += rentsize;
9376     }
9377 }
9378
9379 /* This function merges got entries in the same toc group.  */
9380
9381 static void
9382 merge_got_entries (struct got_entry **pent)
9383 {
9384   struct got_entry *ent, *ent2;
9385
9386   for (ent = *pent; ent != NULL; ent = ent->next)
9387     if (!ent->is_indirect)
9388       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9389         if (!ent2->is_indirect
9390             && ent2->addend == ent->addend
9391             && ent2->tls_type == ent->tls_type
9392             && elf_gp (ent2->owner) == elf_gp (ent->owner))
9393           {
9394             ent2->is_indirect = TRUE;
9395             ent2->got.ent = ent;
9396           }
9397 }
9398
9399 /* Allocate space in .plt, .got and associated reloc sections for
9400    dynamic relocs.  */
9401
9402 static bfd_boolean
9403 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9404 {
9405   struct bfd_link_info *info;
9406   struct ppc_link_hash_table *htab;
9407   asection *s;
9408   struct ppc_link_hash_entry *eh;
9409   struct elf_dyn_relocs *p;
9410   struct got_entry **pgent, *gent;
9411
9412   if (h->root.type == bfd_link_hash_indirect)
9413     return TRUE;
9414
9415   info = (struct bfd_link_info *) inf;
9416   htab = ppc_hash_table (info);
9417   if (htab == NULL)
9418     return FALSE;
9419
9420   if ((htab->elf.dynamic_sections_created
9421        && h->dynindx != -1
9422        && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
9423       || h->type == STT_GNU_IFUNC)
9424     {
9425       struct plt_entry *pent;
9426       bfd_boolean doneone = FALSE;
9427       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9428         if (pent->plt.refcount > 0)
9429           {
9430             if (!htab->elf.dynamic_sections_created
9431                 || h->dynindx == -1)
9432               {
9433                 s = htab->elf.iplt;
9434                 pent->plt.offset = s->size;
9435                 s->size += PLT_ENTRY_SIZE (htab);
9436                 s = htab->elf.irelplt;
9437               }
9438             else
9439               {
9440                 /* If this is the first .plt entry, make room for the special
9441                    first entry.  */
9442                 s = htab->elf.splt;
9443                 if (s->size == 0)
9444                   s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9445
9446                 pent->plt.offset = s->size;
9447
9448                 /* Make room for this entry.  */
9449                 s->size += PLT_ENTRY_SIZE (htab);
9450
9451                 /* Make room for the .glink code.  */
9452                 s = htab->glink;
9453                 if (s->size == 0)
9454                   s->size += GLINK_CALL_STUB_SIZE;
9455                 if (htab->opd_abi)
9456                   {
9457                     /* We need bigger stubs past index 32767.  */
9458                     if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9459                       s->size += 4;
9460                     s->size += 2*4;
9461                   }
9462                 else
9463                   s->size += 4;
9464
9465                 /* We also need to make an entry in the .rela.plt section.  */
9466                 s = htab->elf.srelplt;
9467               }
9468             s->size += sizeof (Elf64_External_Rela);
9469             doneone = TRUE;
9470           }
9471         else
9472           pent->plt.offset = (bfd_vma) -1;
9473       if (!doneone)
9474         {
9475           h->plt.plist = NULL;
9476           h->needs_plt = 0;
9477         }
9478     }
9479   else
9480     {
9481       h->plt.plist = NULL;
9482       h->needs_plt = 0;
9483     }
9484
9485   eh = (struct ppc_link_hash_entry *) h;
9486   /* Run through the TLS GD got entries first if we're changing them
9487      to TPREL.  */
9488   if ((eh->tls_mask & TLS_TPRELGD) != 0)
9489     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9490       if (gent->got.refcount > 0
9491           && (gent->tls_type & TLS_GD) != 0)
9492         {
9493           /* This was a GD entry that has been converted to TPREL.  If
9494              there happens to be a TPREL entry we can use that one.  */
9495           struct got_entry *ent;
9496           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9497             if (ent->got.refcount > 0
9498                 && (ent->tls_type & TLS_TPREL) != 0
9499                 && ent->addend == gent->addend
9500                 && ent->owner == gent->owner)
9501               {
9502                 gent->got.refcount = 0;
9503                 break;
9504               }
9505
9506           /* If not, then we'll be using our own TPREL entry.  */
9507           if (gent->got.refcount != 0)
9508             gent->tls_type = TLS_TLS | TLS_TPREL;
9509         }
9510
9511   /* Remove any list entry that won't generate a word in the GOT before
9512      we call merge_got_entries.  Otherwise we risk merging to empty
9513      entries.  */
9514   pgent = &h->got.glist;
9515   while ((gent = *pgent) != NULL)
9516     if (gent->got.refcount > 0)
9517       {
9518         if ((gent->tls_type & TLS_LD) != 0
9519             && !h->def_dynamic)
9520           {
9521             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9522             *pgent = gent->next;
9523           }
9524         else
9525           pgent = &gent->next;
9526       }
9527     else
9528       *pgent = gent->next;
9529
9530   if (!htab->do_multi_toc)
9531     merge_got_entries (&h->got.glist);
9532
9533   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9534     if (!gent->is_indirect)
9535       {
9536         /* Make sure this symbol is output as a dynamic symbol.
9537            Undefined weak syms won't yet be marked as dynamic,
9538            nor will all TLS symbols.  */
9539         if (h->dynindx == -1
9540             && !h->forced_local
9541             && h->type != STT_GNU_IFUNC
9542             && htab->elf.dynamic_sections_created)
9543           {
9544             if (! bfd_elf_link_record_dynamic_symbol (info, h))
9545               return FALSE;
9546           }
9547
9548         if (!is_ppc64_elf (gent->owner))
9549           abort ();
9550
9551         allocate_got (h, info, gent);
9552       }
9553
9554   if (eh->dyn_relocs == NULL
9555       || (!htab->elf.dynamic_sections_created
9556           && h->type != STT_GNU_IFUNC))
9557     return TRUE;
9558
9559   /* In the shared -Bsymbolic case, discard space allocated for
9560      dynamic pc-relative relocs against symbols which turn out to be
9561      defined in regular objects.  For the normal shared case, discard
9562      space for relocs that have become local due to symbol visibility
9563      changes.  */
9564
9565   if (info->shared)
9566     {
9567       /* Relocs that use pc_count are those that appear on a call insn,
9568          or certain REL relocs (see must_be_dyn_reloc) that can be
9569          generated via assembly.  We want calls to protected symbols to
9570          resolve directly to the function rather than going via the plt.
9571          If people want function pointer comparisons to work as expected
9572          then they should avoid writing weird assembly.  */
9573       if (SYMBOL_CALLS_LOCAL (info, h))
9574         {
9575           struct elf_dyn_relocs **pp;
9576
9577           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9578             {
9579               p->count -= p->pc_count;
9580               p->pc_count = 0;
9581               if (p->count == 0)
9582                 *pp = p->next;
9583               else
9584                 pp = &p->next;
9585             }
9586         }
9587
9588       /* Also discard relocs on undefined weak syms with non-default
9589          visibility.  */
9590       if (eh->dyn_relocs != NULL
9591           && h->root.type == bfd_link_hash_undefweak)
9592         {
9593           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9594             eh->dyn_relocs = NULL;
9595
9596           /* Make sure this symbol is output as a dynamic symbol.
9597              Undefined weak syms won't yet be marked as dynamic.  */
9598           else if (h->dynindx == -1
9599                    && !h->forced_local)
9600             {
9601               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9602                 return FALSE;
9603             }
9604         }
9605     }
9606   else if (h->type == STT_GNU_IFUNC)
9607     {
9608       if (!h->non_got_ref)
9609         eh->dyn_relocs = NULL;
9610     }
9611   else if (ELIMINATE_COPY_RELOCS)
9612     {
9613       /* For the non-shared case, discard space for relocs against
9614          symbols which turn out to need copy relocs or are not
9615          dynamic.  */
9616
9617       if (!h->non_got_ref
9618           && !h->def_regular)
9619         {
9620           /* Make sure this symbol is output as a dynamic symbol.
9621              Undefined weak syms won't yet be marked as dynamic.  */
9622           if (h->dynindx == -1
9623               && !h->forced_local)
9624             {
9625               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9626                 return FALSE;
9627             }
9628
9629           /* If that succeeded, we know we'll be keeping all the
9630              relocs.  */
9631           if (h->dynindx != -1)
9632             goto keep;
9633         }
9634
9635       eh->dyn_relocs = NULL;
9636
9637     keep: ;
9638     }
9639
9640   /* Finally, allocate space.  */
9641   for (p = eh->dyn_relocs; p != NULL; p = p->next)
9642     {
9643       asection *sreloc = elf_section_data (p->sec)->sreloc;
9644       if (eh->elf.type == STT_GNU_IFUNC)
9645         sreloc = htab->elf.irelplt;
9646       sreloc->size += p->count * sizeof (Elf64_External_Rela);
9647     }
9648
9649   return TRUE;
9650 }
9651
9652 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9653    to set up space for global entry stubs.  These are put in glink,
9654    after the branch table.  */
9655
9656 static bfd_boolean
9657 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9658 {
9659   struct bfd_link_info *info;
9660   struct ppc_link_hash_table *htab;
9661   struct plt_entry *pent;
9662   asection *s;
9663
9664   if (h->root.type == bfd_link_hash_indirect)
9665     return TRUE;
9666
9667   if (!h->pointer_equality_needed)
9668     return TRUE;
9669
9670   if (h->def_regular)
9671     return TRUE;
9672
9673   info = inf;
9674   htab = ppc_hash_table (info);
9675   if (htab == NULL)
9676     return FALSE;
9677
9678   s = htab->glink;
9679   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9680     if (pent->plt.offset != (bfd_vma) -1
9681         && pent->addend == 0)
9682       {
9683         /* For ELFv2, if this symbol is not defined in a regular file
9684            and we are not generating a shared library or pie, then we
9685            need to define the symbol in the executable on a call stub.
9686            This is to avoid text relocations.  */
9687         s->size = (s->size + 15) & -16;
9688         h->root.u.def.section = s;
9689         h->root.u.def.value = s->size;
9690         s->size += 16;
9691         break;
9692       }
9693   return TRUE;
9694 }
9695
9696 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9697    read-only sections.  */
9698
9699 static bfd_boolean
9700 maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
9701 {
9702   if (h->root.type == bfd_link_hash_indirect)
9703     return TRUE;
9704
9705   if (readonly_dynrelocs (h))
9706     {
9707       ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
9708
9709       /* Not an error, just cut short the traversal.  */
9710       return FALSE;
9711     }
9712   return TRUE;
9713 }
9714
9715 /* Set the sizes of the dynamic sections.  */
9716
9717 static bfd_boolean
9718 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9719                                  struct bfd_link_info *info)
9720 {
9721   struct ppc_link_hash_table *htab;
9722   bfd *dynobj;
9723   asection *s;
9724   bfd_boolean relocs;
9725   bfd *ibfd;
9726   struct got_entry *first_tlsld;
9727
9728   htab = ppc_hash_table (info);
9729   if (htab == NULL)
9730     return FALSE;
9731
9732   dynobj = htab->elf.dynobj;
9733   if (dynobj == NULL)
9734     abort ();
9735
9736   if (htab->elf.dynamic_sections_created)
9737     {
9738       /* Set the contents of the .interp section to the interpreter.  */
9739       if (info->executable)
9740         {
9741           s = bfd_get_linker_section (dynobj, ".interp");
9742           if (s == NULL)
9743             abort ();
9744           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9745           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9746         }
9747     }
9748
9749   /* Set up .got offsets for local syms, and space for local dynamic
9750      relocs.  */
9751   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9752     {
9753       struct got_entry **lgot_ents;
9754       struct got_entry **end_lgot_ents;
9755       struct plt_entry **local_plt;
9756       struct plt_entry **end_local_plt;
9757       unsigned char *lgot_masks;
9758       bfd_size_type locsymcount;
9759       Elf_Internal_Shdr *symtab_hdr;
9760
9761       if (!is_ppc64_elf (ibfd))
9762         continue;
9763
9764       for (s = ibfd->sections; s != NULL; s = s->next)
9765         {
9766           struct ppc_dyn_relocs *p;
9767
9768           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9769             {
9770               if (!bfd_is_abs_section (p->sec)
9771                   && bfd_is_abs_section (p->sec->output_section))
9772                 {
9773                   /* Input section has been discarded, either because
9774                      it is a copy of a linkonce section or due to
9775                      linker script /DISCARD/, so we'll be discarding
9776                      the relocs too.  */
9777                 }
9778               else if (p->count != 0)
9779                 {
9780                   asection *srel = elf_section_data (p->sec)->sreloc;
9781                   if (p->ifunc)
9782                     srel = htab->elf.irelplt;
9783                   srel->size += p->count * sizeof (Elf64_External_Rela);
9784                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9785                     info->flags |= DF_TEXTREL;
9786                 }
9787             }
9788         }
9789
9790       lgot_ents = elf_local_got_ents (ibfd);
9791       if (!lgot_ents)
9792         continue;
9793
9794       symtab_hdr = &elf_symtab_hdr (ibfd);
9795       locsymcount = symtab_hdr->sh_info;
9796       end_lgot_ents = lgot_ents + locsymcount;
9797       local_plt = (struct plt_entry **) end_lgot_ents;
9798       end_local_plt = local_plt + locsymcount;
9799       lgot_masks = (unsigned char *) end_local_plt;
9800       s = ppc64_elf_tdata (ibfd)->got;
9801       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9802         {
9803           struct got_entry **pent, *ent;
9804
9805           pent = lgot_ents;
9806           while ((ent = *pent) != NULL)
9807             if (ent->got.refcount > 0)
9808               {
9809                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9810                   {
9811                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
9812                     *pent = ent->next;
9813                   }
9814                 else
9815                   {
9816                     unsigned int ent_size = 8;
9817                     unsigned int rel_size = sizeof (Elf64_External_Rela);
9818
9819                     ent->got.offset = s->size;
9820                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9821                       {
9822                         ent_size *= 2;
9823                         rel_size *= 2;
9824                       }
9825                     s->size += ent_size;
9826                     if ((*lgot_masks & PLT_IFUNC) != 0)
9827                       {
9828                         htab->elf.irelplt->size += rel_size;
9829                         htab->got_reli_size += rel_size;
9830                       }
9831                     else if (info->shared)
9832                       {
9833                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9834                         srel->size += rel_size;
9835                       }
9836                     pent = &ent->next;
9837                   }
9838               }
9839             else
9840               *pent = ent->next;
9841         }
9842
9843       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
9844       for (; local_plt < end_local_plt; ++local_plt)
9845         {
9846           struct plt_entry *ent;
9847
9848           for (ent = *local_plt; ent != NULL; ent = ent->next)
9849             if (ent->plt.refcount > 0)
9850               {
9851                 s = htab->elf.iplt;
9852                 ent->plt.offset = s->size;
9853                 s->size += PLT_ENTRY_SIZE (htab);
9854
9855                 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9856               }
9857             else
9858               ent->plt.offset = (bfd_vma) -1;
9859         }
9860     }
9861
9862   /* Allocate global sym .plt and .got entries, and space for global
9863      sym dynamic relocs.  */
9864   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9865   /* Stash the end of glink branch table.  */
9866   if (htab->glink != NULL)
9867     htab->glink->rawsize = htab->glink->size;
9868
9869   if (!htab->opd_abi && !info->shared)
9870     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
9871
9872   first_tlsld = NULL;
9873   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9874     {
9875       struct got_entry *ent;
9876
9877       if (!is_ppc64_elf (ibfd))
9878         continue;
9879
9880       ent = ppc64_tlsld_got (ibfd);
9881       if (ent->got.refcount > 0)
9882         {
9883           if (!htab->do_multi_toc && first_tlsld != NULL)
9884             {
9885               ent->is_indirect = TRUE;
9886               ent->got.ent = first_tlsld;
9887             }
9888           else
9889             {
9890               if (first_tlsld == NULL)
9891                 first_tlsld = ent;
9892               s = ppc64_elf_tdata (ibfd)->got;
9893               ent->got.offset = s->size;
9894               ent->owner = ibfd;
9895               s->size += 16;
9896               if (info->shared)
9897                 {
9898                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9899                   srel->size += sizeof (Elf64_External_Rela);
9900                 }
9901             }
9902         }
9903       else
9904         ent->got.offset = (bfd_vma) -1;
9905     }
9906
9907   /* We now have determined the sizes of the various dynamic sections.
9908      Allocate memory for them.  */
9909   relocs = FALSE;
9910   for (s = dynobj->sections; s != NULL; s = s->next)
9911     {
9912       if ((s->flags & SEC_LINKER_CREATED) == 0)
9913         continue;
9914
9915       if (s == htab->brlt || s == htab->relbrlt)
9916         /* These haven't been allocated yet;  don't strip.  */
9917         continue;
9918       else if (s == htab->elf.sgot
9919                || s == htab->elf.splt
9920                || s == htab->elf.iplt
9921                || s == htab->glink
9922                || s == htab->dynbss)
9923         {
9924           /* Strip this section if we don't need it; see the
9925              comment below.  */
9926         }
9927       else if (s == htab->glink_eh_frame)
9928         {
9929           if (!bfd_is_abs_section (s->output_section))
9930             /* Not sized yet.  */
9931             continue;
9932         }
9933       else if (CONST_STRNEQ (s->name, ".rela"))
9934         {
9935           if (s->size != 0)
9936             {
9937               if (s != htab->elf.srelplt)
9938                 relocs = TRUE;
9939
9940               /* We use the reloc_count field as a counter if we need
9941                  to copy relocs into the output file.  */
9942               s->reloc_count = 0;
9943             }
9944         }
9945       else
9946         {
9947           /* It's not one of our sections, so don't allocate space.  */
9948           continue;
9949         }
9950
9951       if (s->size == 0)
9952         {
9953           /* If we don't need this section, strip it from the
9954              output file.  This is mostly to handle .rela.bss and
9955              .rela.plt.  We must create both sections in
9956              create_dynamic_sections, because they must be created
9957              before the linker maps input sections to output
9958              sections.  The linker does that before
9959              adjust_dynamic_symbol is called, and it is that
9960              function which decides whether anything needs to go
9961              into these sections.  */
9962           s->flags |= SEC_EXCLUDE;
9963           continue;
9964         }
9965
9966       if ((s->flags & SEC_HAS_CONTENTS) == 0)
9967         continue;
9968
9969       /* Allocate memory for the section contents.  We use bfd_zalloc
9970          here in case unused entries are not reclaimed before the
9971          section's contents are written out.  This should not happen,
9972          but this way if it does we get a R_PPC64_NONE reloc in .rela
9973          sections instead of garbage.
9974          We also rely on the section contents being zero when writing
9975          the GOT.  */
9976       s->contents = bfd_zalloc (dynobj, s->size);
9977       if (s->contents == NULL)
9978         return FALSE;
9979     }
9980
9981   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9982     {
9983       if (!is_ppc64_elf (ibfd))
9984         continue;
9985
9986       s = ppc64_elf_tdata (ibfd)->got;
9987       if (s != NULL && s != htab->elf.sgot)
9988         {
9989           if (s->size == 0)
9990             s->flags |= SEC_EXCLUDE;
9991           else
9992             {
9993               s->contents = bfd_zalloc (ibfd, s->size);
9994               if (s->contents == NULL)
9995                 return FALSE;
9996             }
9997         }
9998       s = ppc64_elf_tdata (ibfd)->relgot;
9999       if (s != NULL)
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               relocs = TRUE;
10009               s->reloc_count = 0;
10010             }
10011         }
10012     }
10013
10014   if (htab->elf.dynamic_sections_created)
10015     {
10016       bfd_boolean tls_opt;
10017
10018       /* Add some entries to the .dynamic section.  We fill in the
10019          values later, in ppc64_elf_finish_dynamic_sections, but we
10020          must add the entries now so that we get the correct size for
10021          the .dynamic section.  The DT_DEBUG entry is filled in by the
10022          dynamic linker and used by the debugger.  */
10023 #define add_dynamic_entry(TAG, VAL) \
10024   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10025
10026       if (info->executable)
10027         {
10028           if (!add_dynamic_entry (DT_DEBUG, 0))
10029             return FALSE;
10030         }
10031
10032       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10033         {
10034           if (!add_dynamic_entry (DT_PLTGOT, 0)
10035               || !add_dynamic_entry (DT_PLTRELSZ, 0)
10036               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10037               || !add_dynamic_entry (DT_JMPREL, 0)
10038               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10039             return FALSE;
10040         }
10041
10042       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10043         {
10044           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10045               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10046             return FALSE;
10047         }
10048
10049       tls_opt = (!htab->params->no_tls_get_addr_opt
10050                  && htab->tls_get_addr_fd != NULL
10051                  && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10052       if (tls_opt || !htab->opd_abi)
10053         {
10054           if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10055             return FALSE;
10056         }
10057
10058       if (relocs)
10059         {
10060           if (!add_dynamic_entry (DT_RELA, 0)
10061               || !add_dynamic_entry (DT_RELASZ, 0)
10062               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10063             return FALSE;
10064
10065           /* If any dynamic relocs apply to a read-only section,
10066              then we need a DT_TEXTREL entry.  */
10067           if ((info->flags & DF_TEXTREL) == 0)
10068             elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10069
10070           if ((info->flags & DF_TEXTREL) != 0)
10071             {
10072               if (!add_dynamic_entry (DT_TEXTREL, 0))
10073                 return FALSE;
10074             }
10075         }
10076     }
10077 #undef add_dynamic_entry
10078
10079   return TRUE;
10080 }
10081
10082 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
10083
10084 static bfd_boolean
10085 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10086 {
10087   if (h->plt.plist != NULL
10088       && !h->def_regular
10089       && !h->pointer_equality_needed)
10090     return FALSE;
10091
10092   return _bfd_elf_hash_symbol (h);
10093 }
10094
10095 /* Determine the type of stub needed, if any, for a call.  */
10096
10097 static inline enum ppc_stub_type
10098 ppc_type_of_stub (asection *input_sec,
10099                   const Elf_Internal_Rela *rel,
10100                   struct ppc_link_hash_entry **hash,
10101                   struct plt_entry **plt_ent,
10102                   bfd_vma destination,
10103                   unsigned long local_off)
10104 {
10105   struct ppc_link_hash_entry *h = *hash;
10106   bfd_vma location;
10107   bfd_vma branch_offset;
10108   bfd_vma max_branch_offset;
10109   enum elf_ppc64_reloc_type r_type;
10110
10111   if (h != NULL)
10112     {
10113       struct plt_entry *ent;
10114       struct ppc_link_hash_entry *fdh = h;
10115       if (h->oh != NULL
10116           && h->oh->is_func_descriptor)
10117         {
10118           fdh = ppc_follow_link (h->oh);
10119           *hash = fdh;
10120         }
10121
10122       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10123         if (ent->addend == rel->r_addend
10124             && ent->plt.offset != (bfd_vma) -1)
10125           {
10126             *plt_ent = ent;
10127             return ppc_stub_plt_call;
10128           }
10129
10130       /* Here, we know we don't have a plt entry.  If we don't have a
10131          either a defined function descriptor or a defined entry symbol
10132          in a regular object file, then it is pointless trying to make
10133          any other type of stub.  */
10134       if (!is_static_defined (&fdh->elf)
10135           && !is_static_defined (&h->elf))
10136         return ppc_stub_none;
10137     }
10138   else if (elf_local_got_ents (input_sec->owner) != NULL)
10139     {
10140       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10141       struct plt_entry **local_plt = (struct plt_entry **)
10142         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10143       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10144
10145       if (local_plt[r_symndx] != NULL)
10146         {
10147           struct plt_entry *ent;
10148
10149           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10150             if (ent->addend == rel->r_addend
10151                 && ent->plt.offset != (bfd_vma) -1)
10152               {
10153                 *plt_ent = ent;
10154                 return ppc_stub_plt_call;
10155               }
10156         }
10157     }
10158
10159   /* Determine where the call point is.  */
10160   location = (input_sec->output_offset
10161               + input_sec->output_section->vma
10162               + rel->r_offset);
10163
10164   branch_offset = destination - location;
10165   r_type = ELF64_R_TYPE (rel->r_info);
10166
10167   /* Determine if a long branch stub is needed.  */
10168   max_branch_offset = 1 << 25;
10169   if (r_type != R_PPC64_REL24)
10170     max_branch_offset = 1 << 15;
10171
10172   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10173     /* We need a stub.  Figure out whether a long_branch or plt_branch
10174        is needed later.  */
10175     return ppc_stub_long_branch;
10176
10177   return ppc_stub_none;
10178 }
10179
10180 /* With power7 weakly ordered memory model, it is possible for ld.so
10181    to update a plt entry in one thread and have another thread see a
10182    stale zero toc entry.  To avoid this we need some sort of acquire
10183    barrier in the call stub.  One solution is to make the load of the
10184    toc word seem to appear to depend on the load of the function entry
10185    word.  Another solution is to test for r2 being zero, and branch to
10186    the appropriate glink entry if so.
10187
10188    .    fake dep barrier        compare
10189    .    ld 12,xxx(2)            ld 12,xxx(2)
10190    .    mtctr 12                mtctr 12
10191    .    xor 11,12,12            ld 2,xxx+8(2)
10192    .    add 2,2,11              cmpldi 2,0
10193    .    ld 2,xxx+8(2)           bnectr+
10194    .    bctr                    b <glink_entry>
10195
10196    The solution involving the compare turns out to be faster, so
10197    that's what we use unless the branch won't reach.  */
10198
10199 #define ALWAYS_USE_FAKE_DEP 0
10200 #define ALWAYS_EMIT_R2SAVE 0
10201
10202 #define PPC_LO(v) ((v) & 0xffff)
10203 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10204 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10205
10206 static inline unsigned int
10207 plt_stub_size (struct ppc_link_hash_table *htab,
10208                struct ppc_stub_hash_entry *stub_entry,
10209                bfd_vma off)
10210 {
10211   unsigned size = 12;
10212
10213   if (ALWAYS_EMIT_R2SAVE
10214       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10215     size += 4;
10216   if (PPC_HA (off) != 0)
10217     size += 4;
10218   if (htab->opd_abi)
10219     {
10220       size += 4;
10221       if (htab->params->plt_static_chain)
10222         size += 4;
10223       if (htab->params->plt_thread_safe)
10224         size += 8;
10225       if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10226         size += 4;
10227     }
10228   if (stub_entry->h != NULL
10229       && (stub_entry->h == htab->tls_get_addr_fd
10230           || stub_entry->h == htab->tls_get_addr)
10231       && !htab->params->no_tls_get_addr_opt)
10232     size += 13 * 4;
10233   return size;
10234 }
10235
10236 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10237    then return the padding needed to do so.  */
10238 static inline unsigned int
10239 plt_stub_pad (struct ppc_link_hash_table *htab,
10240               struct ppc_stub_hash_entry *stub_entry,
10241               bfd_vma plt_off)
10242 {
10243   int stub_align = 1 << htab->params->plt_stub_align;
10244   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10245   bfd_vma stub_off = stub_entry->stub_sec->size;
10246
10247   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10248       > ((stub_size - 1) & -stub_align))
10249     return stub_align - (stub_off & (stub_align - 1));
10250   return 0;
10251 }
10252
10253 /* Build a .plt call stub.  */
10254
10255 static inline bfd_byte *
10256 build_plt_stub (struct ppc_link_hash_table *htab,
10257                 struct ppc_stub_hash_entry *stub_entry,
10258                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10259 {
10260   bfd *obfd = htab->params->stub_bfd;
10261   bfd_boolean plt_load_toc = htab->opd_abi;
10262   bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10263   bfd_boolean plt_thread_safe = htab->params->plt_thread_safe;
10264   bfd_boolean use_fake_dep = plt_thread_safe;
10265   bfd_vma cmp_branch_off = 0;
10266
10267   if (!ALWAYS_USE_FAKE_DEP
10268       && plt_load_toc
10269       && plt_thread_safe
10270       && !(stub_entry->h != NULL
10271            && (stub_entry->h == htab->tls_get_addr_fd
10272                || stub_entry->h == htab->tls_get_addr)
10273            && !htab->params->no_tls_get_addr_opt))
10274     {
10275       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10276       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10277                           / PLT_ENTRY_SIZE (htab));
10278       bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10279       bfd_vma to, from;
10280
10281       if (pltindex > 32768)
10282         glinkoff += (pltindex - 32768) * 4;
10283       to = (glinkoff
10284             + htab->glink->output_offset
10285             + htab->glink->output_section->vma);
10286       from = (p - stub_entry->stub_sec->contents
10287               + 4 * (ALWAYS_EMIT_R2SAVE
10288                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10289               + 4 * (PPC_HA (offset) != 0)
10290               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10291                      != PPC_HA (offset))
10292               + 4 * (plt_static_chain != 0)
10293               + 20
10294               + stub_entry->stub_sec->output_offset
10295               + stub_entry->stub_sec->output_section->vma);
10296       cmp_branch_off = to - from;
10297       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10298     }
10299
10300   if (PPC_HA (offset) != 0)
10301     {
10302       if (r != NULL)
10303         {
10304           if (ALWAYS_EMIT_R2SAVE
10305               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10306             r[0].r_offset += 4;
10307           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10308           r[1].r_offset = r[0].r_offset + 4;
10309           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10310           r[1].r_addend = r[0].r_addend;
10311           if (plt_load_toc)
10312             {
10313               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10314                 {
10315                   r[2].r_offset = r[1].r_offset + 4;
10316                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10317                   r[2].r_addend = r[0].r_addend;
10318                 }
10319               else
10320                 {
10321                   r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10322                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10323                   r[2].r_addend = r[0].r_addend + 8;
10324                   if (plt_static_chain)
10325                     {
10326                       r[3].r_offset = r[2].r_offset + 4;
10327                       r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10328                       r[3].r_addend = r[0].r_addend + 16;
10329                     }
10330                 }
10331             }
10332         }
10333       if (ALWAYS_EMIT_R2SAVE
10334           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10335         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10336       if (plt_load_toc)
10337         {
10338           bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10339           bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),  p += 4;
10340         }
10341       else
10342         {
10343           bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10344           bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),  p += 4;
10345         }
10346       if (plt_load_toc
10347           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10348         {
10349           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10350           offset = 0;
10351         }
10352       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10353       if (plt_load_toc)
10354         {
10355           if (use_fake_dep)
10356             {
10357               bfd_put_32 (obfd, XOR_R2_R12_R12, p),             p += 4;
10358               bfd_put_32 (obfd, ADD_R11_R11_R2, p),             p += 4;
10359             }
10360           bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10361           if (plt_static_chain)
10362             bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10363         }
10364     }
10365   else
10366     {
10367       if (r != NULL)
10368         {
10369           if (ALWAYS_EMIT_R2SAVE
10370               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10371             r[0].r_offset += 4;
10372           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10373           if (plt_load_toc)
10374             {
10375               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10376                 {
10377                   r[1].r_offset = r[0].r_offset + 4;
10378                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10379                   r[1].r_addend = r[0].r_addend;
10380                 }
10381               else
10382                 {
10383                   r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10384                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10385                   r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10386                   if (plt_static_chain)
10387                     {
10388                       r[2].r_offset = r[1].r_offset + 4;
10389                       r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10390                       r[2].r_addend = r[0].r_addend + 8;
10391                     }
10392                 }
10393             }
10394         }
10395       if (ALWAYS_EMIT_R2SAVE
10396           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10397         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10398       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
10399       if (plt_load_toc
10400           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10401         {
10402           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
10403           offset = 0;
10404         }
10405       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10406       if (plt_load_toc)
10407         {
10408           if (use_fake_dep)
10409             {
10410               bfd_put_32 (obfd, XOR_R11_R12_R12, p),            p += 4;
10411               bfd_put_32 (obfd, ADD_R2_R2_R11, p),              p += 4;
10412             }
10413           if (plt_static_chain)
10414             bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10415           bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10416         }
10417     }
10418   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10419     {
10420       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
10421       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
10422       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10423     }
10424   else
10425     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
10426   return p;
10427 }
10428
10429 /* Build a special .plt call stub for __tls_get_addr.  */
10430
10431 #define LD_R11_0R3      0xe9630000
10432 #define LD_R12_0R3      0xe9830000
10433 #define MR_R0_R3        0x7c601b78
10434 #define CMPDI_R11_0     0x2c2b0000
10435 #define ADD_R3_R12_R13  0x7c6c6a14
10436 #define BEQLR           0x4d820020
10437 #define MR_R3_R0        0x7c030378
10438 #define STD_R11_0R1     0xf9610000
10439 #define BCTRL           0x4e800421
10440 #define LD_R11_0R1      0xe9610000
10441 #define MTLR_R11        0x7d6803a6
10442
10443 static inline bfd_byte *
10444 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10445                          struct ppc_stub_hash_entry *stub_entry,
10446                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10447 {
10448   bfd *obfd = htab->params->stub_bfd;
10449
10450   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
10451   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
10452   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
10453   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
10454   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
10455   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
10456   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
10457   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
10458   bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10459
10460   if (r != NULL)
10461     r[0].r_offset += 9 * 4;
10462   p = build_plt_stub (htab, stub_entry, p, offset, r);
10463   bfd_put_32 (obfd, BCTRL, p - 4);
10464
10465   bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10466   bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p),     p += 4;
10467   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
10468   bfd_put_32 (obfd, BLR, p),                    p += 4;
10469
10470   return p;
10471 }
10472
10473 static Elf_Internal_Rela *
10474 get_relocs (asection *sec, int count)
10475 {
10476   Elf_Internal_Rela *relocs;
10477   struct bfd_elf_section_data *elfsec_data;
10478
10479   elfsec_data = elf_section_data (sec);
10480   relocs = elfsec_data->relocs;
10481   if (relocs == NULL)
10482     {
10483       bfd_size_type relsize;
10484       relsize = sec->reloc_count * sizeof (*relocs);
10485       relocs = bfd_alloc (sec->owner, relsize);
10486       if (relocs == NULL)
10487         return NULL;
10488       elfsec_data->relocs = relocs;
10489       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10490                                           sizeof (Elf_Internal_Shdr));
10491       if (elfsec_data->rela.hdr == NULL)
10492         return NULL;
10493       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10494                                         * sizeof (Elf64_External_Rela));
10495       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10496       sec->reloc_count = 0;
10497     }
10498   relocs += sec->reloc_count;
10499   sec->reloc_count += count;
10500   return relocs;
10501 }
10502
10503 static bfd_vma
10504 get_r2off (struct bfd_link_info *info,
10505            struct ppc_stub_hash_entry *stub_entry)
10506 {
10507   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10508   bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
10509
10510   if (r2off == 0)
10511     {
10512       /* Support linking -R objects.  Get the toc pointer from the
10513          opd entry.  */
10514       char buf[8];
10515       if (!htab->opd_abi)
10516         return r2off;
10517       asection *opd = stub_entry->h->elf.root.u.def.section;
10518       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10519
10520       if (strcmp (opd->name, ".opd") != 0
10521           || opd->reloc_count != 0)
10522         {
10523           info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10524                                   stub_entry->h->elf.root.root.string);
10525           bfd_set_error (bfd_error_bad_value);
10526           return 0;
10527         }
10528       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10529         return 0;
10530       r2off = bfd_get_64 (opd->owner, buf);
10531       r2off -= elf_gp (info->output_bfd);
10532     }
10533   r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
10534   return r2off;
10535 }
10536
10537 static bfd_boolean
10538 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10539 {
10540   struct ppc_stub_hash_entry *stub_entry;
10541   struct ppc_branch_hash_entry *br_entry;
10542   struct bfd_link_info *info;
10543   struct ppc_link_hash_table *htab;
10544   bfd_byte *loc;
10545   bfd_byte *p;
10546   bfd_vma dest, off;
10547   int size;
10548   Elf_Internal_Rela *r;
10549   asection *plt;
10550
10551   /* Massage our args to the form they really have.  */
10552   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10553   info = in_arg;
10554
10555   htab = ppc_hash_table (info);
10556   if (htab == NULL)
10557     return FALSE;
10558
10559   /* Make a note of the offset within the stubs for this entry.  */
10560   stub_entry->stub_offset = stub_entry->stub_sec->size;
10561   loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
10562
10563   htab->stub_count[stub_entry->stub_type - 1] += 1;
10564   switch (stub_entry->stub_type)
10565     {
10566     case ppc_stub_long_branch:
10567     case ppc_stub_long_branch_r2off:
10568       /* Branches are relative.  This is where we are going to.  */
10569       dest = (stub_entry->target_value
10570               + stub_entry->target_section->output_offset
10571               + stub_entry->target_section->output_section->vma);
10572       dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10573       off = dest;
10574
10575       /* And this is where we are coming from.  */
10576       off -= (stub_entry->stub_offset
10577               + stub_entry->stub_sec->output_offset
10578               + stub_entry->stub_sec->output_section->vma);
10579
10580       size = 4;
10581       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10582         {
10583           bfd_vma r2off = get_r2off (info, stub_entry);
10584
10585           if (r2off == 0)
10586             {
10587               htab->stub_error = TRUE;
10588               return FALSE;
10589             }
10590           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10591           loc += 4;
10592           size = 12;
10593           if (PPC_HA (r2off) != 0)
10594             {
10595               size = 16;
10596               bfd_put_32 (htab->params->stub_bfd,
10597                           ADDIS_R2_R2 | PPC_HA (r2off), loc);
10598               loc += 4;
10599             }
10600           bfd_put_32 (htab->params->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
10601           loc += 4;
10602           off -= size - 4;
10603         }
10604       bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
10605
10606       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10607         {
10608           info->callbacks->einfo
10609             (_("%P: long branch stub `%s' offset overflow\n"),
10610              stub_entry->root.string);
10611           htab->stub_error = TRUE;
10612           return FALSE;
10613         }
10614
10615       if (info->emitrelocations)
10616         {
10617           r = get_relocs (stub_entry->stub_sec, 1);
10618           if (r == NULL)
10619             return FALSE;
10620           r->r_offset = loc - stub_entry->stub_sec->contents;
10621           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10622           r->r_addend = dest;
10623           if (stub_entry->h != NULL)
10624             {
10625               struct elf_link_hash_entry **hashes;
10626               unsigned long symndx;
10627               struct ppc_link_hash_entry *h;
10628
10629               hashes = elf_sym_hashes (htab->params->stub_bfd);
10630               if (hashes == NULL)
10631                 {
10632                   bfd_size_type hsize;
10633
10634                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10635                   hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10636                   if (hashes == NULL)
10637                     return FALSE;
10638                   elf_sym_hashes (htab->params->stub_bfd) = hashes;
10639                   htab->stub_globals = 1;
10640                 }
10641               symndx = htab->stub_globals++;
10642               h = stub_entry->h;
10643               hashes[symndx] = &h->elf;
10644               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10645               if (h->oh != NULL && h->oh->is_func)
10646                 h = ppc_follow_link (h->oh);
10647               if (h->elf.root.u.def.section != stub_entry->target_section)
10648                 /* H is an opd symbol.  The addend must be zero.  */
10649                 r->r_addend = 0;
10650               else
10651                 {
10652                   off = (h->elf.root.u.def.value
10653                          + h->elf.root.u.def.section->output_offset
10654                          + h->elf.root.u.def.section->output_section->vma);
10655                   r->r_addend -= off;
10656                 }
10657             }
10658         }
10659       break;
10660
10661     case ppc_stub_plt_branch:
10662     case ppc_stub_plt_branch_r2off:
10663       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10664                                          stub_entry->root.string + 9,
10665                                          FALSE, FALSE);
10666       if (br_entry == NULL)
10667         {
10668           info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10669                                   stub_entry->root.string);
10670           htab->stub_error = TRUE;
10671           return FALSE;
10672         }
10673
10674       dest = (stub_entry->target_value
10675               + stub_entry->target_section->output_offset
10676               + stub_entry->target_section->output_section->vma);
10677       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10678         dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10679
10680       bfd_put_64 (htab->brlt->owner, dest,
10681                   htab->brlt->contents + br_entry->offset);
10682
10683       if (br_entry->iter == htab->stub_iteration)
10684         {
10685           br_entry->iter = 0;
10686
10687           if (htab->relbrlt != NULL)
10688             {
10689               /* Create a reloc for the branch lookup table entry.  */
10690               Elf_Internal_Rela rela;
10691               bfd_byte *rl;
10692
10693               rela.r_offset = (br_entry->offset
10694                                + htab->brlt->output_offset
10695                                + htab->brlt->output_section->vma);
10696               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10697               rela.r_addend = dest;
10698
10699               rl = htab->relbrlt->contents;
10700               rl += (htab->relbrlt->reloc_count++
10701                      * sizeof (Elf64_External_Rela));
10702               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10703             }
10704           else if (info->emitrelocations)
10705             {
10706               r = get_relocs (htab->brlt, 1);
10707               if (r == NULL)
10708                 return FALSE;
10709               /* brlt, being SEC_LINKER_CREATED does not go through the
10710                  normal reloc processing.  Symbols and offsets are not
10711                  translated from input file to output file form, so
10712                  set up the offset per the output file.  */
10713               r->r_offset = (br_entry->offset
10714                              + htab->brlt->output_offset
10715                              + htab->brlt->output_section->vma);
10716               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10717               r->r_addend = dest;
10718             }
10719         }
10720
10721       dest = (br_entry->offset
10722               + htab->brlt->output_offset
10723               + htab->brlt->output_section->vma);
10724
10725       off = (dest
10726              - elf_gp (htab->brlt->output_section->owner)
10727              - htab->stub_group[stub_entry->id_sec->id].toc_off);
10728
10729       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10730         {
10731           info->callbacks->einfo
10732             (_("%P: linkage table error against `%T'\n"),
10733              stub_entry->root.string);
10734           bfd_set_error (bfd_error_bad_value);
10735           htab->stub_error = TRUE;
10736           return FALSE;
10737         }
10738
10739       if (info->emitrelocations)
10740         {
10741           r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
10742           if (r == NULL)
10743             return FALSE;
10744           r[0].r_offset = loc - stub_entry->stub_sec->contents;
10745           if (bfd_big_endian (info->output_bfd))
10746             r[0].r_offset += 2;
10747           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10748             r[0].r_offset += 4;
10749           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10750           r[0].r_addend = dest;
10751           if (PPC_HA (off) != 0)
10752             {
10753               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10754               r[1].r_offset = r[0].r_offset + 4;
10755               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10756               r[1].r_addend = r[0].r_addend;
10757             }
10758         }
10759
10760       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10761         {
10762           if (PPC_HA (off) != 0)
10763             {
10764               size = 16;
10765               bfd_put_32 (htab->params->stub_bfd,
10766                           ADDIS_R12_R2 | PPC_HA (off), loc);
10767               loc += 4;
10768               bfd_put_32 (htab->params->stub_bfd,
10769                           LD_R12_0R12 | PPC_LO (off), loc);
10770             }
10771           else
10772             {
10773               size = 12;
10774               bfd_put_32 (htab->params->stub_bfd,
10775                           LD_R12_0R2 | PPC_LO (off), loc);
10776             }
10777         }
10778       else
10779         {
10780           bfd_vma r2off = get_r2off (info, stub_entry);
10781
10782           if (r2off == 0 && htab->opd_abi)
10783             {
10784               htab->stub_error = TRUE;
10785               return FALSE;
10786             }
10787
10788           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10789           loc += 4;
10790           size = 16;
10791           if (PPC_HA (off) != 0)
10792             {
10793               size += 4;
10794               bfd_put_32 (htab->params->stub_bfd,
10795                           ADDIS_R12_R2 | PPC_HA (off), loc);
10796               loc += 4;
10797               bfd_put_32 (htab->params->stub_bfd,
10798                           LD_R12_0R12 | PPC_LO (off), loc);
10799             }
10800           else
10801             bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
10802
10803           if (PPC_HA (r2off) != 0)
10804             {
10805               size += 4;
10806               loc += 4;
10807               bfd_put_32 (htab->params->stub_bfd,
10808                           ADDIS_R2_R2 | PPC_HA (r2off), loc);
10809             }
10810           if (PPC_LO (r2off) != 0)
10811             {
10812               size += 4;
10813               loc += 4;
10814               bfd_put_32 (htab->params->stub_bfd,
10815                           ADDI_R2_R2 | PPC_LO (r2off), loc);
10816             }
10817         }
10818       loc += 4;
10819       bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
10820       loc += 4;
10821       bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
10822       break;
10823
10824     case ppc_stub_plt_call:
10825     case ppc_stub_plt_call_r2save:
10826       if (stub_entry->h != NULL
10827           && stub_entry->h->is_func_descriptor
10828           && stub_entry->h->oh != NULL)
10829         {
10830           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10831
10832           /* If the old-ABI "dot-symbol" is undefined make it weak so
10833              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
10834              FIXME: We used to define the symbol on one of the call
10835              stubs instead, which is why we test symbol section id
10836              against htab->top_id in various places.  Likely all
10837              these checks could now disappear.  */
10838           if (fh->elf.root.type == bfd_link_hash_undefined)
10839             fh->elf.root.type = bfd_link_hash_undefweak;
10840           /* Stop undo_symbol_twiddle changing it back to undefined.  */
10841           fh->was_undefined = 0;
10842         }
10843
10844       /* Now build the stub.  */
10845       dest = stub_entry->plt_ent->plt.offset & ~1;
10846       if (dest >= (bfd_vma) -2)
10847         abort ();
10848
10849       plt = htab->elf.splt;
10850       if (!htab->elf.dynamic_sections_created
10851           || stub_entry->h == NULL
10852           || stub_entry->h->elf.dynindx == -1)
10853         plt = htab->elf.iplt;
10854
10855       dest += plt->output_offset + plt->output_section->vma;
10856
10857       if (stub_entry->h == NULL
10858           && (stub_entry->plt_ent->plt.offset & 1) == 0)
10859         {
10860           Elf_Internal_Rela rela;
10861           bfd_byte *rl;
10862
10863           rela.r_offset = dest;
10864           if (htab->opd_abi)
10865             rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
10866           else
10867             rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
10868           rela.r_addend = (stub_entry->target_value
10869                            + stub_entry->target_section->output_offset
10870                            + stub_entry->target_section->output_section->vma);
10871
10872           rl = (htab->elf.irelplt->contents
10873                 + (htab->elf.irelplt->reloc_count++
10874                    * sizeof (Elf64_External_Rela)));
10875           bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
10876           stub_entry->plt_ent->plt.offset |= 1;
10877         }
10878
10879       off = (dest
10880              - elf_gp (plt->output_section->owner)
10881              - htab->stub_group[stub_entry->id_sec->id].toc_off);
10882
10883       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10884         {
10885           info->callbacks->einfo
10886             (_("%P: linkage table error against `%T'\n"),
10887              stub_entry->h != NULL
10888              ? stub_entry->h->elf.root.root.string
10889              : "<local sym>");
10890           bfd_set_error (bfd_error_bad_value);
10891           htab->stub_error = TRUE;
10892           return FALSE;
10893         }
10894
10895       if (htab->params->plt_stub_align != 0)
10896         {
10897           unsigned pad = plt_stub_pad (htab, stub_entry, off);
10898
10899           stub_entry->stub_sec->size += pad;
10900           stub_entry->stub_offset = stub_entry->stub_sec->size;
10901           loc += pad;
10902         }
10903
10904       r = NULL;
10905       if (info->emitrelocations)
10906         {
10907           r = get_relocs (stub_entry->stub_sec,
10908                           ((PPC_HA (off) != 0)
10909                            + (htab->opd_abi
10910                               ? 2 + (htab->params->plt_static_chain
10911                                      && PPC_HA (off + 16) == PPC_HA (off))
10912                               : 1)));
10913           if (r == NULL)
10914             return FALSE;
10915           r[0].r_offset = loc - stub_entry->stub_sec->contents;
10916           if (bfd_big_endian (info->output_bfd))
10917             r[0].r_offset += 2;
10918           r[0].r_addend = dest;
10919         }
10920       if (stub_entry->h != NULL
10921           && (stub_entry->h == htab->tls_get_addr_fd
10922               || stub_entry->h == htab->tls_get_addr)
10923           && !htab->params->no_tls_get_addr_opt)
10924         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
10925       else
10926         p = build_plt_stub (htab, stub_entry, loc, off, r);
10927       size = p - loc;
10928       break;
10929
10930     default:
10931       BFD_FAIL ();
10932       return FALSE;
10933     }
10934
10935   stub_entry->stub_sec->size += size;
10936
10937   if (htab->params->emit_stub_syms)
10938     {
10939       struct elf_link_hash_entry *h;
10940       size_t len1, len2;
10941       char *name;
10942       const char *const stub_str[] = { "long_branch",
10943                                        "long_branch_r2off",
10944                                        "plt_branch",
10945                                        "plt_branch_r2off",
10946                                        "plt_call",
10947                                        "plt_call" };
10948
10949       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10950       len2 = strlen (stub_entry->root.string);
10951       name = bfd_malloc (len1 + len2 + 2);
10952       if (name == NULL)
10953         return FALSE;
10954       memcpy (name, stub_entry->root.string, 9);
10955       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10956       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10957       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
10958       if (h == NULL)
10959         return FALSE;
10960       if (h->root.type == bfd_link_hash_new)
10961         {
10962           h->root.type = bfd_link_hash_defined;
10963           h->root.u.def.section = stub_entry->stub_sec;
10964           h->root.u.def.value = stub_entry->stub_offset;
10965           h->ref_regular = 1;
10966           h->def_regular = 1;
10967           h->ref_regular_nonweak = 1;
10968           h->forced_local = 1;
10969           h->non_elf = 0;
10970           h->root.linker_def = 1;
10971         }
10972     }
10973
10974   return TRUE;
10975 }
10976
10977 /* As above, but don't actually build the stub.  Just bump offset so
10978    we know stub section sizes, and select plt_branch stubs where
10979    long_branch stubs won't do.  */
10980
10981 static bfd_boolean
10982 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10983 {
10984   struct ppc_stub_hash_entry *stub_entry;
10985   struct bfd_link_info *info;
10986   struct ppc_link_hash_table *htab;
10987   bfd_vma off;
10988   int size;
10989
10990   /* Massage our args to the form they really have.  */
10991   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10992   info = in_arg;
10993
10994   htab = ppc_hash_table (info);
10995   if (htab == NULL)
10996     return FALSE;
10997
10998   if (stub_entry->stub_type == ppc_stub_plt_call
10999       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11000     {
11001       asection *plt;
11002       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11003       if (off >= (bfd_vma) -2)
11004         abort ();
11005       plt = htab->elf.splt;
11006       if (!htab->elf.dynamic_sections_created
11007           || stub_entry->h == NULL
11008           || stub_entry->h->elf.dynindx == -1)
11009         plt = htab->elf.iplt;
11010       off += (plt->output_offset
11011               + plt->output_section->vma
11012               - elf_gp (plt->output_section->owner)
11013               - htab->stub_group[stub_entry->id_sec->id].toc_off);
11014
11015       size = plt_stub_size (htab, stub_entry, off);
11016       if (htab->params->plt_stub_align)
11017         size += plt_stub_pad (htab, stub_entry, off);
11018       if (info->emitrelocations)
11019         {
11020           stub_entry->stub_sec->reloc_count
11021             += ((PPC_HA (off) != 0)
11022                 + (htab->opd_abi
11023                    ? 2 + (htab->params->plt_static_chain
11024                           && PPC_HA (off + 16) == PPC_HA (off))
11025                    : 1));
11026           stub_entry->stub_sec->flags |= SEC_RELOC;
11027         }
11028     }
11029   else
11030     {
11031       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11032          variants.  */
11033       bfd_vma r2off = 0;
11034       bfd_vma local_off = 0;
11035
11036       off = (stub_entry->target_value
11037              + stub_entry->target_section->output_offset
11038              + stub_entry->target_section->output_section->vma);
11039       off -= (stub_entry->stub_sec->size
11040               + stub_entry->stub_sec->output_offset
11041               + stub_entry->stub_sec->output_section->vma);
11042
11043       /* Reset the stub type from the plt variant in case we now
11044          can reach with a shorter stub.  */
11045       if (stub_entry->stub_type >= ppc_stub_plt_branch)
11046         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11047
11048       size = 4;
11049       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11050         {
11051           r2off = get_r2off (info, stub_entry);
11052           if (r2off == 0 && htab->opd_abi)
11053             {
11054               htab->stub_error = TRUE;
11055               return FALSE;
11056             }
11057           size = 12;
11058           if (PPC_HA (r2off) != 0)
11059             size = 16;
11060           off -= size - 4;
11061         }
11062
11063       local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11064
11065       /* If the branch offset if too big, use a ppc_stub_plt_branch.
11066          Do the same for -R objects without function descriptors.  */
11067       if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11068           || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11069               && r2off == 0))
11070         {
11071           struct ppc_branch_hash_entry *br_entry;
11072
11073           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11074                                              stub_entry->root.string + 9,
11075                                              TRUE, FALSE);
11076           if (br_entry == NULL)
11077             {
11078               info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
11079                                       stub_entry->root.string);
11080               htab->stub_error = TRUE;
11081               return FALSE;
11082             }
11083
11084           if (br_entry->iter != htab->stub_iteration)
11085             {
11086               br_entry->iter = htab->stub_iteration;
11087               br_entry->offset = htab->brlt->size;
11088               htab->brlt->size += 8;
11089
11090               if (htab->relbrlt != NULL)
11091                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11092               else if (info->emitrelocations)
11093                 {
11094                   htab->brlt->reloc_count += 1;
11095                   htab->brlt->flags |= SEC_RELOC;
11096                 }
11097             }
11098
11099           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11100           off = (br_entry->offset
11101                  + htab->brlt->output_offset
11102                  + htab->brlt->output_section->vma
11103                  - elf_gp (htab->brlt->output_section->owner)
11104                  - htab->stub_group[stub_entry->id_sec->id].toc_off);
11105
11106           if (info->emitrelocations)
11107             {
11108               stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
11109               stub_entry->stub_sec->flags |= SEC_RELOC;
11110             }
11111
11112           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11113             {
11114               size = 12;
11115               if (PPC_HA (off) != 0)
11116                 size = 16;
11117             }
11118           else
11119             {
11120               size = 16;
11121               if (PPC_HA (off) != 0)
11122                 size += 4;
11123
11124               if (PPC_HA (r2off) != 0)
11125                 size += 4;
11126               if (PPC_LO (r2off) != 0)
11127                 size += 4;
11128             }
11129         }
11130       else if (info->emitrelocations)
11131         {
11132           stub_entry->stub_sec->reloc_count += 1;
11133           stub_entry->stub_sec->flags |= SEC_RELOC;
11134         }
11135     }
11136
11137   stub_entry->stub_sec->size += size;
11138   return TRUE;
11139 }
11140
11141 /* Set up various things so that we can make a list of input sections
11142    for each output section included in the link.  Returns -1 on error,
11143    0 when no stubs will be needed, and 1 on success.  */
11144
11145 int
11146 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11147 {
11148   bfd *input_bfd;
11149   int top_id, top_index, id;
11150   asection *section;
11151   asection **input_list;
11152   bfd_size_type amt;
11153   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11154
11155   if (htab == NULL)
11156     return -1;
11157
11158   /* Find the top input section id.  */
11159   for (input_bfd = info->input_bfds, top_id = 3;
11160        input_bfd != NULL;
11161        input_bfd = input_bfd->link.next)
11162     {
11163       for (section = input_bfd->sections;
11164            section != NULL;
11165            section = section->next)
11166         {
11167           if (top_id < section->id)
11168             top_id = section->id;
11169         }
11170     }
11171
11172   htab->top_id = top_id;
11173   amt = sizeof (struct map_stub) * (top_id + 1);
11174   htab->stub_group = bfd_zmalloc (amt);
11175   if (htab->stub_group == NULL)
11176     return -1;
11177
11178   /* Set toc_off for com, und, abs and ind sections.  */
11179   for (id = 0; id < 3; id++)
11180     htab->stub_group[id].toc_off = TOC_BASE_OFF;
11181
11182   /* We can't use output_bfd->section_count here to find the top output
11183      section index as some sections may have been removed, and
11184      strip_excluded_output_sections doesn't renumber the indices.  */
11185   for (section = info->output_bfd->sections, top_index = 0;
11186        section != NULL;
11187        section = section->next)
11188     {
11189       if (top_index < section->index)
11190         top_index = section->index;
11191     }
11192
11193   htab->top_index = top_index;
11194   amt = sizeof (asection *) * (top_index + 1);
11195   input_list = bfd_zmalloc (amt);
11196   htab->input_list = input_list;
11197   if (input_list == NULL)
11198     return -1;
11199
11200   return 1;
11201 }
11202
11203 /* Set up for first pass at multitoc partitioning.  */
11204
11205 void
11206 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11207 {
11208   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11209
11210   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11211   htab->toc_bfd = NULL;
11212   htab->toc_first_sec = NULL;
11213 }
11214
11215 /* The linker repeatedly calls this function for each TOC input section
11216    and linker generated GOT section.  Group input bfds such that the toc
11217    within a group is less than 64k in size.  */
11218
11219 bfd_boolean
11220 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11221 {
11222   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11223   bfd_vma addr, off, limit;
11224
11225   if (htab == NULL)
11226     return FALSE;
11227
11228   if (!htab->second_toc_pass)
11229     {
11230       /* Keep track of the first .toc or .got section for this input bfd.  */
11231       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11232
11233       if (new_bfd)
11234         {
11235           htab->toc_bfd = isec->owner;
11236           htab->toc_first_sec = isec;
11237         }
11238
11239       addr = isec->output_offset + isec->output_section->vma;
11240       off = addr - htab->toc_curr;
11241       limit = 0x80008000;
11242       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11243         limit = 0x10000;
11244       if (off + isec->size > limit)
11245         {
11246           addr = (htab->toc_first_sec->output_offset
11247                   + htab->toc_first_sec->output_section->vma);
11248           htab->toc_curr = addr;
11249         }
11250
11251       /* toc_curr is the base address of this toc group.  Set elf_gp
11252          for the input section to be the offset relative to the
11253          output toc base plus 0x8000.  Making the input elf_gp an
11254          offset allows us to move the toc as a whole without
11255          recalculating input elf_gp.  */
11256       off = htab->toc_curr - elf_gp (isec->output_section->owner);
11257       off += TOC_BASE_OFF;
11258
11259       /* Die if someone uses a linker script that doesn't keep input
11260          file .toc and .got together.  */
11261       if (new_bfd
11262           && elf_gp (isec->owner) != 0
11263           && elf_gp (isec->owner) != off)
11264         return FALSE;
11265
11266       elf_gp (isec->owner) = off;
11267       return TRUE;
11268     }
11269
11270   /* During the second pass toc_first_sec points to the start of
11271      a toc group, and toc_curr is used to track the old elf_gp.
11272      We use toc_bfd to ensure we only look at each bfd once.  */
11273   if (htab->toc_bfd == isec->owner)
11274     return TRUE;
11275   htab->toc_bfd = isec->owner;
11276
11277   if (htab->toc_first_sec == NULL
11278       || htab->toc_curr != elf_gp (isec->owner))
11279     {
11280       htab->toc_curr = elf_gp (isec->owner);
11281       htab->toc_first_sec = isec;
11282     }
11283   addr = (htab->toc_first_sec->output_offset
11284           + htab->toc_first_sec->output_section->vma);
11285   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11286   elf_gp (isec->owner) = off;
11287
11288   return TRUE;
11289 }
11290
11291 /* Called via elf_link_hash_traverse to merge GOT entries for global
11292    symbol H.  */
11293
11294 static bfd_boolean
11295 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11296 {
11297   if (h->root.type == bfd_link_hash_indirect)
11298     return TRUE;
11299
11300   merge_got_entries (&h->got.glist);
11301
11302   return TRUE;
11303 }
11304
11305 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11306    symbol H.  */
11307
11308 static bfd_boolean
11309 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11310 {
11311   struct got_entry *gent;
11312
11313   if (h->root.type == bfd_link_hash_indirect)
11314     return TRUE;
11315
11316   for (gent = h->got.glist; gent != NULL; gent = gent->next)
11317     if (!gent->is_indirect)
11318       allocate_got (h, (struct bfd_link_info *) inf, gent);
11319   return TRUE;
11320 }
11321
11322 /* Called on the first multitoc pass after the last call to
11323    ppc64_elf_next_toc_section.  This function removes duplicate GOT
11324    entries.  */
11325
11326 bfd_boolean
11327 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11328 {
11329   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11330   struct bfd *ibfd, *ibfd2;
11331   bfd_boolean done_something;
11332
11333   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11334
11335   if (!htab->do_multi_toc)
11336     return FALSE;
11337
11338   /* Merge global sym got entries within a toc group.  */
11339   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11340
11341   /* And tlsld_got.  */
11342   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11343     {
11344       struct got_entry *ent, *ent2;
11345
11346       if (!is_ppc64_elf (ibfd))
11347         continue;
11348
11349       ent = ppc64_tlsld_got (ibfd);
11350       if (!ent->is_indirect
11351           && ent->got.offset != (bfd_vma) -1)
11352         {
11353           for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11354             {
11355               if (!is_ppc64_elf (ibfd2))
11356                 continue;
11357
11358               ent2 = ppc64_tlsld_got (ibfd2);
11359               if (!ent2->is_indirect
11360                   && ent2->got.offset != (bfd_vma) -1
11361                   && elf_gp (ibfd2) == elf_gp (ibfd))
11362                 {
11363                   ent2->is_indirect = TRUE;
11364                   ent2->got.ent = ent;
11365                 }
11366             }
11367         }
11368     }
11369
11370   /* Zap sizes of got sections.  */
11371   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11372   htab->elf.irelplt->size -= htab->got_reli_size;
11373   htab->got_reli_size = 0;
11374
11375   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11376     {
11377       asection *got, *relgot;
11378
11379       if (!is_ppc64_elf (ibfd))
11380         continue;
11381
11382       got = ppc64_elf_tdata (ibfd)->got;
11383       if (got != NULL)
11384         {
11385           got->rawsize = got->size;
11386           got->size = 0;
11387           relgot = ppc64_elf_tdata (ibfd)->relgot;
11388           relgot->rawsize = relgot->size;
11389           relgot->size = 0;
11390         }
11391     }
11392
11393   /* Now reallocate the got, local syms first.  We don't need to
11394      allocate section contents again since we never increase size.  */
11395   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11396     {
11397       struct got_entry **lgot_ents;
11398       struct got_entry **end_lgot_ents;
11399       struct plt_entry **local_plt;
11400       struct plt_entry **end_local_plt;
11401       unsigned char *lgot_masks;
11402       bfd_size_type locsymcount;
11403       Elf_Internal_Shdr *symtab_hdr;
11404       asection *s;
11405
11406       if (!is_ppc64_elf (ibfd))
11407         continue;
11408
11409       lgot_ents = elf_local_got_ents (ibfd);
11410       if (!lgot_ents)
11411         continue;
11412
11413       symtab_hdr = &elf_symtab_hdr (ibfd);
11414       locsymcount = symtab_hdr->sh_info;
11415       end_lgot_ents = lgot_ents + locsymcount;
11416       local_plt = (struct plt_entry **) end_lgot_ents;
11417       end_local_plt = local_plt + locsymcount;
11418       lgot_masks = (unsigned char *) end_local_plt;
11419       s = ppc64_elf_tdata (ibfd)->got;
11420       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11421         {
11422           struct got_entry *ent;
11423
11424           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11425             {
11426               unsigned int ent_size = 8;
11427               unsigned int rel_size = sizeof (Elf64_External_Rela);
11428
11429               ent->got.offset = s->size;
11430               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11431                 {
11432                   ent_size *= 2;
11433                   rel_size *= 2;
11434                 }
11435               s->size += ent_size;
11436               if ((*lgot_masks & PLT_IFUNC) != 0)
11437                 {
11438                   htab->elf.irelplt->size += rel_size;
11439                   htab->got_reli_size += rel_size;
11440                 }
11441               else if (info->shared)
11442                 {
11443                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11444                   srel->size += rel_size;
11445                 }
11446             }
11447         }
11448     }
11449
11450   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11451
11452   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11453     {
11454       struct got_entry *ent;
11455
11456       if (!is_ppc64_elf (ibfd))
11457         continue;
11458
11459       ent = ppc64_tlsld_got (ibfd);
11460       if (!ent->is_indirect
11461           && ent->got.offset != (bfd_vma) -1)
11462         {
11463           asection *s = ppc64_elf_tdata (ibfd)->got;
11464           ent->got.offset = s->size;
11465           s->size += 16;
11466           if (info->shared)
11467             {
11468               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11469               srel->size += sizeof (Elf64_External_Rela);
11470             }
11471         }
11472     }
11473
11474   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11475   if (!done_something)
11476     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11477       {
11478         asection *got;
11479
11480         if (!is_ppc64_elf (ibfd))
11481           continue;
11482
11483         got = ppc64_elf_tdata (ibfd)->got;
11484         if (got != NULL)
11485           {
11486             done_something = got->rawsize != got->size;
11487             if (done_something)
11488               break;
11489           }
11490       }
11491
11492   if (done_something)
11493     (*htab->params->layout_sections_again) ();
11494
11495   /* Set up for second pass over toc sections to recalculate elf_gp
11496      on input sections.  */
11497   htab->toc_bfd = NULL;
11498   htab->toc_first_sec = NULL;
11499   htab->second_toc_pass = TRUE;
11500   return done_something;
11501 }
11502
11503 /* Called after second pass of multitoc partitioning.  */
11504
11505 void
11506 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11507 {
11508   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11509
11510   /* After the second pass, toc_curr tracks the TOC offset used
11511      for code sections below in ppc64_elf_next_input_section.  */
11512   htab->toc_curr = TOC_BASE_OFF;
11513 }
11514
11515 /* No toc references were found in ISEC.  If the code in ISEC makes no
11516    calls, then there's no need to use toc adjusting stubs when branching
11517    into ISEC.  Actually, indirect calls from ISEC are OK as they will
11518    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
11519    needed, and 2 if a cyclical call-graph was found but no other reason
11520    for a stub was detected.  If called from the top level, a return of
11521    2 means the same as a return of 0.  */
11522
11523 static int
11524 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11525 {
11526   int ret;
11527
11528   /* Mark this section as checked.  */
11529   isec->call_check_done = 1;
11530
11531   /* We know none of our code bearing sections will need toc stubs.  */
11532   if ((isec->flags & SEC_LINKER_CREATED) != 0)
11533     return 0;
11534
11535   if (isec->size == 0)
11536     return 0;
11537
11538   if (isec->output_section == NULL)
11539     return 0;
11540
11541   ret = 0;
11542   if (isec->reloc_count != 0)
11543     {
11544       Elf_Internal_Rela *relstart, *rel;
11545       Elf_Internal_Sym *local_syms;
11546       struct ppc_link_hash_table *htab;
11547
11548       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11549                                             info->keep_memory);
11550       if (relstart == NULL)
11551         return -1;
11552
11553       /* Look for branches to outside of this section.  */
11554       local_syms = NULL;
11555       htab = ppc_hash_table (info);
11556       if (htab == NULL)
11557         return -1;
11558
11559       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11560         {
11561           enum elf_ppc64_reloc_type r_type;
11562           unsigned long r_symndx;
11563           struct elf_link_hash_entry *h;
11564           struct ppc_link_hash_entry *eh;
11565           Elf_Internal_Sym *sym;
11566           asection *sym_sec;
11567           struct _opd_sec_data *opd;
11568           bfd_vma sym_value;
11569           bfd_vma dest;
11570
11571           r_type = ELF64_R_TYPE (rel->r_info);
11572           if (r_type != R_PPC64_REL24
11573               && r_type != R_PPC64_REL14
11574               && r_type != R_PPC64_REL14_BRTAKEN
11575               && r_type != R_PPC64_REL14_BRNTAKEN)
11576             continue;
11577
11578           r_symndx = ELF64_R_SYM (rel->r_info);
11579           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11580                           isec->owner))
11581             {
11582               ret = -1;
11583               break;
11584             }
11585
11586           /* Calls to dynamic lib functions go through a plt call stub
11587              that uses r2.  */
11588           eh = (struct ppc_link_hash_entry *) h;
11589           if (eh != NULL
11590               && (eh->elf.plt.plist != NULL
11591                   || (eh->oh != NULL
11592                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11593             {
11594               ret = 1;
11595               break;
11596             }
11597
11598           if (sym_sec == NULL)
11599             /* Ignore other undefined symbols.  */
11600             continue;
11601
11602           /* Assume branches to other sections not included in the
11603              link need stubs too, to cover -R and absolute syms.  */
11604           if (sym_sec->output_section == NULL)
11605             {
11606               ret = 1;
11607               break;
11608             }
11609
11610           if (h == NULL)
11611             sym_value = sym->st_value;
11612           else
11613             {
11614               if (h->root.type != bfd_link_hash_defined
11615                   && h->root.type != bfd_link_hash_defweak)
11616                 abort ();
11617               sym_value = h->root.u.def.value;
11618             }
11619           sym_value += rel->r_addend;
11620
11621           /* If this branch reloc uses an opd sym, find the code section.  */
11622           opd = get_opd_info (sym_sec);
11623           if (opd != NULL)
11624             {
11625               if (h == NULL && opd->adjust != NULL)
11626                 {
11627                   long adjust;
11628
11629                   adjust = opd->adjust[OPD_NDX (sym->st_value)];
11630                   if (adjust == -1)
11631                     /* Assume deleted functions won't ever be called.  */
11632                     continue;
11633                   sym_value += adjust;
11634                 }
11635
11636               dest = opd_entry_value (sym_sec, sym_value,
11637                                       &sym_sec, NULL, FALSE);
11638               if (dest == (bfd_vma) -1)
11639                 continue;
11640             }
11641           else
11642             dest = (sym_value
11643                     + sym_sec->output_offset
11644                     + sym_sec->output_section->vma);
11645
11646           /* Ignore branch to self.  */
11647           if (sym_sec == isec)
11648             continue;
11649
11650           /* If the called function uses the toc, we need a stub.  */
11651           if (sym_sec->has_toc_reloc
11652               || sym_sec->makes_toc_func_call)
11653             {
11654               ret = 1;
11655               break;
11656             }
11657
11658           /* Assume any branch that needs a long branch stub might in fact
11659              need a plt_branch stub.  A plt_branch stub uses r2.  */
11660           else if (dest - (isec->output_offset
11661                            + isec->output_section->vma
11662                            + rel->r_offset) + (1 << 25)
11663                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11664                                                              ? h->other
11665                                                              : sym->st_other))
11666             {
11667               ret = 1;
11668               break;
11669             }
11670
11671           /* If calling back to a section in the process of being
11672              tested, we can't say for sure that no toc adjusting stubs
11673              are needed, so don't return zero.  */
11674           else if (sym_sec->call_check_in_progress)
11675             ret = 2;
11676
11677           /* Branches to another section that itself doesn't have any TOC
11678              references are OK.  Recursively call ourselves to check.  */
11679           else if (!sym_sec->call_check_done)
11680             {
11681               int recur;
11682
11683               /* Mark current section as indeterminate, so that other
11684                  sections that call back to current won't be marked as
11685                  known.  */
11686               isec->call_check_in_progress = 1;
11687               recur = toc_adjusting_stub_needed (info, sym_sec);
11688               isec->call_check_in_progress = 0;
11689
11690               if (recur != 0)
11691                 {
11692                   ret = recur;
11693                   if (recur != 2)
11694                     break;
11695                 }
11696             }
11697         }
11698
11699       if (local_syms != NULL
11700           && (elf_symtab_hdr (isec->owner).contents
11701               != (unsigned char *) local_syms))
11702         free (local_syms);
11703       if (elf_section_data (isec)->relocs != relstart)
11704         free (relstart);
11705     }
11706
11707   if ((ret & 1) == 0
11708       && isec->map_head.s != NULL
11709       && (strcmp (isec->output_section->name, ".init") == 0
11710           || strcmp (isec->output_section->name, ".fini") == 0))
11711     {
11712       if (isec->map_head.s->has_toc_reloc
11713           || isec->map_head.s->makes_toc_func_call)
11714         ret = 1;
11715       else if (!isec->map_head.s->call_check_done)
11716         {
11717           int recur;
11718           isec->call_check_in_progress = 1;
11719           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11720           isec->call_check_in_progress = 0;
11721           if (recur != 0)
11722             ret = recur;
11723         }
11724     }
11725
11726   if (ret == 1)
11727     isec->makes_toc_func_call = 1;
11728
11729   return ret;
11730 }
11731
11732 /* The linker repeatedly calls this function for each input section,
11733    in the order that input sections are linked into output sections.
11734    Build lists of input sections to determine groupings between which
11735    we may insert linker stubs.  */
11736
11737 bfd_boolean
11738 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11739 {
11740   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11741
11742   if (htab == NULL)
11743     return FALSE;
11744
11745   if ((isec->output_section->flags & SEC_CODE) != 0
11746       && isec->output_section->index <= htab->top_index)
11747     {
11748       asection **list = htab->input_list + isec->output_section->index;
11749       /* Steal the link_sec pointer for our list.  */
11750 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
11751       /* This happens to make the list in reverse order,
11752          which is what we want.  */
11753       PREV_SEC (isec) = *list;
11754       *list = isec;
11755     }
11756
11757   if (htab->multi_toc_needed)
11758     {
11759       /* Analyse sections that aren't already flagged as needing a
11760          valid toc pointer.  Exclude .fixup for the linux kernel.
11761          .fixup contains branches, but only back to the function that
11762          hit an exception.  */
11763       if (!(isec->has_toc_reloc
11764             || (isec->flags & SEC_CODE) == 0
11765             || strcmp (isec->name, ".fixup") == 0
11766             || isec->call_check_done))
11767         {
11768           if (toc_adjusting_stub_needed (info, isec) < 0)
11769             return FALSE;
11770         }
11771       /* Make all sections use the TOC assigned for this object file.
11772          This will be wrong for pasted sections;  We fix that in
11773          check_pasted_section().  */
11774       if (elf_gp (isec->owner) != 0)
11775         htab->toc_curr = elf_gp (isec->owner);
11776     }
11777
11778   htab->stub_group[isec->id].toc_off = htab->toc_curr;
11779   return TRUE;
11780 }
11781
11782 /* Check that all .init and .fini sections use the same toc, if they
11783    have toc relocs.  */
11784
11785 static bfd_boolean
11786 check_pasted_section (struct bfd_link_info *info, const char *name)
11787 {
11788   asection *o = bfd_get_section_by_name (info->output_bfd, name);
11789
11790   if (o != NULL)
11791     {
11792       struct ppc_link_hash_table *htab = ppc_hash_table (info);
11793       bfd_vma toc_off = 0;
11794       asection *i;
11795
11796       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11797         if (i->has_toc_reloc)
11798           {
11799             if (toc_off == 0)
11800               toc_off = htab->stub_group[i->id].toc_off;
11801             else if (toc_off != htab->stub_group[i->id].toc_off)
11802               return FALSE;
11803           }
11804
11805       if (toc_off == 0)
11806         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11807           if (i->makes_toc_func_call)
11808             {
11809               toc_off = htab->stub_group[i->id].toc_off;
11810               break;
11811             }
11812
11813       /* Make sure the whole pasted function uses the same toc offset.  */
11814       if (toc_off != 0)
11815         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11816           htab->stub_group[i->id].toc_off = toc_off;
11817     }
11818   return TRUE;
11819 }
11820
11821 bfd_boolean
11822 ppc64_elf_check_init_fini (struct bfd_link_info *info)
11823 {
11824   return (check_pasted_section (info, ".init")
11825           & check_pasted_section (info, ".fini"));
11826 }
11827
11828 /* See whether we can group stub sections together.  Grouping stub
11829    sections may result in fewer stubs.  More importantly, we need to
11830    put all .init* and .fini* stubs at the beginning of the .init or
11831    .fini output sections respectively, because glibc splits the
11832    _init and _fini functions into multiple parts.  Putting a stub in
11833    the middle of a function is not a good idea.  */
11834
11835 static void
11836 group_sections (struct ppc_link_hash_table *htab,
11837                 bfd_size_type stub_group_size,
11838                 bfd_boolean stubs_always_before_branch)
11839 {
11840   asection **list;
11841   bfd_size_type stub14_group_size;
11842   bfd_boolean suppress_size_errors;
11843
11844   suppress_size_errors = FALSE;
11845   stub14_group_size = stub_group_size >> 10;
11846   if (stub_group_size == 1)
11847     {
11848       /* Default values.  */
11849       if (stubs_always_before_branch)
11850         {
11851           stub_group_size = 0x1e00000;
11852           stub14_group_size = 0x7800;
11853         }
11854       else
11855         {
11856           stub_group_size = 0x1c00000;
11857           stub14_group_size = 0x7000;
11858         }
11859       suppress_size_errors = TRUE;
11860     }
11861
11862   list = htab->input_list + htab->top_index;
11863   do
11864     {
11865       asection *tail = *list;
11866       while (tail != NULL)
11867         {
11868           asection *curr;
11869           asection *prev;
11870           bfd_size_type total;
11871           bfd_boolean big_sec;
11872           bfd_vma curr_toc;
11873
11874           curr = tail;
11875           total = tail->size;
11876           big_sec = total > (ppc64_elf_section_data (tail) != NULL
11877                              && ppc64_elf_section_data (tail)->has_14bit_branch
11878                              ? stub14_group_size : stub_group_size);
11879           if (big_sec && !suppress_size_errors)
11880             (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
11881                                      tail->owner, tail);
11882           curr_toc = htab->stub_group[tail->id].toc_off;
11883
11884           while ((prev = PREV_SEC (curr)) != NULL
11885                  && ((total += curr->output_offset - prev->output_offset)
11886                      < (ppc64_elf_section_data (prev) != NULL
11887                         && ppc64_elf_section_data (prev)->has_14bit_branch
11888                         ? stub14_group_size : stub_group_size))
11889                  && htab->stub_group[prev->id].toc_off == curr_toc)
11890             curr = prev;
11891
11892           /* OK, the size from the start of CURR to the end is less
11893              than stub_group_size and thus can be handled by one stub
11894              section.  (or the tail section is itself larger than
11895              stub_group_size, in which case we may be toast.)  We
11896              should really be keeping track of the total size of stubs
11897              added here, as stubs contribute to the final output
11898              section size.  That's a little tricky, and this way will
11899              only break if stubs added make the total size more than
11900              2^25, ie. for the default stub_group_size, if stubs total
11901              more than 2097152 bytes, or nearly 75000 plt call stubs.  */
11902           do
11903             {
11904               prev = PREV_SEC (tail);
11905               /* Set up this stub group.  */
11906               htab->stub_group[tail->id].link_sec = curr;
11907             }
11908           while (tail != curr && (tail = prev) != NULL);
11909
11910           /* But wait, there's more!  Input sections up to stub_group_size
11911              bytes before the stub section can be handled by it too.
11912              Don't do this if we have a really large section after the
11913              stubs, as adding more stubs increases the chance that
11914              branches may not reach into the stub section.  */
11915           if (!stubs_always_before_branch && !big_sec)
11916             {
11917               total = 0;
11918               while (prev != NULL
11919                      && ((total += tail->output_offset - prev->output_offset)
11920                          < (ppc64_elf_section_data (prev) != NULL
11921                             && ppc64_elf_section_data (prev)->has_14bit_branch
11922                             ? stub14_group_size : stub_group_size))
11923                      && htab->stub_group[prev->id].toc_off == curr_toc)
11924                 {
11925                   tail = prev;
11926                   prev = PREV_SEC (tail);
11927                   htab->stub_group[tail->id].link_sec = curr;
11928                 }
11929             }
11930           tail = prev;
11931         }
11932     }
11933   while (list-- != htab->input_list);
11934   free (htab->input_list);
11935 #undef PREV_SEC
11936 }
11937
11938 static const unsigned char glink_eh_frame_cie[] =
11939 {
11940   0, 0, 0, 16,                          /* length.  */
11941   0, 0, 0, 0,                           /* id.  */
11942   1,                                    /* CIE version.  */
11943   'z', 'R', 0,                          /* Augmentation string.  */
11944   4,                                    /* Code alignment.  */
11945   0x78,                                 /* Data alignment.  */
11946   65,                                   /* RA reg.  */
11947   1,                                    /* Augmentation size.  */
11948   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
11949   DW_CFA_def_cfa, 1, 0,                 /* def_cfa: r1 offset 0.  */
11950   0, 0, 0, 0
11951 };
11952
11953 /* Stripping output sections is normally done before dynamic section
11954    symbols have been allocated.  This function is called later, and
11955    handles cases like htab->brlt which is mapped to its own output
11956    section.  */
11957
11958 static void
11959 maybe_strip_output (struct bfd_link_info *info, asection *isec)
11960 {
11961   if (isec->size == 0
11962       && isec->output_section->size == 0
11963       && !(isec->output_section->flags & SEC_KEEP)
11964       && !bfd_section_removed_from_list (info->output_bfd,
11965                                          isec->output_section)
11966       && elf_section_data (isec->output_section)->dynindx == 0)
11967     {
11968       isec->output_section->flags |= SEC_EXCLUDE;
11969       bfd_section_list_remove (info->output_bfd, isec->output_section);
11970       info->output_bfd->section_count--;
11971     }
11972 }
11973
11974 /* Determine and set the size of the stub section for a final link.
11975
11976    The basic idea here is to examine all the relocations looking for
11977    PC-relative calls to a target that is unreachable with a "bl"
11978    instruction.  */
11979
11980 bfd_boolean
11981 ppc64_elf_size_stubs (struct bfd_link_info *info)
11982 {
11983   bfd_size_type stub_group_size;
11984   bfd_boolean stubs_always_before_branch;
11985   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11986
11987   if (htab == NULL)
11988     return FALSE;
11989
11990   if (htab->params->plt_thread_safe == -1 && !info->executable)
11991     htab->params->plt_thread_safe = 1;
11992   if (!htab->opd_abi)
11993     htab->params->plt_thread_safe = 0;
11994   else if (htab->params->plt_thread_safe == -1)
11995     {
11996       static const char *const thread_starter[] =
11997         {
11998           "pthread_create",
11999           /* libstdc++ */
12000           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12001           /* librt */
12002           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12003           "mq_notify", "create_timer",
12004           /* libanl */
12005           "getaddrinfo_a",
12006           /* libgomp */
12007           "GOMP_parallel",
12008           "GOMP_parallel_start",
12009           "GOMP_parallel_loop_static",
12010           "GOMP_parallel_loop_static_start",
12011           "GOMP_parallel_loop_dynamic",
12012           "GOMP_parallel_loop_dynamic_start",
12013           "GOMP_parallel_loop_guided",
12014           "GOMP_parallel_loop_guided_start",
12015           "GOMP_parallel_loop_runtime",
12016           "GOMP_parallel_loop_runtime_start",
12017           "GOMP_parallel_sections",
12018           "GOMP_parallel_sections_start",
12019           /* libgo */
12020           "__go_go",
12021         };
12022       unsigned i;
12023
12024       for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
12025         {
12026           struct elf_link_hash_entry *h;
12027           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12028                                     FALSE, FALSE, TRUE);
12029           htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12030           if (htab->params->plt_thread_safe)
12031             break;
12032         }
12033     }
12034   stubs_always_before_branch = htab->params->group_size < 0;
12035   if (htab->params->group_size < 0)
12036     stub_group_size = -htab->params->group_size;
12037   else
12038     stub_group_size = htab->params->group_size;
12039
12040   group_sections (htab, stub_group_size, stubs_always_before_branch);
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->stub_group[code_sec->id].toc_off
12244                               != htab->stub_group[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->stub_group[section->id].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;
12528
12529   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12530      order.  The TOC starts where the first of these sections starts.  */
12531   s = bfd_get_section_by_name (obfd, ".got");
12532   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12533     s = bfd_get_section_by_name (obfd, ".toc");
12534   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12535     s = bfd_get_section_by_name (obfd, ".tocbss");
12536   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12537     s = bfd_get_section_by_name (obfd, ".plt");
12538   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12539     {
12540       /* This may happen for
12541          o  references to TOC base (SYM@toc / TOC[tc0]) without a
12542          .toc directive
12543          o  bad linker script
12544          o --gc-sections and empty TOC sections
12545
12546          FIXME: Warn user?  */
12547
12548       /* Look for a likely section.  We probably won't even be
12549          using TOCstart.  */
12550       for (s = obfd->sections; s != NULL; s = s->next)
12551         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12552                          | SEC_EXCLUDE))
12553             == (SEC_ALLOC | SEC_SMALL_DATA))
12554           break;
12555       if (s == NULL)
12556         for (s = obfd->sections; s != NULL; s = s->next)
12557           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12558               == (SEC_ALLOC | SEC_SMALL_DATA))
12559             break;
12560       if (s == NULL)
12561         for (s = obfd->sections; s != NULL; s = s->next)
12562           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12563               == SEC_ALLOC)
12564             break;
12565       if (s == NULL)
12566         for (s = obfd->sections; s != NULL; s = s->next)
12567           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12568             break;
12569     }
12570
12571   TOCstart = 0;
12572   if (s != NULL)
12573     TOCstart = s->output_section->vma + s->output_offset;
12574
12575   _bfd_set_gp_value (obfd, TOCstart);
12576
12577   if (info != NULL && s != NULL)
12578     {
12579       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12580
12581       if (htab != NULL)
12582         {
12583           if (htab->elf.hgot != NULL)
12584             {
12585               htab->elf.hgot->root.u.def.value = TOC_BASE_OFF;
12586               htab->elf.hgot->root.u.def.section = s;
12587             }
12588         }
12589       else
12590         {
12591           struct bfd_link_hash_entry *bh = NULL;
12592           _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
12593                                             s, TOC_BASE_OFF, NULL, FALSE,
12594                                             FALSE, &bh);
12595         }
12596     }
12597   return TOCstart;
12598 }
12599
12600 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12601    write out any global entry stubs.  */
12602
12603 static bfd_boolean
12604 build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12605 {
12606   struct bfd_link_info *info;
12607   struct ppc_link_hash_table *htab;
12608   struct plt_entry *pent;
12609   asection *s;
12610
12611   if (h->root.type == bfd_link_hash_indirect)
12612     return TRUE;
12613
12614   if (!h->pointer_equality_needed)
12615     return TRUE;
12616
12617   if (h->def_regular)
12618     return TRUE;
12619
12620   info = inf;
12621   htab = ppc_hash_table (info);
12622   if (htab == NULL)
12623     return FALSE;
12624
12625   s = htab->glink;
12626   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12627     if (pent->plt.offset != (bfd_vma) -1
12628         && pent->addend == 0)
12629       {
12630         bfd_byte *p;
12631         asection *plt;
12632         bfd_vma off;
12633
12634         p = s->contents + h->root.u.def.value;
12635         plt = htab->elf.splt;
12636         if (!htab->elf.dynamic_sections_created
12637             || h->dynindx == -1)
12638           plt = htab->elf.iplt;
12639         off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12640         off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12641
12642         if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12643           {
12644             info->callbacks->einfo
12645               (_("%P: linkage table error against `%T'\n"),
12646                h->root.root.string);
12647             bfd_set_error (bfd_error_bad_value);
12648             htab->stub_error = TRUE;
12649           }
12650
12651         htab->stub_count[ppc_stub_global_entry - 1] += 1;
12652         if (htab->params->emit_stub_syms)
12653           {
12654             size_t len = strlen (h->root.root.string);
12655             char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12656
12657             if (name == NULL)
12658               return FALSE;
12659
12660             sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12661             h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12662             if (h == NULL)
12663               return FALSE;
12664             if (h->root.type == bfd_link_hash_new)
12665               {
12666                 h->root.type = bfd_link_hash_defined;
12667                 h->root.u.def.section = s;
12668                 h->root.u.def.value = p - s->contents;
12669                 h->ref_regular = 1;
12670                 h->def_regular = 1;
12671                 h->ref_regular_nonweak = 1;
12672                 h->forced_local = 1;
12673                 h->non_elf = 0;
12674                 h->root.linker_def = 1;
12675               }
12676           }
12677
12678         if (PPC_HA (off) != 0)
12679           {
12680             bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12681             p += 4;
12682           }
12683         bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12684         p += 4;
12685         bfd_put_32 (s->owner, MTCTR_R12, p);
12686         p += 4;
12687         bfd_put_32 (s->owner, BCTR, p);
12688         break;
12689       }
12690   return TRUE;
12691 }
12692
12693 /* Build all the stubs associated with the current output file.
12694    The stubs are kept in a hash table attached to the main linker
12695    hash table.  This function is called via gldelf64ppc_finish.  */
12696
12697 bfd_boolean
12698 ppc64_elf_build_stubs (struct bfd_link_info *info,
12699                        char **stats)
12700 {
12701   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12702   asection *stub_sec;
12703   bfd_byte *p;
12704   int stub_sec_count = 0;
12705
12706   if (htab == NULL)
12707     return FALSE;
12708
12709   /* Allocate memory to hold the linker stubs.  */
12710   for (stub_sec = htab->params->stub_bfd->sections;
12711        stub_sec != NULL;
12712        stub_sec = stub_sec->next)
12713     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12714         && stub_sec->size != 0)
12715       {
12716         stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
12717         if (stub_sec->contents == NULL)
12718           return FALSE;
12719         /* We want to check that built size is the same as calculated
12720            size.  rawsize is a convenient location to use.  */
12721         stub_sec->rawsize = stub_sec->size;
12722         stub_sec->size = 0;
12723       }
12724
12725   if (htab->glink != NULL && htab->glink->size != 0)
12726     {
12727       unsigned int indx;
12728       bfd_vma plt0;
12729
12730       /* Build the .glink plt call stub.  */
12731       if (htab->params->emit_stub_syms)
12732         {
12733           struct elf_link_hash_entry *h;
12734           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12735                                     TRUE, FALSE, FALSE);
12736           if (h == NULL)
12737             return FALSE;
12738           if (h->root.type == bfd_link_hash_new)
12739             {
12740               h->root.type = bfd_link_hash_defined;
12741               h->root.u.def.section = htab->glink;
12742               h->root.u.def.value = 8;
12743               h->ref_regular = 1;
12744               h->def_regular = 1;
12745               h->ref_regular_nonweak = 1;
12746               h->forced_local = 1;
12747               h->non_elf = 0;
12748               h->root.linker_def = 1;
12749             }
12750         }
12751       plt0 = (htab->elf.splt->output_section->vma
12752               + htab->elf.splt->output_offset
12753               - 16);
12754       if (info->emitrelocations)
12755         {
12756           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12757           if (r == NULL)
12758             return FALSE;
12759           r->r_offset = (htab->glink->output_offset
12760                          + htab->glink->output_section->vma);
12761           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12762           r->r_addend = plt0;
12763         }
12764       p = htab->glink->contents;
12765       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
12766       bfd_put_64 (htab->glink->owner, plt0, p);
12767       p += 8;
12768       if (htab->opd_abi)
12769         {
12770           bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12771           p += 4;
12772           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12773           p += 4;
12774           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12775           p += 4;
12776           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12777           p += 4;
12778           bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12779           p += 4;
12780           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12781           p += 4;
12782           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12783           p += 4;
12784           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12785           p += 4;
12786           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12787           p += 4;
12788           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12789           p += 4;
12790         }
12791       else
12792         {
12793           bfd_put_32 (htab->glink->owner, MFLR_R0, p);
12794           p += 4;
12795           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12796           p += 4;
12797           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12798           p += 4;
12799           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12800           p += 4;
12801           bfd_put_32 (htab->glink->owner, MTLR_R0, p);
12802           p += 4;
12803           bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
12804           p += 4;
12805           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12806           p += 4;
12807           bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
12808           p += 4;
12809           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12810           p += 4;
12811           bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
12812           p += 4;
12813           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12814           p += 4;
12815           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
12816           p += 4;
12817         }
12818       bfd_put_32 (htab->glink->owner, BCTR, p);
12819       p += 4;
12820       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
12821         {
12822           bfd_put_32 (htab->glink->owner, NOP, p);
12823           p += 4;
12824         }
12825
12826       /* Build the .glink lazy link call stubs.  */
12827       indx = 0;
12828       while (p < htab->glink->contents + htab->glink->rawsize)
12829         {
12830           if (htab->opd_abi)
12831             {
12832               if (indx < 0x8000)
12833                 {
12834                   bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
12835                   p += 4;
12836                 }
12837               else
12838                 {
12839                   bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
12840                   p += 4;
12841                   bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
12842                               p);
12843                   p += 4;
12844                 }
12845             }
12846           bfd_put_32 (htab->glink->owner,
12847                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
12848           indx++;
12849           p += 4;
12850         }
12851
12852       /* Build .glink global entry stubs.  */
12853       if (htab->glink->size > htab->glink->rawsize)
12854         elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
12855     }
12856
12857   if (htab->brlt != NULL && htab->brlt->size != 0)
12858     {
12859       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
12860                                          htab->brlt->size);
12861       if (htab->brlt->contents == NULL)
12862         return FALSE;
12863     }
12864   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
12865     {
12866       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
12867                                             htab->relbrlt->size);
12868       if (htab->relbrlt->contents == NULL)
12869         return FALSE;
12870     }
12871
12872   /* Build the stubs as directed by the stub hash table.  */
12873   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
12874
12875   if (htab->relbrlt != NULL)
12876     htab->relbrlt->reloc_count = 0;
12877
12878   if (htab->params->plt_stub_align != 0)
12879     for (stub_sec = htab->params->stub_bfd->sections;
12880          stub_sec != NULL;
12881          stub_sec = stub_sec->next)
12882       if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12883         stub_sec->size = ((stub_sec->size
12884                            + (1 << htab->params->plt_stub_align) - 1)
12885                           & (-1 << htab->params->plt_stub_align));
12886
12887   for (stub_sec = htab->params->stub_bfd->sections;
12888        stub_sec != NULL;
12889        stub_sec = stub_sec->next)
12890     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12891       {
12892         stub_sec_count += 1;
12893         if (stub_sec->rawsize != stub_sec->size)
12894           break;
12895       }
12896
12897   /* Note that the glink_eh_frame check here is not only testing that
12898      the generated size matched the calculated size but also that
12899      bfd_elf_discard_info didn't make any changes to the section.  */
12900   if (stub_sec != NULL
12901       || (htab->glink_eh_frame != NULL
12902           && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
12903     {
12904       htab->stub_error = TRUE;
12905       info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
12906     }
12907
12908   if (htab->stub_error)
12909     return FALSE;
12910
12911   if (stats != NULL)
12912     {
12913       *stats = bfd_malloc (500);
12914       if (*stats == NULL)
12915         return FALSE;
12916
12917       sprintf (*stats, _("linker stubs in %u group%s\n"
12918                          "  branch       %lu\n"
12919                          "  toc adjust   %lu\n"
12920                          "  long branch  %lu\n"
12921                          "  long toc adj %lu\n"
12922                          "  plt call     %lu\n"
12923                          "  plt call toc %lu\n"
12924                          "  global entry %lu"),
12925                stub_sec_count,
12926                stub_sec_count == 1 ? "" : "s",
12927                htab->stub_count[ppc_stub_long_branch - 1],
12928                htab->stub_count[ppc_stub_long_branch_r2off - 1],
12929                htab->stub_count[ppc_stub_plt_branch - 1],
12930                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
12931                htab->stub_count[ppc_stub_plt_call - 1],
12932                htab->stub_count[ppc_stub_plt_call_r2save - 1],
12933                htab->stub_count[ppc_stub_global_entry - 1]);
12934     }
12935   return TRUE;
12936 }
12937
12938 /* This function undoes the changes made by add_symbol_adjust.  */
12939
12940 static bfd_boolean
12941 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12942 {
12943   struct ppc_link_hash_entry *eh;
12944
12945   if (h->root.type == bfd_link_hash_indirect)
12946     return TRUE;
12947
12948   eh = (struct ppc_link_hash_entry *) h;
12949   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
12950     return TRUE;
12951
12952   eh->elf.root.type = bfd_link_hash_undefined;
12953   return TRUE;
12954 }
12955
12956 void
12957 ppc64_elf_restore_symbols (struct bfd_link_info *info)
12958 {
12959   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12960
12961   if (htab != NULL)
12962     elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
12963 }
12964
12965 /* What to do when ld finds relocations against symbols defined in
12966    discarded sections.  */
12967
12968 static unsigned int
12969 ppc64_elf_action_discarded (asection *sec)
12970 {
12971   if (strcmp (".opd", sec->name) == 0)
12972     return 0;
12973
12974   if (strcmp (".toc", sec->name) == 0)
12975     return 0;
12976
12977   if (strcmp (".toc1", sec->name) == 0)
12978     return 0;
12979
12980   return _bfd_elf_default_action_discarded (sec);
12981 }
12982
12983 /* The RELOCATE_SECTION function is called by the ELF backend linker
12984    to handle the relocations for a section.
12985
12986    The relocs are always passed as Rela structures; if the section
12987    actually uses Rel structures, the r_addend field will always be
12988    zero.
12989
12990    This function is responsible for adjust the section contents as
12991    necessary, and (if using Rela relocs and generating a
12992    relocatable output file) adjusting the reloc addend as
12993    necessary.
12994
12995    This function does not have to worry about setting the reloc
12996    address or the reloc symbol index.
12997
12998    LOCAL_SYMS is a pointer to the swapped in local symbols.
12999
13000    LOCAL_SECTIONS is an array giving the section in the input file
13001    corresponding to the st_shndx field of each local symbol.
13002
13003    The global hash table entry for the global symbols can be found
13004    via elf_sym_hashes (input_bfd).
13005
13006    When generating relocatable output, this function must handle
13007    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
13008    going to be the section symbol corresponding to the output
13009    section, which means that the addend must be adjusted
13010    accordingly.  */
13011
13012 static bfd_boolean
13013 ppc64_elf_relocate_section (bfd *output_bfd,
13014                             struct bfd_link_info *info,
13015                             bfd *input_bfd,
13016                             asection *input_section,
13017                             bfd_byte *contents,
13018                             Elf_Internal_Rela *relocs,
13019                             Elf_Internal_Sym *local_syms,
13020                             asection **local_sections)
13021 {
13022   struct ppc_link_hash_table *htab;
13023   Elf_Internal_Shdr *symtab_hdr;
13024   struct elf_link_hash_entry **sym_hashes;
13025   Elf_Internal_Rela *rel;
13026   Elf_Internal_Rela *relend;
13027   Elf_Internal_Rela outrel;
13028   bfd_byte *loc;
13029   struct got_entry **local_got_ents;
13030   bfd_vma TOCstart;
13031   bfd_boolean ret = TRUE;
13032   bfd_boolean is_opd;
13033   /* Assume 'at' branch hints.  */
13034   bfd_boolean is_isa_v2 = TRUE;
13035   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
13036
13037   /* Initialize howto table if needed.  */
13038   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13039     ppc_howto_init ();
13040
13041   htab = ppc_hash_table (info);
13042   if (htab == NULL)
13043     return FALSE;
13044
13045   /* Don't relocate stub sections.  */
13046   if (input_section->owner == htab->params->stub_bfd)
13047     return TRUE;
13048
13049   BFD_ASSERT (is_ppc64_elf (input_bfd));
13050
13051   local_got_ents = elf_local_got_ents (input_bfd);
13052   TOCstart = elf_gp (output_bfd);
13053   symtab_hdr = &elf_symtab_hdr (input_bfd);
13054   sym_hashes = elf_sym_hashes (input_bfd);
13055   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13056
13057   rel = relocs;
13058   relend = relocs + input_section->reloc_count;
13059   for (; rel < relend; rel++)
13060     {
13061       enum elf_ppc64_reloc_type r_type;
13062       bfd_vma addend;
13063       bfd_reloc_status_type r;
13064       Elf_Internal_Sym *sym;
13065       asection *sec;
13066       struct elf_link_hash_entry *h_elf;
13067       struct ppc_link_hash_entry *h;
13068       struct ppc_link_hash_entry *fdh;
13069       const char *sym_name;
13070       unsigned long r_symndx, toc_symndx;
13071       bfd_vma toc_addend;
13072       unsigned char tls_mask, tls_gd, tls_type;
13073       unsigned char sym_type;
13074       bfd_vma relocation;
13075       bfd_boolean unresolved_reloc;
13076       bfd_boolean warned;
13077       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13078       unsigned int insn;
13079       unsigned int mask;
13080       struct ppc_stub_hash_entry *stub_entry;
13081       bfd_vma max_br_offset;
13082       bfd_vma from;
13083       const Elf_Internal_Rela orig_rel = *rel;
13084       reloc_howto_type *howto;
13085       struct reloc_howto_struct alt_howto;
13086
13087       r_type = ELF64_R_TYPE (rel->r_info);
13088       r_symndx = ELF64_R_SYM (rel->r_info);
13089
13090       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13091          symbol of the previous ADDR64 reloc.  The symbol gives us the
13092          proper TOC base to use.  */
13093       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13094           && rel != relocs
13095           && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
13096           && is_opd)
13097         r_symndx = ELF64_R_SYM (rel[-1].r_info);
13098
13099       sym = NULL;
13100       sec = NULL;
13101       h_elf = NULL;
13102       sym_name = NULL;
13103       unresolved_reloc = FALSE;
13104       warned = FALSE;
13105
13106       if (r_symndx < symtab_hdr->sh_info)
13107         {
13108           /* It's a local symbol.  */
13109           struct _opd_sec_data *opd;
13110
13111           sym = local_syms + r_symndx;
13112           sec = local_sections[r_symndx];
13113           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13114           sym_type = ELF64_ST_TYPE (sym->st_info);
13115           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13116           opd = get_opd_info (sec);
13117           if (opd != NULL && opd->adjust != NULL)
13118             {
13119               long adjust = opd->adjust[OPD_NDX (sym->st_value
13120                                                  + rel->r_addend)];
13121               if (adjust == -1)
13122                 relocation = 0;
13123               else
13124                 {
13125                   /* If this is a relocation against the opd section sym
13126                      and we have edited .opd, adjust the reloc addend so
13127                      that ld -r and ld --emit-relocs output is correct.
13128                      If it is a reloc against some other .opd symbol,
13129                      then the symbol value will be adjusted later.  */
13130                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13131                     rel->r_addend += adjust;
13132                   else
13133                     relocation += adjust;
13134                 }
13135             }
13136         }
13137       else
13138         {
13139           bfd_boolean ignored;
13140
13141           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13142                                    r_symndx, symtab_hdr, sym_hashes,
13143                                    h_elf, sec, relocation,
13144                                    unresolved_reloc, warned, ignored);
13145           sym_name = h_elf->root.root.string;
13146           sym_type = h_elf->type;
13147           if (sec != NULL
13148               && sec->owner == output_bfd
13149               && strcmp (sec->name, ".opd") == 0)
13150             {
13151               /* This is a symbol defined in a linker script.  All
13152                  such are defined in output sections, even those
13153                  defined by simple assignment from a symbol defined in
13154                  an input section.  Transfer the symbol to an
13155                  appropriate input .opd section, so that a branch to
13156                  this symbol will be mapped to the location specified
13157                  by the opd entry.  */
13158               struct bfd_link_order *lo;
13159               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13160                 if (lo->type == bfd_indirect_link_order)
13161                   {
13162                     asection *isec = lo->u.indirect.section;
13163                     if (h_elf->root.u.def.value >= isec->output_offset
13164                         && h_elf->root.u.def.value < (isec->output_offset
13165                                                       + isec->size))
13166                       {
13167                         h_elf->root.u.def.value -= isec->output_offset;
13168                         h_elf->root.u.def.section = isec;
13169                         sec = isec;
13170                         break;
13171                       }
13172                   }
13173             }
13174         }
13175       h = (struct ppc_link_hash_entry *) h_elf;
13176
13177       if (sec != NULL && discarded_section (sec))
13178         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
13179                                          rel, 1, relend,
13180                                          ppc64_elf_howto_table[r_type], 0,
13181                                          contents);
13182
13183       if (info->relocatable)
13184         continue;
13185
13186       if (h != NULL && &h->elf == htab->elf.hgot)
13187         {
13188           relocation = (TOCstart
13189                         + htab->stub_group[input_section->id].toc_off);
13190           sec = bfd_abs_section_ptr;
13191           unresolved_reloc = FALSE;
13192         }
13193
13194       /* TLS optimizations.  Replace instruction sequences and relocs
13195          based on information we collected in tls_optimize.  We edit
13196          RELOCS so that --emit-relocs will output something sensible
13197          for the final instruction stream.  */
13198       tls_mask = 0;
13199       tls_gd = 0;
13200       toc_symndx = 0;
13201       if (h != NULL)
13202         tls_mask = h->tls_mask;
13203       else if (local_got_ents != NULL)
13204         {
13205           struct plt_entry **local_plt = (struct plt_entry **)
13206             (local_got_ents + symtab_hdr->sh_info);
13207           unsigned char *lgot_masks = (unsigned char *)
13208             (local_plt + symtab_hdr->sh_info);
13209           tls_mask = lgot_masks[r_symndx];
13210         }
13211       if (tls_mask == 0
13212           && (r_type == R_PPC64_TLS
13213               || r_type == R_PPC64_TLSGD
13214               || r_type == R_PPC64_TLSLD))
13215         {
13216           /* Check for toc tls entries.  */
13217           unsigned char *toc_tls;
13218
13219           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13220                              &local_syms, rel, input_bfd))
13221             return FALSE;
13222
13223           if (toc_tls)
13224             tls_mask = *toc_tls;
13225         }
13226
13227       /* Check that tls relocs are used with tls syms, and non-tls
13228          relocs are used with non-tls syms.  */
13229       if (r_symndx != STN_UNDEF
13230           && r_type != R_PPC64_NONE
13231           && (h == NULL
13232               || h->elf.root.type == bfd_link_hash_defined
13233               || h->elf.root.type == bfd_link_hash_defweak)
13234           && (IS_PPC64_TLS_RELOC (r_type)
13235               != (sym_type == STT_TLS
13236                   || (sym_type == STT_SECTION
13237                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13238         {
13239           if (tls_mask != 0
13240               && (r_type == R_PPC64_TLS
13241                   || r_type == R_PPC64_TLSGD
13242                   || r_type == R_PPC64_TLSLD))
13243             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
13244             ;
13245           else
13246             info->callbacks->einfo
13247               (!IS_PPC64_TLS_RELOC (r_type)
13248                ? _("%P: %H: %s used with TLS symbol `%T'\n")
13249                : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
13250                input_bfd, input_section, rel->r_offset,
13251                ppc64_elf_howto_table[r_type]->name,
13252                sym_name);
13253         }
13254
13255       /* Ensure reloc mapping code below stays sane.  */
13256       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13257           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13258           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
13259           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13260           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13261           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13262           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
13263           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13264           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13265           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13266         abort ();
13267
13268       switch (r_type)
13269         {
13270         default:
13271           break;
13272
13273         case R_PPC64_LO_DS_OPT:
13274           insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
13275           if ((insn & (0x3f << 26)) != 58u << 26)
13276             abort ();
13277           insn += (14u << 26) - (58u << 26);
13278           bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
13279           r_type = R_PPC64_TOC16_LO;
13280           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13281           break;
13282
13283         case R_PPC64_TOC16:
13284         case R_PPC64_TOC16_LO:
13285         case R_PPC64_TOC16_DS:
13286         case R_PPC64_TOC16_LO_DS:
13287           {
13288             /* Check for toc tls entries.  */
13289             unsigned char *toc_tls;
13290             int retval;
13291
13292             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13293                                    &local_syms, rel, input_bfd);
13294             if (retval == 0)
13295               return FALSE;
13296
13297             if (toc_tls)
13298               {
13299                 tls_mask = *toc_tls;
13300                 if (r_type == R_PPC64_TOC16_DS
13301                     || r_type == R_PPC64_TOC16_LO_DS)
13302                   {
13303                     if (tls_mask != 0
13304                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13305                       goto toctprel;
13306                   }
13307                 else
13308                   {
13309                     /* If we found a GD reloc pair, then we might be
13310                        doing a GD->IE transition.  */
13311                     if (retval == 2)
13312                       {
13313                         tls_gd = TLS_TPRELGD;
13314                         if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13315                           goto tls_ldgd_opt;
13316                       }
13317                     else if (retval == 3)
13318                       {
13319                         if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13320                           goto tls_ldgd_opt;
13321                       }
13322                   }
13323               }
13324           }
13325           break;
13326
13327         case R_PPC64_GOT_TPREL16_HI:
13328         case R_PPC64_GOT_TPREL16_HA:
13329           if (tls_mask != 0
13330               && (tls_mask & TLS_TPREL) == 0)
13331             {
13332               rel->r_offset -= d_offset;
13333               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13334               r_type = R_PPC64_NONE;
13335               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13336             }
13337           break;
13338
13339         case R_PPC64_GOT_TPREL16_DS:
13340         case R_PPC64_GOT_TPREL16_LO_DS:
13341           if (tls_mask != 0
13342               && (tls_mask & TLS_TPREL) == 0)
13343             {
13344             toctprel:
13345               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
13346               insn &= 31 << 21;
13347               insn |= 0x3c0d0000;       /* addis 0,13,0 */
13348               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
13349               r_type = R_PPC64_TPREL16_HA;
13350               if (toc_symndx != 0)
13351                 {
13352                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13353                   rel->r_addend = toc_addend;
13354                   /* We changed the symbol.  Start over in order to
13355                      get h, sym, sec etc. right.  */
13356                   rel--;
13357                   continue;
13358                 }
13359               else
13360                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13361             }
13362           break;
13363
13364         case R_PPC64_TLS:
13365           if (tls_mask != 0
13366               && (tls_mask & TLS_TPREL) == 0)
13367             {
13368               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
13369               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13370               if (insn == 0)
13371                 abort ();
13372               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
13373               /* Was PPC64_TLS which sits on insn boundary, now
13374                  PPC64_TPREL16_LO which is at low-order half-word.  */
13375               rel->r_offset += d_offset;
13376               r_type = R_PPC64_TPREL16_LO;
13377               if (toc_symndx != 0)
13378                 {
13379                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13380                   rel->r_addend = toc_addend;
13381                   /* We changed the symbol.  Start over in order to
13382                      get h, sym, sec etc. right.  */
13383                   rel--;
13384                   continue;
13385                 }
13386               else
13387                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13388             }
13389           break;
13390
13391         case R_PPC64_GOT_TLSGD16_HI:
13392         case R_PPC64_GOT_TLSGD16_HA:
13393           tls_gd = TLS_TPRELGD;
13394           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13395             goto tls_gdld_hi;
13396           break;
13397
13398         case R_PPC64_GOT_TLSLD16_HI:
13399         case R_PPC64_GOT_TLSLD16_HA:
13400           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13401             {
13402             tls_gdld_hi:
13403               if ((tls_mask & tls_gd) != 0)
13404                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13405                           + R_PPC64_GOT_TPREL16_DS);
13406               else
13407                 {
13408                   rel->r_offset -= d_offset;
13409                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13410                   r_type = R_PPC64_NONE;
13411                 }
13412               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13413             }
13414           break;
13415
13416         case R_PPC64_GOT_TLSGD16:
13417         case R_PPC64_GOT_TLSGD16_LO:
13418           tls_gd = TLS_TPRELGD;
13419           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13420             goto tls_ldgd_opt;
13421           break;
13422
13423         case R_PPC64_GOT_TLSLD16:
13424         case R_PPC64_GOT_TLSLD16_LO:
13425           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13426             {
13427               unsigned int insn1, insn2, insn3;
13428               bfd_vma offset;
13429
13430             tls_ldgd_opt:
13431               offset = (bfd_vma) -1;
13432               /* If not using the newer R_PPC64_TLSGD/LD to mark
13433                  __tls_get_addr calls, we must trust that the call
13434                  stays with its arg setup insns, ie. that the next
13435                  reloc is the __tls_get_addr call associated with
13436                  the current reloc.  Edit both insns.  */
13437               if (input_section->has_tls_get_addr_call
13438                   && rel + 1 < relend
13439                   && branch_reloc_hash_match (input_bfd, rel + 1,
13440                                               htab->tls_get_addr,
13441                                               htab->tls_get_addr_fd))
13442                 offset = rel[1].r_offset;
13443               /* We read the low GOT_TLS (or TOC16) insn because we
13444                  need to keep the destination reg.  It may be
13445                  something other than the usual r3, and moved to r3
13446                  before the call by intervening code.  */
13447               insn1 = bfd_get_32 (output_bfd,
13448                                   contents + rel->r_offset - d_offset);
13449               if ((tls_mask & tls_gd) != 0)
13450                 {
13451                   /* IE */
13452                   insn1 &= (0x1f << 21) | (0x1f << 16);
13453                   insn1 |= 58 << 26;    /* ld */
13454                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13455                   if (offset != (bfd_vma) -1)
13456                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13457                   if ((tls_mask & TLS_EXPLICIT) == 0)
13458                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13459                               + R_PPC64_GOT_TPREL16_DS);
13460                   else
13461                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13462                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13463                 }
13464               else
13465                 {
13466                   /* LE */
13467                   insn1 &= 0x1f << 21;
13468                   insn1 |= 0x3c0d0000;  /* addis r,13,0 */
13469                   insn2 = 0x38630000;   /* addi 3,3,0 */
13470                   if (tls_gd == 0)
13471                     {
13472                       /* Was an LD reloc.  */
13473                       if (toc_symndx)
13474                         sec = local_sections[toc_symndx];
13475                       for (r_symndx = 0;
13476                            r_symndx < symtab_hdr->sh_info;
13477                            r_symndx++)
13478                         if (local_sections[r_symndx] == sec)
13479                           break;
13480                       if (r_symndx >= symtab_hdr->sh_info)
13481                         r_symndx = STN_UNDEF;
13482                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13483                       if (r_symndx != STN_UNDEF)
13484                         rel->r_addend -= (local_syms[r_symndx].st_value
13485                                           + sec->output_offset
13486                                           + sec->output_section->vma);
13487                     }
13488                   else if (toc_symndx != 0)
13489                     {
13490                       r_symndx = toc_symndx;
13491                       rel->r_addend = toc_addend;
13492                     }
13493                   r_type = R_PPC64_TPREL16_HA;
13494                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13495                   if (offset != (bfd_vma) -1)
13496                     {
13497                       rel[1].r_info = ELF64_R_INFO (r_symndx,
13498                                                     R_PPC64_TPREL16_LO);
13499                       rel[1].r_offset = offset + d_offset;
13500                       rel[1].r_addend = rel->r_addend;
13501                     }
13502                 }
13503               bfd_put_32 (output_bfd, insn1,
13504                           contents + rel->r_offset - d_offset);
13505               if (offset != (bfd_vma) -1)
13506                 {
13507                   insn3 = bfd_get_32 (output_bfd,
13508                                       contents + offset + 4);
13509                   if (insn3 == NOP
13510                       || insn3 == CROR_151515 || insn3 == CROR_313131)
13511                     {
13512                       rel[1].r_offset += 4;
13513                       bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13514                       insn2 = NOP;
13515                     }
13516                   bfd_put_32 (output_bfd, insn2, contents + offset);
13517                 }
13518               if ((tls_mask & tls_gd) == 0
13519                   && (tls_gd == 0 || toc_symndx != 0))
13520                 {
13521                   /* We changed the symbol.  Start over in order
13522                      to get h, sym, sec etc. right.  */
13523                   rel--;
13524                   continue;
13525                 }
13526             }
13527           break;
13528
13529         case R_PPC64_TLSGD:
13530           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13531             {
13532               unsigned int insn2, insn3;
13533               bfd_vma offset = rel->r_offset;
13534
13535               if ((tls_mask & TLS_TPRELGD) != 0)
13536                 {
13537                   /* IE */
13538                   r_type = R_PPC64_NONE;
13539                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13540                 }
13541               else
13542                 {
13543                   /* LE */
13544                   if (toc_symndx != 0)
13545                     {
13546                       r_symndx = toc_symndx;
13547                       rel->r_addend = toc_addend;
13548                     }
13549                   r_type = R_PPC64_TPREL16_LO;
13550                   rel->r_offset = offset + d_offset;
13551                   insn2 = 0x38630000;   /* addi 3,3,0 */
13552                 }
13553               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13554               /* Zap the reloc on the _tls_get_addr call too.  */
13555               BFD_ASSERT (offset == rel[1].r_offset);
13556               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13557               insn3 = bfd_get_32 (output_bfd,
13558                                   contents + offset + 4);
13559               if (insn3 == NOP
13560                   || insn3 == CROR_151515 || insn3 == CROR_313131)
13561                 {
13562                   rel->r_offset += 4;
13563                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13564                   insn2 = NOP;
13565                 }
13566               bfd_put_32 (output_bfd, insn2, contents + offset);
13567               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13568                 {
13569                   rel--;
13570                   continue;
13571                 }
13572             }
13573           break;
13574
13575         case R_PPC64_TLSLD:
13576           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13577             {
13578               unsigned int insn2, insn3;
13579               bfd_vma offset = rel->r_offset;
13580
13581               if (toc_symndx)
13582                 sec = local_sections[toc_symndx];
13583               for (r_symndx = 0;
13584                    r_symndx < symtab_hdr->sh_info;
13585                    r_symndx++)
13586                 if (local_sections[r_symndx] == sec)
13587                   break;
13588               if (r_symndx >= symtab_hdr->sh_info)
13589                 r_symndx = STN_UNDEF;
13590               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13591               if (r_symndx != STN_UNDEF)
13592                 rel->r_addend -= (local_syms[r_symndx].st_value
13593                                   + sec->output_offset
13594                                   + sec->output_section->vma);
13595
13596               r_type = R_PPC64_TPREL16_LO;
13597               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13598               rel->r_offset = offset + d_offset;
13599               /* Zap the reloc on the _tls_get_addr call too.  */
13600               BFD_ASSERT (offset == rel[1].r_offset);
13601               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13602               insn2 = 0x38630000;       /* addi 3,3,0 */
13603               insn3 = bfd_get_32 (output_bfd,
13604                                   contents + offset + 4);
13605               if (insn3 == NOP
13606                   || insn3 == CROR_151515 || insn3 == CROR_313131)
13607                 {
13608                   rel->r_offset += 4;
13609                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13610                   insn2 = NOP;
13611                 }
13612               bfd_put_32 (output_bfd, insn2, contents + offset);
13613               rel--;
13614               continue;
13615             }
13616           break;
13617
13618         case R_PPC64_DTPMOD64:
13619           if (rel + 1 < relend
13620               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13621               && rel[1].r_offset == rel->r_offset + 8)
13622             {
13623               if ((tls_mask & TLS_GD) == 0)
13624                 {
13625                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13626                   if ((tls_mask & TLS_TPRELGD) != 0)
13627                     r_type = R_PPC64_TPREL64;
13628                   else
13629                     {
13630                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13631                       r_type = R_PPC64_NONE;
13632                     }
13633                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13634                 }
13635             }
13636           else
13637             {
13638               if ((tls_mask & TLS_LD) == 0)
13639                 {
13640                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13641                   r_type = R_PPC64_NONE;
13642                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13643                 }
13644             }
13645           break;
13646
13647         case R_PPC64_TPREL64:
13648           if ((tls_mask & TLS_TPREL) == 0)
13649             {
13650               r_type = R_PPC64_NONE;
13651               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13652             }
13653           break;
13654
13655         case R_PPC64_REL16_HA:
13656           /* If we are generating a non-PIC executable, edit
13657              .  0:      addis 2,12,.TOC.-0b@ha
13658              .          addi 2,2,.TOC.-0b@l
13659              used by ELFv2 global entry points to set up r2, to
13660              .          lis 2,.TOC.@ha
13661              .          addi 2,2,.TOC.@l
13662              if .TOC. is in range.  */
13663           if (!info->shared
13664               && !info->traditional_format
13665               && h != NULL && &h->elf == htab->elf.hgot
13666               && rel + 1 < relend
13667               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
13668               && rel[1].r_offset == rel->r_offset + 4
13669               && rel[1].r_addend == rel->r_addend + 4
13670               && relocation + 0x80008000 <= 0xffffffff)
13671             {
13672               unsigned int insn1, insn2;
13673               bfd_vma offset = rel->r_offset - d_offset;
13674               insn1 = bfd_get_32 (output_bfd, contents + offset);
13675               insn2 = bfd_get_32 (output_bfd, contents + offset + 4);
13676               if ((insn1 & 0xffff0000) == 0x3c4c0000 /* addis 2,12 */
13677                   && (insn2 & 0xffff0000) == 0x38420000 /* addi 2,2 */)
13678                 {
13679                   r_type = R_PPC64_ADDR16_HA;
13680                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13681                   rel->r_addend -= d_offset;
13682                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
13683                   rel[1].r_addend -= d_offset + 4;
13684                   bfd_put_32 (output_bfd, 0x3c400000, contents + offset);
13685                 }
13686             }
13687           break;
13688         }
13689
13690       /* Handle other relocations that tweak non-addend part of insn.  */
13691       insn = 0;
13692       max_br_offset = 1 << 25;
13693       addend = rel->r_addend;
13694       reloc_dest = DEST_NORMAL;
13695       switch (r_type)
13696         {
13697         default:
13698           break;
13699
13700         case R_PPC64_TOCSAVE:
13701           if (relocation + addend == (rel->r_offset
13702                                       + input_section->output_offset
13703                                       + input_section->output_section->vma)
13704               && tocsave_find (htab, NO_INSERT,
13705                                &local_syms, rel, input_bfd))
13706             {
13707               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13708               if (insn == NOP
13709                   || insn == CROR_151515 || insn == CROR_313131)
13710                 bfd_put_32 (input_bfd,
13711                             STD_R2_0R1 + STK_TOC (htab),
13712                             contents + rel->r_offset);
13713             }
13714           break;
13715
13716           /* Branch taken prediction relocations.  */
13717         case R_PPC64_ADDR14_BRTAKEN:
13718         case R_PPC64_REL14_BRTAKEN:
13719           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
13720           /* Fall thru.  */
13721
13722           /* Branch not taken prediction relocations.  */
13723         case R_PPC64_ADDR14_BRNTAKEN:
13724         case R_PPC64_REL14_BRNTAKEN:
13725           insn |= bfd_get_32 (output_bfd,
13726                               contents + rel->r_offset) & ~(0x01 << 21);
13727           /* Fall thru.  */
13728
13729         case R_PPC64_REL14:
13730           max_br_offset = 1 << 15;
13731           /* Fall thru.  */
13732
13733         case R_PPC64_REL24:
13734           /* Calls to functions with a different TOC, such as calls to
13735              shared objects, need to alter the TOC pointer.  This is
13736              done using a linkage stub.  A REL24 branching to these
13737              linkage stubs needs to be followed by a nop, as the nop
13738              will be replaced with an instruction to restore the TOC
13739              base pointer.  */
13740           fdh = h;
13741           if (h != NULL
13742               && h->oh != NULL
13743               && h->oh->is_func_descriptor)
13744             fdh = ppc_follow_link (h->oh);
13745           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13746                                            htab);
13747           if (stub_entry != NULL
13748               && (stub_entry->stub_type == ppc_stub_plt_call
13749                   || stub_entry->stub_type == ppc_stub_plt_call_r2save
13750                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13751                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
13752             {
13753               bfd_boolean can_plt_call = FALSE;
13754
13755               /* All of these stubs will modify r2, so there must be a
13756                  branch and link followed by a nop.  The nop is
13757                  replaced by an insn to restore r2.  */
13758               if (rel->r_offset + 8 <= input_section->size)
13759                 {
13760                   unsigned long br;
13761
13762                   br = bfd_get_32 (input_bfd,
13763                                    contents + rel->r_offset);
13764                   if ((br & 1) != 0)
13765                     {
13766                       unsigned long nop;
13767
13768                       nop = bfd_get_32 (input_bfd,
13769                                         contents + rel->r_offset + 4);
13770                       if (nop == NOP
13771                           || nop == CROR_151515 || nop == CROR_313131)
13772                         {
13773                           if (h != NULL
13774                               && (h == htab->tls_get_addr_fd
13775                                   || h == htab->tls_get_addr)
13776                               && !htab->params->no_tls_get_addr_opt)
13777                             {
13778                               /* Special stub used, leave nop alone.  */
13779                             }
13780                           else
13781                             bfd_put_32 (input_bfd,
13782                                         LD_R2_0R1 + STK_TOC (htab),
13783                                         contents + rel->r_offset + 4);
13784                           can_plt_call = TRUE;
13785                         }
13786                     }
13787                 }
13788
13789               if (!can_plt_call && h != NULL)
13790                 {
13791                   const char *name = h->elf.root.root.string;
13792
13793                   if (*name == '.')
13794                     ++name;
13795
13796                   if (strncmp (name, "__libc_start_main", 17) == 0
13797                       && (name[17] == 0 || name[17] == '@'))
13798                     {
13799                       /* Allow crt1 branch to go via a toc adjusting
13800                          stub.  Other calls that never return could do
13801                          the same, if we could detect such.  */
13802                       can_plt_call = TRUE;
13803                     }
13804                 }
13805
13806               if (!can_plt_call)
13807                 {
13808                   /* g++ as of 20130507 emits self-calls without a
13809                      following nop.  This is arguably wrong since we
13810                      have conflicting information.  On the one hand a
13811                      global symbol and on the other a local call
13812                      sequence, but don't error for this special case.
13813                      It isn't possible to cheaply verify we have
13814                      exactly such a call.  Allow all calls to the same
13815                      section.  */
13816                   asection *code_sec = sec;
13817
13818                   if (get_opd_info (sec) != NULL)
13819                     {
13820                       bfd_vma off = (relocation + addend
13821                                      - sec->output_section->vma
13822                                      - sec->output_offset);
13823
13824                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
13825                     }
13826                   if (code_sec == input_section)
13827                     can_plt_call = TRUE;
13828                 }
13829
13830               if (!can_plt_call)
13831                 {
13832                   if (stub_entry->stub_type == ppc_stub_plt_call
13833                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13834                     info->callbacks->einfo
13835                       (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13836                          "recompile with -fPIC\n"),
13837                        input_bfd, input_section, rel->r_offset, sym_name);
13838                   else
13839                     info->callbacks->einfo
13840                       (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13841                          "(-mcmodel=small toc adjust stub)\n"),
13842                        input_bfd, input_section, rel->r_offset, sym_name);
13843
13844                   bfd_set_error (bfd_error_bad_value);
13845                   ret = FALSE;
13846                 }
13847
13848               if (can_plt_call
13849                   && (stub_entry->stub_type == ppc_stub_plt_call
13850                       || stub_entry->stub_type == ppc_stub_plt_call_r2save))
13851                 unresolved_reloc = FALSE;
13852             }
13853
13854           if ((stub_entry == NULL
13855                || stub_entry->stub_type == ppc_stub_long_branch
13856                || stub_entry->stub_type == ppc_stub_plt_branch)
13857               && get_opd_info (sec) != NULL)
13858             {
13859               /* The branch destination is the value of the opd entry. */
13860               bfd_vma off = (relocation + addend
13861                              - sec->output_section->vma
13862                              - sec->output_offset);
13863               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
13864               if (dest != (bfd_vma) -1)
13865                 {
13866                   relocation = dest;
13867                   addend = 0;
13868                   reloc_dest = DEST_OPD;
13869                 }
13870             }
13871
13872           /* If the branch is out of reach we ought to have a long
13873              branch stub.  */
13874           from = (rel->r_offset
13875                   + input_section->output_offset
13876                   + input_section->output_section->vma);
13877
13878           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
13879                                                   ? fdh->elf.other
13880                                                   : sym->st_other);
13881
13882           if (stub_entry != NULL
13883               && (stub_entry->stub_type == ppc_stub_long_branch
13884                   || stub_entry->stub_type == ppc_stub_plt_branch)
13885               && (r_type == R_PPC64_ADDR14_BRTAKEN
13886                   || r_type == R_PPC64_ADDR14_BRNTAKEN
13887                   || (relocation + addend - from + max_br_offset
13888                       < 2 * max_br_offset)))
13889             /* Don't use the stub if this branch is in range.  */
13890             stub_entry = NULL;
13891
13892           if (stub_entry != NULL)
13893             {
13894               /* Munge up the value and addend so that we call the stub
13895                  rather than the procedure directly.  */
13896               relocation = (stub_entry->stub_offset
13897                             + stub_entry->stub_sec->output_offset
13898                             + stub_entry->stub_sec->output_section->vma);
13899               addend = 0;
13900               reloc_dest = DEST_STUB;
13901
13902               if ((stub_entry->stub_type == ppc_stub_plt_call
13903                    || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13904                   && (ALWAYS_EMIT_R2SAVE
13905                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13906                   && rel + 1 < relend
13907                   && rel[1].r_offset == rel->r_offset + 4
13908                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
13909                 relocation += 4;
13910             }
13911
13912           if (insn != 0)
13913             {
13914               if (is_isa_v2)
13915                 {
13916                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
13917                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
13918                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
13919                   if ((insn & (0x14 << 21)) == (0x04 << 21))
13920                     insn |= 0x02 << 21;
13921                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
13922                     insn |= 0x08 << 21;
13923                   else
13924                     break;
13925                 }
13926               else
13927                 {
13928                   /* Invert 'y' bit if not the default.  */
13929                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
13930                     insn ^= 0x01 << 21;
13931                 }
13932
13933               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
13934             }
13935
13936           /* NOP out calls to undefined weak functions.
13937              We can thus call a weak function without first
13938              checking whether the function is defined.  */
13939           else if (h != NULL
13940                    && h->elf.root.type == bfd_link_hash_undefweak
13941                    && h->elf.dynindx == -1
13942                    && r_type == R_PPC64_REL24
13943                    && relocation == 0
13944                    && addend == 0)
13945             {
13946               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13947               continue;
13948             }
13949           break;
13950         }
13951
13952       /* Set `addend'.  */
13953       tls_type = 0;
13954       switch (r_type)
13955         {
13956         default:
13957           info->callbacks->einfo
13958             (_("%P: %B: unknown relocation type %d for `%T'\n"),
13959              input_bfd, (int) r_type, sym_name);
13960
13961           bfd_set_error (bfd_error_bad_value);
13962           ret = FALSE;
13963           continue;
13964
13965         case R_PPC64_NONE:
13966         case R_PPC64_TLS:
13967         case R_PPC64_TLSGD:
13968         case R_PPC64_TLSLD:
13969         case R_PPC64_TOCSAVE:
13970         case R_PPC64_GNU_VTINHERIT:
13971         case R_PPC64_GNU_VTENTRY:
13972           continue;
13973
13974           /* GOT16 relocations.  Like an ADDR16 using the symbol's
13975              address in the GOT as relocation value instead of the
13976              symbol's value itself.  Also, create a GOT entry for the
13977              symbol and put the symbol value there.  */
13978         case R_PPC64_GOT_TLSGD16:
13979         case R_PPC64_GOT_TLSGD16_LO:
13980         case R_PPC64_GOT_TLSGD16_HI:
13981         case R_PPC64_GOT_TLSGD16_HA:
13982           tls_type = TLS_TLS | TLS_GD;
13983           goto dogot;
13984
13985         case R_PPC64_GOT_TLSLD16:
13986         case R_PPC64_GOT_TLSLD16_LO:
13987         case R_PPC64_GOT_TLSLD16_HI:
13988         case R_PPC64_GOT_TLSLD16_HA:
13989           tls_type = TLS_TLS | TLS_LD;
13990           goto dogot;
13991
13992         case R_PPC64_GOT_TPREL16_DS:
13993         case R_PPC64_GOT_TPREL16_LO_DS:
13994         case R_PPC64_GOT_TPREL16_HI:
13995         case R_PPC64_GOT_TPREL16_HA:
13996           tls_type = TLS_TLS | TLS_TPREL;
13997           goto dogot;
13998
13999         case R_PPC64_GOT_DTPREL16_DS:
14000         case R_PPC64_GOT_DTPREL16_LO_DS:
14001         case R_PPC64_GOT_DTPREL16_HI:
14002         case R_PPC64_GOT_DTPREL16_HA:
14003           tls_type = TLS_TLS | TLS_DTPREL;
14004           goto dogot;
14005
14006         case R_PPC64_GOT16:
14007         case R_PPC64_GOT16_LO:
14008         case R_PPC64_GOT16_HI:
14009         case R_PPC64_GOT16_HA:
14010         case R_PPC64_GOT16_DS:
14011         case R_PPC64_GOT16_LO_DS:
14012         dogot:
14013           {
14014             /* Relocation is to the entry for this symbol in the global
14015                offset table.  */
14016             asection *got;
14017             bfd_vma *offp;
14018             bfd_vma off;
14019             unsigned long indx = 0;
14020             struct got_entry *ent;
14021
14022             if (tls_type == (TLS_TLS | TLS_LD)
14023                 && (h == NULL
14024                     || !h->elf.def_dynamic))
14025               ent = ppc64_tlsld_got (input_bfd);
14026             else
14027               {
14028
14029                 if (h != NULL)
14030                   {
14031                     bfd_boolean dyn = htab->elf.dynamic_sections_created;
14032                     if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
14033                                                           &h->elf)
14034                         || (info->shared
14035                             && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
14036                       /* This is actually a static link, or it is a
14037                          -Bsymbolic link and the symbol is defined
14038                          locally, or the symbol was forced to be local
14039                          because of a version file.  */
14040                       ;
14041                     else
14042                       {
14043                         BFD_ASSERT (h->elf.dynindx != -1);
14044                         indx = h->elf.dynindx;
14045                         unresolved_reloc = FALSE;
14046                       }
14047                     ent = h->elf.got.glist;
14048                   }
14049                 else
14050                   {
14051                     if (local_got_ents == NULL)
14052                       abort ();
14053                     ent = local_got_ents[r_symndx];
14054                   }
14055
14056                 for (; ent != NULL; ent = ent->next)
14057                   if (ent->addend == orig_rel.r_addend
14058                       && ent->owner == input_bfd
14059                       && ent->tls_type == tls_type)
14060                     break;
14061               }
14062
14063             if (ent == NULL)
14064               abort ();
14065             if (ent->is_indirect)
14066               ent = ent->got.ent;
14067             offp = &ent->got.offset;
14068             got = ppc64_elf_tdata (ent->owner)->got;
14069             if (got == NULL)
14070               abort ();
14071
14072             /* The offset must always be a multiple of 8.  We use the
14073                least significant bit to record whether we have already
14074                processed this entry.  */
14075             off = *offp;
14076             if ((off & 1) != 0)
14077               off &= ~1;
14078             else
14079               {
14080                 /* Generate relocs for the dynamic linker, except in
14081                    the case of TLSLD where we'll use one entry per
14082                    module.  */
14083                 asection *relgot;
14084                 bfd_boolean ifunc;
14085
14086                 *offp = off | 1;
14087                 relgot = NULL;
14088                 ifunc = (h != NULL
14089                          ? h->elf.type == STT_GNU_IFUNC
14090                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14091                 if (ifunc)
14092                   relgot = htab->elf.irelplt;
14093                 else if ((info->shared || indx != 0)
14094                          && (h == NULL
14095                              || (tls_type == (TLS_TLS | TLS_LD)
14096                                  && !h->elf.def_dynamic)
14097                              || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14098                              || h->elf.root.type != bfd_link_hash_undefweak))
14099                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
14100                 if (relgot != NULL)
14101                   {
14102                     outrel.r_offset = (got->output_section->vma
14103                                        + got->output_offset
14104                                        + off);
14105                     outrel.r_addend = addend;
14106                     if (tls_type & (TLS_LD | TLS_GD))
14107                       {
14108                         outrel.r_addend = 0;
14109                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14110                         if (tls_type == (TLS_TLS | TLS_GD))
14111                           {
14112                             loc = relgot->contents;
14113                             loc += (relgot->reloc_count++
14114                                     * sizeof (Elf64_External_Rela));
14115                             bfd_elf64_swap_reloca_out (output_bfd,
14116                                                        &outrel, loc);
14117                             outrel.r_offset += 8;
14118                             outrel.r_addend = addend;
14119                             outrel.r_info
14120                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14121                           }
14122                       }
14123                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
14124                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14125                     else if (tls_type == (TLS_TLS | TLS_TPREL))
14126                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14127                     else if (indx != 0)
14128                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14129                     else
14130                       {
14131                         if (ifunc)
14132                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14133                         else
14134                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14135
14136                         /* Write the .got section contents for the sake
14137                            of prelink.  */
14138                         loc = got->contents + off;
14139                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14140                                     loc);
14141                       }
14142
14143                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14144                       {
14145                         outrel.r_addend += relocation;
14146                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14147                           {
14148                             if (htab->elf.tls_sec == NULL)
14149                               outrel.r_addend = 0;
14150                             else
14151                               outrel.r_addend -= htab->elf.tls_sec->vma;
14152                           }
14153                       }
14154                     loc = relgot->contents;
14155                     loc += (relgot->reloc_count++
14156                             * sizeof (Elf64_External_Rela));
14157                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14158                   }
14159
14160                 /* Init the .got section contents here if we're not
14161                    emitting a reloc.  */
14162                 else
14163                   {
14164                     relocation += addend;
14165                     if (tls_type == (TLS_TLS | TLS_LD))
14166                       relocation = 1;
14167                     else if (tls_type != 0)
14168                       {
14169                         if (htab->elf.tls_sec == NULL)
14170                           relocation = 0;
14171                         else
14172                           {
14173                             relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14174                             if (tls_type == (TLS_TLS | TLS_TPREL))
14175                               relocation += DTP_OFFSET - TP_OFFSET;
14176                           }
14177
14178                         if (tls_type == (TLS_TLS | TLS_GD))
14179                           {
14180                             bfd_put_64 (output_bfd, relocation,
14181                                         got->contents + off + 8);
14182                             relocation = 1;
14183                           }
14184                       }
14185
14186                     bfd_put_64 (output_bfd, relocation,
14187                                 got->contents + off);
14188                   }
14189               }
14190
14191             if (off >= (bfd_vma) -2)
14192               abort ();
14193
14194             relocation = got->output_section->vma + got->output_offset + off;
14195             addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
14196           }
14197           break;
14198
14199         case R_PPC64_PLT16_HA:
14200         case R_PPC64_PLT16_HI:
14201         case R_PPC64_PLT16_LO:
14202         case R_PPC64_PLT32:
14203         case R_PPC64_PLT64:
14204           /* Relocation is to the entry for this symbol in the
14205              procedure linkage table.  */
14206
14207           /* Resolve a PLT reloc against a local symbol directly,
14208              without using the procedure linkage table.  */
14209           if (h == NULL)
14210             break;
14211
14212           /* It's possible that we didn't make a PLT entry for this
14213              symbol.  This happens when statically linking PIC code,
14214              or when using -Bsymbolic.  Go find a match if there is a
14215              PLT entry.  */
14216           if (htab->elf.splt != NULL)
14217             {
14218               struct plt_entry *ent;
14219               for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
14220                 if (ent->plt.offset != (bfd_vma) -1
14221                     && ent->addend == orig_rel.r_addend)
14222                   {
14223                     relocation = (htab->elf.splt->output_section->vma
14224                                   + htab->elf.splt->output_offset
14225                                   + ent->plt.offset);
14226                     unresolved_reloc = FALSE;
14227                     break;
14228                   }
14229             }
14230           break;
14231
14232         case R_PPC64_TOC:
14233           /* Relocation value is TOC base.  */
14234           relocation = TOCstart;
14235           if (r_symndx == STN_UNDEF)
14236             relocation += htab->stub_group[input_section->id].toc_off;
14237           else if (unresolved_reloc)
14238             ;
14239           else if (sec != NULL && sec->id <= htab->top_id)
14240             relocation += htab->stub_group[sec->id].toc_off;
14241           else
14242             unresolved_reloc = TRUE;
14243           goto dodyn;
14244
14245           /* TOC16 relocs.  We want the offset relative to the TOC base,
14246              which is the address of the start of the TOC plus 0x8000.
14247              The TOC consists of sections .got, .toc, .tocbss, and .plt,
14248              in this order.  */
14249         case R_PPC64_TOC16:
14250         case R_PPC64_TOC16_LO:
14251         case R_PPC64_TOC16_HI:
14252         case R_PPC64_TOC16_DS:
14253         case R_PPC64_TOC16_LO_DS:
14254         case R_PPC64_TOC16_HA:
14255           addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
14256           break;
14257
14258           /* Relocate against the beginning of the section.  */
14259         case R_PPC64_SECTOFF:
14260         case R_PPC64_SECTOFF_LO:
14261         case R_PPC64_SECTOFF_HI:
14262         case R_PPC64_SECTOFF_DS:
14263         case R_PPC64_SECTOFF_LO_DS:
14264         case R_PPC64_SECTOFF_HA:
14265           if (sec != NULL)
14266             addend -= sec->output_section->vma;
14267           break;
14268
14269         case R_PPC64_REL16:
14270         case R_PPC64_REL16_LO:
14271         case R_PPC64_REL16_HI:
14272         case R_PPC64_REL16_HA:
14273           break;
14274
14275         case R_PPC64_REL14:
14276         case R_PPC64_REL14_BRNTAKEN:
14277         case R_PPC64_REL14_BRTAKEN:
14278         case R_PPC64_REL24:
14279           break;
14280
14281         case R_PPC64_TPREL16:
14282         case R_PPC64_TPREL16_LO:
14283         case R_PPC64_TPREL16_HI:
14284         case R_PPC64_TPREL16_HA:
14285         case R_PPC64_TPREL16_DS:
14286         case R_PPC64_TPREL16_LO_DS:
14287         case R_PPC64_TPREL16_HIGH:
14288         case R_PPC64_TPREL16_HIGHA:
14289         case R_PPC64_TPREL16_HIGHER:
14290         case R_PPC64_TPREL16_HIGHERA:
14291         case R_PPC64_TPREL16_HIGHEST:
14292         case R_PPC64_TPREL16_HIGHESTA:
14293           if (h != NULL
14294               && h->elf.root.type == bfd_link_hash_undefweak
14295               && h->elf.dynindx == -1)
14296             {
14297               /* Make this relocation against an undefined weak symbol
14298                  resolve to zero.  This is really just a tweak, since
14299                  code using weak externs ought to check that they are
14300                  defined before using them.  */
14301               bfd_byte *p = contents + rel->r_offset - d_offset;
14302
14303               insn = bfd_get_32 (output_bfd, p);
14304               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14305               if (insn != 0)
14306                 bfd_put_32 (output_bfd, insn, p);
14307               break;
14308             }
14309           if (htab->elf.tls_sec != NULL)
14310             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14311           if (info->shared)
14312             /* The TPREL16 relocs shouldn't really be used in shared
14313                libs as they will result in DT_TEXTREL being set, but
14314                support them anyway.  */
14315             goto dodyn;
14316           break;
14317
14318         case R_PPC64_DTPREL16:
14319         case R_PPC64_DTPREL16_LO:
14320         case R_PPC64_DTPREL16_HI:
14321         case R_PPC64_DTPREL16_HA:
14322         case R_PPC64_DTPREL16_DS:
14323         case R_PPC64_DTPREL16_LO_DS:
14324         case R_PPC64_DTPREL16_HIGH:
14325         case R_PPC64_DTPREL16_HIGHA:
14326         case R_PPC64_DTPREL16_HIGHER:
14327         case R_PPC64_DTPREL16_HIGHERA:
14328         case R_PPC64_DTPREL16_HIGHEST:
14329         case R_PPC64_DTPREL16_HIGHESTA:
14330           if (htab->elf.tls_sec != NULL)
14331             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14332           break;
14333
14334         case R_PPC64_ADDR64_LOCAL:
14335           addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14336                                               ? h->elf.other
14337                                               : sym->st_other);
14338           break;
14339
14340         case R_PPC64_DTPMOD64:
14341           relocation = 1;
14342           addend = 0;
14343           goto dodyn;
14344
14345         case R_PPC64_TPREL64:
14346           if (htab->elf.tls_sec != NULL)
14347             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14348           goto dodyn;
14349
14350         case R_PPC64_DTPREL64:
14351           if (htab->elf.tls_sec != NULL)
14352             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14353           /* Fall thru */
14354
14355           /* Relocations that may need to be propagated if this is a
14356              dynamic object.  */
14357         case R_PPC64_REL30:
14358         case R_PPC64_REL32:
14359         case R_PPC64_REL64:
14360         case R_PPC64_ADDR14:
14361         case R_PPC64_ADDR14_BRNTAKEN:
14362         case R_PPC64_ADDR14_BRTAKEN:
14363         case R_PPC64_ADDR16:
14364         case R_PPC64_ADDR16_DS:
14365         case R_PPC64_ADDR16_HA:
14366         case R_PPC64_ADDR16_HI:
14367         case R_PPC64_ADDR16_HIGH:
14368         case R_PPC64_ADDR16_HIGHA:
14369         case R_PPC64_ADDR16_HIGHER:
14370         case R_PPC64_ADDR16_HIGHERA:
14371         case R_PPC64_ADDR16_HIGHEST:
14372         case R_PPC64_ADDR16_HIGHESTA:
14373         case R_PPC64_ADDR16_LO:
14374         case R_PPC64_ADDR16_LO_DS:
14375         case R_PPC64_ADDR24:
14376         case R_PPC64_ADDR32:
14377         case R_PPC64_ADDR64:
14378         case R_PPC64_UADDR16:
14379         case R_PPC64_UADDR32:
14380         case R_PPC64_UADDR64:
14381         dodyn:
14382           if ((input_section->flags & SEC_ALLOC) == 0)
14383             break;
14384
14385           if (NO_OPD_RELOCS && is_opd)
14386             break;
14387
14388           if ((info->shared
14389                && (h == NULL
14390                    || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14391                    || h->elf.root.type != bfd_link_hash_undefweak)
14392                && (must_be_dyn_reloc (info, r_type)
14393                    || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
14394               || (ELIMINATE_COPY_RELOCS
14395                   && !info->shared
14396                   && h != NULL
14397                   && h->elf.dynindx != -1
14398                   && !h->elf.non_got_ref
14399                   && !h->elf.def_regular)
14400               || (!info->shared
14401                   && (h != NULL
14402                       ? h->elf.type == STT_GNU_IFUNC
14403                       : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
14404             {
14405               bfd_boolean skip, relocate;
14406               asection *sreloc;
14407               bfd_vma out_off;
14408
14409               /* When generating a dynamic object, these relocations
14410                  are copied into the output file to be resolved at run
14411                  time.  */
14412
14413               skip = FALSE;
14414               relocate = FALSE;
14415
14416               out_off = _bfd_elf_section_offset (output_bfd, info,
14417                                                  input_section, rel->r_offset);
14418               if (out_off == (bfd_vma) -1)
14419                 skip = TRUE;
14420               else if (out_off == (bfd_vma) -2)
14421                 skip = TRUE, relocate = TRUE;
14422               out_off += (input_section->output_section->vma
14423                           + input_section->output_offset);
14424               outrel.r_offset = out_off;
14425               outrel.r_addend = rel->r_addend;
14426
14427               /* Optimize unaligned reloc use.  */
14428               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14429                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14430                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14431               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14432                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14433                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14434               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14435                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14436                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14437
14438               if (skip)
14439                 memset (&outrel, 0, sizeof outrel);
14440               else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14441                        && !is_opd
14442                        && r_type != R_PPC64_TOC)
14443                 {
14444                   BFD_ASSERT (h->elf.dynindx != -1);
14445                   outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14446                 }
14447               else
14448                 {
14449                   /* This symbol is local, or marked to become local,
14450                      or this is an opd section reloc which must point
14451                      at a local function.  */
14452                   outrel.r_addend += relocation;
14453                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14454                     {
14455                       if (is_opd && h != NULL)
14456                         {
14457                           /* Lie about opd entries.  This case occurs
14458                              when building shared libraries and we
14459                              reference a function in another shared
14460                              lib.  The same thing happens for a weak
14461                              definition in an application that's
14462                              overridden by a strong definition in a
14463                              shared lib.  (I believe this is a generic
14464                              bug in binutils handling of weak syms.)
14465                              In these cases we won't use the opd
14466                              entry in this lib.  */
14467                           unresolved_reloc = FALSE;
14468                         }
14469                       if (!is_opd
14470                           && r_type == R_PPC64_ADDR64
14471                           && (h != NULL
14472                               ? h->elf.type == STT_GNU_IFUNC
14473                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14474                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14475                       else
14476                         {
14477                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14478
14479                           /* We need to relocate .opd contents for ld.so.
14480                              Prelink also wants simple and consistent rules
14481                              for relocs.  This make all RELATIVE relocs have
14482                              *r_offset equal to r_addend.  */
14483                           relocate = TRUE;
14484                         }
14485                     }
14486                   else
14487                     {
14488                       long indx = 0;
14489
14490                       if (h != NULL
14491                           ? h->elf.type == STT_GNU_IFUNC
14492                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14493                         {
14494                           info->callbacks->einfo
14495                             (_("%P: %H: %s for indirect "
14496                                "function `%T' unsupported\n"),
14497                              input_bfd, input_section, rel->r_offset,
14498                              ppc64_elf_howto_table[r_type]->name,
14499                              sym_name);
14500                           ret = FALSE;
14501                         }
14502                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14503                         ;
14504                       else if (sec == NULL || sec->owner == NULL)
14505                         {
14506                           bfd_set_error (bfd_error_bad_value);
14507                           return FALSE;
14508                         }
14509                       else
14510                         {
14511                           asection *osec;
14512
14513                           osec = sec->output_section;
14514                           indx = elf_section_data (osec)->dynindx;
14515
14516                           if (indx == 0)
14517                             {
14518                               if ((osec->flags & SEC_READONLY) == 0
14519                                   && htab->elf.data_index_section != NULL)
14520                                 osec = htab->elf.data_index_section;
14521                               else
14522                                 osec = htab->elf.text_index_section;
14523                               indx = elf_section_data (osec)->dynindx;
14524                             }
14525                           BFD_ASSERT (indx != 0);
14526
14527                           /* We are turning this relocation into one
14528                              against a section symbol, so subtract out
14529                              the output section's address but not the
14530                              offset of the input section in the output
14531                              section.  */
14532                           outrel.r_addend -= osec->vma;
14533                         }
14534
14535                       outrel.r_info = ELF64_R_INFO (indx, r_type);
14536                     }
14537                 }
14538
14539               sreloc = elf_section_data (input_section)->sreloc;
14540               if (h != NULL
14541                   ? h->elf.type == STT_GNU_IFUNC
14542                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14543                 sreloc = htab->elf.irelplt;
14544               if (sreloc == NULL)
14545                 abort ();
14546
14547               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14548                   >= sreloc->size)
14549                 abort ();
14550               loc = sreloc->contents;
14551               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14552               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14553
14554               /* If this reloc is against an external symbol, it will
14555                  be computed at runtime, so there's no need to do
14556                  anything now.  However, for the sake of prelink ensure
14557                  that the section contents are a known value.  */
14558               if (! relocate)
14559                 {
14560                   unresolved_reloc = FALSE;
14561                   /* The value chosen here is quite arbitrary as ld.so
14562                      ignores section contents except for the special
14563                      case of .opd where the contents might be accessed
14564                      before relocation.  Choose zero, as that won't
14565                      cause reloc overflow.  */
14566                   relocation = 0;
14567                   addend = 0;
14568                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14569                      to improve backward compatibility with older
14570                      versions of ld.  */
14571                   if (r_type == R_PPC64_ADDR64)
14572                     addend = outrel.r_addend;
14573                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
14574                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
14575                     addend = (input_section->output_section->vma
14576                               + input_section->output_offset
14577                               + rel->r_offset);
14578                 }
14579             }
14580           break;
14581
14582         case R_PPC64_COPY:
14583         case R_PPC64_GLOB_DAT:
14584         case R_PPC64_JMP_SLOT:
14585         case R_PPC64_JMP_IREL:
14586         case R_PPC64_RELATIVE:
14587           /* We shouldn't ever see these dynamic relocs in relocatable
14588              files.  */
14589           /* Fall through.  */
14590
14591         case R_PPC64_PLTGOT16:
14592         case R_PPC64_PLTGOT16_DS:
14593         case R_PPC64_PLTGOT16_HA:
14594         case R_PPC64_PLTGOT16_HI:
14595         case R_PPC64_PLTGOT16_LO:
14596         case R_PPC64_PLTGOT16_LO_DS:
14597         case R_PPC64_PLTREL32:
14598         case R_PPC64_PLTREL64:
14599           /* These ones haven't been implemented yet.  */
14600
14601           info->callbacks->einfo
14602             (_("%P: %B: %s is not supported for `%T'\n"),
14603              input_bfd,
14604              ppc64_elf_howto_table[r_type]->name, sym_name);
14605
14606           bfd_set_error (bfd_error_invalid_operation);
14607           ret = FALSE;
14608           continue;
14609         }
14610
14611       /* Multi-instruction sequences that access the TOC can be
14612          optimized, eg. addis ra,r2,0; addi rb,ra,x;
14613          to             nop;           addi rb,r2,x;  */
14614       switch (r_type)
14615         {
14616         default:
14617           break;
14618
14619         case R_PPC64_GOT_TLSLD16_HI:
14620         case R_PPC64_GOT_TLSGD16_HI:
14621         case R_PPC64_GOT_TPREL16_HI:
14622         case R_PPC64_GOT_DTPREL16_HI:
14623         case R_PPC64_GOT16_HI:
14624         case R_PPC64_TOC16_HI:
14625           /* These relocs would only be useful if building up an
14626              offset to later add to r2, perhaps in an indexed
14627              addressing mode instruction.  Don't try to optimize.
14628              Unfortunately, the possibility of someone building up an
14629              offset like this or even with the HA relocs, means that
14630              we need to check the high insn when optimizing the low
14631              insn.  */
14632           break;
14633
14634         case R_PPC64_GOT_TLSLD16_HA:
14635         case R_PPC64_GOT_TLSGD16_HA:
14636         case R_PPC64_GOT_TPREL16_HA:
14637         case R_PPC64_GOT_DTPREL16_HA:
14638         case R_PPC64_GOT16_HA:
14639         case R_PPC64_TOC16_HA:
14640           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14641               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14642             {
14643               bfd_byte *p = contents + (rel->r_offset & ~3);
14644               bfd_put_32 (input_bfd, NOP, p);
14645             }
14646           break;
14647
14648         case R_PPC64_GOT_TLSLD16_LO:
14649         case R_PPC64_GOT_TLSGD16_LO:
14650         case R_PPC64_GOT_TPREL16_LO_DS:
14651         case R_PPC64_GOT_DTPREL16_LO_DS:
14652         case R_PPC64_GOT16_LO:
14653         case R_PPC64_GOT16_LO_DS:
14654         case R_PPC64_TOC16_LO:
14655         case R_PPC64_TOC16_LO_DS:
14656           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14657               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14658             {
14659               bfd_byte *p = contents + (rel->r_offset & ~3);
14660               insn = bfd_get_32 (input_bfd, p);
14661               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14662                 {
14663                   /* Transform addic to addi when we change reg.  */
14664                   insn &= ~((0x3f << 26) | (0x1f << 16));
14665                   insn |= (14u << 26) | (2 << 16);
14666                 }
14667               else
14668                 {
14669                   insn &= ~(0x1f << 16);
14670                   insn |= 2 << 16;
14671                 }
14672               bfd_put_32 (input_bfd, insn, p);
14673             }
14674           break;
14675         }
14676
14677       /* Do any further special processing.  */
14678       howto = ppc64_elf_howto_table[(int) r_type];
14679       switch (r_type)
14680         {
14681         default:
14682           break;
14683
14684         case R_PPC64_REL16_HA:
14685         case R_PPC64_ADDR16_HA:
14686         case R_PPC64_ADDR16_HIGHA:
14687         case R_PPC64_ADDR16_HIGHERA:
14688         case R_PPC64_ADDR16_HIGHESTA:
14689         case R_PPC64_TOC16_HA:
14690         case R_PPC64_SECTOFF_HA:
14691         case R_PPC64_TPREL16_HA:
14692         case R_PPC64_TPREL16_HIGHA:
14693         case R_PPC64_TPREL16_HIGHERA:
14694         case R_PPC64_TPREL16_HIGHESTA:
14695         case R_PPC64_DTPREL16_HA:
14696         case R_PPC64_DTPREL16_HIGHA:
14697         case R_PPC64_DTPREL16_HIGHERA:
14698         case R_PPC64_DTPREL16_HIGHESTA:
14699           /* It's just possible that this symbol is a weak symbol
14700              that's not actually defined anywhere. In that case,
14701              'sec' would be NULL, and we should leave the symbol
14702              alone (it will be set to zero elsewhere in the link).  */
14703           if (sec == NULL)
14704             break;
14705           /* Fall thru */
14706
14707         case R_PPC64_GOT16_HA:
14708         case R_PPC64_PLTGOT16_HA:
14709         case R_PPC64_PLT16_HA:
14710         case R_PPC64_GOT_TLSGD16_HA:
14711         case R_PPC64_GOT_TLSLD16_HA:
14712         case R_PPC64_GOT_TPREL16_HA:
14713         case R_PPC64_GOT_DTPREL16_HA:
14714           /* Add 0x10000 if sign bit in 0:15 is set.
14715              Bits 0:15 are not used.  */
14716           addend += 0x8000;
14717           break;
14718
14719         case R_PPC64_ADDR16_DS:
14720         case R_PPC64_ADDR16_LO_DS:
14721         case R_PPC64_GOT16_DS:
14722         case R_PPC64_GOT16_LO_DS:
14723         case R_PPC64_PLT16_LO_DS:
14724         case R_PPC64_SECTOFF_DS:
14725         case R_PPC64_SECTOFF_LO_DS:
14726         case R_PPC64_TOC16_DS:
14727         case R_PPC64_TOC16_LO_DS:
14728         case R_PPC64_PLTGOT16_DS:
14729         case R_PPC64_PLTGOT16_LO_DS:
14730         case R_PPC64_GOT_TPREL16_DS:
14731         case R_PPC64_GOT_TPREL16_LO_DS:
14732         case R_PPC64_GOT_DTPREL16_DS:
14733         case R_PPC64_GOT_DTPREL16_LO_DS:
14734         case R_PPC64_TPREL16_DS:
14735         case R_PPC64_TPREL16_LO_DS:
14736         case R_PPC64_DTPREL16_DS:
14737         case R_PPC64_DTPREL16_LO_DS:
14738           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14739           mask = 3;
14740           /* If this reloc is against an lq insn, then the value must be
14741              a multiple of 16.  This is somewhat of a hack, but the
14742              "correct" way to do this by defining _DQ forms of all the
14743              _DS relocs bloats all reloc switches in this file.  It
14744              doesn't seem to make much sense to use any of these relocs
14745              in data, so testing the insn should be safe.  */
14746           if ((insn & (0x3f << 26)) == (56u << 26))
14747             mask = 15;
14748           if (((relocation + addend) & mask) != 0)
14749             {
14750               info->callbacks->einfo
14751                 (_("%P: %H: error: %s not a multiple of %u\n"),
14752                  input_bfd, input_section, rel->r_offset,
14753                  howto->name,
14754                  mask + 1);
14755               bfd_set_error (bfd_error_bad_value);
14756               ret = FALSE;
14757               continue;
14758             }
14759           break;
14760         }
14761
14762       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
14763          because such sections are not SEC_ALLOC and thus ld.so will
14764          not process them.  */
14765       if (unresolved_reloc
14766           && !((input_section->flags & SEC_DEBUGGING) != 0
14767                && h->elf.def_dynamic)
14768           && _bfd_elf_section_offset (output_bfd, info, input_section,
14769                                       rel->r_offset) != (bfd_vma) -1)
14770         {
14771           info->callbacks->einfo
14772             (_("%P: %H: unresolvable %s against `%T'\n"),
14773              input_bfd, input_section, rel->r_offset,
14774              howto->name,
14775              h->elf.root.root.string);
14776           ret = FALSE;
14777         }
14778
14779       /* 16-bit fields in insns mostly have signed values, but a
14780          few insns have 16-bit unsigned values.  Really, we should
14781          have different reloc types.  */
14782       if (howto->complain_on_overflow != complain_overflow_dont
14783           && howto->dst_mask == 0xffff
14784           && (input_section->flags & SEC_CODE) != 0)
14785         {
14786           enum complain_overflow complain = complain_overflow_signed;
14787
14788           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14789           if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
14790             complain = complain_overflow_bitfield;
14791           else if (howto->rightshift == 0
14792                    ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
14793                       || (insn & (0x3f << 26)) == 24u << 26 /* ori */
14794                       || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
14795                    : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
14796                       || (insn & (0x3f << 26)) == 25u << 26 /* oris */
14797                       || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
14798             complain = complain_overflow_unsigned;
14799           if (howto->complain_on_overflow != complain)
14800             {
14801               alt_howto = *howto;
14802               alt_howto.complain_on_overflow = complain;
14803               howto = &alt_howto;
14804             }
14805         }
14806
14807       r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
14808                                     rel->r_offset, relocation, addend);
14809
14810       if (r != bfd_reloc_ok)
14811         {
14812           char *more_info = NULL;
14813           const char *reloc_name = howto->name;
14814
14815           if (reloc_dest != DEST_NORMAL)
14816             {
14817               more_info = bfd_malloc (strlen (reloc_name) + 8);
14818               if (more_info != NULL)
14819                 {
14820                   strcpy (more_info, reloc_name);
14821                   strcat (more_info, (reloc_dest == DEST_OPD
14822                                       ? " (OPD)" : " (stub)"));
14823                   reloc_name = more_info;
14824                 }
14825             }
14826
14827           if (r == bfd_reloc_overflow)
14828             {
14829               if (warned)
14830                 continue;
14831               if (h != NULL
14832                   && h->elf.root.type == bfd_link_hash_undefweak
14833                   && howto->pc_relative)
14834                 {
14835                   /* Assume this is a call protected by other code that
14836                      detects the symbol is undefined.  If this is the case,
14837                      we can safely ignore the overflow.  If not, the
14838                      program is hosed anyway, and a little warning isn't
14839                      going to help.  */
14840
14841                   continue;
14842                 }
14843
14844               if (!((*info->callbacks->reloc_overflow)
14845                     (info, &h->elf.root, sym_name,
14846                      reloc_name, orig_rel.r_addend,
14847                      input_bfd, input_section, rel->r_offset)))
14848                 return FALSE;
14849             }
14850           else
14851             {
14852               info->callbacks->einfo
14853                 (_("%P: %H: %s against `%T': error %d\n"),
14854                  input_bfd, input_section, rel->r_offset,
14855                  reloc_name, sym_name, (int) r);
14856               ret = FALSE;
14857             }
14858           if (more_info != NULL)
14859             free (more_info);
14860         }
14861     }
14862
14863   /* If we're emitting relocations, then shortly after this function
14864      returns, reloc offsets and addends for this section will be
14865      adjusted.  Worse, reloc symbol indices will be for the output
14866      file rather than the input.  Save a copy of the relocs for
14867      opd_entry_value.  */
14868   if (is_opd && (info->emitrelocations || info->relocatable))
14869     {
14870       bfd_size_type amt;
14871       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
14872       rel = bfd_alloc (input_bfd, amt);
14873       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
14874       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
14875       if (rel == NULL)
14876         return FALSE;
14877       memcpy (rel, relocs, amt);
14878     }
14879   return ret;
14880 }
14881
14882 /* Adjust the value of any local symbols in opd sections.  */
14883
14884 static int
14885 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
14886                               const char *name ATTRIBUTE_UNUSED,
14887                               Elf_Internal_Sym *elfsym,
14888                               asection *input_sec,
14889                               struct elf_link_hash_entry *h)
14890 {
14891   struct _opd_sec_data *opd;
14892   long adjust;
14893   bfd_vma value;
14894
14895   if (h != NULL)
14896     return 1;
14897
14898   opd = get_opd_info (input_sec);
14899   if (opd == NULL || opd->adjust == NULL)
14900     return 1;
14901
14902   value = elfsym->st_value - input_sec->output_offset;
14903   if (!info->relocatable)
14904     value -= input_sec->output_section->vma;
14905
14906   adjust = opd->adjust[OPD_NDX (value)];
14907   if (adjust == -1)
14908     return 2;
14909
14910   elfsym->st_value += adjust;
14911   return 1;
14912 }
14913
14914 /* Finish up dynamic symbol handling.  We set the contents of various
14915    dynamic sections here.  */
14916
14917 static bfd_boolean
14918 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
14919                                  struct bfd_link_info *info,
14920                                  struct elf_link_hash_entry *h,
14921                                  Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
14922 {
14923   struct ppc_link_hash_table *htab;
14924   struct plt_entry *ent;
14925   Elf_Internal_Rela rela;
14926   bfd_byte *loc;
14927
14928   htab = ppc_hash_table (info);
14929   if (htab == NULL)
14930     return FALSE;
14931
14932   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14933     if (ent->plt.offset != (bfd_vma) -1)
14934       {
14935         /* This symbol has an entry in the procedure linkage
14936            table.  Set it up.  */
14937         if (!htab->elf.dynamic_sections_created
14938             || h->dynindx == -1)
14939           {
14940             BFD_ASSERT (h->type == STT_GNU_IFUNC
14941                         && h->def_regular
14942                         && (h->root.type == bfd_link_hash_defined
14943                             || h->root.type == bfd_link_hash_defweak));
14944             rela.r_offset = (htab->elf.iplt->output_section->vma
14945                              + htab->elf.iplt->output_offset
14946                              + ent->plt.offset);
14947             if (htab->opd_abi)
14948               rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14949             else
14950               rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14951             rela.r_addend = (h->root.u.def.value
14952                              + h->root.u.def.section->output_offset
14953                              + h->root.u.def.section->output_section->vma
14954                              + ent->addend);
14955             loc = (htab->elf.irelplt->contents
14956                    + (htab->elf.irelplt->reloc_count++
14957                       * sizeof (Elf64_External_Rela)));
14958           }
14959         else
14960           {
14961             rela.r_offset = (htab->elf.splt->output_section->vma
14962                              + htab->elf.splt->output_offset
14963                              + ent->plt.offset);
14964             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14965             rela.r_addend = ent->addend;
14966             loc = (htab->elf.srelplt->contents
14967                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14968                       / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14969           }
14970         bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
14971
14972         if (!htab->opd_abi)
14973           {
14974             if (!h->def_regular)
14975               {
14976                 /* Mark the symbol as undefined, rather than as
14977                    defined in glink.  Leave the value if there were
14978                    any relocations where pointer equality matters
14979                    (this is a clue for the dynamic linker, to make
14980                    function pointer comparisons work between an
14981                    application and shared library), otherwise set it
14982                    to zero.  */
14983                 sym->st_shndx = SHN_UNDEF;
14984                 if (!h->pointer_equality_needed)
14985                   sym->st_value = 0;
14986                 else if (!h->ref_regular_nonweak)
14987                   {
14988                     /* This breaks function pointer comparisons, but
14989                        that is better than breaking tests for a NULL
14990                        function pointer.  */
14991                     sym->st_value = 0;
14992                   }
14993               }
14994           }
14995       }
14996
14997   if (h->needs_copy)
14998     {
14999       /* This symbol needs a copy reloc.  Set it up.  */
15000
15001       if (h->dynindx == -1
15002           || (h->root.type != bfd_link_hash_defined
15003               && h->root.type != bfd_link_hash_defweak)
15004           || htab->relbss == NULL)
15005         abort ();
15006
15007       rela.r_offset = (h->root.u.def.value
15008                        + h->root.u.def.section->output_section->vma
15009                        + h->root.u.def.section->output_offset);
15010       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15011       rela.r_addend = 0;
15012       loc = htab->relbss->contents;
15013       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
15014       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15015     }
15016
15017   return TRUE;
15018 }
15019
15020 /* Used to decide how to sort relocs in an optimal manner for the
15021    dynamic linker, before writing them out.  */
15022
15023 static enum elf_reloc_type_class
15024 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15025                             const asection *rel_sec,
15026                             const Elf_Internal_Rela *rela)
15027 {
15028   enum elf_ppc64_reloc_type r_type;
15029   struct ppc_link_hash_table *htab = ppc_hash_table (info);
15030
15031   if (rel_sec == htab->elf.irelplt)
15032     return reloc_class_ifunc;
15033
15034   r_type = ELF64_R_TYPE (rela->r_info);
15035   switch (r_type)
15036     {
15037     case R_PPC64_RELATIVE:
15038       return reloc_class_relative;
15039     case R_PPC64_JMP_SLOT:
15040       return reloc_class_plt;
15041     case R_PPC64_COPY:
15042       return reloc_class_copy;
15043     default:
15044       return reloc_class_normal;
15045     }
15046 }
15047
15048 /* Finish up the dynamic sections.  */
15049
15050 static bfd_boolean
15051 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15052                                    struct bfd_link_info *info)
15053 {
15054   struct ppc_link_hash_table *htab;
15055   bfd *dynobj;
15056   asection *sdyn;
15057
15058   htab = ppc_hash_table (info);
15059   if (htab == NULL)
15060     return FALSE;
15061
15062   dynobj = htab->elf.dynobj;
15063   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15064
15065   if (htab->elf.dynamic_sections_created)
15066     {
15067       Elf64_External_Dyn *dyncon, *dynconend;
15068
15069       if (sdyn == NULL || htab->elf.sgot == NULL)
15070         abort ();
15071
15072       dyncon = (Elf64_External_Dyn *) sdyn->contents;
15073       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15074       for (; dyncon < dynconend; dyncon++)
15075         {
15076           Elf_Internal_Dyn dyn;
15077           asection *s;
15078
15079           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15080
15081           switch (dyn.d_tag)
15082             {
15083             default:
15084               continue;
15085
15086             case DT_PPC64_GLINK:
15087               s = htab->glink;
15088               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15089               /* We stupidly defined DT_PPC64_GLINK to be the start
15090                  of glink rather than the first entry point, which is
15091                  what ld.so needs, and now have a bigger stub to
15092                  support automatic multiple TOCs.  */
15093               dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
15094               break;
15095
15096             case DT_PPC64_OPD:
15097               s = bfd_get_section_by_name (output_bfd, ".opd");
15098               if (s == NULL)
15099                 continue;
15100               dyn.d_un.d_ptr = s->vma;
15101               break;
15102
15103             case DT_PPC64_OPT:
15104               if (htab->do_multi_toc && htab->multi_toc_needed)
15105                 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15106               break;
15107
15108             case DT_PPC64_OPDSZ:
15109               s = bfd_get_section_by_name (output_bfd, ".opd");
15110               if (s == NULL)
15111                 continue;
15112               dyn.d_un.d_val = s->size;
15113               break;
15114
15115             case DT_PLTGOT:
15116               s = htab->elf.splt;
15117               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15118               break;
15119
15120             case DT_JMPREL:
15121               s = htab->elf.srelplt;
15122               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15123               break;
15124
15125             case DT_PLTRELSZ:
15126               dyn.d_un.d_val = htab->elf.srelplt->size;
15127               break;
15128
15129             case DT_RELASZ:
15130               /* Don't count procedure linkage table relocs in the
15131                  overall reloc count.  */
15132               s = htab->elf.srelplt;
15133               if (s == NULL)
15134                 continue;
15135               dyn.d_un.d_val -= s->size;
15136               break;
15137
15138             case DT_RELA:
15139               /* We may not be using the standard ELF linker script.
15140                  If .rela.plt is the first .rela section, we adjust
15141                  DT_RELA to not include it.  */
15142               s = htab->elf.srelplt;
15143               if (s == NULL)
15144                 continue;
15145               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
15146                 continue;
15147               dyn.d_un.d_ptr += s->size;
15148               break;
15149             }
15150
15151           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15152         }
15153     }
15154
15155   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0)
15156     {
15157       /* Fill in the first entry in the global offset table.
15158          We use it to hold the link-time TOCbase.  */
15159       bfd_put_64 (output_bfd,
15160                   elf_gp (output_bfd) + TOC_BASE_OFF,
15161                   htab->elf.sgot->contents);
15162
15163       /* Set .got entry size.  */
15164       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
15165     }
15166
15167   if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
15168     {
15169       /* Set .plt entry size.  */
15170       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15171         = PLT_ENTRY_SIZE (htab);
15172     }
15173
15174   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15175      brlt ourselves if emitrelocations.  */
15176   if (htab->brlt != NULL
15177       && htab->brlt->reloc_count != 0
15178       && !_bfd_elf_link_output_relocs (output_bfd,
15179                                        htab->brlt,
15180                                        elf_section_data (htab->brlt)->rela.hdr,
15181                                        elf_section_data (htab->brlt)->relocs,
15182                                        NULL))
15183     return FALSE;
15184
15185   if (htab->glink != NULL
15186       && htab->glink->reloc_count != 0
15187       && !_bfd_elf_link_output_relocs (output_bfd,
15188                                        htab->glink,
15189                                        elf_section_data (htab->glink)->rela.hdr,
15190                                        elf_section_data (htab->glink)->relocs,
15191                                        NULL))
15192     return FALSE;
15193
15194   if (htab->glink_eh_frame != NULL
15195       && htab->glink_eh_frame->size != 0)
15196     {
15197       bfd_vma val;
15198       bfd_byte *p;
15199       asection *stub_sec;
15200
15201       p = htab->glink_eh_frame->contents + sizeof (glink_eh_frame_cie);
15202       for (stub_sec = htab->params->stub_bfd->sections;
15203            stub_sec != NULL;
15204            stub_sec = stub_sec->next)
15205         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
15206           {
15207             /* FDE length.  */
15208             p += 4;
15209             /* CIE pointer.  */
15210             p += 4;
15211             /* Offset to stub section.  */
15212             val = (stub_sec->output_section->vma
15213                    + stub_sec->output_offset);
15214             val -= (htab->glink_eh_frame->output_section->vma
15215                     + htab->glink_eh_frame->output_offset
15216                     + (p - htab->glink_eh_frame->contents));
15217             if (val + 0x80000000 > 0xffffffff)
15218               {
15219                 info->callbacks->einfo
15220                   (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15221                    stub_sec->name);
15222                 return FALSE;
15223               }
15224             bfd_put_32 (dynobj, val, p);
15225             p += 4;
15226             /* stub section size.  */
15227             p += 4;
15228             /* Augmentation.  */
15229             p += 1;
15230             /* Pad.  */
15231             p += 7;
15232           }
15233       if (htab->glink != NULL && htab->glink->size != 0)
15234         {
15235           /* FDE length.  */
15236           p += 4;
15237           /* CIE pointer.  */
15238           p += 4;
15239           /* Offset to .glink.  */
15240           val = (htab->glink->output_section->vma
15241                  + htab->glink->output_offset
15242                  + 8);
15243           val -= (htab->glink_eh_frame->output_section->vma
15244                   + htab->glink_eh_frame->output_offset
15245                   + (p - htab->glink_eh_frame->contents));
15246           if (val + 0x80000000 > 0xffffffff)
15247             {
15248               info->callbacks->einfo
15249                 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15250                  htab->glink->name);
15251               return FALSE;
15252             }
15253           bfd_put_32 (dynobj, val, p);
15254           p += 4;
15255           /* .glink size.  */
15256           p += 4;
15257           /* Augmentation.  */
15258           p += 1;
15259           /* Ops.  */
15260           p += 7;
15261         }
15262
15263       if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15264           && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15265                                                htab->glink_eh_frame,
15266                                                htab->glink_eh_frame->contents))
15267         return FALSE;
15268     }
15269
15270   /* We need to handle writing out multiple GOT sections ourselves,
15271      since we didn't add them to DYNOBJ.  We know dynobj is the first
15272      bfd.  */
15273   while ((dynobj = dynobj->link.next) != NULL)
15274     {
15275       asection *s;
15276
15277       if (!is_ppc64_elf (dynobj))
15278         continue;
15279
15280       s = ppc64_elf_tdata (dynobj)->got;
15281       if (s != NULL
15282           && s->size != 0
15283           && s->output_section != bfd_abs_section_ptr
15284           && !bfd_set_section_contents (output_bfd, s->output_section,
15285                                         s->contents, s->output_offset,
15286                                         s->size))
15287         return FALSE;
15288       s = ppc64_elf_tdata (dynobj)->relgot;
15289       if (s != NULL
15290           && s->size != 0
15291           && s->output_section != bfd_abs_section_ptr
15292           && !bfd_set_section_contents (output_bfd, s->output_section,
15293                                         s->contents, s->output_offset,
15294                                         s->size))
15295         return FALSE;
15296     }
15297
15298   return TRUE;
15299 }
15300
15301 #include "elf64-target.h"
15302
15303 /* FreeBSD support */
15304
15305 #undef  TARGET_LITTLE_SYM
15306 #undef  TARGET_LITTLE_NAME
15307
15308 #undef  TARGET_BIG_SYM
15309 #define TARGET_BIG_SYM  powerpc_elf64_fbsd_vec
15310 #undef  TARGET_BIG_NAME
15311 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15312
15313 #undef  ELF_OSABI
15314 #define ELF_OSABI       ELFOSABI_FREEBSD
15315
15316 #undef  elf64_bed
15317 #define elf64_bed       elf64_powerpc_fbsd_bed
15318
15319 #include "elf64-target.h"
15320