ELFv2 stub, plt and glink changes
[external/binutils.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3    2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4    Written by Linus Nordberg, Swox AB <info@swox.com>,
5    based on elf32-ppc.c by Ian Lance Taylor.
6    Largely rewritten by Alan Modra.
7
8    This file is part of BFD, the Binary File Descriptor library.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License along
21    with this program; if not, write to the Free Software Foundation, Inc.,
22    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
23
24
25 /* The 64-bit PowerPC ELF ABI may be found at
26    http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
27    http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
28
29 #include "sysdep.h"
30 #include <stdarg.h>
31 #include "bfd.h"
32 #include "bfdlink.h"
33 #include "libbfd.h"
34 #include "elf-bfd.h"
35 #include "elf/ppc64.h"
36 #include "elf64-ppc.h"
37 #include "dwarf2.h"
38
39 static bfd_reloc_status_type ppc64_elf_ha_reloc
40   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
41 static bfd_reloc_status_type ppc64_elf_branch_reloc
42   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
44   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
45 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
46   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
47 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
48   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
49 static bfd_reloc_status_type ppc64_elf_toc_reloc
50   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
51 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
52   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
53 static bfd_reloc_status_type ppc64_elf_toc64_reloc
54   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
55 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
56   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
57 static bfd_vma opd_entry_value
58   (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
59
60 #define TARGET_LITTLE_SYM       bfd_elf64_powerpcle_vec
61 #define TARGET_LITTLE_NAME      "elf64-powerpcle"
62 #define TARGET_BIG_SYM          bfd_elf64_powerpc_vec
63 #define TARGET_BIG_NAME         "elf64-powerpc"
64 #define ELF_ARCH                bfd_arch_powerpc
65 #define ELF_TARGET_ID           PPC64_ELF_DATA
66 #define ELF_MACHINE_CODE        EM_PPC64
67 #define ELF_MAXPAGESIZE         0x10000
68 #define ELF_COMMONPAGESIZE      0x1000
69 #define elf_info_to_howto       ppc64_elf_info_to_howto
70
71 #define elf_backend_want_got_sym 0
72 #define elf_backend_want_plt_sym 0
73 #define elf_backend_plt_alignment 3
74 #define elf_backend_plt_not_loaded 1
75 #define elf_backend_got_header_size 8
76 #define elf_backend_can_gc_sections 1
77 #define elf_backend_can_refcount 1
78 #define elf_backend_rela_normal 1
79 #define elf_backend_default_execstack 0
80
81 #define bfd_elf64_mkobject                    ppc64_elf_mkobject
82 #define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
83 #define bfd_elf64_bfd_reloc_name_lookup       ppc64_elf_reloc_name_lookup
84 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
85 #define bfd_elf64_bfd_print_private_bfd_data  ppc64_elf_print_private_bfd_data
86 #define bfd_elf64_new_section_hook            ppc64_elf_new_section_hook
87 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
88 #define bfd_elf64_bfd_link_hash_table_free    ppc64_elf_link_hash_table_free
89 #define bfd_elf64_get_synthetic_symtab        ppc64_elf_get_synthetic_symtab
90 #define bfd_elf64_bfd_link_just_syms          ppc64_elf_link_just_syms
91
92 #define elf_backend_object_p                  ppc64_elf_object_p
93 #define elf_backend_grok_prstatus             ppc64_elf_grok_prstatus
94 #define elf_backend_grok_psinfo               ppc64_elf_grok_psinfo
95 #define elf_backend_write_core_note           ppc64_elf_write_core_note
96 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
97 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
98 #define elf_backend_add_symbol_hook           ppc64_elf_add_symbol_hook
99 #define elf_backend_check_directives          ppc64_elf_process_dot_syms
100 #define elf_backend_notice_as_needed          ppc64_elf_notice_as_needed
101 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
102 #define elf_backend_check_relocs              ppc64_elf_check_relocs
103 #define elf_backend_gc_keep                   ppc64_elf_gc_keep
104 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
105 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
106 #define elf_backend_gc_sweep_hook             ppc64_elf_gc_sweep_hook
107 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
108 #define elf_backend_hide_symbol               ppc64_elf_hide_symbol
109 #define elf_backend_maybe_function_sym        ppc64_elf_maybe_function_sym
110 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
111 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
112 #define elf_backend_init_index_section        _bfd_elf_init_2_index_sections
113 #define elf_backend_action_discarded          ppc64_elf_action_discarded
114 #define elf_backend_relocate_section          ppc64_elf_relocate_section
115 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
116 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
117 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
118 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
119 #define elf_backend_special_sections          ppc64_elf_special_sections
120 #define elf_backend_post_process_headers      _bfd_elf_set_osabi
121 #define elf_backend_merge_symbol_attribute    ppc64_elf_merge_symbol_attribute
122
123 /* The name of the dynamic interpreter.  This is put in the .interp
124    section.  */
125 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
126
127 /* The size in bytes of an entry in the procedure linkage table.  */
128 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
129
130 /* The initial size of the plt reserved for the dynamic linker.  */
131 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
132
133 /* TOC base pointers offset from start of TOC.  */
134 #define TOC_BASE_OFF    0x8000
135
136 /* Offset of tp and dtp pointers from start of TLS block.  */
137 #define TP_OFFSET       0x7000
138 #define DTP_OFFSET      0x8000
139
140 /* .plt call stub instructions.  The normal stub is like this, but
141    sometimes the .plt entry crosses a 64k boundary and we need to
142    insert an addi to adjust r11.  */
143 #define STD_R2_40R1     0xf8410028      /* std   %r2,40(%r1)         */
144 #define ADDIS_R11_R2    0x3d620000      /* addis %r11,%r2,xxx@ha     */
145 #define LD_R12_0R11     0xe98b0000      /* ld    %r12,xxx+0@l(%r11)  */
146 #define MTCTR_R12       0x7d8903a6      /* mtctr %r12                */
147 #define LD_R2_0R11      0xe84b0000      /* ld    %r2,xxx+8@l(%r11)   */
148 #define LD_R11_0R11     0xe96b0000      /* ld    %r11,xxx+16@l(%r11) */
149 #define BCTR            0x4e800420      /* bctr                      */
150
151 #define ADDI_R11_R11    0x396b0000      /* addi %r11,%r11,off@l  */
152 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
153 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
154
155 #define XOR_R2_R12_R12  0x7d826278      /* xor   %r2,%r12,%r12   */
156 #define ADD_R11_R11_R2  0x7d6b1214      /* add   %r11,%r11,%r2   */
157 #define XOR_R11_R12_R12 0x7d8b6278      /* xor   %r11,%r12,%r12  */
158 #define ADD_R2_R2_R11   0x7c425a14      /* add   %r2,%r2,%r11    */
159 #define CMPLDI_R2_0     0x28220000      /* cmpldi %r2,0          */
160 #define BNECTR          0x4ca20420      /* bnectr+               */
161 #define BNECTR_P4       0x4ce20420      /* bnectr+               */
162
163 #define LD_R12_0R2      0xe9820000      /* ld    %r12,xxx+0(%r2) */
164 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
165 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
166
167 #define LD_R2_40R1      0xe8410028      /* ld    %r2,40(%r1)     */
168
169 /* glink call stub instructions.  We enter with the index in R0.  */
170 #define GLINK_CALL_STUB_SIZE (16*4)
171                                         /* 0:                           */
172                                         /*  .quad plt0-1f               */
173                                         /* __glink:                     */
174 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
175 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
176                                         /* 1:                           */
177 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
178                                         /*  ld %2,(0b-1b)(%11)          */
179 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
180 #define ADD_R11_R2_R11  0x7d625a14      /*  add %11,%2,%11              */
181                                         /*  ld %12,0(%11)               */
182                                         /*  ld %2,8(%11)                */
183                                         /*  mtctr %12                   */
184                                         /*  ld %11,16(%11)              */
185                                         /*  bctr                        */
186 #define MFLR_R0         0x7c0802a6      /*  mflr %r0                    */
187 #define MTLR_R0         0x7c0803a6      /*  mtlr %r0                    */
188 #define SUB_R12_R12_R11 0x7d8b6050      /*  subf %r12,%r11,%r12         */
189 #define ADDI_R0_R12     0x380c0000      /*  addi %r0,%r12,0             */
190 #define SRDI_R0_R0_2    0x7800f082      /*  rldicl %r0,%r0,62,2         */
191
192 /* Pad with this.  */
193 #define NOP             0x60000000
194
195 /* Some other nops.  */
196 #define CROR_151515     0x4def7b82
197 #define CROR_313131     0x4ffffb82
198
199 /* .glink entries for the first 32k functions are two instructions.  */
200 #define LI_R0_0         0x38000000      /* li    %r0,0          */
201 #define B_DOT           0x48000000      /* b     .              */
202
203 /* After that, we need two instructions to load the index, followed by
204    a branch.  */
205 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
206 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
207
208 /* Instructions used by the save and restore reg functions.  */
209 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
210 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
211 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
212 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
213 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
214 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
215 #define LI_R12_0        0x39800000      /* li    %r12,0         */
216 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
217 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
218 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
219 #define BLR             0x4e800020      /* blr                  */
220
221 /* Since .opd is an array of descriptors and each entry will end up
222    with identical R_PPC64_RELATIVE relocs, there is really no need to
223    propagate .opd relocs;  The dynamic linker should be taught to
224    relocate .opd without reloc entries.  */
225 #ifndef NO_OPD_RELOCS
226 #define NO_OPD_RELOCS 0
227 #endif
228 \f
229 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
230
231 /* Relocation HOWTO's.  */
232 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
233
234 static reloc_howto_type ppc64_elf_howto_raw[] = {
235   /* This reloc does nothing.  */
236   HOWTO (R_PPC64_NONE,          /* type */
237          0,                     /* rightshift */
238          2,                     /* size (0 = byte, 1 = short, 2 = long) */
239          32,                    /* bitsize */
240          FALSE,                 /* pc_relative */
241          0,                     /* bitpos */
242          complain_overflow_dont, /* complain_on_overflow */
243          bfd_elf_generic_reloc, /* special_function */
244          "R_PPC64_NONE",        /* name */
245          FALSE,                 /* partial_inplace */
246          0,                     /* src_mask */
247          0,                     /* dst_mask */
248          FALSE),                /* pcrel_offset */
249
250   /* A standard 32 bit relocation.  */
251   HOWTO (R_PPC64_ADDR32,        /* type */
252          0,                     /* rightshift */
253          2,                     /* size (0 = byte, 1 = short, 2 = long) */
254          32,                    /* bitsize */
255          FALSE,                 /* pc_relative */
256          0,                     /* bitpos */
257          complain_overflow_bitfield, /* complain_on_overflow */
258          bfd_elf_generic_reloc, /* special_function */
259          "R_PPC64_ADDR32",      /* name */
260          FALSE,                 /* partial_inplace */
261          0,                     /* src_mask */
262          0xffffffff,            /* dst_mask */
263          FALSE),                /* pcrel_offset */
264
265   /* An absolute 26 bit branch; the lower two bits must be zero.
266      FIXME: we don't check that, we just clear them.  */
267   HOWTO (R_PPC64_ADDR24,        /* type */
268          0,                     /* rightshift */
269          2,                     /* size (0 = byte, 1 = short, 2 = long) */
270          26,                    /* bitsize */
271          FALSE,                 /* pc_relative */
272          0,                     /* bitpos */
273          complain_overflow_bitfield, /* complain_on_overflow */
274          bfd_elf_generic_reloc, /* special_function */
275          "R_PPC64_ADDR24",      /* name */
276          FALSE,                 /* partial_inplace */
277          0,                     /* src_mask */
278          0x03fffffc,            /* dst_mask */
279          FALSE),                /* pcrel_offset */
280
281   /* A standard 16 bit relocation.  */
282   HOWTO (R_PPC64_ADDR16,        /* type */
283          0,                     /* rightshift */
284          1,                     /* size (0 = byte, 1 = short, 2 = long) */
285          16,                    /* bitsize */
286          FALSE,                 /* pc_relative */
287          0,                     /* bitpos */
288          complain_overflow_bitfield, /* complain_on_overflow */
289          bfd_elf_generic_reloc, /* special_function */
290          "R_PPC64_ADDR16",      /* name */
291          FALSE,                 /* partial_inplace */
292          0,                     /* src_mask */
293          0xffff,                /* dst_mask */
294          FALSE),                /* pcrel_offset */
295
296   /* A 16 bit relocation without overflow.  */
297   HOWTO (R_PPC64_ADDR16_LO,     /* type */
298          0,                     /* rightshift */
299          1,                     /* size (0 = byte, 1 = short, 2 = long) */
300          16,                    /* bitsize */
301          FALSE,                 /* pc_relative */
302          0,                     /* bitpos */
303          complain_overflow_dont,/* complain_on_overflow */
304          bfd_elf_generic_reloc, /* special_function */
305          "R_PPC64_ADDR16_LO",   /* name */
306          FALSE,                 /* partial_inplace */
307          0,                     /* src_mask */
308          0xffff,                /* dst_mask */
309          FALSE),                /* pcrel_offset */
310
311   /* Bits 16-31 of an address.  */
312   HOWTO (R_PPC64_ADDR16_HI,     /* type */
313          16,                    /* rightshift */
314          1,                     /* size (0 = byte, 1 = short, 2 = long) */
315          16,                    /* bitsize */
316          FALSE,                 /* pc_relative */
317          0,                     /* bitpos */
318          complain_overflow_signed, /* complain_on_overflow */
319          bfd_elf_generic_reloc, /* special_function */
320          "R_PPC64_ADDR16_HI",   /* name */
321          FALSE,                 /* partial_inplace */
322          0,                     /* src_mask */
323          0xffff,                /* dst_mask */
324          FALSE),                /* pcrel_offset */
325
326   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
327      bits, treated as a signed number, is negative.  */
328   HOWTO (R_PPC64_ADDR16_HA,     /* type */
329          16,                    /* rightshift */
330          1,                     /* size (0 = byte, 1 = short, 2 = long) */
331          16,                    /* bitsize */
332          FALSE,                 /* pc_relative */
333          0,                     /* bitpos */
334          complain_overflow_signed, /* complain_on_overflow */
335          ppc64_elf_ha_reloc,    /* special_function */
336          "R_PPC64_ADDR16_HA",   /* name */
337          FALSE,                 /* partial_inplace */
338          0,                     /* src_mask */
339          0xffff,                /* dst_mask */
340          FALSE),                /* pcrel_offset */
341
342   /* An absolute 16 bit branch; the lower two bits must be zero.
343      FIXME: we don't check that, we just clear them.  */
344   HOWTO (R_PPC64_ADDR14,        /* type */
345          0,                     /* rightshift */
346          2,                     /* size (0 = byte, 1 = short, 2 = long) */
347          16,                    /* bitsize */
348          FALSE,                 /* pc_relative */
349          0,                     /* bitpos */
350          complain_overflow_bitfield, /* complain_on_overflow */
351          ppc64_elf_branch_reloc, /* special_function */
352          "R_PPC64_ADDR14",      /* name */
353          FALSE,                 /* partial_inplace */
354          0,                     /* src_mask */
355          0x0000fffc,            /* dst_mask */
356          FALSE),                /* pcrel_offset */
357
358   /* An absolute 16 bit branch, for which bit 10 should be set to
359      indicate that the branch is expected to be taken.  The lower two
360      bits must be zero.  */
361   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
362          0,                     /* rightshift */
363          2,                     /* size (0 = byte, 1 = short, 2 = long) */
364          16,                    /* bitsize */
365          FALSE,                 /* pc_relative */
366          0,                     /* bitpos */
367          complain_overflow_bitfield, /* complain_on_overflow */
368          ppc64_elf_brtaken_reloc, /* special_function */
369          "R_PPC64_ADDR14_BRTAKEN",/* name */
370          FALSE,                 /* partial_inplace */
371          0,                     /* src_mask */
372          0x0000fffc,            /* dst_mask */
373          FALSE),                /* pcrel_offset */
374
375   /* An absolute 16 bit branch, for which bit 10 should be set to
376      indicate that the branch is not expected to be taken.  The lower
377      two bits must be zero.  */
378   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
379          0,                     /* rightshift */
380          2,                     /* size (0 = byte, 1 = short, 2 = long) */
381          16,                    /* bitsize */
382          FALSE,                 /* pc_relative */
383          0,                     /* bitpos */
384          complain_overflow_bitfield, /* complain_on_overflow */
385          ppc64_elf_brtaken_reloc, /* special_function */
386          "R_PPC64_ADDR14_BRNTAKEN",/* name */
387          FALSE,                 /* partial_inplace */
388          0,                     /* src_mask */
389          0x0000fffc,            /* dst_mask */
390          FALSE),                /* pcrel_offset */
391
392   /* A relative 26 bit branch; the lower two bits must be zero.  */
393   HOWTO (R_PPC64_REL24,         /* type */
394          0,                     /* rightshift */
395          2,                     /* size (0 = byte, 1 = short, 2 = long) */
396          26,                    /* bitsize */
397          TRUE,                  /* pc_relative */
398          0,                     /* bitpos */
399          complain_overflow_signed, /* complain_on_overflow */
400          ppc64_elf_branch_reloc, /* special_function */
401          "R_PPC64_REL24",       /* name */
402          FALSE,                 /* partial_inplace */
403          0,                     /* src_mask */
404          0x03fffffc,            /* dst_mask */
405          TRUE),                 /* pcrel_offset */
406
407   /* A relative 16 bit branch; the lower two bits must be zero.  */
408   HOWTO (R_PPC64_REL14,         /* type */
409          0,                     /* rightshift */
410          2,                     /* size (0 = byte, 1 = short, 2 = long) */
411          16,                    /* bitsize */
412          TRUE,                  /* pc_relative */
413          0,                     /* bitpos */
414          complain_overflow_signed, /* complain_on_overflow */
415          ppc64_elf_branch_reloc, /* special_function */
416          "R_PPC64_REL14",       /* name */
417          FALSE,                 /* partial_inplace */
418          0,                     /* src_mask */
419          0x0000fffc,            /* dst_mask */
420          TRUE),                 /* pcrel_offset */
421
422   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
423      the branch is expected to be taken.  The lower two bits must be
424      zero.  */
425   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
426          0,                     /* rightshift */
427          2,                     /* size (0 = byte, 1 = short, 2 = long) */
428          16,                    /* bitsize */
429          TRUE,                  /* pc_relative */
430          0,                     /* bitpos */
431          complain_overflow_signed, /* complain_on_overflow */
432          ppc64_elf_brtaken_reloc, /* special_function */
433          "R_PPC64_REL14_BRTAKEN", /* name */
434          FALSE,                 /* partial_inplace */
435          0,                     /* src_mask */
436          0x0000fffc,            /* dst_mask */
437          TRUE),                 /* pcrel_offset */
438
439   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
440      the branch is not expected to be taken.  The lower two bits must
441      be zero.  */
442   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
443          0,                     /* rightshift */
444          2,                     /* size (0 = byte, 1 = short, 2 = long) */
445          16,                    /* bitsize */
446          TRUE,                  /* pc_relative */
447          0,                     /* bitpos */
448          complain_overflow_signed, /* complain_on_overflow */
449          ppc64_elf_brtaken_reloc, /* special_function */
450          "R_PPC64_REL14_BRNTAKEN",/* name */
451          FALSE,                 /* partial_inplace */
452          0,                     /* src_mask */
453          0x0000fffc,            /* dst_mask */
454          TRUE),                 /* pcrel_offset */
455
456   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
457      symbol.  */
458   HOWTO (R_PPC64_GOT16,         /* type */
459          0,                     /* rightshift */
460          1,                     /* size (0 = byte, 1 = short, 2 = long) */
461          16,                    /* bitsize */
462          FALSE,                 /* pc_relative */
463          0,                     /* bitpos */
464          complain_overflow_signed, /* complain_on_overflow */
465          ppc64_elf_unhandled_reloc, /* special_function */
466          "R_PPC64_GOT16",       /* name */
467          FALSE,                 /* partial_inplace */
468          0,                     /* src_mask */
469          0xffff,                /* dst_mask */
470          FALSE),                /* pcrel_offset */
471
472   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
473      the symbol.  */
474   HOWTO (R_PPC64_GOT16_LO,      /* type */
475          0,                     /* rightshift */
476          1,                     /* size (0 = byte, 1 = short, 2 = long) */
477          16,                    /* bitsize */
478          FALSE,                 /* pc_relative */
479          0,                     /* bitpos */
480          complain_overflow_dont, /* complain_on_overflow */
481          ppc64_elf_unhandled_reloc, /* special_function */
482          "R_PPC64_GOT16_LO",    /* name */
483          FALSE,                 /* partial_inplace */
484          0,                     /* src_mask */
485          0xffff,                /* dst_mask */
486          FALSE),                /* pcrel_offset */
487
488   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
489      the symbol.  */
490   HOWTO (R_PPC64_GOT16_HI,      /* type */
491          16,                    /* rightshift */
492          1,                     /* size (0 = byte, 1 = short, 2 = long) */
493          16,                    /* bitsize */
494          FALSE,                 /* pc_relative */
495          0,                     /* bitpos */
496          complain_overflow_signed,/* complain_on_overflow */
497          ppc64_elf_unhandled_reloc, /* special_function */
498          "R_PPC64_GOT16_HI",    /* name */
499          FALSE,                 /* partial_inplace */
500          0,                     /* src_mask */
501          0xffff,                /* dst_mask */
502          FALSE),                /* pcrel_offset */
503
504   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
505      the symbol.  */
506   HOWTO (R_PPC64_GOT16_HA,      /* type */
507          16,                    /* rightshift */
508          1,                     /* size (0 = byte, 1 = short, 2 = long) */
509          16,                    /* bitsize */
510          FALSE,                 /* pc_relative */
511          0,                     /* bitpos */
512          complain_overflow_signed,/* complain_on_overflow */
513          ppc64_elf_unhandled_reloc, /* special_function */
514          "R_PPC64_GOT16_HA",    /* name */
515          FALSE,                 /* partial_inplace */
516          0,                     /* src_mask */
517          0xffff,                /* dst_mask */
518          FALSE),                /* pcrel_offset */
519
520   /* This is used only by the dynamic linker.  The symbol should exist
521      both in the object being run and in some shared library.  The
522      dynamic linker copies the data addressed by the symbol from the
523      shared library into the object, because the object being
524      run has to have the data at some particular address.  */
525   HOWTO (R_PPC64_COPY,          /* type */
526          0,                     /* rightshift */
527          0,                     /* this one is variable size */
528          0,                     /* bitsize */
529          FALSE,                 /* pc_relative */
530          0,                     /* bitpos */
531          complain_overflow_dont, /* complain_on_overflow */
532          ppc64_elf_unhandled_reloc, /* special_function */
533          "R_PPC64_COPY",        /* name */
534          FALSE,                 /* partial_inplace */
535          0,                     /* src_mask */
536          0,                     /* dst_mask */
537          FALSE),                /* pcrel_offset */
538
539   /* Like R_PPC64_ADDR64, but used when setting global offset table
540      entries.  */
541   HOWTO (R_PPC64_GLOB_DAT,      /* type */
542          0,                     /* rightshift */
543          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
544          64,                    /* bitsize */
545          FALSE,                 /* pc_relative */
546          0,                     /* bitpos */
547          complain_overflow_dont, /* complain_on_overflow */
548          ppc64_elf_unhandled_reloc,  /* special_function */
549          "R_PPC64_GLOB_DAT",    /* name */
550          FALSE,                 /* partial_inplace */
551          0,                     /* src_mask */
552          ONES (64),             /* dst_mask */
553          FALSE),                /* pcrel_offset */
554
555   /* Created by the link editor.  Marks a procedure linkage table
556      entry for a symbol.  */
557   HOWTO (R_PPC64_JMP_SLOT,      /* type */
558          0,                     /* rightshift */
559          0,                     /* size (0 = byte, 1 = short, 2 = long) */
560          0,                     /* bitsize */
561          FALSE,                 /* pc_relative */
562          0,                     /* bitpos */
563          complain_overflow_dont, /* complain_on_overflow */
564          ppc64_elf_unhandled_reloc, /* special_function */
565          "R_PPC64_JMP_SLOT",    /* name */
566          FALSE,                 /* partial_inplace */
567          0,                     /* src_mask */
568          0,                     /* dst_mask */
569          FALSE),                /* pcrel_offset */
570
571   /* Used only by the dynamic linker.  When the object is run, this
572      doubleword64 is set to the load address of the object, plus the
573      addend.  */
574   HOWTO (R_PPC64_RELATIVE,      /* type */
575          0,                     /* rightshift */
576          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
577          64,                    /* bitsize */
578          FALSE,                 /* pc_relative */
579          0,                     /* bitpos */
580          complain_overflow_dont, /* complain_on_overflow */
581          bfd_elf_generic_reloc, /* special_function */
582          "R_PPC64_RELATIVE",    /* name */
583          FALSE,                 /* partial_inplace */
584          0,                     /* src_mask */
585          ONES (64),             /* dst_mask */
586          FALSE),                /* pcrel_offset */
587
588   /* Like R_PPC64_ADDR32, but may be unaligned.  */
589   HOWTO (R_PPC64_UADDR32,       /* type */
590          0,                     /* rightshift */
591          2,                     /* size (0 = byte, 1 = short, 2 = long) */
592          32,                    /* bitsize */
593          FALSE,                 /* pc_relative */
594          0,                     /* bitpos */
595          complain_overflow_bitfield, /* complain_on_overflow */
596          bfd_elf_generic_reloc, /* special_function */
597          "R_PPC64_UADDR32",     /* name */
598          FALSE,                 /* partial_inplace */
599          0,                     /* src_mask */
600          0xffffffff,            /* dst_mask */
601          FALSE),                /* pcrel_offset */
602
603   /* Like R_PPC64_ADDR16, but may be unaligned.  */
604   HOWTO (R_PPC64_UADDR16,       /* type */
605          0,                     /* rightshift */
606          1,                     /* size (0 = byte, 1 = short, 2 = long) */
607          16,                    /* bitsize */
608          FALSE,                 /* pc_relative */
609          0,                     /* bitpos */
610          complain_overflow_bitfield, /* complain_on_overflow */
611          bfd_elf_generic_reloc, /* special_function */
612          "R_PPC64_UADDR16",     /* name */
613          FALSE,                 /* partial_inplace */
614          0,                     /* src_mask */
615          0xffff,                /* dst_mask */
616          FALSE),                /* pcrel_offset */
617
618   /* 32-bit PC relative.  */
619   HOWTO (R_PPC64_REL32,         /* type */
620          0,                     /* rightshift */
621          2,                     /* size (0 = byte, 1 = short, 2 = long) */
622          32,                    /* bitsize */
623          TRUE,                  /* pc_relative */
624          0,                     /* bitpos */
625          /* FIXME: Verify.  Was complain_overflow_bitfield.  */
626          complain_overflow_signed, /* complain_on_overflow */
627          bfd_elf_generic_reloc, /* special_function */
628          "R_PPC64_REL32",       /* name */
629          FALSE,                 /* partial_inplace */
630          0,                     /* src_mask */
631          0xffffffff,            /* dst_mask */
632          TRUE),                 /* pcrel_offset */
633
634   /* 32-bit relocation to the symbol's procedure linkage table.  */
635   HOWTO (R_PPC64_PLT32,         /* type */
636          0,                     /* rightshift */
637          2,                     /* size (0 = byte, 1 = short, 2 = long) */
638          32,                    /* bitsize */
639          FALSE,                 /* pc_relative */
640          0,                     /* bitpos */
641          complain_overflow_bitfield, /* complain_on_overflow */
642          ppc64_elf_unhandled_reloc, /* special_function */
643          "R_PPC64_PLT32",       /* name */
644          FALSE,                 /* partial_inplace */
645          0,                     /* src_mask */
646          0xffffffff,            /* dst_mask */
647          FALSE),                /* pcrel_offset */
648
649   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
650      FIXME: R_PPC64_PLTREL32 not supported.  */
651   HOWTO (R_PPC64_PLTREL32,      /* type */
652          0,                     /* rightshift */
653          2,                     /* size (0 = byte, 1 = short, 2 = long) */
654          32,                    /* bitsize */
655          TRUE,                  /* pc_relative */
656          0,                     /* bitpos */
657          complain_overflow_signed, /* complain_on_overflow */
658          bfd_elf_generic_reloc, /* special_function */
659          "R_PPC64_PLTREL32",    /* name */
660          FALSE,                 /* partial_inplace */
661          0,                     /* src_mask */
662          0xffffffff,            /* dst_mask */
663          TRUE),                 /* pcrel_offset */
664
665   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
666      the symbol.  */
667   HOWTO (R_PPC64_PLT16_LO,      /* type */
668          0,                     /* rightshift */
669          1,                     /* size (0 = byte, 1 = short, 2 = long) */
670          16,                    /* bitsize */
671          FALSE,                 /* pc_relative */
672          0,                     /* bitpos */
673          complain_overflow_dont, /* complain_on_overflow */
674          ppc64_elf_unhandled_reloc, /* special_function */
675          "R_PPC64_PLT16_LO",    /* name */
676          FALSE,                 /* partial_inplace */
677          0,                     /* src_mask */
678          0xffff,                /* dst_mask */
679          FALSE),                /* pcrel_offset */
680
681   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
682      the symbol.  */
683   HOWTO (R_PPC64_PLT16_HI,      /* type */
684          16,                    /* rightshift */
685          1,                     /* size (0 = byte, 1 = short, 2 = long) */
686          16,                    /* bitsize */
687          FALSE,                 /* pc_relative */
688          0,                     /* bitpos */
689          complain_overflow_signed, /* complain_on_overflow */
690          ppc64_elf_unhandled_reloc, /* special_function */
691          "R_PPC64_PLT16_HI",    /* name */
692          FALSE,                 /* partial_inplace */
693          0,                     /* src_mask */
694          0xffff,                /* dst_mask */
695          FALSE),                /* pcrel_offset */
696
697   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
698      the symbol.  */
699   HOWTO (R_PPC64_PLT16_HA,      /* type */
700          16,                    /* rightshift */
701          1,                     /* size (0 = byte, 1 = short, 2 = long) */
702          16,                    /* bitsize */
703          FALSE,                 /* pc_relative */
704          0,                     /* bitpos */
705          complain_overflow_signed, /* complain_on_overflow */
706          ppc64_elf_unhandled_reloc, /* special_function */
707          "R_PPC64_PLT16_HA",    /* name */
708          FALSE,                 /* partial_inplace */
709          0,                     /* src_mask */
710          0xffff,                /* dst_mask */
711          FALSE),                /* pcrel_offset */
712
713   /* 16-bit section relative relocation.  */
714   HOWTO (R_PPC64_SECTOFF,       /* type */
715          0,                     /* rightshift */
716          1,                     /* size (0 = byte, 1 = short, 2 = long) */
717          16,                    /* bitsize */
718          FALSE,                 /* pc_relative */
719          0,                     /* bitpos */
720          complain_overflow_bitfield, /* complain_on_overflow */
721          ppc64_elf_sectoff_reloc, /* special_function */
722          "R_PPC64_SECTOFF",     /* name */
723          FALSE,                 /* partial_inplace */
724          0,                     /* src_mask */
725          0xffff,                /* dst_mask */
726          FALSE),                /* pcrel_offset */
727
728   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
729   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
730          0,                     /* rightshift */
731          1,                     /* size (0 = byte, 1 = short, 2 = long) */
732          16,                    /* bitsize */
733          FALSE,                 /* pc_relative */
734          0,                     /* bitpos */
735          complain_overflow_dont, /* complain_on_overflow */
736          ppc64_elf_sectoff_reloc, /* special_function */
737          "R_PPC64_SECTOFF_LO",  /* name */
738          FALSE,                 /* partial_inplace */
739          0,                     /* src_mask */
740          0xffff,                /* dst_mask */
741          FALSE),                /* pcrel_offset */
742
743   /* 16-bit upper half section relative relocation.  */
744   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
745          16,                    /* rightshift */
746          1,                     /* size (0 = byte, 1 = short, 2 = long) */
747          16,                    /* bitsize */
748          FALSE,                 /* pc_relative */
749          0,                     /* bitpos */
750          complain_overflow_signed, /* complain_on_overflow */
751          ppc64_elf_sectoff_reloc, /* special_function */
752          "R_PPC64_SECTOFF_HI",  /* name */
753          FALSE,                 /* partial_inplace */
754          0,                     /* src_mask */
755          0xffff,                /* dst_mask */
756          FALSE),                /* pcrel_offset */
757
758   /* 16-bit upper half adjusted section relative relocation.  */
759   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
760          16,                    /* rightshift */
761          1,                     /* size (0 = byte, 1 = short, 2 = long) */
762          16,                    /* bitsize */
763          FALSE,                 /* pc_relative */
764          0,                     /* bitpos */
765          complain_overflow_signed, /* complain_on_overflow */
766          ppc64_elf_sectoff_ha_reloc, /* special_function */
767          "R_PPC64_SECTOFF_HA",  /* name */
768          FALSE,                 /* partial_inplace */
769          0,                     /* src_mask */
770          0xffff,                /* dst_mask */
771          FALSE),                /* pcrel_offset */
772
773   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
774   HOWTO (R_PPC64_REL30,         /* type */
775          2,                     /* rightshift */
776          2,                     /* size (0 = byte, 1 = short, 2 = long) */
777          30,                    /* bitsize */
778          TRUE,                  /* pc_relative */
779          0,                     /* bitpos */
780          complain_overflow_dont, /* complain_on_overflow */
781          bfd_elf_generic_reloc, /* special_function */
782          "R_PPC64_REL30",       /* name */
783          FALSE,                 /* partial_inplace */
784          0,                     /* src_mask */
785          0xfffffffc,            /* dst_mask */
786          TRUE),                 /* pcrel_offset */
787
788   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
789
790   /* A standard 64-bit relocation.  */
791   HOWTO (R_PPC64_ADDR64,        /* type */
792          0,                     /* rightshift */
793          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
794          64,                    /* bitsize */
795          FALSE,                 /* pc_relative */
796          0,                     /* bitpos */
797          complain_overflow_dont, /* complain_on_overflow */
798          bfd_elf_generic_reloc, /* special_function */
799          "R_PPC64_ADDR64",      /* name */
800          FALSE,                 /* partial_inplace */
801          0,                     /* src_mask */
802          ONES (64),             /* dst_mask */
803          FALSE),                /* pcrel_offset */
804
805   /* The bits 32-47 of an address.  */
806   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
807          32,                    /* rightshift */
808          1,                     /* size (0 = byte, 1 = short, 2 = long) */
809          16,                    /* bitsize */
810          FALSE,                 /* pc_relative */
811          0,                     /* bitpos */
812          complain_overflow_dont, /* complain_on_overflow */
813          bfd_elf_generic_reloc, /* special_function */
814          "R_PPC64_ADDR16_HIGHER", /* name */
815          FALSE,                 /* partial_inplace */
816          0,                     /* src_mask */
817          0xffff,                /* dst_mask */
818          FALSE),                /* pcrel_offset */
819
820   /* The bits 32-47 of an address, plus 1 if the contents of the low
821      16 bits, treated as a signed number, is negative.  */
822   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
823          32,                    /* rightshift */
824          1,                     /* size (0 = byte, 1 = short, 2 = long) */
825          16,                    /* bitsize */
826          FALSE,                 /* pc_relative */
827          0,                     /* bitpos */
828          complain_overflow_dont, /* complain_on_overflow */
829          ppc64_elf_ha_reloc,    /* special_function */
830          "R_PPC64_ADDR16_HIGHERA", /* name */
831          FALSE,                 /* partial_inplace */
832          0,                     /* src_mask */
833          0xffff,                /* dst_mask */
834          FALSE),                /* pcrel_offset */
835
836   /* The bits 48-63 of an address.  */
837   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
838          48,                    /* rightshift */
839          1,                     /* size (0 = byte, 1 = short, 2 = long) */
840          16,                    /* bitsize */
841          FALSE,                 /* pc_relative */
842          0,                     /* bitpos */
843          complain_overflow_dont, /* complain_on_overflow */
844          bfd_elf_generic_reloc, /* special_function */
845          "R_PPC64_ADDR16_HIGHEST", /* name */
846          FALSE,                 /* partial_inplace */
847          0,                     /* src_mask */
848          0xffff,                /* dst_mask */
849          FALSE),                /* pcrel_offset */
850
851   /* The bits 48-63 of an address, plus 1 if the contents of the low
852      16 bits, treated as a signed number, is negative.  */
853   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
854          48,                    /* rightshift */
855          1,                     /* size (0 = byte, 1 = short, 2 = long) */
856          16,                    /* bitsize */
857          FALSE,                 /* pc_relative */
858          0,                     /* bitpos */
859          complain_overflow_dont, /* complain_on_overflow */
860          ppc64_elf_ha_reloc,    /* special_function */
861          "R_PPC64_ADDR16_HIGHESTA", /* name */
862          FALSE,                 /* partial_inplace */
863          0,                     /* src_mask */
864          0xffff,                /* dst_mask */
865          FALSE),                /* pcrel_offset */
866
867   /* Like ADDR64, but may be unaligned.  */
868   HOWTO (R_PPC64_UADDR64,       /* type */
869          0,                     /* rightshift */
870          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
871          64,                    /* bitsize */
872          FALSE,                 /* pc_relative */
873          0,                     /* bitpos */
874          complain_overflow_dont, /* complain_on_overflow */
875          bfd_elf_generic_reloc, /* special_function */
876          "R_PPC64_UADDR64",     /* name */
877          FALSE,                 /* partial_inplace */
878          0,                     /* src_mask */
879          ONES (64),             /* dst_mask */
880          FALSE),                /* pcrel_offset */
881
882   /* 64-bit relative relocation.  */
883   HOWTO (R_PPC64_REL64,         /* type */
884          0,                     /* rightshift */
885          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
886          64,                    /* bitsize */
887          TRUE,                  /* pc_relative */
888          0,                     /* bitpos */
889          complain_overflow_dont, /* complain_on_overflow */
890          bfd_elf_generic_reloc, /* special_function */
891          "R_PPC64_REL64",       /* name */
892          FALSE,                 /* partial_inplace */
893          0,                     /* src_mask */
894          ONES (64),             /* dst_mask */
895          TRUE),                 /* pcrel_offset */
896
897   /* 64-bit relocation to the symbol's procedure linkage table.  */
898   HOWTO (R_PPC64_PLT64,         /* type */
899          0,                     /* rightshift */
900          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
901          64,                    /* bitsize */
902          FALSE,                 /* pc_relative */
903          0,                     /* bitpos */
904          complain_overflow_dont, /* complain_on_overflow */
905          ppc64_elf_unhandled_reloc, /* special_function */
906          "R_PPC64_PLT64",       /* name */
907          FALSE,                 /* partial_inplace */
908          0,                     /* src_mask */
909          ONES (64),             /* dst_mask */
910          FALSE),                /* pcrel_offset */
911
912   /* 64-bit PC relative relocation to the symbol's procedure linkage
913      table.  */
914   /* FIXME: R_PPC64_PLTREL64 not supported.  */
915   HOWTO (R_PPC64_PLTREL64,      /* type */
916          0,                     /* rightshift */
917          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
918          64,                    /* bitsize */
919          TRUE,                  /* pc_relative */
920          0,                     /* bitpos */
921          complain_overflow_dont, /* complain_on_overflow */
922          ppc64_elf_unhandled_reloc, /* special_function */
923          "R_PPC64_PLTREL64",    /* name */
924          FALSE,                 /* partial_inplace */
925          0,                     /* src_mask */
926          ONES (64),             /* dst_mask */
927          TRUE),                 /* pcrel_offset */
928
929   /* 16 bit TOC-relative relocation.  */
930
931   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
932   HOWTO (R_PPC64_TOC16,         /* type */
933          0,                     /* rightshift */
934          1,                     /* size (0 = byte, 1 = short, 2 = long) */
935          16,                    /* bitsize */
936          FALSE,                 /* pc_relative */
937          0,                     /* bitpos */
938          complain_overflow_signed, /* complain_on_overflow */
939          ppc64_elf_toc_reloc,   /* special_function */
940          "R_PPC64_TOC16",       /* name */
941          FALSE,                 /* partial_inplace */
942          0,                     /* src_mask */
943          0xffff,                /* dst_mask */
944          FALSE),                /* pcrel_offset */
945
946   /* 16 bit TOC-relative relocation without overflow.  */
947
948   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
949   HOWTO (R_PPC64_TOC16_LO,      /* type */
950          0,                     /* rightshift */
951          1,                     /* size (0 = byte, 1 = short, 2 = long) */
952          16,                    /* bitsize */
953          FALSE,                 /* pc_relative */
954          0,                     /* bitpos */
955          complain_overflow_dont, /* complain_on_overflow */
956          ppc64_elf_toc_reloc,   /* special_function */
957          "R_PPC64_TOC16_LO",    /* name */
958          FALSE,                 /* partial_inplace */
959          0,                     /* src_mask */
960          0xffff,                /* dst_mask */
961          FALSE),                /* pcrel_offset */
962
963   /* 16 bit TOC-relative relocation, high 16 bits.  */
964
965   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
966   HOWTO (R_PPC64_TOC16_HI,      /* type */
967          16,                    /* rightshift */
968          1,                     /* size (0 = byte, 1 = short, 2 = long) */
969          16,                    /* bitsize */
970          FALSE,                 /* pc_relative */
971          0,                     /* bitpos */
972          complain_overflow_signed, /* complain_on_overflow */
973          ppc64_elf_toc_reloc,   /* special_function */
974          "R_PPC64_TOC16_HI",    /* name */
975          FALSE,                 /* partial_inplace */
976          0,                     /* src_mask */
977          0xffff,                /* dst_mask */
978          FALSE),                /* pcrel_offset */
979
980   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
981      contents of the low 16 bits, treated as a signed number, is
982      negative.  */
983
984   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
985   HOWTO (R_PPC64_TOC16_HA,      /* type */
986          16,                    /* rightshift */
987          1,                     /* size (0 = byte, 1 = short, 2 = long) */
988          16,                    /* bitsize */
989          FALSE,                 /* pc_relative */
990          0,                     /* bitpos */
991          complain_overflow_signed, /* complain_on_overflow */
992          ppc64_elf_toc_ha_reloc, /* special_function */
993          "R_PPC64_TOC16_HA",    /* name */
994          FALSE,                 /* partial_inplace */
995          0,                     /* src_mask */
996          0xffff,                /* dst_mask */
997          FALSE),                /* pcrel_offset */
998
999   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
1000
1001   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
1002   HOWTO (R_PPC64_TOC,           /* type */
1003          0,                     /* rightshift */
1004          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1005          64,                    /* bitsize */
1006          FALSE,                 /* pc_relative */
1007          0,                     /* bitpos */
1008          complain_overflow_bitfield, /* complain_on_overflow */
1009          ppc64_elf_toc64_reloc, /* special_function */
1010          "R_PPC64_TOC",         /* name */
1011          FALSE,                 /* partial_inplace */
1012          0,                     /* src_mask */
1013          ONES (64),             /* dst_mask */
1014          FALSE),                /* pcrel_offset */
1015
1016   /* Like R_PPC64_GOT16, but also informs the link editor that the
1017      value to relocate may (!) refer to a PLT entry which the link
1018      editor (a) may replace with the symbol value.  If the link editor
1019      is unable to fully resolve the symbol, it may (b) create a PLT
1020      entry and store the address to the new PLT entry in the GOT.
1021      This permits lazy resolution of function symbols at run time.
1022      The link editor may also skip all of this and just (c) emit a
1023      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
1024   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
1025     HOWTO (R_PPC64_PLTGOT16,    /* type */
1026          0,                     /* rightshift */
1027          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1028          16,                    /* bitsize */
1029          FALSE,                 /* pc_relative */
1030          0,                     /* bitpos */
1031          complain_overflow_signed, /* complain_on_overflow */
1032          ppc64_elf_unhandled_reloc, /* special_function */
1033          "R_PPC64_PLTGOT16",    /* name */
1034          FALSE,                 /* partial_inplace */
1035          0,                     /* src_mask */
1036          0xffff,                /* dst_mask */
1037          FALSE),                /* pcrel_offset */
1038
1039   /* Like R_PPC64_PLTGOT16, but without overflow.  */
1040   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1041   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1042          0,                     /* rightshift */
1043          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1044          16,                    /* bitsize */
1045          FALSE,                 /* pc_relative */
1046          0,                     /* bitpos */
1047          complain_overflow_dont, /* complain_on_overflow */
1048          ppc64_elf_unhandled_reloc, /* special_function */
1049          "R_PPC64_PLTGOT16_LO", /* name */
1050          FALSE,                 /* partial_inplace */
1051          0,                     /* src_mask */
1052          0xffff,                /* dst_mask */
1053          FALSE),                /* pcrel_offset */
1054
1055   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1056   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1057   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1058          16,                    /* rightshift */
1059          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1060          16,                    /* bitsize */
1061          FALSE,                 /* pc_relative */
1062          0,                     /* bitpos */
1063          complain_overflow_signed, /* complain_on_overflow */
1064          ppc64_elf_unhandled_reloc, /* special_function */
1065          "R_PPC64_PLTGOT16_HI", /* name */
1066          FALSE,                 /* partial_inplace */
1067          0,                     /* src_mask */
1068          0xffff,                /* dst_mask */
1069          FALSE),                /* pcrel_offset */
1070
1071   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1072      1 if the contents of the low 16 bits, treated as a signed number,
1073      is negative.  */
1074   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1075   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1076          16,                    /* rightshift */
1077          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1078          16,                    /* bitsize */
1079          FALSE,                 /* pc_relative */
1080          0,                     /* bitpos */
1081          complain_overflow_signed, /* complain_on_overflow */
1082          ppc64_elf_unhandled_reloc, /* special_function */
1083          "R_PPC64_PLTGOT16_HA", /* name */
1084          FALSE,                 /* partial_inplace */
1085          0,                     /* src_mask */
1086          0xffff,                /* dst_mask */
1087          FALSE),                /* pcrel_offset */
1088
1089   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1090   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1091          0,                     /* rightshift */
1092          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1093          16,                    /* bitsize */
1094          FALSE,                 /* pc_relative */
1095          0,                     /* bitpos */
1096          complain_overflow_bitfield, /* complain_on_overflow */
1097          bfd_elf_generic_reloc, /* special_function */
1098          "R_PPC64_ADDR16_DS",   /* name */
1099          FALSE,                 /* partial_inplace */
1100          0,                     /* src_mask */
1101          0xfffc,                /* dst_mask */
1102          FALSE),                /* pcrel_offset */
1103
1104   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1105   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1106          0,                     /* rightshift */
1107          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1108          16,                    /* bitsize */
1109          FALSE,                 /* pc_relative */
1110          0,                     /* bitpos */
1111          complain_overflow_dont,/* complain_on_overflow */
1112          bfd_elf_generic_reloc, /* special_function */
1113          "R_PPC64_ADDR16_LO_DS",/* name */
1114          FALSE,                 /* partial_inplace */
1115          0,                     /* src_mask */
1116          0xfffc,                /* dst_mask */
1117          FALSE),                /* pcrel_offset */
1118
1119   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1120   HOWTO (R_PPC64_GOT16_DS,      /* type */
1121          0,                     /* rightshift */
1122          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1123          16,                    /* bitsize */
1124          FALSE,                 /* pc_relative */
1125          0,                     /* bitpos */
1126          complain_overflow_signed, /* complain_on_overflow */
1127          ppc64_elf_unhandled_reloc, /* special_function */
1128          "R_PPC64_GOT16_DS",    /* name */
1129          FALSE,                 /* partial_inplace */
1130          0,                     /* src_mask */
1131          0xfffc,                /* dst_mask */
1132          FALSE),                /* pcrel_offset */
1133
1134   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1135   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1136          0,                     /* rightshift */
1137          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1138          16,                    /* bitsize */
1139          FALSE,                 /* pc_relative */
1140          0,                     /* bitpos */
1141          complain_overflow_dont, /* complain_on_overflow */
1142          ppc64_elf_unhandled_reloc, /* special_function */
1143          "R_PPC64_GOT16_LO_DS", /* name */
1144          FALSE,                 /* partial_inplace */
1145          0,                     /* src_mask */
1146          0xfffc,                /* dst_mask */
1147          FALSE),                /* pcrel_offset */
1148
1149   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1150   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1151          0,                     /* rightshift */
1152          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1153          16,                    /* bitsize */
1154          FALSE,                 /* pc_relative */
1155          0,                     /* bitpos */
1156          complain_overflow_dont, /* complain_on_overflow */
1157          ppc64_elf_unhandled_reloc, /* special_function */
1158          "R_PPC64_PLT16_LO_DS", /* name */
1159          FALSE,                 /* partial_inplace */
1160          0,                     /* src_mask */
1161          0xfffc,                /* dst_mask */
1162          FALSE),                /* pcrel_offset */
1163
1164   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1165   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1166          0,                     /* rightshift */
1167          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1168          16,                    /* bitsize */
1169          FALSE,                 /* pc_relative */
1170          0,                     /* bitpos */
1171          complain_overflow_bitfield, /* complain_on_overflow */
1172          ppc64_elf_sectoff_reloc, /* special_function */
1173          "R_PPC64_SECTOFF_DS",  /* name */
1174          FALSE,                 /* partial_inplace */
1175          0,                     /* src_mask */
1176          0xfffc,                /* dst_mask */
1177          FALSE),                /* pcrel_offset */
1178
1179   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1180   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1181          0,                     /* rightshift */
1182          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1183          16,                    /* bitsize */
1184          FALSE,                 /* pc_relative */
1185          0,                     /* bitpos */
1186          complain_overflow_dont, /* complain_on_overflow */
1187          ppc64_elf_sectoff_reloc, /* special_function */
1188          "R_PPC64_SECTOFF_LO_DS",/* name */
1189          FALSE,                 /* partial_inplace */
1190          0,                     /* src_mask */
1191          0xfffc,                /* dst_mask */
1192          FALSE),                /* pcrel_offset */
1193
1194   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1195   HOWTO (R_PPC64_TOC16_DS,      /* type */
1196          0,                     /* rightshift */
1197          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1198          16,                    /* bitsize */
1199          FALSE,                 /* pc_relative */
1200          0,                     /* bitpos */
1201          complain_overflow_signed, /* complain_on_overflow */
1202          ppc64_elf_toc_reloc,   /* special_function */
1203          "R_PPC64_TOC16_DS",    /* name */
1204          FALSE,                 /* partial_inplace */
1205          0,                     /* src_mask */
1206          0xfffc,                /* dst_mask */
1207          FALSE),                /* pcrel_offset */
1208
1209   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1210   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1211          0,                     /* rightshift */
1212          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1213          16,                    /* bitsize */
1214          FALSE,                 /* pc_relative */
1215          0,                     /* bitpos */
1216          complain_overflow_dont, /* complain_on_overflow */
1217          ppc64_elf_toc_reloc,   /* special_function */
1218          "R_PPC64_TOC16_LO_DS", /* name */
1219          FALSE,                 /* partial_inplace */
1220          0,                     /* src_mask */
1221          0xfffc,                /* dst_mask */
1222          FALSE),                /* pcrel_offset */
1223
1224   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1225   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1226   HOWTO (R_PPC64_PLTGOT16_DS,   /* type */
1227          0,                     /* rightshift */
1228          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1229          16,                    /* bitsize */
1230          FALSE,                 /* pc_relative */
1231          0,                     /* bitpos */
1232          complain_overflow_signed, /* complain_on_overflow */
1233          ppc64_elf_unhandled_reloc, /* special_function */
1234          "R_PPC64_PLTGOT16_DS", /* name */
1235          FALSE,                 /* partial_inplace */
1236          0,                     /* src_mask */
1237          0xfffc,                /* dst_mask */
1238          FALSE),                /* pcrel_offset */
1239
1240   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1241   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1242   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1243          0,                     /* rightshift */
1244          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1245          16,                    /* bitsize */
1246          FALSE,                 /* pc_relative */
1247          0,                     /* bitpos */
1248          complain_overflow_dont, /* complain_on_overflow */
1249          ppc64_elf_unhandled_reloc, /* special_function */
1250          "R_PPC64_PLTGOT16_LO_DS",/* name */
1251          FALSE,                 /* partial_inplace */
1252          0,                     /* src_mask */
1253          0xfffc,                /* dst_mask */
1254          FALSE),                /* pcrel_offset */
1255
1256   /* Marker relocs for TLS.  */
1257   HOWTO (R_PPC64_TLS,
1258          0,                     /* rightshift */
1259          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1260          32,                    /* bitsize */
1261          FALSE,                 /* pc_relative */
1262          0,                     /* bitpos */
1263          complain_overflow_dont, /* complain_on_overflow */
1264          bfd_elf_generic_reloc, /* special_function */
1265          "R_PPC64_TLS",         /* name */
1266          FALSE,                 /* partial_inplace */
1267          0,                     /* src_mask */
1268          0,                     /* dst_mask */
1269          FALSE),                /* pcrel_offset */
1270
1271   HOWTO (R_PPC64_TLSGD,
1272          0,                     /* rightshift */
1273          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1274          32,                    /* bitsize */
1275          FALSE,                 /* pc_relative */
1276          0,                     /* bitpos */
1277          complain_overflow_dont, /* complain_on_overflow */
1278          bfd_elf_generic_reloc, /* special_function */
1279          "R_PPC64_TLSGD",       /* name */
1280          FALSE,                 /* partial_inplace */
1281          0,                     /* src_mask */
1282          0,                     /* dst_mask */
1283          FALSE),                /* pcrel_offset */
1284
1285   HOWTO (R_PPC64_TLSLD,
1286          0,                     /* rightshift */
1287          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1288          32,                    /* bitsize */
1289          FALSE,                 /* pc_relative */
1290          0,                     /* bitpos */
1291          complain_overflow_dont, /* complain_on_overflow */
1292          bfd_elf_generic_reloc, /* special_function */
1293          "R_PPC64_TLSLD",       /* name */
1294          FALSE,                 /* partial_inplace */
1295          0,                     /* src_mask */
1296          0,                     /* dst_mask */
1297          FALSE),                /* pcrel_offset */
1298
1299   HOWTO (R_PPC64_TOCSAVE,
1300          0,                     /* rightshift */
1301          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1302          32,                    /* bitsize */
1303          FALSE,                 /* pc_relative */
1304          0,                     /* bitpos */
1305          complain_overflow_dont, /* complain_on_overflow */
1306          bfd_elf_generic_reloc, /* special_function */
1307          "R_PPC64_TOCSAVE",     /* name */
1308          FALSE,                 /* partial_inplace */
1309          0,                     /* src_mask */
1310          0,                     /* dst_mask */
1311          FALSE),                /* pcrel_offset */
1312
1313   /* Computes the load module index of the load module that contains the
1314      definition of its TLS sym.  */
1315   HOWTO (R_PPC64_DTPMOD64,
1316          0,                     /* rightshift */
1317          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1318          64,                    /* bitsize */
1319          FALSE,                 /* pc_relative */
1320          0,                     /* bitpos */
1321          complain_overflow_dont, /* complain_on_overflow */
1322          ppc64_elf_unhandled_reloc, /* special_function */
1323          "R_PPC64_DTPMOD64",    /* name */
1324          FALSE,                 /* partial_inplace */
1325          0,                     /* src_mask */
1326          ONES (64),             /* dst_mask */
1327          FALSE),                /* pcrel_offset */
1328
1329   /* Computes a dtv-relative displacement, the difference between the value
1330      of sym+add and the base address of the thread-local storage block that
1331      contains the definition of sym, minus 0x8000.  */
1332   HOWTO (R_PPC64_DTPREL64,
1333          0,                     /* rightshift */
1334          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1335          64,                    /* bitsize */
1336          FALSE,                 /* pc_relative */
1337          0,                     /* bitpos */
1338          complain_overflow_dont, /* complain_on_overflow */
1339          ppc64_elf_unhandled_reloc, /* special_function */
1340          "R_PPC64_DTPREL64",    /* name */
1341          FALSE,                 /* partial_inplace */
1342          0,                     /* src_mask */
1343          ONES (64),             /* dst_mask */
1344          FALSE),                /* pcrel_offset */
1345
1346   /* A 16 bit dtprel reloc.  */
1347   HOWTO (R_PPC64_DTPREL16,
1348          0,                     /* rightshift */
1349          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1350          16,                    /* bitsize */
1351          FALSE,                 /* pc_relative */
1352          0,                     /* bitpos */
1353          complain_overflow_signed, /* complain_on_overflow */
1354          ppc64_elf_unhandled_reloc, /* special_function */
1355          "R_PPC64_DTPREL16",    /* name */
1356          FALSE,                 /* partial_inplace */
1357          0,                     /* src_mask */
1358          0xffff,                /* dst_mask */
1359          FALSE),                /* pcrel_offset */
1360
1361   /* Like DTPREL16, but no overflow.  */
1362   HOWTO (R_PPC64_DTPREL16_LO,
1363          0,                     /* rightshift */
1364          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1365          16,                    /* bitsize */
1366          FALSE,                 /* pc_relative */
1367          0,                     /* bitpos */
1368          complain_overflow_dont, /* complain_on_overflow */
1369          ppc64_elf_unhandled_reloc, /* special_function */
1370          "R_PPC64_DTPREL16_LO", /* name */
1371          FALSE,                 /* partial_inplace */
1372          0,                     /* src_mask */
1373          0xffff,                /* dst_mask */
1374          FALSE),                /* pcrel_offset */
1375
1376   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1377   HOWTO (R_PPC64_DTPREL16_HI,
1378          16,                    /* rightshift */
1379          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1380          16,                    /* bitsize */
1381          FALSE,                 /* pc_relative */
1382          0,                     /* bitpos */
1383          complain_overflow_signed, /* complain_on_overflow */
1384          ppc64_elf_unhandled_reloc, /* special_function */
1385          "R_PPC64_DTPREL16_HI", /* name */
1386          FALSE,                 /* partial_inplace */
1387          0,                     /* src_mask */
1388          0xffff,                /* dst_mask */
1389          FALSE),                /* pcrel_offset */
1390
1391   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1392   HOWTO (R_PPC64_DTPREL16_HA,
1393          16,                    /* rightshift */
1394          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1395          16,                    /* bitsize */
1396          FALSE,                 /* pc_relative */
1397          0,                     /* bitpos */
1398          complain_overflow_signed, /* complain_on_overflow */
1399          ppc64_elf_unhandled_reloc, /* special_function */
1400          "R_PPC64_DTPREL16_HA", /* name */
1401          FALSE,                 /* partial_inplace */
1402          0,                     /* src_mask */
1403          0xffff,                /* dst_mask */
1404          FALSE),                /* pcrel_offset */
1405
1406   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1407   HOWTO (R_PPC64_DTPREL16_HIGHER,
1408          32,                    /* rightshift */
1409          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1410          16,                    /* bitsize */
1411          FALSE,                 /* pc_relative */
1412          0,                     /* bitpos */
1413          complain_overflow_dont, /* complain_on_overflow */
1414          ppc64_elf_unhandled_reloc, /* special_function */
1415          "R_PPC64_DTPREL16_HIGHER", /* name */
1416          FALSE,                 /* partial_inplace */
1417          0,                     /* src_mask */
1418          0xffff,                /* dst_mask */
1419          FALSE),                /* pcrel_offset */
1420
1421   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1422   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1423          32,                    /* rightshift */
1424          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1425          16,                    /* bitsize */
1426          FALSE,                 /* pc_relative */
1427          0,                     /* bitpos */
1428          complain_overflow_dont, /* complain_on_overflow */
1429          ppc64_elf_unhandled_reloc, /* special_function */
1430          "R_PPC64_DTPREL16_HIGHERA", /* name */
1431          FALSE,                 /* partial_inplace */
1432          0,                     /* src_mask */
1433          0xffff,                /* dst_mask */
1434          FALSE),                /* pcrel_offset */
1435
1436   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1437   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1438          48,                    /* rightshift */
1439          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1440          16,                    /* bitsize */
1441          FALSE,                 /* pc_relative */
1442          0,                     /* bitpos */
1443          complain_overflow_dont, /* complain_on_overflow */
1444          ppc64_elf_unhandled_reloc, /* special_function */
1445          "R_PPC64_DTPREL16_HIGHEST", /* name */
1446          FALSE,                 /* partial_inplace */
1447          0,                     /* src_mask */
1448          0xffff,                /* dst_mask */
1449          FALSE),                /* pcrel_offset */
1450
1451   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1452   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1453          48,                    /* rightshift */
1454          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1455          16,                    /* bitsize */
1456          FALSE,                 /* pc_relative */
1457          0,                     /* bitpos */
1458          complain_overflow_dont, /* complain_on_overflow */
1459          ppc64_elf_unhandled_reloc, /* special_function */
1460          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1461          FALSE,                 /* partial_inplace */
1462          0,                     /* src_mask */
1463          0xffff,                /* dst_mask */
1464          FALSE),                /* pcrel_offset */
1465
1466   /* Like DTPREL16, but for insns with a DS field.  */
1467   HOWTO (R_PPC64_DTPREL16_DS,
1468          0,                     /* rightshift */
1469          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1470          16,                    /* bitsize */
1471          FALSE,                 /* pc_relative */
1472          0,                     /* bitpos */
1473          complain_overflow_signed, /* complain_on_overflow */
1474          ppc64_elf_unhandled_reloc, /* special_function */
1475          "R_PPC64_DTPREL16_DS", /* name */
1476          FALSE,                 /* partial_inplace */
1477          0,                     /* src_mask */
1478          0xfffc,                /* dst_mask */
1479          FALSE),                /* pcrel_offset */
1480
1481   /* Like DTPREL16_DS, but no overflow.  */
1482   HOWTO (R_PPC64_DTPREL16_LO_DS,
1483          0,                     /* rightshift */
1484          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1485          16,                    /* bitsize */
1486          FALSE,                 /* pc_relative */
1487          0,                     /* bitpos */
1488          complain_overflow_dont, /* complain_on_overflow */
1489          ppc64_elf_unhandled_reloc, /* special_function */
1490          "R_PPC64_DTPREL16_LO_DS", /* name */
1491          FALSE,                 /* partial_inplace */
1492          0,                     /* src_mask */
1493          0xfffc,                /* dst_mask */
1494          FALSE),                /* pcrel_offset */
1495
1496   /* Computes a tp-relative displacement, the difference between the value of
1497      sym+add and the value of the thread pointer (r13).  */
1498   HOWTO (R_PPC64_TPREL64,
1499          0,                     /* rightshift */
1500          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1501          64,                    /* bitsize */
1502          FALSE,                 /* pc_relative */
1503          0,                     /* bitpos */
1504          complain_overflow_dont, /* complain_on_overflow */
1505          ppc64_elf_unhandled_reloc, /* special_function */
1506          "R_PPC64_TPREL64",     /* name */
1507          FALSE,                 /* partial_inplace */
1508          0,                     /* src_mask */
1509          ONES (64),             /* dst_mask */
1510          FALSE),                /* pcrel_offset */
1511
1512   /* A 16 bit tprel reloc.  */
1513   HOWTO (R_PPC64_TPREL16,
1514          0,                     /* rightshift */
1515          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1516          16,                    /* bitsize */
1517          FALSE,                 /* pc_relative */
1518          0,                     /* bitpos */
1519          complain_overflow_signed, /* complain_on_overflow */
1520          ppc64_elf_unhandled_reloc, /* special_function */
1521          "R_PPC64_TPREL16",     /* name */
1522          FALSE,                 /* partial_inplace */
1523          0,                     /* src_mask */
1524          0xffff,                /* dst_mask */
1525          FALSE),                /* pcrel_offset */
1526
1527   /* Like TPREL16, but no overflow.  */
1528   HOWTO (R_PPC64_TPREL16_LO,
1529          0,                     /* rightshift */
1530          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1531          16,                    /* bitsize */
1532          FALSE,                 /* pc_relative */
1533          0,                     /* bitpos */
1534          complain_overflow_dont, /* complain_on_overflow */
1535          ppc64_elf_unhandled_reloc, /* special_function */
1536          "R_PPC64_TPREL16_LO",  /* name */
1537          FALSE,                 /* partial_inplace */
1538          0,                     /* src_mask */
1539          0xffff,                /* dst_mask */
1540          FALSE),                /* pcrel_offset */
1541
1542   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1543   HOWTO (R_PPC64_TPREL16_HI,
1544          16,                    /* rightshift */
1545          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1546          16,                    /* bitsize */
1547          FALSE,                 /* pc_relative */
1548          0,                     /* bitpos */
1549          complain_overflow_signed, /* complain_on_overflow */
1550          ppc64_elf_unhandled_reloc, /* special_function */
1551          "R_PPC64_TPREL16_HI",  /* name */
1552          FALSE,                 /* partial_inplace */
1553          0,                     /* src_mask */
1554          0xffff,                /* dst_mask */
1555          FALSE),                /* pcrel_offset */
1556
1557   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1558   HOWTO (R_PPC64_TPREL16_HA,
1559          16,                    /* rightshift */
1560          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1561          16,                    /* bitsize */
1562          FALSE,                 /* pc_relative */
1563          0,                     /* bitpos */
1564          complain_overflow_signed, /* complain_on_overflow */
1565          ppc64_elf_unhandled_reloc, /* special_function */
1566          "R_PPC64_TPREL16_HA",  /* name */
1567          FALSE,                 /* partial_inplace */
1568          0,                     /* src_mask */
1569          0xffff,                /* dst_mask */
1570          FALSE),                /* pcrel_offset */
1571
1572   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1573   HOWTO (R_PPC64_TPREL16_HIGHER,
1574          32,                    /* rightshift */
1575          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1576          16,                    /* bitsize */
1577          FALSE,                 /* pc_relative */
1578          0,                     /* bitpos */
1579          complain_overflow_dont, /* complain_on_overflow */
1580          ppc64_elf_unhandled_reloc, /* special_function */
1581          "R_PPC64_TPREL16_HIGHER",      /* name */
1582          FALSE,                 /* partial_inplace */
1583          0,                     /* src_mask */
1584          0xffff,                /* dst_mask */
1585          FALSE),                /* pcrel_offset */
1586
1587   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1588   HOWTO (R_PPC64_TPREL16_HIGHERA,
1589          32,                    /* rightshift */
1590          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1591          16,                    /* bitsize */
1592          FALSE,                 /* pc_relative */
1593          0,                     /* bitpos */
1594          complain_overflow_dont, /* complain_on_overflow */
1595          ppc64_elf_unhandled_reloc, /* special_function */
1596          "R_PPC64_TPREL16_HIGHERA", /* name */
1597          FALSE,                 /* partial_inplace */
1598          0,                     /* src_mask */
1599          0xffff,                /* dst_mask */
1600          FALSE),                /* pcrel_offset */
1601
1602   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1603   HOWTO (R_PPC64_TPREL16_HIGHEST,
1604          48,                    /* rightshift */
1605          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1606          16,                    /* bitsize */
1607          FALSE,                 /* pc_relative */
1608          0,                     /* bitpos */
1609          complain_overflow_dont, /* complain_on_overflow */
1610          ppc64_elf_unhandled_reloc, /* special_function */
1611          "R_PPC64_TPREL16_HIGHEST", /* name */
1612          FALSE,                 /* partial_inplace */
1613          0,                     /* src_mask */
1614          0xffff,                /* dst_mask */
1615          FALSE),                /* pcrel_offset */
1616
1617   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1618   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1619          48,                    /* rightshift */
1620          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1621          16,                    /* bitsize */
1622          FALSE,                 /* pc_relative */
1623          0,                     /* bitpos */
1624          complain_overflow_dont, /* complain_on_overflow */
1625          ppc64_elf_unhandled_reloc, /* special_function */
1626          "R_PPC64_TPREL16_HIGHESTA", /* name */
1627          FALSE,                 /* partial_inplace */
1628          0,                     /* src_mask */
1629          0xffff,                /* dst_mask */
1630          FALSE),                /* pcrel_offset */
1631
1632   /* Like TPREL16, but for insns with a DS field.  */
1633   HOWTO (R_PPC64_TPREL16_DS,
1634          0,                     /* rightshift */
1635          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1636          16,                    /* bitsize */
1637          FALSE,                 /* pc_relative */
1638          0,                     /* bitpos */
1639          complain_overflow_signed, /* complain_on_overflow */
1640          ppc64_elf_unhandled_reloc, /* special_function */
1641          "R_PPC64_TPREL16_DS",  /* name */
1642          FALSE,                 /* partial_inplace */
1643          0,                     /* src_mask */
1644          0xfffc,                /* dst_mask */
1645          FALSE),                /* pcrel_offset */
1646
1647   /* Like TPREL16_DS, but no overflow.  */
1648   HOWTO (R_PPC64_TPREL16_LO_DS,
1649          0,                     /* rightshift */
1650          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1651          16,                    /* bitsize */
1652          FALSE,                 /* pc_relative */
1653          0,                     /* bitpos */
1654          complain_overflow_dont, /* complain_on_overflow */
1655          ppc64_elf_unhandled_reloc, /* special_function */
1656          "R_PPC64_TPREL16_LO_DS", /* name */
1657          FALSE,                 /* partial_inplace */
1658          0,                     /* src_mask */
1659          0xfffc,                /* dst_mask */
1660          FALSE),                /* pcrel_offset */
1661
1662   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1663      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1664      to the first entry relative to the TOC base (r2).  */
1665   HOWTO (R_PPC64_GOT_TLSGD16,
1666          0,                     /* rightshift */
1667          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1668          16,                    /* bitsize */
1669          FALSE,                 /* pc_relative */
1670          0,                     /* bitpos */
1671          complain_overflow_signed, /* complain_on_overflow */
1672          ppc64_elf_unhandled_reloc, /* special_function */
1673          "R_PPC64_GOT_TLSGD16", /* name */
1674          FALSE,                 /* partial_inplace */
1675          0,                     /* src_mask */
1676          0xffff,                /* dst_mask */
1677          FALSE),                /* pcrel_offset */
1678
1679   /* Like GOT_TLSGD16, but no overflow.  */
1680   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1681          0,                     /* rightshift */
1682          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1683          16,                    /* bitsize */
1684          FALSE,                 /* pc_relative */
1685          0,                     /* bitpos */
1686          complain_overflow_dont, /* complain_on_overflow */
1687          ppc64_elf_unhandled_reloc, /* special_function */
1688          "R_PPC64_GOT_TLSGD16_LO", /* name */
1689          FALSE,                 /* partial_inplace */
1690          0,                     /* src_mask */
1691          0xffff,                /* dst_mask */
1692          FALSE),                /* pcrel_offset */
1693
1694   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1695   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1696          16,                    /* rightshift */
1697          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1698          16,                    /* bitsize */
1699          FALSE,                 /* pc_relative */
1700          0,                     /* bitpos */
1701          complain_overflow_signed, /* complain_on_overflow */
1702          ppc64_elf_unhandled_reloc, /* special_function */
1703          "R_PPC64_GOT_TLSGD16_HI", /* name */
1704          FALSE,                 /* partial_inplace */
1705          0,                     /* src_mask */
1706          0xffff,                /* dst_mask */
1707          FALSE),                /* pcrel_offset */
1708
1709   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1710   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1711          16,                    /* rightshift */
1712          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1713          16,                    /* bitsize */
1714          FALSE,                 /* pc_relative */
1715          0,                     /* bitpos */
1716          complain_overflow_signed, /* complain_on_overflow */
1717          ppc64_elf_unhandled_reloc, /* special_function */
1718          "R_PPC64_GOT_TLSGD16_HA", /* name */
1719          FALSE,                 /* partial_inplace */
1720          0,                     /* src_mask */
1721          0xffff,                /* dst_mask */
1722          FALSE),                /* pcrel_offset */
1723
1724   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1725      with values (sym+add)@dtpmod and zero, and computes the offset to the
1726      first entry relative to the TOC base (r2).  */
1727   HOWTO (R_PPC64_GOT_TLSLD16,
1728          0,                     /* rightshift */
1729          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1730          16,                    /* bitsize */
1731          FALSE,                 /* pc_relative */
1732          0,                     /* bitpos */
1733          complain_overflow_signed, /* complain_on_overflow */
1734          ppc64_elf_unhandled_reloc, /* special_function */
1735          "R_PPC64_GOT_TLSLD16", /* name */
1736          FALSE,                 /* partial_inplace */
1737          0,                     /* src_mask */
1738          0xffff,                /* dst_mask */
1739          FALSE),                /* pcrel_offset */
1740
1741   /* Like GOT_TLSLD16, but no overflow.  */
1742   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1743          0,                     /* rightshift */
1744          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1745          16,                    /* bitsize */
1746          FALSE,                 /* pc_relative */
1747          0,                     /* bitpos */
1748          complain_overflow_dont, /* complain_on_overflow */
1749          ppc64_elf_unhandled_reloc, /* special_function */
1750          "R_PPC64_GOT_TLSLD16_LO", /* name */
1751          FALSE,                 /* partial_inplace */
1752          0,                     /* src_mask */
1753          0xffff,                /* dst_mask */
1754          FALSE),                /* pcrel_offset */
1755
1756   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1757   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1758          16,                    /* rightshift */
1759          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1760          16,                    /* bitsize */
1761          FALSE,                 /* pc_relative */
1762          0,                     /* bitpos */
1763          complain_overflow_signed, /* complain_on_overflow */
1764          ppc64_elf_unhandled_reloc, /* special_function */
1765          "R_PPC64_GOT_TLSLD16_HI", /* name */
1766          FALSE,                 /* partial_inplace */
1767          0,                     /* src_mask */
1768          0xffff,                /* dst_mask */
1769          FALSE),                /* pcrel_offset */
1770
1771   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1772   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1773          16,                    /* rightshift */
1774          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1775          16,                    /* bitsize */
1776          FALSE,                 /* pc_relative */
1777          0,                     /* bitpos */
1778          complain_overflow_signed, /* complain_on_overflow */
1779          ppc64_elf_unhandled_reloc, /* special_function */
1780          "R_PPC64_GOT_TLSLD16_HA", /* name */
1781          FALSE,                 /* partial_inplace */
1782          0,                     /* src_mask */
1783          0xffff,                /* dst_mask */
1784          FALSE),                /* pcrel_offset */
1785
1786   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1787      the offset to the entry relative to the TOC base (r2).  */
1788   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1789          0,                     /* rightshift */
1790          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1791          16,                    /* bitsize */
1792          FALSE,                 /* pc_relative */
1793          0,                     /* bitpos */
1794          complain_overflow_signed, /* complain_on_overflow */
1795          ppc64_elf_unhandled_reloc, /* special_function */
1796          "R_PPC64_GOT_DTPREL16_DS", /* name */
1797          FALSE,                 /* partial_inplace */
1798          0,                     /* src_mask */
1799          0xfffc,                /* dst_mask */
1800          FALSE),                /* pcrel_offset */
1801
1802   /* Like GOT_DTPREL16_DS, but no overflow.  */
1803   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1804          0,                     /* rightshift */
1805          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1806          16,                    /* bitsize */
1807          FALSE,                 /* pc_relative */
1808          0,                     /* bitpos */
1809          complain_overflow_dont, /* complain_on_overflow */
1810          ppc64_elf_unhandled_reloc, /* special_function */
1811          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1812          FALSE,                 /* partial_inplace */
1813          0,                     /* src_mask */
1814          0xfffc,                /* dst_mask */
1815          FALSE),                /* pcrel_offset */
1816
1817   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1818   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1819          16,                    /* rightshift */
1820          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1821          16,                    /* bitsize */
1822          FALSE,                 /* pc_relative */
1823          0,                     /* bitpos */
1824          complain_overflow_signed, /* complain_on_overflow */
1825          ppc64_elf_unhandled_reloc, /* special_function */
1826          "R_PPC64_GOT_DTPREL16_HI", /* name */
1827          FALSE,                 /* partial_inplace */
1828          0,                     /* src_mask */
1829          0xffff,                /* dst_mask */
1830          FALSE),                /* pcrel_offset */
1831
1832   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1833   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1834          16,                    /* rightshift */
1835          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1836          16,                    /* bitsize */
1837          FALSE,                 /* pc_relative */
1838          0,                     /* bitpos */
1839          complain_overflow_signed, /* complain_on_overflow */
1840          ppc64_elf_unhandled_reloc, /* special_function */
1841          "R_PPC64_GOT_DTPREL16_HA", /* name */
1842          FALSE,                 /* partial_inplace */
1843          0,                     /* src_mask */
1844          0xffff,                /* dst_mask */
1845          FALSE),                /* pcrel_offset */
1846
1847   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1848      offset to the entry relative to the TOC base (r2).  */
1849   HOWTO (R_PPC64_GOT_TPREL16_DS,
1850          0,                     /* rightshift */
1851          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1852          16,                    /* bitsize */
1853          FALSE,                 /* pc_relative */
1854          0,                     /* bitpos */
1855          complain_overflow_signed, /* complain_on_overflow */
1856          ppc64_elf_unhandled_reloc, /* special_function */
1857          "R_PPC64_GOT_TPREL16_DS", /* name */
1858          FALSE,                 /* partial_inplace */
1859          0,                     /* src_mask */
1860          0xfffc,                /* dst_mask */
1861          FALSE),                /* pcrel_offset */
1862
1863   /* Like GOT_TPREL16_DS, but no overflow.  */
1864   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1865          0,                     /* rightshift */
1866          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1867          16,                    /* bitsize */
1868          FALSE,                 /* pc_relative */
1869          0,                     /* bitpos */
1870          complain_overflow_dont, /* complain_on_overflow */
1871          ppc64_elf_unhandled_reloc, /* special_function */
1872          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1873          FALSE,                 /* partial_inplace */
1874          0,                     /* src_mask */
1875          0xfffc,                /* dst_mask */
1876          FALSE),                /* pcrel_offset */
1877
1878   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1879   HOWTO (R_PPC64_GOT_TPREL16_HI,
1880          16,                    /* rightshift */
1881          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1882          16,                    /* bitsize */
1883          FALSE,                 /* pc_relative */
1884          0,                     /* bitpos */
1885          complain_overflow_signed, /* complain_on_overflow */
1886          ppc64_elf_unhandled_reloc, /* special_function */
1887          "R_PPC64_GOT_TPREL16_HI", /* name */
1888          FALSE,                 /* partial_inplace */
1889          0,                     /* src_mask */
1890          0xffff,                /* dst_mask */
1891          FALSE),                /* pcrel_offset */
1892
1893   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1894   HOWTO (R_PPC64_GOT_TPREL16_HA,
1895          16,                    /* rightshift */
1896          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1897          16,                    /* bitsize */
1898          FALSE,                 /* pc_relative */
1899          0,                     /* bitpos */
1900          complain_overflow_signed, /* complain_on_overflow */
1901          ppc64_elf_unhandled_reloc, /* special_function */
1902          "R_PPC64_GOT_TPREL16_HA", /* name */
1903          FALSE,                 /* partial_inplace */
1904          0,                     /* src_mask */
1905          0xffff,                /* dst_mask */
1906          FALSE),                /* pcrel_offset */
1907
1908   HOWTO (R_PPC64_JMP_IREL,      /* type */
1909          0,                     /* rightshift */
1910          0,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1911          0,                     /* bitsize */
1912          FALSE,                 /* pc_relative */
1913          0,                     /* bitpos */
1914          complain_overflow_dont, /* complain_on_overflow */
1915          ppc64_elf_unhandled_reloc, /* special_function */
1916          "R_PPC64_JMP_IREL",    /* name */
1917          FALSE,                 /* partial_inplace */
1918          0,                     /* src_mask */
1919          0,                     /* dst_mask */
1920          FALSE),                /* pcrel_offset */
1921
1922   HOWTO (R_PPC64_IRELATIVE,     /* type */
1923          0,                     /* rightshift */
1924          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1925          64,                    /* bitsize */
1926          FALSE,                 /* pc_relative */
1927          0,                     /* bitpos */
1928          complain_overflow_dont, /* complain_on_overflow */
1929          bfd_elf_generic_reloc, /* special_function */
1930          "R_PPC64_IRELATIVE",   /* name */
1931          FALSE,                 /* partial_inplace */
1932          0,                     /* src_mask */
1933          ONES (64),             /* dst_mask */
1934          FALSE),                /* pcrel_offset */
1935
1936   /* A 16 bit relative relocation.  */
1937   HOWTO (R_PPC64_REL16,         /* type */
1938          0,                     /* rightshift */
1939          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1940          16,                    /* bitsize */
1941          TRUE,                  /* pc_relative */
1942          0,                     /* bitpos */
1943          complain_overflow_bitfield, /* complain_on_overflow */
1944          bfd_elf_generic_reloc, /* special_function */
1945          "R_PPC64_REL16",       /* name */
1946          FALSE,                 /* partial_inplace */
1947          0,                     /* src_mask */
1948          0xffff,                /* dst_mask */
1949          TRUE),                 /* pcrel_offset */
1950
1951   /* A 16 bit relative relocation without overflow.  */
1952   HOWTO (R_PPC64_REL16_LO,      /* type */
1953          0,                     /* rightshift */
1954          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1955          16,                    /* bitsize */
1956          TRUE,                  /* pc_relative */
1957          0,                     /* bitpos */
1958          complain_overflow_dont,/* complain_on_overflow */
1959          bfd_elf_generic_reloc, /* special_function */
1960          "R_PPC64_REL16_LO",    /* name */
1961          FALSE,                 /* partial_inplace */
1962          0,                     /* src_mask */
1963          0xffff,                /* dst_mask */
1964          TRUE),                 /* pcrel_offset */
1965
1966   /* The high order 16 bits of a relative address.  */
1967   HOWTO (R_PPC64_REL16_HI,      /* type */
1968          16,                    /* rightshift */
1969          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1970          16,                    /* bitsize */
1971          TRUE,                  /* pc_relative */
1972          0,                     /* bitpos */
1973          complain_overflow_signed, /* complain_on_overflow */
1974          bfd_elf_generic_reloc, /* special_function */
1975          "R_PPC64_REL16_HI",    /* name */
1976          FALSE,                 /* partial_inplace */
1977          0,                     /* src_mask */
1978          0xffff,                /* dst_mask */
1979          TRUE),                 /* pcrel_offset */
1980
1981   /* The high order 16 bits of a relative address, plus 1 if the contents of
1982      the low 16 bits, treated as a signed number, is negative.  */
1983   HOWTO (R_PPC64_REL16_HA,      /* type */
1984          16,                    /* rightshift */
1985          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1986          16,                    /* bitsize */
1987          TRUE,                  /* pc_relative */
1988          0,                     /* bitpos */
1989          complain_overflow_signed, /* complain_on_overflow */
1990          ppc64_elf_ha_reloc,    /* special_function */
1991          "R_PPC64_REL16_HA",    /* name */
1992          FALSE,                 /* partial_inplace */
1993          0,                     /* src_mask */
1994          0xffff,                /* dst_mask */
1995          TRUE),                 /* pcrel_offset */
1996
1997   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
1998   HOWTO (R_PPC64_ADDR16_HIGH,   /* type */
1999          16,                    /* rightshift */
2000          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2001          16,                    /* bitsize */
2002          FALSE,                 /* pc_relative */
2003          0,                     /* bitpos */
2004          complain_overflow_dont, /* complain_on_overflow */
2005          bfd_elf_generic_reloc, /* special_function */
2006          "R_PPC64_ADDR16_HIGH", /* name */
2007          FALSE,                 /* partial_inplace */
2008          0,                     /* src_mask */
2009          0xffff,                /* dst_mask */
2010          FALSE),                /* pcrel_offset */
2011
2012   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
2013   HOWTO (R_PPC64_ADDR16_HIGHA,  /* type */
2014          16,                    /* rightshift */
2015          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2016          16,                    /* bitsize */
2017          FALSE,                 /* pc_relative */
2018          0,                     /* bitpos */
2019          complain_overflow_dont, /* complain_on_overflow */
2020          ppc64_elf_ha_reloc,    /* special_function */
2021          "R_PPC64_ADDR16_HIGHA",        /* name */
2022          FALSE,                 /* partial_inplace */
2023          0,                     /* src_mask */
2024          0xffff,                /* dst_mask */
2025          FALSE),                /* pcrel_offset */
2026
2027   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
2028   HOWTO (R_PPC64_DTPREL16_HIGH,
2029          16,                    /* rightshift */
2030          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2031          16,                    /* bitsize */
2032          FALSE,                 /* pc_relative */
2033          0,                     /* bitpos */
2034          complain_overflow_dont, /* complain_on_overflow */
2035          ppc64_elf_unhandled_reloc, /* special_function */
2036          "R_PPC64_DTPREL16_HIGH", /* name */
2037          FALSE,                 /* partial_inplace */
2038          0,                     /* src_mask */
2039          0xffff,                /* dst_mask */
2040          FALSE),                /* pcrel_offset */
2041
2042   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
2043   HOWTO (R_PPC64_DTPREL16_HIGHA,
2044          16,                    /* rightshift */
2045          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2046          16,                    /* bitsize */
2047          FALSE,                 /* pc_relative */
2048          0,                     /* bitpos */
2049          complain_overflow_dont, /* complain_on_overflow */
2050          ppc64_elf_unhandled_reloc, /* special_function */
2051          "R_PPC64_DTPREL16_HIGHA", /* name */
2052          FALSE,                 /* partial_inplace */
2053          0,                     /* src_mask */
2054          0xffff,                /* dst_mask */
2055          FALSE),                /* pcrel_offset */
2056
2057   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
2058   HOWTO (R_PPC64_TPREL16_HIGH,
2059          16,                    /* rightshift */
2060          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2061          16,                    /* bitsize */
2062          FALSE,                 /* pc_relative */
2063          0,                     /* bitpos */
2064          complain_overflow_dont, /* complain_on_overflow */
2065          ppc64_elf_unhandled_reloc, /* special_function */
2066          "R_PPC64_TPREL16_HIGH",        /* name */
2067          FALSE,                 /* partial_inplace */
2068          0,                     /* src_mask */
2069          0xffff,                /* dst_mask */
2070          FALSE),                /* pcrel_offset */
2071
2072   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
2073   HOWTO (R_PPC64_TPREL16_HIGHA,
2074          16,                    /* rightshift */
2075          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2076          16,                    /* bitsize */
2077          FALSE,                 /* pc_relative */
2078          0,                     /* bitpos */
2079          complain_overflow_dont, /* complain_on_overflow */
2080          ppc64_elf_unhandled_reloc, /* special_function */
2081          "R_PPC64_TPREL16_HIGHA",       /* name */
2082          FALSE,                 /* partial_inplace */
2083          0,                     /* src_mask */
2084          0xffff,                /* dst_mask */
2085          FALSE),                /* pcrel_offset */
2086
2087   /* GNU extension to record C++ vtable hierarchy.  */
2088   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2089          0,                     /* rightshift */
2090          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2091          0,                     /* bitsize */
2092          FALSE,                 /* pc_relative */
2093          0,                     /* bitpos */
2094          complain_overflow_dont, /* complain_on_overflow */
2095          NULL,                  /* special_function */
2096          "R_PPC64_GNU_VTINHERIT", /* name */
2097          FALSE,                 /* partial_inplace */
2098          0,                     /* src_mask */
2099          0,                     /* dst_mask */
2100          FALSE),                /* pcrel_offset */
2101
2102   /* GNU extension to record C++ vtable member usage.  */
2103   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
2104          0,                     /* rightshift */
2105          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2106          0,                     /* bitsize */
2107          FALSE,                 /* pc_relative */
2108          0,                     /* bitpos */
2109          complain_overflow_dont, /* complain_on_overflow */
2110          NULL,                  /* special_function */
2111          "R_PPC64_GNU_VTENTRY", /* name */
2112          FALSE,                 /* partial_inplace */
2113          0,                     /* src_mask */
2114          0,                     /* dst_mask */
2115          FALSE),                /* pcrel_offset */
2116 };
2117
2118 \f
2119 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2120    be done.  */
2121
2122 static void
2123 ppc_howto_init (void)
2124 {
2125   unsigned int i, type;
2126
2127   for (i = 0;
2128        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2129        i++)
2130     {
2131       type = ppc64_elf_howto_raw[i].type;
2132       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2133                           / sizeof (ppc64_elf_howto_table[0])));
2134       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2135     }
2136 }
2137
2138 static reloc_howto_type *
2139 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2140                              bfd_reloc_code_real_type code)
2141 {
2142   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2143
2144   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2145     /* Initialize howto table if needed.  */
2146     ppc_howto_init ();
2147
2148   switch (code)
2149     {
2150     default:
2151       return NULL;
2152
2153     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
2154       break;
2155     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
2156       break;
2157     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
2158       break;
2159     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
2160       break;
2161     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
2162       break;
2163     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
2164       break;
2165     case BFD_RELOC_PPC64_ADDR16_HIGH:           r = R_PPC64_ADDR16_HIGH;
2166       break;
2167     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
2168       break;
2169     case BFD_RELOC_PPC64_ADDR16_HIGHA:          r = R_PPC64_ADDR16_HIGHA;
2170       break;
2171     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
2172       break;
2173     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
2174       break;
2175     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
2176       break;
2177     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
2178       break;
2179     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
2180       break;
2181     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
2182       break;
2183     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
2184       break;
2185     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
2186       break;
2187     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
2188       break;
2189     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
2190       break;
2191     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
2192       break;
2193     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
2194       break;
2195     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
2196       break;
2197     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
2198       break;
2199     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
2200       break;
2201     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
2202       break;
2203     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
2204       break;
2205     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
2206       break;
2207     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
2208       break;
2209     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
2210       break;
2211     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
2212       break;
2213     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
2214       break;
2215     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
2216       break;
2217     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
2218       break;
2219     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
2220       break;
2221     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
2222       break;
2223     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
2224       break;
2225     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
2226       break;
2227     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
2228       break;
2229     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
2230       break;
2231     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
2232       break;
2233     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
2234       break;
2235     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
2236       break;
2237     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
2238       break;
2239     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
2240       break;
2241     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
2242       break;
2243     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
2244       break;
2245     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
2246       break;
2247     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
2248       break;
2249     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
2250       break;
2251     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
2252       break;
2253     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
2254       break;
2255     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
2256       break;
2257     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
2258       break;
2259     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
2260       break;
2261     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
2262       break;
2263     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
2264       break;
2265     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2266       break;
2267     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2268       break;
2269     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2270       break;
2271     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2272       break;
2273     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2274       break;
2275     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2276       break;
2277     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
2278       break;
2279     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
2280       break;
2281     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2282       break;
2283     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2284       break;
2285     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2286       break;
2287     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2288       break;
2289     case BFD_RELOC_PPC64_TPREL16_HIGH:          r = R_PPC64_TPREL16_HIGH;
2290       break;
2291     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2292       break;
2293     case BFD_RELOC_PPC64_TPREL16_HIGHA:         r = R_PPC64_TPREL16_HIGHA;
2294       break;
2295     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2296       break;
2297     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2298       break;
2299     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2300       break;
2301     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2302       break;
2303     case BFD_RELOC_PPC64_DTPREL16_HIGH:         r = R_PPC64_DTPREL16_HIGH;
2304       break;
2305     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2306       break;
2307     case BFD_RELOC_PPC64_DTPREL16_HIGHA:        r = R_PPC64_DTPREL16_HIGHA;
2308       break;
2309     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2310       break;
2311     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2312       break;
2313     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2314       break;
2315     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2316       break;
2317     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2318       break;
2319     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2320       break;
2321     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2322       break;
2323     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2324       break;
2325     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2326       break;
2327     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2328       break;
2329     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2330       break;
2331     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2332       break;
2333     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2334       break;
2335     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2336       break;
2337     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2338       break;
2339     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2340       break;
2341     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2342       break;
2343     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2344       break;
2345     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2346       break;
2347     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2348       break;
2349     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2350       break;
2351     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2352       break;
2353     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2354       break;
2355     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2356       break;
2357     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2358       break;
2359     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2360       break;
2361     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2362       break;
2363     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2364       break;
2365     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2366       break;
2367     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
2368       break;
2369     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
2370       break;
2371     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
2372       break;
2373     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
2374       break;
2375     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2376       break;
2377     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2378       break;
2379     }
2380
2381   return ppc64_elf_howto_table[r];
2382 };
2383
2384 static reloc_howto_type *
2385 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2386                              const char *r_name)
2387 {
2388   unsigned int i;
2389
2390   for (i = 0;
2391        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2392        i++)
2393     if (ppc64_elf_howto_raw[i].name != NULL
2394         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2395       return &ppc64_elf_howto_raw[i];
2396
2397   return NULL;
2398 }
2399
2400 /* Set the howto pointer for a PowerPC ELF reloc.  */
2401
2402 static void
2403 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2404                          Elf_Internal_Rela *dst)
2405 {
2406   unsigned int type;
2407
2408   /* Initialize howto table if needed.  */
2409   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2410     ppc_howto_init ();
2411
2412   type = ELF64_R_TYPE (dst->r_info);
2413   if (type >= (sizeof (ppc64_elf_howto_table)
2414                / sizeof (ppc64_elf_howto_table[0])))
2415     {
2416       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2417                              abfd, (int) type);
2418       type = R_PPC64_NONE;
2419     }
2420   cache_ptr->howto = ppc64_elf_howto_table[type];
2421 }
2422
2423 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2424
2425 static bfd_reloc_status_type
2426 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2427                     void *data, asection *input_section,
2428                     bfd *output_bfd, char **error_message)
2429 {
2430   /* If this is a relocatable link (output_bfd test tells us), just
2431      call the generic function.  Any adjustment will be done at final
2432      link time.  */
2433   if (output_bfd != NULL)
2434     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2435                                   input_section, output_bfd, error_message);
2436
2437   /* Adjust the addend for sign extension of the low 16 bits.
2438      We won't actually be using the low 16 bits, so trashing them
2439      doesn't matter.  */
2440   reloc_entry->addend += 0x8000;
2441   return bfd_reloc_continue;
2442 }
2443
2444 static bfd_reloc_status_type
2445 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2446                         void *data, asection *input_section,
2447                         bfd *output_bfd, char **error_message)
2448 {
2449   if (output_bfd != NULL)
2450     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2451                                   input_section, output_bfd, error_message);
2452
2453   if (strcmp (symbol->section->name, ".opd") == 0
2454       && (symbol->section->owner->flags & DYNAMIC) == 0)
2455     {
2456       bfd_vma dest = opd_entry_value (symbol->section,
2457                                       symbol->value + reloc_entry->addend,
2458                                       NULL, NULL, FALSE);
2459       if (dest != (bfd_vma) -1)
2460         reloc_entry->addend = dest - (symbol->value
2461                                       + symbol->section->output_section->vma
2462                                       + symbol->section->output_offset);
2463     }
2464   return bfd_reloc_continue;
2465 }
2466
2467 static bfd_reloc_status_type
2468 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2469                          void *data, asection *input_section,
2470                          bfd *output_bfd, char **error_message)
2471 {
2472   long insn;
2473   enum elf_ppc64_reloc_type r_type;
2474   bfd_size_type octets;
2475   /* Assume 'at' branch hints.  */
2476   bfd_boolean is_isa_v2 = TRUE;
2477
2478   /* If this is a relocatable link (output_bfd test tells us), just
2479      call the generic function.  Any adjustment will be done at final
2480      link time.  */
2481   if (output_bfd != NULL)
2482     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2483                                   input_section, output_bfd, error_message);
2484
2485   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2486   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2487   insn &= ~(0x01 << 21);
2488   r_type = reloc_entry->howto->type;
2489   if (r_type == R_PPC64_ADDR14_BRTAKEN
2490       || r_type == R_PPC64_REL14_BRTAKEN)
2491     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2492
2493   if (is_isa_v2)
2494     {
2495       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2496          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2497          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2498       if ((insn & (0x14 << 21)) == (0x04 << 21))
2499         insn |= 0x02 << 21;
2500       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2501         insn |= 0x08 << 21;
2502       else
2503         goto out;
2504     }
2505   else
2506     {
2507       bfd_vma target = 0;
2508       bfd_vma from;
2509
2510       if (!bfd_is_com_section (symbol->section))
2511         target = symbol->value;
2512       target += symbol->section->output_section->vma;
2513       target += symbol->section->output_offset;
2514       target += reloc_entry->addend;
2515
2516       from = (reloc_entry->address
2517               + input_section->output_offset
2518               + input_section->output_section->vma);
2519
2520       /* Invert 'y' bit if not the default.  */
2521       if ((bfd_signed_vma) (target - from) < 0)
2522         insn ^= 0x01 << 21;
2523     }
2524   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2525  out:
2526   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2527                                  input_section, output_bfd, error_message);
2528 }
2529
2530 static bfd_reloc_status_type
2531 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2532                          void *data, asection *input_section,
2533                          bfd *output_bfd, char **error_message)
2534 {
2535   /* If this is a relocatable link (output_bfd test tells us), just
2536      call the generic function.  Any adjustment will be done at final
2537      link time.  */
2538   if (output_bfd != NULL)
2539     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2540                                   input_section, output_bfd, error_message);
2541
2542   /* Subtract the symbol section base address.  */
2543   reloc_entry->addend -= symbol->section->output_section->vma;
2544   return bfd_reloc_continue;
2545 }
2546
2547 static bfd_reloc_status_type
2548 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2549                             void *data, asection *input_section,
2550                             bfd *output_bfd, char **error_message)
2551 {
2552   /* If this is a relocatable link (output_bfd test tells us), just
2553      call the generic function.  Any adjustment will be done at final
2554      link time.  */
2555   if (output_bfd != NULL)
2556     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2557                                   input_section, output_bfd, error_message);
2558
2559   /* Subtract the symbol section base address.  */
2560   reloc_entry->addend -= symbol->section->output_section->vma;
2561
2562   /* Adjust the addend for sign extension of the low 16 bits.  */
2563   reloc_entry->addend += 0x8000;
2564   return bfd_reloc_continue;
2565 }
2566
2567 static bfd_reloc_status_type
2568 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2569                      void *data, asection *input_section,
2570                      bfd *output_bfd, char **error_message)
2571 {
2572   bfd_vma TOCstart;
2573
2574   /* If this is a relocatable link (output_bfd test tells us), just
2575      call the generic function.  Any adjustment will be done at final
2576      link time.  */
2577   if (output_bfd != NULL)
2578     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2579                                   input_section, output_bfd, error_message);
2580
2581   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2582   if (TOCstart == 0)
2583     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2584
2585   /* Subtract the TOC base address.  */
2586   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2587   return bfd_reloc_continue;
2588 }
2589
2590 static bfd_reloc_status_type
2591 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2592                         void *data, asection *input_section,
2593                         bfd *output_bfd, char **error_message)
2594 {
2595   bfd_vma TOCstart;
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   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2605   if (TOCstart == 0)
2606     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2607
2608   /* Subtract the TOC base address.  */
2609   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2610
2611   /* Adjust the addend for sign extension of the low 16 bits.  */
2612   reloc_entry->addend += 0x8000;
2613   return bfd_reloc_continue;
2614 }
2615
2616 static bfd_reloc_status_type
2617 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2618                        void *data, asection *input_section,
2619                        bfd *output_bfd, char **error_message)
2620 {
2621   bfd_vma TOCstart;
2622   bfd_size_type octets;
2623
2624   /* If this is a relocatable link (output_bfd test tells us), just
2625      call the generic function.  Any adjustment will be done at final
2626      link time.  */
2627   if (output_bfd != NULL)
2628     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2629                                   input_section, output_bfd, error_message);
2630
2631   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2632   if (TOCstart == 0)
2633     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2634
2635   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2636   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2637   return bfd_reloc_ok;
2638 }
2639
2640 static bfd_reloc_status_type
2641 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2642                            void *data, asection *input_section,
2643                            bfd *output_bfd, char **error_message)
2644 {
2645   /* If this is a relocatable link (output_bfd test tells us), just
2646      call the generic function.  Any adjustment will be done at final
2647      link time.  */
2648   if (output_bfd != NULL)
2649     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2650                                   input_section, output_bfd, error_message);
2651
2652   if (error_message != NULL)
2653     {
2654       static char buf[60];
2655       sprintf (buf, "generic linker can't handle %s",
2656                reloc_entry->howto->name);
2657       *error_message = buf;
2658     }
2659   return bfd_reloc_dangerous;
2660 }
2661
2662 /* Track GOT entries needed for a given symbol.  We might need more
2663    than one got entry per symbol.  */
2664 struct got_entry
2665 {
2666   struct got_entry *next;
2667
2668   /* The symbol addend that we'll be placing in the GOT.  */
2669   bfd_vma addend;
2670
2671   /* Unlike other ELF targets, we use separate GOT entries for the same
2672      symbol referenced from different input files.  This is to support
2673      automatic multiple TOC/GOT sections, where the TOC base can vary
2674      from one input file to another.  After partitioning into TOC groups
2675      we merge entries within the group.
2676
2677      Point to the BFD owning this GOT entry.  */
2678   bfd *owner;
2679
2680   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2681      TLS_TPREL or TLS_DTPREL for tls entries.  */
2682   unsigned char tls_type;
2683
2684   /* Non-zero if got.ent points to real entry.  */
2685   unsigned char is_indirect;
2686
2687   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2688   union
2689     {
2690       bfd_signed_vma refcount;
2691       bfd_vma offset;
2692       struct got_entry *ent;
2693     } got;
2694 };
2695
2696 /* The same for PLT.  */
2697 struct plt_entry
2698 {
2699   struct plt_entry *next;
2700
2701   bfd_vma addend;
2702
2703   union
2704     {
2705       bfd_signed_vma refcount;
2706       bfd_vma offset;
2707     } plt;
2708 };
2709
2710 struct ppc64_elf_obj_tdata
2711 {
2712   struct elf_obj_tdata elf;
2713
2714   /* Shortcuts to dynamic linker sections.  */
2715   asection *got;
2716   asection *relgot;
2717
2718   /* Used during garbage collection.  We attach global symbols defined
2719      on removed .opd entries to this section so that the sym is removed.  */
2720   asection *deleted_section;
2721
2722   /* TLS local dynamic got entry handling.  Support for multiple GOT
2723      sections means we potentially need one of these for each input bfd.  */
2724   struct got_entry tlsld_got;
2725
2726   union {
2727     /* A copy of relocs before they are modified for --emit-relocs.  */
2728     Elf_Internal_Rela *relocs;
2729
2730     /* Section contents.  */
2731     bfd_byte *contents;
2732   } opd;
2733
2734   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2735      the reloc to be in the range -32768 to 32767.  */
2736   unsigned int has_small_toc_reloc : 1;
2737
2738   /* Set if toc/got ha relocs detected not using r2, or lo reloc
2739      instruction not one we handle.  */
2740   unsigned int unexpected_toc_insn : 1;
2741 };
2742
2743 #define ppc64_elf_tdata(bfd) \
2744   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2745
2746 #define ppc64_tlsld_got(bfd) \
2747   (&ppc64_elf_tdata (bfd)->tlsld_got)
2748
2749 #define is_ppc64_elf(bfd) \
2750   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2751    && elf_object_id (bfd) == PPC64_ELF_DATA)
2752
2753 /* Override the generic function because we store some extras.  */
2754
2755 static bfd_boolean
2756 ppc64_elf_mkobject (bfd *abfd)
2757 {
2758   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2759                                   PPC64_ELF_DATA);
2760 }
2761
2762 /* Fix bad default arch selected for a 64 bit input bfd when the
2763    default is 32 bit.  */
2764
2765 static bfd_boolean
2766 ppc64_elf_object_p (bfd *abfd)
2767 {
2768   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2769     {
2770       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2771
2772       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2773         {
2774           /* Relies on arch after 32 bit default being 64 bit default.  */
2775           abfd->arch_info = abfd->arch_info->next;
2776           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2777         }
2778     }
2779   return TRUE;
2780 }
2781
2782 /* Support for core dump NOTE sections.  */
2783
2784 static bfd_boolean
2785 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2786 {
2787   size_t offset, size;
2788
2789   if (note->descsz != 504)
2790     return FALSE;
2791
2792   /* pr_cursig */
2793   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2794
2795   /* pr_pid */
2796   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2797
2798   /* pr_reg */
2799   offset = 112;
2800   size = 384;
2801
2802   /* Make a ".reg/999" section.  */
2803   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2804                                           size, note->descpos + offset);
2805 }
2806
2807 static bfd_boolean
2808 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2809 {
2810   if (note->descsz != 136)
2811     return FALSE;
2812
2813   elf_tdata (abfd)->core->pid
2814     = bfd_get_32 (abfd, note->descdata + 24);
2815   elf_tdata (abfd)->core->program
2816     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2817   elf_tdata (abfd)->core->command
2818     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2819
2820   return TRUE;
2821 }
2822
2823 static char *
2824 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2825                            ...)
2826 {
2827   switch (note_type)
2828     {
2829     default:
2830       return NULL;
2831
2832     case NT_PRPSINFO:
2833       {
2834         char data[136];
2835         va_list ap;
2836
2837         va_start (ap, note_type);
2838         memset (data, 0, sizeof (data));
2839         strncpy (data + 40, va_arg (ap, const char *), 16);
2840         strncpy (data + 56, va_arg (ap, const char *), 80);
2841         va_end (ap);
2842         return elfcore_write_note (abfd, buf, bufsiz,
2843                                    "CORE", note_type, data, sizeof (data));
2844       }
2845
2846     case NT_PRSTATUS:
2847       {
2848         char data[504];
2849         va_list ap;
2850         long pid;
2851         int cursig;
2852         const void *greg;
2853
2854         va_start (ap, note_type);
2855         memset (data, 0, 112);
2856         pid = va_arg (ap, long);
2857         bfd_put_32 (abfd, pid, data + 32);
2858         cursig = va_arg (ap, int);
2859         bfd_put_16 (abfd, cursig, data + 12);
2860         greg = va_arg (ap, const void *);
2861         memcpy (data + 112, greg, 384);
2862         memset (data + 496, 0, 8);
2863         va_end (ap);
2864         return elfcore_write_note (abfd, buf, bufsiz,
2865                                    "CORE", note_type, data, sizeof (data));
2866       }
2867     }
2868 }
2869
2870 /* Add extra PPC sections.  */
2871
2872 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2873 {
2874   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
2875   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2876   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2877   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2878   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2879   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2880   { NULL,                     0,  0, 0,            0 }
2881 };
2882
2883 enum _ppc64_sec_type {
2884   sec_normal = 0,
2885   sec_opd = 1,
2886   sec_toc = 2
2887 };
2888
2889 struct _ppc64_elf_section_data
2890 {
2891   struct bfd_elf_section_data elf;
2892
2893   union
2894   {
2895     /* An array with one entry for each opd function descriptor.  */
2896     struct _opd_sec_data
2897     {
2898       /* Points to the function code section for local opd entries.  */
2899       asection **func_sec;
2900
2901       /* After editing .opd, adjust references to opd local syms.  */
2902       long *adjust;
2903     } opd;
2904
2905     /* An array for toc sections, indexed by offset/8.  */
2906     struct _toc_sec_data
2907     {
2908       /* Specifies the relocation symbol index used at a given toc offset.  */
2909       unsigned *symndx;
2910
2911       /* And the relocation addend.  */
2912       bfd_vma *add;
2913     } toc;
2914   } u;
2915
2916   enum _ppc64_sec_type sec_type:2;
2917
2918   /* Flag set when small branches are detected.  Used to
2919      select suitable defaults for the stub group size.  */
2920   unsigned int has_14bit_branch:1;
2921 };
2922
2923 #define ppc64_elf_section_data(sec) \
2924   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2925
2926 static bfd_boolean
2927 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2928 {
2929   if (!sec->used_by_bfd)
2930     {
2931       struct _ppc64_elf_section_data *sdata;
2932       bfd_size_type amt = sizeof (*sdata);
2933
2934       sdata = bfd_zalloc (abfd, amt);
2935       if (sdata == NULL)
2936         return FALSE;
2937       sec->used_by_bfd = sdata;
2938     }
2939
2940   return _bfd_elf_new_section_hook (abfd, sec);
2941 }
2942
2943 static struct _opd_sec_data *
2944 get_opd_info (asection * sec)
2945 {
2946   if (sec != NULL
2947       && ppc64_elf_section_data (sec) != NULL
2948       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2949     return &ppc64_elf_section_data (sec)->u.opd;
2950   return NULL;
2951 }
2952
2953 static inline int
2954 abiversion (bfd *abfd)
2955 {
2956   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
2957 }
2958
2959 static inline void
2960 set_abiversion (bfd *abfd, int ver)
2961 {
2962   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
2963   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
2964 }
2965 \f
2966 /* Parameters for the qsort hook.  */
2967 static bfd_boolean synthetic_relocatable;
2968
2969 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
2970
2971 static int
2972 compare_symbols (const void *ap, const void *bp)
2973 {
2974   const asymbol *a = * (const asymbol **) ap;
2975   const asymbol *b = * (const asymbol **) bp;
2976
2977   /* Section symbols first.  */
2978   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2979     return -1;
2980   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2981     return 1;
2982
2983   /* then .opd symbols.  */
2984   if (strcmp (a->section->name, ".opd") == 0
2985       && strcmp (b->section->name, ".opd") != 0)
2986     return -1;
2987   if (strcmp (a->section->name, ".opd") != 0
2988       && strcmp (b->section->name, ".opd") == 0)
2989     return 1;
2990
2991   /* then other code symbols.  */
2992   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2993       == (SEC_CODE | SEC_ALLOC)
2994       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2995          != (SEC_CODE | SEC_ALLOC))
2996     return -1;
2997
2998   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2999       != (SEC_CODE | SEC_ALLOC)
3000       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3001          == (SEC_CODE | SEC_ALLOC))
3002     return 1;
3003
3004   if (synthetic_relocatable)
3005     {
3006       if (a->section->id < b->section->id)
3007         return -1;
3008
3009       if (a->section->id > b->section->id)
3010         return 1;
3011     }
3012
3013   if (a->value + a->section->vma < b->value + b->section->vma)
3014     return -1;
3015
3016   if (a->value + a->section->vma > b->value + b->section->vma)
3017     return 1;
3018
3019   /* For syms with the same value, prefer strong dynamic global function
3020      syms over other syms.  */
3021   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3022     return -1;
3023
3024   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3025     return 1;
3026
3027   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3028     return -1;
3029
3030   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3031     return 1;
3032
3033   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3034     return -1;
3035
3036   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3037     return 1;
3038
3039   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3040     return -1;
3041
3042   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3043     return 1;
3044
3045   return 0;
3046 }
3047
3048 /* Search SYMS for a symbol of the given VALUE.  */
3049
3050 static asymbol *
3051 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
3052 {
3053   long mid;
3054
3055   if (id == -1)
3056     {
3057       while (lo < hi)
3058         {
3059           mid = (lo + hi) >> 1;
3060           if (syms[mid]->value + syms[mid]->section->vma < value)
3061             lo = mid + 1;
3062           else if (syms[mid]->value + syms[mid]->section->vma > value)
3063             hi = mid;
3064           else
3065             return syms[mid];
3066         }
3067     }
3068   else
3069     {
3070       while (lo < hi)
3071         {
3072           mid = (lo + hi) >> 1;
3073           if (syms[mid]->section->id < id)
3074             lo = mid + 1;
3075           else if (syms[mid]->section->id > id)
3076             hi = mid;
3077           else if (syms[mid]->value < value)
3078             lo = mid + 1;
3079           else if (syms[mid]->value > value)
3080             hi = mid;
3081           else
3082             return syms[mid];
3083         }
3084     }
3085   return NULL;
3086 }
3087
3088 static bfd_boolean
3089 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3090 {
3091   bfd_vma vma = *(bfd_vma *) ptr;
3092   return ((section->flags & SEC_ALLOC) != 0
3093           && section->vma <= vma
3094           && vma < section->vma + section->size);
3095 }
3096
3097 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3098    entry syms.  Also generate @plt symbols for the glink branch table.  */
3099
3100 static long
3101 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3102                                 long static_count, asymbol **static_syms,
3103                                 long dyn_count, asymbol **dyn_syms,
3104                                 asymbol **ret)
3105 {
3106   asymbol *s;
3107   long i;
3108   long count;
3109   char *names;
3110   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3111   asection *opd = NULL;
3112   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3113   asymbol **syms;
3114   int abi = abiversion (abfd);
3115
3116   *ret = NULL;
3117
3118   if (abi < 2)
3119     {
3120       opd = bfd_get_section_by_name (abfd, ".opd");
3121       if (opd == NULL && abi == 1)
3122         return 0;
3123     }
3124
3125   symcount = static_count;
3126   if (!relocatable)
3127     symcount += dyn_count;
3128   if (symcount == 0)
3129     return 0;
3130
3131   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3132   if (syms == NULL)
3133     return -1;
3134
3135   if (!relocatable && static_count != 0 && dyn_count != 0)
3136     {
3137       /* Use both symbol tables.  */
3138       memcpy (syms, static_syms, static_count * sizeof (*syms));
3139       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3140     }
3141   else if (!relocatable && static_count == 0)
3142     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3143   else
3144     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3145
3146   synthetic_relocatable = relocatable;
3147   qsort (syms, symcount, sizeof (*syms), compare_symbols);
3148
3149   if (!relocatable && symcount > 1)
3150     {
3151       long j;
3152       /* Trim duplicate syms, since we may have merged the normal and
3153          dynamic symbols.  Actually, we only care about syms that have
3154          different values, so trim any with the same value.  */
3155       for (i = 1, j = 1; i < symcount; ++i)
3156         if (syms[i - 1]->value + syms[i - 1]->section->vma
3157             != syms[i]->value + syms[i]->section->vma)
3158           syms[j++] = syms[i];
3159       symcount = j;
3160     }
3161
3162   i = 0;
3163   if (strcmp (syms[i]->section->name, ".opd") == 0)
3164     ++i;
3165   codesecsym = i;
3166
3167   for (; i < symcount; ++i)
3168     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3169          != (SEC_CODE | SEC_ALLOC))
3170         || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3171       break;
3172   codesecsymend = i;
3173
3174   for (; i < symcount; ++i)
3175     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3176       break;
3177   secsymend = i;
3178
3179   for (; i < symcount; ++i)
3180     if (strcmp (syms[i]->section->name, ".opd") != 0)
3181       break;
3182   opdsymend = i;
3183
3184   for (; i < symcount; ++i)
3185     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3186         != (SEC_CODE | SEC_ALLOC))
3187       break;
3188   symcount = i;
3189
3190   count = 0;
3191
3192   if (relocatable)
3193     {
3194       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3195       arelent *r;
3196       size_t size;
3197       long relcount;
3198
3199       if (opdsymend == secsymend)
3200         goto done;
3201
3202       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3203       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3204       if (relcount == 0)
3205         goto done;
3206
3207       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3208         {
3209           count = -1;
3210           goto done;
3211         }
3212
3213       size = 0;
3214       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3215         {
3216           asymbol *sym;
3217
3218           while (r < opd->relocation + relcount
3219                  && r->address < syms[i]->value + opd->vma)
3220             ++r;
3221
3222           if (r == opd->relocation + relcount)
3223             break;
3224
3225           if (r->address != syms[i]->value + opd->vma)
3226             continue;
3227
3228           if (r->howto->type != R_PPC64_ADDR64)
3229             continue;
3230
3231           sym = *r->sym_ptr_ptr;
3232           if (!sym_exists_at (syms, opdsymend, symcount,
3233                               sym->section->id, sym->value + r->addend))
3234             {
3235               ++count;
3236               size += sizeof (asymbol);
3237               size += strlen (syms[i]->name) + 2;
3238             }
3239         }
3240
3241       s = *ret = bfd_malloc (size);
3242       if (s == NULL)
3243         {
3244           count = -1;
3245           goto done;
3246         }
3247
3248       names = (char *) (s + count);
3249
3250       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3251         {
3252           asymbol *sym;
3253
3254           while (r < opd->relocation + relcount
3255                  && r->address < syms[i]->value + opd->vma)
3256             ++r;
3257
3258           if (r == opd->relocation + relcount)
3259             break;
3260
3261           if (r->address != syms[i]->value + opd->vma)
3262             continue;
3263
3264           if (r->howto->type != R_PPC64_ADDR64)
3265             continue;
3266
3267           sym = *r->sym_ptr_ptr;
3268           if (!sym_exists_at (syms, opdsymend, symcount,
3269                               sym->section->id, sym->value + r->addend))
3270             {
3271               size_t len;
3272
3273               *s = *syms[i];
3274               s->flags |= BSF_SYNTHETIC;
3275               s->section = sym->section;
3276               s->value = sym->value + r->addend;
3277               s->name = names;
3278               *names++ = '.';
3279               len = strlen (syms[i]->name);
3280               memcpy (names, syms[i]->name, len + 1);
3281               names += len + 1;
3282               /* Have udata.p point back to the original symbol this
3283                  synthetic symbol was derived from.  */
3284               s->udata.p = syms[i];
3285               s++;
3286             }
3287         }
3288     }
3289   else
3290     {
3291       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3292       bfd_byte *contents = NULL;
3293       size_t size;
3294       long plt_count = 0;
3295       bfd_vma glink_vma = 0, resolv_vma = 0;
3296       asection *dynamic, *glink = NULL, *relplt = NULL;
3297       arelent *p;
3298
3299       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3300         {
3301         free_contents_and_exit:
3302           if (contents)
3303             free (contents);
3304           count = -1;
3305           goto done;
3306         }
3307
3308       size = 0;
3309       for (i = secsymend; i < opdsymend; ++i)
3310         {
3311           bfd_vma ent;
3312
3313           /* Ignore bogus symbols.  */
3314           if (syms[i]->value > opd->size - 8)
3315             continue;
3316
3317           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3318           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3319             {
3320               ++count;
3321               size += sizeof (asymbol);
3322               size += strlen (syms[i]->name) + 2;
3323             }
3324         }
3325
3326       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
3327       if (dyn_count != 0
3328           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3329         {
3330           bfd_byte *dynbuf, *extdyn, *extdynend;
3331           size_t extdynsize;
3332           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3333
3334           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3335             goto free_contents_and_exit;
3336
3337           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3338           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3339
3340           extdyn = dynbuf;
3341           extdynend = extdyn + dynamic->size;
3342           for (; extdyn < extdynend; extdyn += extdynsize)
3343             {
3344               Elf_Internal_Dyn dyn;
3345               (*swap_dyn_in) (abfd, extdyn, &dyn);
3346
3347               if (dyn.d_tag == DT_NULL)
3348                 break;
3349
3350               if (dyn.d_tag == DT_PPC64_GLINK)
3351                 {
3352                   /* The first glink stub starts at offset 32; see
3353                      comment in ppc64_elf_finish_dynamic_sections. */
3354                   glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
3355                   /* The .glink section usually does not survive the final
3356                      link; search for the section (usually .text) where the
3357                      glink stubs now reside.  */
3358                   glink = bfd_sections_find_if (abfd, section_covers_vma,
3359                                                 &glink_vma);
3360                   break;
3361                 }
3362             }
3363
3364           free (dynbuf);
3365         }
3366
3367       if (glink != NULL)
3368         {
3369           /* Determine __glink trampoline by reading the relative branch
3370              from the first glink stub.  */
3371           bfd_byte buf[4];
3372           unsigned int off = 0;
3373
3374           while (bfd_get_section_contents (abfd, glink, buf,
3375                                            glink_vma + off - glink->vma, 4))
3376             {
3377               unsigned int insn = bfd_get_32 (abfd, buf);
3378               insn ^= B_DOT;
3379               if ((insn & ~0x3fffffc) == 0)
3380                 {
3381                   resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3382                   break;
3383                 }
3384               off += 4;
3385               if (off > 4)
3386                 break;
3387             }
3388
3389           if (resolv_vma)
3390             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3391
3392           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3393           if (relplt != NULL)
3394             {
3395               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3396               if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3397                 goto free_contents_and_exit;
3398
3399               plt_count = relplt->size / sizeof (Elf64_External_Rela);
3400               size += plt_count * sizeof (asymbol);
3401
3402               p = relplt->relocation;
3403               for (i = 0; i < plt_count; i++, p++)
3404                 {
3405                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3406                   if (p->addend != 0)
3407                     size += sizeof ("+0x") - 1 + 16;
3408                 }
3409             }
3410         }
3411
3412       s = *ret = bfd_malloc (size);
3413       if (s == NULL)
3414         goto free_contents_and_exit;
3415
3416       names = (char *) (s + count + plt_count + (resolv_vma != 0));
3417
3418       for (i = secsymend; i < opdsymend; ++i)
3419         {
3420           bfd_vma ent;
3421
3422           if (syms[i]->value > opd->size - 8)
3423             continue;
3424
3425           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3426           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3427             {
3428               long lo, hi;
3429               size_t len;
3430               asection *sec = abfd->sections;
3431
3432               *s = *syms[i];
3433               lo = codesecsym;
3434               hi = codesecsymend;
3435               while (lo < hi)
3436                 {
3437                   long mid = (lo + hi) >> 1;
3438                   if (syms[mid]->section->vma < ent)
3439                     lo = mid + 1;
3440                   else if (syms[mid]->section->vma > ent)
3441                     hi = mid;
3442                   else
3443                     {
3444                       sec = syms[mid]->section;
3445                       break;
3446                     }
3447                 }
3448
3449               if (lo >= hi && lo > codesecsym)
3450                 sec = syms[lo - 1]->section;
3451
3452               for (; sec != NULL; sec = sec->next)
3453                 {
3454                   if (sec->vma > ent)
3455                     break;
3456                   /* SEC_LOAD may not be set if SEC is from a separate debug
3457                      info file.  */
3458                   if ((sec->flags & SEC_ALLOC) == 0)
3459                     break;
3460                   if ((sec->flags & SEC_CODE) != 0)
3461                     s->section = sec;
3462                 }
3463               s->flags |= BSF_SYNTHETIC;
3464               s->value = ent - s->section->vma;
3465               s->name = names;
3466               *names++ = '.';
3467               len = strlen (syms[i]->name);
3468               memcpy (names, syms[i]->name, len + 1);
3469               names += len + 1;
3470               /* Have udata.p point back to the original symbol this
3471                  synthetic symbol was derived from.  */
3472               s->udata.p = syms[i];
3473               s++;
3474             }
3475         }
3476       free (contents);
3477
3478       if (glink != NULL && relplt != NULL)
3479         {
3480           if (resolv_vma)
3481             {
3482               /* Add a symbol for the main glink trampoline.  */
3483               memset (s, 0, sizeof *s);
3484               s->the_bfd = abfd;
3485               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3486               s->section = glink;
3487               s->value = resolv_vma - glink->vma;
3488               s->name = names;
3489               memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3490               names += sizeof ("__glink_PLTresolve");
3491               s++;
3492               count++;
3493             }
3494
3495           /* FIXME: It would be very much nicer to put sym@plt on the
3496              stub rather than on the glink branch table entry.  The
3497              objdump disassembler would then use a sensible symbol
3498              name on plt calls.  The difficulty in doing so is
3499              a) finding the stubs, and,
3500              b) matching stubs against plt entries, and,
3501              c) there can be multiple stubs for a given plt entry.
3502
3503              Solving (a) could be done by code scanning, but older
3504              ppc64 binaries used different stubs to current code.
3505              (b) is the tricky one since you need to known the toc
3506              pointer for at least one function that uses a pic stub to
3507              be able to calculate the plt address referenced.
3508              (c) means gdb would need to set multiple breakpoints (or
3509              find the glink branch itself) when setting breakpoints
3510              for pending shared library loads.  */
3511           p = relplt->relocation;
3512           for (i = 0; i < plt_count; i++, p++)
3513             {
3514               size_t len;
3515
3516               *s = **p->sym_ptr_ptr;
3517               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
3518                  we are defining a symbol, ensure one of them is set.  */
3519               if ((s->flags & BSF_LOCAL) == 0)
3520                 s->flags |= BSF_GLOBAL;
3521               s->flags |= BSF_SYNTHETIC;
3522               s->section = glink;
3523               s->value = glink_vma - glink->vma;
3524               s->name = names;
3525               s->udata.p = NULL;
3526               len = strlen ((*p->sym_ptr_ptr)->name);
3527               memcpy (names, (*p->sym_ptr_ptr)->name, len);
3528               names += len;
3529               if (p->addend != 0)
3530                 {
3531                   memcpy (names, "+0x", sizeof ("+0x") - 1);
3532                   names += sizeof ("+0x") - 1;
3533                   bfd_sprintf_vma (abfd, names, p->addend);
3534                   names += strlen (names);
3535                 }
3536               memcpy (names, "@plt", sizeof ("@plt"));
3537               names += sizeof ("@plt");
3538               s++;
3539               if (abi < 2)
3540                 {
3541                   glink_vma += 8;
3542                   if (i >= 0x8000)
3543                     glink_vma += 4;
3544                 }
3545               else
3546                 glink_vma += 4;
3547             }
3548           count += plt_count;
3549         }
3550     }
3551
3552  done:
3553   free (syms);
3554   return count;
3555 }
3556 \f
3557 /* The following functions are specific to the ELF linker, while
3558    functions above are used generally.  Those named ppc64_elf_* are
3559    called by the main ELF linker code.  They appear in this file more
3560    or less in the order in which they are called.  eg.
3561    ppc64_elf_check_relocs is called early in the link process,
3562    ppc64_elf_finish_dynamic_sections is one of the last functions
3563    called.
3564
3565    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3566    functions have both a function code symbol and a function descriptor
3567    symbol.  A call to foo in a relocatable object file looks like:
3568
3569    .            .text
3570    .    x:
3571    .            bl      .foo
3572    .            nop
3573
3574    The function definition in another object file might be:
3575
3576    .            .section .opd
3577    .    foo:    .quad   .foo
3578    .            .quad   .TOC.@tocbase
3579    .            .quad   0
3580    .
3581    .            .text
3582    .    .foo:   blr
3583
3584    When the linker resolves the call during a static link, the branch
3585    unsurprisingly just goes to .foo and the .opd information is unused.
3586    If the function definition is in a shared library, things are a little
3587    different:  The call goes via a plt call stub, the opd information gets
3588    copied to the plt, and the linker patches the nop.
3589
3590    .    x:
3591    .            bl      .foo_stub
3592    .            ld      2,40(1)
3593    .
3594    .
3595    .    .foo_stub:
3596    .            std     2,40(1)                 # in practice, the call stub
3597    .            addis   11,2,Lfoo@toc@ha        # is slightly optimized, but
3598    .            addi    11,11,Lfoo@toc@l        # this is the general idea
3599    .            ld      12,0(11)
3600    .            ld      2,8(11)
3601    .            mtctr   12
3602    .            ld      11,16(11)
3603    .            bctr
3604    .
3605    .            .section .plt
3606    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
3607
3608    The "reloc ()" notation is supposed to indicate that the linker emits
3609    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3610    copying.
3611
3612    What are the difficulties here?  Well, firstly, the relocations
3613    examined by the linker in check_relocs are against the function code
3614    sym .foo, while the dynamic relocation in the plt is emitted against
3615    the function descriptor symbol, foo.  Somewhere along the line, we need
3616    to carefully copy dynamic link information from one symbol to the other.
3617    Secondly, the generic part of the elf linker will make .foo a dynamic
3618    symbol as is normal for most other backends.  We need foo dynamic
3619    instead, at least for an application final link.  However, when
3620    creating a shared library containing foo, we need to have both symbols
3621    dynamic so that references to .foo are satisfied during the early
3622    stages of linking.  Otherwise the linker might decide to pull in a
3623    definition from some other object, eg. a static library.
3624
3625    Update: As of August 2004, we support a new convention.  Function
3626    calls may use the function descriptor symbol, ie. "bl foo".  This
3627    behaves exactly as "bl .foo".  */
3628
3629 /* Of those relocs that might be copied as dynamic relocs, this function
3630    selects those that must be copied when linking a shared library,
3631    even when the symbol is local.  */
3632
3633 static int
3634 must_be_dyn_reloc (struct bfd_link_info *info,
3635                    enum elf_ppc64_reloc_type r_type)
3636 {
3637   switch (r_type)
3638     {
3639     default:
3640       return 1;
3641
3642     case R_PPC64_REL32:
3643     case R_PPC64_REL64:
3644     case R_PPC64_REL30:
3645       return 0;
3646
3647     case R_PPC64_TPREL16:
3648     case R_PPC64_TPREL16_LO:
3649     case R_PPC64_TPREL16_HI:
3650     case R_PPC64_TPREL16_HA:
3651     case R_PPC64_TPREL16_DS:
3652     case R_PPC64_TPREL16_LO_DS:
3653     case R_PPC64_TPREL16_HIGH:
3654     case R_PPC64_TPREL16_HIGHA:
3655     case R_PPC64_TPREL16_HIGHER:
3656     case R_PPC64_TPREL16_HIGHERA:
3657     case R_PPC64_TPREL16_HIGHEST:
3658     case R_PPC64_TPREL16_HIGHESTA:
3659     case R_PPC64_TPREL64:
3660       return !info->executable;
3661     }
3662 }
3663
3664 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3665    copying dynamic variables from a shared lib into an app's dynbss
3666    section, and instead use a dynamic relocation to point into the
3667    shared lib.  With code that gcc generates, it's vital that this be
3668    enabled;  In the PowerPC64 ABI, the address of a function is actually
3669    the address of a function descriptor, which resides in the .opd
3670    section.  gcc uses the descriptor directly rather than going via the
3671    GOT as some other ABI's do, which means that initialized function
3672    pointers must reference the descriptor.  Thus, a function pointer
3673    initialized to the address of a function in a shared library will
3674    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3675    redefines the function descriptor symbol to point to the copy.  This
3676    presents a problem as a plt entry for that function is also
3677    initialized from the function descriptor symbol and the copy reloc
3678    may not be initialized first.  */
3679 #define ELIMINATE_COPY_RELOCS 1
3680
3681 /* Section name for stubs is the associated section name plus this
3682    string.  */
3683 #define STUB_SUFFIX ".stub"
3684
3685 /* Linker stubs.
3686    ppc_stub_long_branch:
3687    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3688    destination, but a 24 bit branch in a stub section will reach.
3689    .    b       dest
3690
3691    ppc_stub_plt_branch:
3692    Similar to the above, but a 24 bit branch in the stub section won't
3693    reach its destination.
3694    .    addis   %r11,%r2,xxx@toc@ha
3695    .    ld      %r12,xxx@toc@l(%r11)
3696    .    mtctr   %r12
3697    .    bctr
3698
3699    ppc_stub_plt_call:
3700    Used to call a function in a shared library.  If it so happens that
3701    the plt entry referenced crosses a 64k boundary, then an extra
3702    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3703    .    std     %r2,40(%r1)
3704    .    addis   %r11,%r2,xxx@toc@ha
3705    .    ld      %r12,xxx+0@toc@l(%r11)
3706    .    mtctr   %r12
3707    .    ld      %r2,xxx+8@toc@l(%r11)
3708    .    ld      %r11,xxx+16@toc@l(%r11)
3709    .    bctr
3710
3711    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3712    code to adjust the value and save r2 to support multiple toc sections.
3713    A ppc_stub_long_branch with an r2 offset looks like:
3714    .    std     %r2,40(%r1)
3715    .    addis   %r2,%r2,off@ha
3716    .    addi    %r2,%r2,off@l
3717    .    b       dest
3718
3719    A ppc_stub_plt_branch with an r2 offset looks like:
3720    .    std     %r2,40(%r1)
3721    .    addis   %r11,%r2,xxx@toc@ha
3722    .    ld      %r12,xxx@toc@l(%r11)
3723    .    addis   %r2,%r2,off@ha
3724    .    addi    %r2,%r2,off@l
3725    .    mtctr   %r12
3726    .    bctr
3727
3728    In cases where the "addis" instruction would add zero, the "addis" is
3729    omitted and following instructions modified slightly in some cases.
3730 */
3731
3732 enum ppc_stub_type {
3733   ppc_stub_none,
3734   ppc_stub_long_branch,
3735   ppc_stub_long_branch_r2off,
3736   ppc_stub_plt_branch,
3737   ppc_stub_plt_branch_r2off,
3738   ppc_stub_plt_call,
3739   ppc_stub_plt_call_r2save
3740 };
3741
3742 struct ppc_stub_hash_entry {
3743
3744   /* Base hash table entry structure.  */
3745   struct bfd_hash_entry root;
3746
3747   enum ppc_stub_type stub_type;
3748
3749   /* The stub section.  */
3750   asection *stub_sec;
3751
3752   /* Offset within stub_sec of the beginning of this stub.  */
3753   bfd_vma stub_offset;
3754
3755   /* Given the symbol's value and its section we can determine its final
3756      value when building the stubs (so the stub knows where to jump.  */
3757   bfd_vma target_value;
3758   asection *target_section;
3759
3760   /* The symbol table entry, if any, that this was derived from.  */
3761   struct ppc_link_hash_entry *h;
3762   struct plt_entry *plt_ent;
3763
3764   /* Where this stub is being called from, or, in the case of combined
3765      stub sections, the first input section in the group.  */
3766   asection *id_sec;
3767
3768   /* Symbol st_other.  */
3769   unsigned char other;
3770 };
3771
3772 struct ppc_branch_hash_entry {
3773
3774   /* Base hash table entry structure.  */
3775   struct bfd_hash_entry root;
3776
3777   /* Offset within branch lookup table.  */
3778   unsigned int offset;
3779
3780   /* Generation marker.  */
3781   unsigned int iter;
3782 };
3783
3784 /* Used to track dynamic relocations for local symbols.  */
3785 struct ppc_dyn_relocs
3786 {
3787   struct ppc_dyn_relocs *next;
3788
3789   /* The input section of the reloc.  */
3790   asection *sec;
3791
3792   /* Total number of relocs copied for the input section.  */
3793   unsigned int count : 31;
3794
3795   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
3796   unsigned int ifunc : 1;
3797 };
3798
3799 struct ppc_link_hash_entry
3800 {
3801   struct elf_link_hash_entry elf;
3802
3803   union {
3804     /* A pointer to the most recently used stub hash entry against this
3805        symbol.  */
3806     struct ppc_stub_hash_entry *stub_cache;
3807
3808     /* A pointer to the next symbol starting with a '.'  */
3809     struct ppc_link_hash_entry *next_dot_sym;
3810   } u;
3811
3812   /* Track dynamic relocs copied for this symbol.  */
3813   struct elf_dyn_relocs *dyn_relocs;
3814
3815   /* Link between function code and descriptor symbols.  */
3816   struct ppc_link_hash_entry *oh;
3817
3818   /* Flag function code and descriptor symbols.  */
3819   unsigned int is_func:1;
3820   unsigned int is_func_descriptor:1;
3821   unsigned int fake:1;
3822
3823   /* Whether global opd/toc sym has been adjusted or not.
3824      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3825      should be set for all globals defined in any opd/toc section.  */
3826   unsigned int adjust_done:1;
3827
3828   /* Set if we twiddled this symbol to weak at some stage.  */
3829   unsigned int was_undefined:1;
3830
3831   /* Contexts in which symbol is used in the GOT (or TOC).
3832      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3833      corresponding relocs are encountered during check_relocs.
3834      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3835      indicate the corresponding GOT entry type is not needed.
3836      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3837      a TPREL one.  We use a separate flag rather than setting TPREL
3838      just for convenience in distinguishing the two cases.  */
3839 #define TLS_GD           1      /* GD reloc. */
3840 #define TLS_LD           2      /* LD reloc. */
3841 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
3842 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
3843 #define TLS_TLS         16      /* Any TLS reloc.  */
3844 #define TLS_EXPLICIT    32      /* Marks TOC section TLS relocs. */
3845 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
3846 #define PLT_IFUNC      128      /* STT_GNU_IFUNC.  */
3847   unsigned char tls_mask;
3848 };
3849
3850 /* ppc64 ELF linker hash table.  */
3851
3852 struct ppc_link_hash_table
3853 {
3854   struct elf_link_hash_table elf;
3855
3856   /* The stub hash table.  */
3857   struct bfd_hash_table stub_hash_table;
3858
3859   /* Another hash table for plt_branch stubs.  */
3860   struct bfd_hash_table branch_hash_table;
3861
3862   /* Hash table for function prologue tocsave.  */
3863   htab_t tocsave_htab;
3864
3865   /* Linker stub bfd.  */
3866   bfd *stub_bfd;
3867
3868   /* Linker call-backs.  */
3869   asection * (*add_stub_section) (const char *, asection *);
3870   void (*layout_sections_again) (void);
3871
3872   /* Array to keep track of which stub sections have been created, and
3873      information on stub grouping.  */
3874   struct map_stub {
3875     /* This is the section to which stubs in the group will be attached.  */
3876     asection *link_sec;
3877     /* The stub section.  */
3878     asection *stub_sec;
3879     /* Along with elf_gp, specifies the TOC pointer used in this group.  */
3880     bfd_vma toc_off;
3881   } *stub_group;
3882
3883   /* Temp used when calculating TOC pointers.  */
3884   bfd_vma toc_curr;
3885   bfd *toc_bfd;
3886   asection *toc_first_sec;
3887
3888   /* Highest input section id.  */
3889   int top_id;
3890
3891   /* Highest output section index.  */
3892   int top_index;
3893
3894   /* Used when adding symbols.  */
3895   struct ppc_link_hash_entry *dot_syms;
3896
3897   /* List of input sections for each output section.  */
3898   asection **input_list;
3899
3900   /* Short-cuts to get to dynamic linker sections.  */
3901   asection *got;
3902   asection *plt;
3903   asection *relplt;
3904   asection *iplt;
3905   asection *reliplt;
3906   asection *dynbss;
3907   asection *relbss;
3908   asection *glink;
3909   asection *sfpr;
3910   asection *brlt;
3911   asection *relbrlt;
3912   asection *glink_eh_frame;
3913
3914   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
3915   struct ppc_link_hash_entry *tls_get_addr;
3916   struct ppc_link_hash_entry *tls_get_addr_fd;
3917
3918   /* The size of reliplt used by got entry relocs.  */
3919   bfd_size_type got_reli_size;
3920
3921   /* Statistics.  */
3922   unsigned long stub_count[ppc_stub_plt_call_r2save];
3923
3924   /* Number of stubs against global syms.  */
3925   unsigned long stub_globals;
3926
3927   /* Alignment of PLT call stubs.  */
3928   unsigned int plt_stub_align:4;
3929
3930   /* Set if we're linking code with function descriptors.  */
3931   unsigned int opd_abi:1;
3932
3933   /* Set if PLT call stubs should load r11.  */
3934   unsigned int plt_static_chain:1;
3935
3936   /* Set if PLT call stubs need a read-read barrier.  */
3937   unsigned int plt_thread_safe:1;
3938
3939   /* Set if we should emit symbols for stubs.  */
3940   unsigned int emit_stub_syms:1;
3941
3942   /* Set if __tls_get_addr optimization should not be done.  */
3943   unsigned int no_tls_get_addr_opt:1;
3944
3945   /* Support for multiple toc sections.  */
3946   unsigned int do_multi_toc:1;
3947   unsigned int multi_toc_needed:1;
3948   unsigned int second_toc_pass:1;
3949   unsigned int do_toc_opt:1;
3950
3951   /* Set on error.  */
3952   unsigned int stub_error:1;
3953
3954   /* Temp used by ppc64_elf_process_dot_syms.  */
3955   unsigned int twiddled_syms:1;
3956
3957   /* Incremented every time we size stubs.  */
3958   unsigned int stub_iteration;
3959
3960   /* Small local sym cache.  */
3961   struct sym_cache sym_cache;
3962 };
3963
3964 /* Rename some of the generic section flags to better document how they
3965    are used here.  */
3966
3967 /* Nonzero if this section has TLS related relocations.  */
3968 #define has_tls_reloc sec_flg0
3969
3970 /* Nonzero if this section has a call to __tls_get_addr.  */
3971 #define has_tls_get_addr_call sec_flg1
3972
3973 /* Nonzero if this section has any toc or got relocs.  */
3974 #define has_toc_reloc sec_flg2
3975
3976 /* Nonzero if this section has a call to another section that uses
3977    the toc or got.  */
3978 #define makes_toc_func_call sec_flg3
3979
3980 /* Recursion protection when determining above flag.  */
3981 #define call_check_in_progress sec_flg4
3982 #define call_check_done sec_flg5
3983
3984 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
3985
3986 #define ppc_hash_table(p) \
3987   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3988   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3989
3990 #define ppc_stub_hash_lookup(table, string, create, copy) \
3991   ((struct ppc_stub_hash_entry *) \
3992    bfd_hash_lookup ((table), (string), (create), (copy)))
3993
3994 #define ppc_branch_hash_lookup(table, string, create, copy) \
3995   ((struct ppc_branch_hash_entry *) \
3996    bfd_hash_lookup ((table), (string), (create), (copy)))
3997
3998 /* Create an entry in the stub hash table.  */
3999
4000 static struct bfd_hash_entry *
4001 stub_hash_newfunc (struct bfd_hash_entry *entry,
4002                    struct bfd_hash_table *table,
4003                    const char *string)
4004 {
4005   /* Allocate the structure if it has not already been allocated by a
4006      subclass.  */
4007   if (entry == NULL)
4008     {
4009       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4010       if (entry == NULL)
4011         return entry;
4012     }
4013
4014   /* Call the allocation method of the superclass.  */
4015   entry = bfd_hash_newfunc (entry, table, string);
4016   if (entry != NULL)
4017     {
4018       struct ppc_stub_hash_entry *eh;
4019
4020       /* Initialize the local fields.  */
4021       eh = (struct ppc_stub_hash_entry *) entry;
4022       eh->stub_type = ppc_stub_none;
4023       eh->stub_sec = NULL;
4024       eh->stub_offset = 0;
4025       eh->target_value = 0;
4026       eh->target_section = NULL;
4027       eh->h = NULL;
4028       eh->plt_ent = NULL;
4029       eh->id_sec = NULL;
4030       eh->other = 0;
4031     }
4032
4033   return entry;
4034 }
4035
4036 /* Create an entry in the branch hash table.  */
4037
4038 static struct bfd_hash_entry *
4039 branch_hash_newfunc (struct bfd_hash_entry *entry,
4040                      struct bfd_hash_table *table,
4041                      const char *string)
4042 {
4043   /* Allocate the structure if it has not already been allocated by a
4044      subclass.  */
4045   if (entry == NULL)
4046     {
4047       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4048       if (entry == NULL)
4049         return entry;
4050     }
4051
4052   /* Call the allocation method of the superclass.  */
4053   entry = bfd_hash_newfunc (entry, table, string);
4054   if (entry != NULL)
4055     {
4056       struct ppc_branch_hash_entry *eh;
4057
4058       /* Initialize the local fields.  */
4059       eh = (struct ppc_branch_hash_entry *) entry;
4060       eh->offset = 0;
4061       eh->iter = 0;
4062     }
4063
4064   return entry;
4065 }
4066
4067 /* Create an entry in a ppc64 ELF linker hash table.  */
4068
4069 static struct bfd_hash_entry *
4070 link_hash_newfunc (struct bfd_hash_entry *entry,
4071                    struct bfd_hash_table *table,
4072                    const char *string)
4073 {
4074   /* Allocate the structure if it has not already been allocated by a
4075      subclass.  */
4076   if (entry == NULL)
4077     {
4078       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4079       if (entry == NULL)
4080         return entry;
4081     }
4082
4083   /* Call the allocation method of the superclass.  */
4084   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4085   if (entry != NULL)
4086     {
4087       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4088
4089       memset (&eh->u.stub_cache, 0,
4090               (sizeof (struct ppc_link_hash_entry)
4091                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4092
4093       /* When making function calls, old ABI code references function entry
4094          points (dot symbols), while new ABI code references the function
4095          descriptor symbol.  We need to make any combination of reference and
4096          definition work together, without breaking archive linking.
4097
4098          For a defined function "foo" and an undefined call to "bar":
4099          An old object defines "foo" and ".foo", references ".bar" (possibly
4100          "bar" too).
4101          A new object defines "foo" and references "bar".
4102
4103          A new object thus has no problem with its undefined symbols being
4104          satisfied by definitions in an old object.  On the other hand, the
4105          old object won't have ".bar" satisfied by a new object.
4106
4107          Keep a list of newly added dot-symbols.  */
4108
4109       if (string[0] == '.')
4110         {
4111           struct ppc_link_hash_table *htab;
4112
4113           htab = (struct ppc_link_hash_table *) table;
4114           eh->u.next_dot_sym = htab->dot_syms;
4115           htab->dot_syms = eh;
4116         }
4117     }
4118
4119   return entry;
4120 }
4121
4122 struct tocsave_entry {
4123   asection *sec;
4124   bfd_vma offset;
4125 };
4126
4127 static hashval_t
4128 tocsave_htab_hash (const void *p)
4129 {
4130   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4131   return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4132 }
4133
4134 static int
4135 tocsave_htab_eq (const void *p1, const void *p2)
4136 {
4137   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4138   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4139   return e1->sec == e2->sec && e1->offset == e2->offset;
4140 }
4141
4142 /* Create a ppc64 ELF linker hash table.  */
4143
4144 static struct bfd_link_hash_table *
4145 ppc64_elf_link_hash_table_create (bfd *abfd)
4146 {
4147   struct ppc_link_hash_table *htab;
4148   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4149
4150   htab = bfd_zmalloc (amt);
4151   if (htab == NULL)
4152     return NULL;
4153
4154   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4155                                       sizeof (struct ppc_link_hash_entry),
4156                                       PPC64_ELF_DATA))
4157     {
4158       free (htab);
4159       return NULL;
4160     }
4161
4162   /* Init the stub hash table too.  */
4163   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4164                             sizeof (struct ppc_stub_hash_entry)))
4165     return NULL;
4166
4167   /* And the branch hash table.  */
4168   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4169                             sizeof (struct ppc_branch_hash_entry)))
4170     return NULL;
4171
4172   htab->tocsave_htab = htab_try_create (1024,
4173                                         tocsave_htab_hash,
4174                                         tocsave_htab_eq,
4175                                         NULL);
4176   if (htab->tocsave_htab == NULL)
4177     return NULL;
4178
4179   /* Initializing two fields of the union is just cosmetic.  We really
4180      only care about glist, but when compiled on a 32-bit host the
4181      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
4182      debugger inspection of these fields look nicer.  */
4183   htab->elf.init_got_refcount.refcount = 0;
4184   htab->elf.init_got_refcount.glist = NULL;
4185   htab->elf.init_plt_refcount.refcount = 0;
4186   htab->elf.init_plt_refcount.glist = NULL;
4187   htab->elf.init_got_offset.offset = 0;
4188   htab->elf.init_got_offset.glist = NULL;
4189   htab->elf.init_plt_offset.offset = 0;
4190   htab->elf.init_plt_offset.glist = NULL;
4191
4192   return &htab->elf.root;
4193 }
4194
4195 /* Free the derived linker hash table.  */
4196
4197 static void
4198 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
4199 {
4200   struct ppc_link_hash_table *htab = (struct ppc_link_hash_table *) hash;
4201
4202   bfd_hash_table_free (&htab->stub_hash_table);
4203   bfd_hash_table_free (&htab->branch_hash_table);
4204   if (htab->tocsave_htab)
4205     htab_delete (htab->tocsave_htab);
4206   _bfd_elf_link_hash_table_free (hash);
4207 }
4208
4209 /* Create sections for linker generated code.  */
4210
4211 static bfd_boolean
4212 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4213 {
4214   struct ppc_link_hash_table *htab;
4215   flagword flags;
4216
4217   htab = ppc_hash_table (info);
4218
4219   /* Create .sfpr for code to save and restore fp regs.  */
4220   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4221            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4222   htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4223                                                    flags);
4224   if (htab->sfpr == NULL
4225       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4226     return FALSE;
4227
4228   /* Create .glink for lazy dynamic linking support.  */
4229   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4230                                                     flags);
4231   if (htab->glink == NULL
4232       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4233     return FALSE;
4234
4235   if (!info->no_ld_generated_unwind_info)
4236     {
4237       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4238                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4239       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4240                                                                  ".eh_frame",
4241                                                                  flags);
4242       if (htab->glink_eh_frame == NULL
4243           || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4244         return FALSE;
4245     }
4246
4247   flags = SEC_ALLOC | SEC_LINKER_CREATED;
4248   htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4249   if (htab->iplt == NULL
4250       || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4251     return FALSE;
4252
4253   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4254            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4255   htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4256                                                       ".rela.iplt",
4257                                                       flags);
4258   if (htab->reliplt == NULL
4259       || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4260     return FALSE;
4261
4262   /* Create branch lookup table for plt_branch stubs.  */
4263   flags = (SEC_ALLOC | SEC_LOAD
4264            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4265   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4266                                                    flags);
4267   if (htab->brlt == NULL
4268       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4269     return FALSE;
4270
4271   if (!info->shared)
4272     return TRUE;
4273
4274   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4275            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4276   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4277                                                       ".rela.branch_lt",
4278                                                       flags);
4279   if (htab->relbrlt == NULL
4280       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4281     return FALSE;
4282
4283   return TRUE;
4284 }
4285
4286 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
4287
4288 bfd_boolean
4289 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4290 {
4291   struct ppc_link_hash_table *htab;
4292
4293   elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4294
4295 /* Always hook our dynamic sections into the first bfd, which is the
4296    linker created stub bfd.  This ensures that the GOT header is at
4297    the start of the output TOC section.  */
4298   htab = ppc_hash_table (info);
4299   if (htab == NULL)
4300     return FALSE;
4301   htab->stub_bfd = abfd;
4302   htab->elf.dynobj = abfd;
4303
4304   if (info->relocatable)
4305     return TRUE;
4306
4307   return create_linkage_sections (htab->elf.dynobj, info);
4308 }
4309
4310 /* Build a name for an entry in the stub hash table.  */
4311
4312 static char *
4313 ppc_stub_name (const asection *input_section,
4314                const asection *sym_sec,
4315                const struct ppc_link_hash_entry *h,
4316                const Elf_Internal_Rela *rel)
4317 {
4318   char *stub_name;
4319   ssize_t len;
4320
4321   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4322      offsets from a sym as a branch target?  In fact, we could
4323      probably assume the addend is always zero.  */
4324   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4325
4326   if (h)
4327     {
4328       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4329       stub_name = bfd_malloc (len);
4330       if (stub_name == NULL)
4331         return stub_name;
4332
4333       len = sprintf (stub_name, "%08x.%s+%x",
4334                      input_section->id & 0xffffffff,
4335                      h->elf.root.root.string,
4336                      (int) rel->r_addend & 0xffffffff);
4337     }
4338   else
4339     {
4340       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4341       stub_name = bfd_malloc (len);
4342       if (stub_name == NULL)
4343         return stub_name;
4344
4345       len = sprintf (stub_name, "%08x.%x:%x+%x",
4346                      input_section->id & 0xffffffff,
4347                      sym_sec->id & 0xffffffff,
4348                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4349                      (int) rel->r_addend & 0xffffffff);
4350     }
4351   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4352     stub_name[len - 2] = 0;
4353   return stub_name;
4354 }
4355
4356 /* Look up an entry in the stub hash.  Stub entries are cached because
4357    creating the stub name takes a bit of time.  */
4358
4359 static struct ppc_stub_hash_entry *
4360 ppc_get_stub_entry (const asection *input_section,
4361                     const asection *sym_sec,
4362                     struct ppc_link_hash_entry *h,
4363                     const Elf_Internal_Rela *rel,
4364                     struct ppc_link_hash_table *htab)
4365 {
4366   struct ppc_stub_hash_entry *stub_entry;
4367   const asection *id_sec;
4368
4369   /* If this input section is part of a group of sections sharing one
4370      stub section, then use the id of the first section in the group.
4371      Stub names need to include a section id, as there may well be
4372      more than one stub used to reach say, printf, and we need to
4373      distinguish between them.  */
4374   id_sec = htab->stub_group[input_section->id].link_sec;
4375
4376   if (h != NULL && h->u.stub_cache != NULL
4377       && h->u.stub_cache->h == h
4378       && h->u.stub_cache->id_sec == id_sec)
4379     {
4380       stub_entry = h->u.stub_cache;
4381     }
4382   else
4383     {
4384       char *stub_name;
4385
4386       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4387       if (stub_name == NULL)
4388         return NULL;
4389
4390       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4391                                          stub_name, FALSE, FALSE);
4392       if (h != NULL)
4393         h->u.stub_cache = stub_entry;
4394
4395       free (stub_name);
4396     }
4397
4398   return stub_entry;
4399 }
4400
4401 /* Add a new stub entry to the stub hash.  Not all fields of the new
4402    stub entry are initialised.  */
4403
4404 static struct ppc_stub_hash_entry *
4405 ppc_add_stub (const char *stub_name,
4406               asection *section,
4407               struct bfd_link_info *info)
4408 {
4409   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4410   asection *link_sec;
4411   asection *stub_sec;
4412   struct ppc_stub_hash_entry *stub_entry;
4413
4414   link_sec = htab->stub_group[section->id].link_sec;
4415   stub_sec = htab->stub_group[section->id].stub_sec;
4416   if (stub_sec == NULL)
4417     {
4418       stub_sec = htab->stub_group[link_sec->id].stub_sec;
4419       if (stub_sec == NULL)
4420         {
4421           size_t namelen;
4422           bfd_size_type len;
4423           char *s_name;
4424
4425           namelen = strlen (link_sec->name);
4426           len = namelen + sizeof (STUB_SUFFIX);
4427           s_name = bfd_alloc (htab->stub_bfd, len);
4428           if (s_name == NULL)
4429             return NULL;
4430
4431           memcpy (s_name, link_sec->name, namelen);
4432           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4433           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4434           if (stub_sec == NULL)
4435             return NULL;
4436           htab->stub_group[link_sec->id].stub_sec = stub_sec;
4437         }
4438       htab->stub_group[section->id].stub_sec = stub_sec;
4439     }
4440
4441   /* Enter this entry into the linker stub hash table.  */
4442   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4443                                      TRUE, FALSE);
4444   if (stub_entry == NULL)
4445     {
4446       info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4447                               section->owner, stub_name);
4448       return NULL;
4449     }
4450
4451   stub_entry->stub_sec = stub_sec;
4452   stub_entry->stub_offset = 0;
4453   stub_entry->id_sec = link_sec;
4454   return stub_entry;
4455 }
4456
4457 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4458    not already done.  */
4459
4460 static bfd_boolean
4461 create_got_section (bfd *abfd, struct bfd_link_info *info)
4462 {
4463   asection *got, *relgot;
4464   flagword flags;
4465   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4466
4467   if (!is_ppc64_elf (abfd))
4468     return FALSE;
4469   if (htab == NULL)
4470     return FALSE;
4471
4472   if (!htab->got)
4473     {
4474       if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4475         return FALSE;
4476
4477       htab->got = bfd_get_linker_section (htab->elf.dynobj, ".got");
4478       if (!htab->got)
4479         abort ();
4480     }
4481
4482   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4483            | SEC_LINKER_CREATED);
4484
4485   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4486   if (!got
4487       || !bfd_set_section_alignment (abfd, got, 3))
4488     return FALSE;
4489
4490   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4491                                                flags | SEC_READONLY);
4492   if (!relgot
4493       || ! bfd_set_section_alignment (abfd, relgot, 3))
4494     return FALSE;
4495
4496   ppc64_elf_tdata (abfd)->got = got;
4497   ppc64_elf_tdata (abfd)->relgot = relgot;
4498   return TRUE;
4499 }
4500
4501 /* Create the dynamic sections, and set up shortcuts.  */
4502
4503 static bfd_boolean
4504 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4505 {
4506   struct ppc_link_hash_table *htab;
4507
4508   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4509     return FALSE;
4510
4511   htab = ppc_hash_table (info);
4512   if (htab == NULL)
4513     return FALSE;
4514
4515   if (!htab->got)
4516     htab->got = bfd_get_linker_section (dynobj, ".got");
4517   htab->plt = bfd_get_linker_section (dynobj, ".plt");
4518   htab->relplt = bfd_get_linker_section (dynobj, ".rela.plt");
4519   htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
4520   if (!info->shared)
4521     htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
4522
4523   if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4524       || (!info->shared && !htab->relbss))
4525     abort ();
4526
4527   return TRUE;
4528 }
4529
4530 /* Follow indirect and warning symbol links.  */
4531
4532 static inline struct bfd_link_hash_entry *
4533 follow_link (struct bfd_link_hash_entry *h)
4534 {
4535   while (h->type == bfd_link_hash_indirect
4536          || h->type == bfd_link_hash_warning)
4537     h = h->u.i.link;
4538   return h;
4539 }
4540
4541 static inline struct elf_link_hash_entry *
4542 elf_follow_link (struct elf_link_hash_entry *h)
4543 {
4544   return (struct elf_link_hash_entry *) follow_link (&h->root);
4545 }
4546
4547 static inline struct ppc_link_hash_entry *
4548 ppc_follow_link (struct ppc_link_hash_entry *h)
4549 {
4550   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4551 }
4552
4553 /* Merge PLT info on FROM with that on TO.  */
4554
4555 static void
4556 move_plt_plist (struct ppc_link_hash_entry *from,
4557                 struct ppc_link_hash_entry *to)
4558 {
4559   if (from->elf.plt.plist != NULL)
4560     {
4561       if (to->elf.plt.plist != NULL)
4562         {
4563           struct plt_entry **entp;
4564           struct plt_entry *ent;
4565
4566           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4567             {
4568               struct plt_entry *dent;
4569
4570               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4571                 if (dent->addend == ent->addend)
4572                   {
4573                     dent->plt.refcount += ent->plt.refcount;
4574                     *entp = ent->next;
4575                     break;
4576                   }
4577               if (dent == NULL)
4578                 entp = &ent->next;
4579             }
4580           *entp = to->elf.plt.plist;
4581         }
4582
4583       to->elf.plt.plist = from->elf.plt.plist;
4584       from->elf.plt.plist = NULL;
4585     }
4586 }
4587
4588 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
4589
4590 static void
4591 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4592                                 struct elf_link_hash_entry *dir,
4593                                 struct elf_link_hash_entry *ind)
4594 {
4595   struct ppc_link_hash_entry *edir, *eind;
4596
4597   edir = (struct ppc_link_hash_entry *) dir;
4598   eind = (struct ppc_link_hash_entry *) ind;
4599
4600   edir->is_func |= eind->is_func;
4601   edir->is_func_descriptor |= eind->is_func_descriptor;
4602   edir->tls_mask |= eind->tls_mask;
4603   if (eind->oh != NULL)
4604     edir->oh = ppc_follow_link (eind->oh);
4605
4606   /* If called to transfer flags for a weakdef during processing
4607      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4608      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
4609   if (!(ELIMINATE_COPY_RELOCS
4610         && eind->elf.root.type != bfd_link_hash_indirect
4611         && edir->elf.dynamic_adjusted))
4612     edir->elf.non_got_ref |= eind->elf.non_got_ref;
4613
4614   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4615   edir->elf.ref_regular |= eind->elf.ref_regular;
4616   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4617   edir->elf.needs_plt |= eind->elf.needs_plt;
4618
4619   /* Copy over any dynamic relocs we may have on the indirect sym.  */
4620   if (eind->dyn_relocs != NULL)
4621     {
4622       if (edir->dyn_relocs != NULL)
4623         {
4624           struct elf_dyn_relocs **pp;
4625           struct elf_dyn_relocs *p;
4626
4627           /* Add reloc counts against the indirect sym to the direct sym
4628              list.  Merge any entries against the same section.  */
4629           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4630             {
4631               struct elf_dyn_relocs *q;
4632
4633               for (q = edir->dyn_relocs; q != NULL; q = q->next)
4634                 if (q->sec == p->sec)
4635                   {
4636                     q->pc_count += p->pc_count;
4637                     q->count += p->count;
4638                     *pp = p->next;
4639                     break;
4640                   }
4641               if (q == NULL)
4642                 pp = &p->next;
4643             }
4644           *pp = edir->dyn_relocs;
4645         }
4646
4647       edir->dyn_relocs = eind->dyn_relocs;
4648       eind->dyn_relocs = NULL;
4649     }
4650
4651   /* If we were called to copy over info for a weak sym, that's all.
4652      You might think dyn_relocs need not be copied over;  After all,
4653      both syms will be dynamic or both non-dynamic so we're just
4654      moving reloc accounting around.  However, ELIMINATE_COPY_RELOCS
4655      code in ppc64_elf_adjust_dynamic_symbol needs to check for
4656      dyn_relocs in read-only sections, and it does so on what is the
4657      DIR sym here.  */
4658   if (eind->elf.root.type != bfd_link_hash_indirect)
4659     return;
4660
4661   /* Copy over got entries that we may have already seen to the
4662      symbol which just became indirect.  */
4663   if (eind->elf.got.glist != NULL)
4664     {
4665       if (edir->elf.got.glist != NULL)
4666         {
4667           struct got_entry **entp;
4668           struct got_entry *ent;
4669
4670           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4671             {
4672               struct got_entry *dent;
4673
4674               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4675                 if (dent->addend == ent->addend
4676                     && dent->owner == ent->owner
4677                     && dent->tls_type == ent->tls_type)
4678                   {
4679                     dent->got.refcount += ent->got.refcount;
4680                     *entp = ent->next;
4681                     break;
4682                   }
4683               if (dent == NULL)
4684                 entp = &ent->next;
4685             }
4686           *entp = edir->elf.got.glist;
4687         }
4688
4689       edir->elf.got.glist = eind->elf.got.glist;
4690       eind->elf.got.glist = NULL;
4691     }
4692
4693   /* And plt entries.  */
4694   move_plt_plist (eind, edir);
4695
4696   if (eind->elf.dynindx != -1)
4697     {
4698       if (edir->elf.dynindx != -1)
4699         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4700                                 edir->elf.dynstr_index);
4701       edir->elf.dynindx = eind->elf.dynindx;
4702       edir->elf.dynstr_index = eind->elf.dynstr_index;
4703       eind->elf.dynindx = -1;
4704       eind->elf.dynstr_index = 0;
4705     }
4706 }
4707
4708 /* Find the function descriptor hash entry from the given function code
4709    hash entry FH.  Link the entries via their OH fields.  */
4710
4711 static struct ppc_link_hash_entry *
4712 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4713 {
4714   struct ppc_link_hash_entry *fdh = fh->oh;
4715
4716   if (fdh == NULL)
4717     {
4718       const char *fd_name = fh->elf.root.root.string + 1;
4719
4720       fdh = (struct ppc_link_hash_entry *)
4721         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4722       if (fdh == NULL)
4723         return fdh;
4724
4725       fdh->is_func_descriptor = 1;
4726       fdh->oh = fh;
4727       fh->is_func = 1;
4728       fh->oh = fdh;
4729     }
4730
4731   return ppc_follow_link (fdh);
4732 }
4733
4734 /* Make a fake function descriptor sym for the code sym FH.  */
4735
4736 static struct ppc_link_hash_entry *
4737 make_fdh (struct bfd_link_info *info,
4738           struct ppc_link_hash_entry *fh)
4739 {
4740   bfd *abfd;
4741   asymbol *newsym;
4742   struct bfd_link_hash_entry *bh;
4743   struct ppc_link_hash_entry *fdh;
4744
4745   abfd = fh->elf.root.u.undef.abfd;
4746   newsym = bfd_make_empty_symbol (abfd);
4747   newsym->name = fh->elf.root.root.string + 1;
4748   newsym->section = bfd_und_section_ptr;
4749   newsym->value = 0;
4750   newsym->flags = BSF_WEAK;
4751
4752   bh = NULL;
4753   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4754                                          newsym->flags, newsym->section,
4755                                          newsym->value, NULL, FALSE, FALSE,
4756                                          &bh))
4757     return NULL;
4758
4759   fdh = (struct ppc_link_hash_entry *) bh;
4760   fdh->elf.non_elf = 0;
4761   fdh->fake = 1;
4762   fdh->is_func_descriptor = 1;
4763   fdh->oh = fh;
4764   fh->is_func = 1;
4765   fh->oh = fdh;
4766   return fdh;
4767 }
4768
4769 /* Fix function descriptor symbols defined in .opd sections to be
4770    function type.  */
4771
4772 static bfd_boolean
4773 ppc64_elf_add_symbol_hook (bfd *ibfd,
4774                            struct bfd_link_info *info,
4775                            Elf_Internal_Sym *isym,
4776                            const char **name,
4777                            flagword *flags ATTRIBUTE_UNUSED,
4778                            asection **sec,
4779                            bfd_vma *value ATTRIBUTE_UNUSED)
4780 {
4781   if ((ibfd->flags & DYNAMIC) == 0
4782       && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4783     elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4784
4785   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4786     {
4787       if ((ibfd->flags & DYNAMIC) == 0)
4788         elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4789     }
4790   else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4791     ;
4792   else if (*sec != NULL
4793            && strcmp ((*sec)->name, ".opd") == 0)
4794     isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4795
4796   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4797     {
4798       if (abiversion (ibfd) == 0)
4799         set_abiversion (ibfd, 2);
4800       else if (abiversion (ibfd) == 1)
4801         {
4802           info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4803                                     " for ABI version 1\n"), name);
4804           bfd_set_error (bfd_error_bad_value);
4805           return FALSE;
4806         }
4807     }
4808
4809   return TRUE;
4810 }
4811
4812 /* Merge non-visibility st_other attributes: local entry point.  */
4813
4814 static void
4815 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4816                                   const Elf_Internal_Sym *isym,
4817                                   bfd_boolean definition,
4818                                   bfd_boolean dynamic)
4819 {
4820   if (definition && !dynamic)
4821     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4822                 | ELF_ST_VISIBILITY (h->other));
4823 }
4824
4825 /* This function makes an old ABI object reference to ".bar" cause the
4826    inclusion of a new ABI object archive that defines "bar".
4827    NAME is a symbol defined in an archive.  Return a symbol in the hash
4828    table that might be satisfied by the archive symbols.  */
4829
4830 static struct elf_link_hash_entry *
4831 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4832                                  struct bfd_link_info *info,
4833                                  const char *name)
4834 {
4835   struct elf_link_hash_entry *h;
4836   char *dot_name;
4837   size_t len;
4838
4839   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4840   if (h != NULL
4841       /* Don't return this sym if it is a fake function descriptor
4842          created by add_symbol_adjust.  */
4843       && !(h->root.type == bfd_link_hash_undefweak
4844            && ((struct ppc_link_hash_entry *) h)->fake))
4845     return h;
4846
4847   if (name[0] == '.')
4848     return h;
4849
4850   len = strlen (name);
4851   dot_name = bfd_alloc (abfd, len + 2);
4852   if (dot_name == NULL)
4853     return (struct elf_link_hash_entry *) 0 - 1;
4854   dot_name[0] = '.';
4855   memcpy (dot_name + 1, name, len + 1);
4856   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4857   bfd_release (abfd, dot_name);
4858   return h;
4859 }
4860
4861 /* This function satisfies all old ABI object references to ".bar" if a
4862    new ABI object defines "bar".  Well, at least, undefined dot symbols
4863    are made weak.  This stops later archive searches from including an
4864    object if we already have a function descriptor definition.  It also
4865    prevents the linker complaining about undefined symbols.
4866    We also check and correct mismatched symbol visibility here.  The
4867    most restrictive visibility of the function descriptor and the
4868    function entry symbol is used.  */
4869
4870 static bfd_boolean
4871 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4872 {
4873   struct ppc_link_hash_table *htab;
4874   struct ppc_link_hash_entry *fdh;
4875
4876   if (eh->elf.root.type == bfd_link_hash_indirect)
4877     return TRUE;
4878
4879   if (eh->elf.root.type == bfd_link_hash_warning)
4880     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4881
4882   if (eh->elf.root.root.string[0] != '.')
4883     abort ();
4884
4885   htab = ppc_hash_table (info);
4886   if (htab == NULL)
4887     return FALSE;
4888
4889   fdh = lookup_fdh (eh, htab);
4890   if (fdh == NULL)
4891     {
4892       if (!info->relocatable
4893           && (eh->elf.root.type == bfd_link_hash_undefined
4894               || eh->elf.root.type == bfd_link_hash_undefweak)
4895           && eh->elf.ref_regular)
4896         {
4897           /* Make an undefweak function descriptor sym, which is enough to
4898              pull in an --as-needed shared lib, but won't cause link
4899              errors.  Archives are handled elsewhere.  */
4900           fdh = make_fdh (info, eh);
4901           if (fdh == NULL)
4902             return FALSE;
4903           fdh->elf.ref_regular = 1;
4904         }
4905     }
4906   else
4907     {
4908       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4909       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4910       if (entry_vis < descr_vis)
4911         fdh->elf.other += entry_vis - descr_vis;
4912       else if (entry_vis > descr_vis)
4913         eh->elf.other += descr_vis - entry_vis;
4914
4915       if ((fdh->elf.root.type == bfd_link_hash_defined
4916            || fdh->elf.root.type == bfd_link_hash_defweak)
4917           && eh->elf.root.type == bfd_link_hash_undefined)
4918         {
4919           eh->elf.root.type = bfd_link_hash_undefweak;
4920           eh->was_undefined = 1;
4921           htab->twiddled_syms = 1;
4922         }
4923     }
4924
4925   return TRUE;
4926 }
4927
4928 /* Process list of dot-symbols we made in link_hash_newfunc.  */
4929
4930 static bfd_boolean
4931 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
4932 {
4933   struct ppc_link_hash_table *htab;
4934   struct ppc_link_hash_entry **p, *eh;
4935
4936   if (!is_ppc64_elf (info->output_bfd))
4937     return TRUE;
4938   htab = ppc_hash_table (info);
4939   if (htab == NULL)
4940     return FALSE;
4941
4942   if (is_ppc64_elf (ibfd))
4943     {
4944       p = &htab->dot_syms;
4945       while ((eh = *p) != NULL)
4946         {
4947           *p = NULL;
4948           if (&eh->elf == htab->elf.hgot)
4949             ;
4950           else if (htab->elf.hgot == NULL
4951                    && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4952             htab->elf.hgot = &eh->elf;
4953           else if (!add_symbol_adjust (eh, info))
4954             return FALSE;
4955           p = &eh->u.next_dot_sym;
4956         }
4957     }
4958
4959   /* Clear the list for non-ppc64 input files.  */
4960   p = &htab->dot_syms;
4961   while ((eh = *p) != NULL)
4962     {
4963       *p = NULL;
4964       p = &eh->u.next_dot_sym;
4965     }
4966
4967   /* We need to fix the undefs list for any syms we have twiddled to
4968      undef_weak.  */
4969   if (htab->twiddled_syms)
4970     {
4971       bfd_link_repair_undef_list (&htab->elf.root);
4972       htab->twiddled_syms = 0;
4973     }
4974   return TRUE;
4975 }
4976
4977 /* Undo hash table changes when an --as-needed input file is determined
4978    not to be needed.  */
4979
4980 static bfd_boolean
4981 ppc64_elf_notice_as_needed (bfd *ibfd,
4982                             struct bfd_link_info *info,
4983                             enum notice_asneeded_action act)
4984 {
4985   if (act == notice_not_needed)
4986     {
4987       struct ppc_link_hash_table *htab = ppc_hash_table (info);
4988
4989       if (htab == NULL)
4990         return FALSE;
4991
4992       htab->dot_syms = NULL;
4993     }
4994   return _bfd_elf_notice_as_needed (ibfd, info, act);
4995 }
4996
4997 /* If --just-symbols against a final linked binary, then assume we need
4998    toc adjusting stubs when calling functions defined there.  */
4999
5000 static void
5001 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5002 {
5003   if ((sec->flags & SEC_CODE) != 0
5004       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5005       && is_ppc64_elf (sec->owner))
5006     {
5007       asection *got = bfd_get_section_by_name (sec->owner, ".got");
5008       if (got != NULL
5009           && got->size >= elf_backend_got_header_size
5010           && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5011         sec->has_toc_reloc = 1;
5012     }
5013   _bfd_elf_link_just_syms (sec, info);
5014 }
5015
5016 static struct plt_entry **
5017 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5018                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5019 {
5020   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5021   struct plt_entry **local_plt;
5022   unsigned char *local_got_tls_masks;
5023
5024   if (local_got_ents == NULL)
5025     {
5026       bfd_size_type size = symtab_hdr->sh_info;
5027
5028       size *= (sizeof (*local_got_ents)
5029                + sizeof (*local_plt)
5030                + sizeof (*local_got_tls_masks));
5031       local_got_ents = bfd_zalloc (abfd, size);
5032       if (local_got_ents == NULL)
5033         return NULL;
5034       elf_local_got_ents (abfd) = local_got_ents;
5035     }
5036
5037   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5038     {
5039       struct got_entry *ent;
5040
5041       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5042         if (ent->addend == r_addend
5043             && ent->owner == abfd
5044             && ent->tls_type == tls_type)
5045           break;
5046       if (ent == NULL)
5047         {
5048           bfd_size_type amt = sizeof (*ent);
5049           ent = bfd_alloc (abfd, amt);
5050           if (ent == NULL)
5051             return FALSE;
5052           ent->next = local_got_ents[r_symndx];
5053           ent->addend = r_addend;
5054           ent->owner = abfd;
5055           ent->tls_type = tls_type;
5056           ent->is_indirect = FALSE;
5057           ent->got.refcount = 0;
5058           local_got_ents[r_symndx] = ent;
5059         }
5060       ent->got.refcount += 1;
5061     }
5062
5063   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5064   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5065   local_got_tls_masks[r_symndx] |= tls_type;
5066
5067   return local_plt + r_symndx;
5068 }
5069
5070 static bfd_boolean
5071 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5072 {
5073   struct plt_entry *ent;
5074
5075   for (ent = *plist; ent != NULL; ent = ent->next)
5076     if (ent->addend == addend)
5077       break;
5078   if (ent == NULL)
5079     {
5080       bfd_size_type amt = sizeof (*ent);
5081       ent = bfd_alloc (abfd, amt);
5082       if (ent == NULL)
5083         return FALSE;
5084       ent->next = *plist;
5085       ent->addend = addend;
5086       ent->plt.refcount = 0;
5087       *plist = ent;
5088     }
5089   ent->plt.refcount += 1;
5090   return TRUE;
5091 }
5092
5093 static bfd_boolean
5094 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5095 {
5096   return (r_type == R_PPC64_REL24
5097           || r_type == R_PPC64_REL14
5098           || r_type == R_PPC64_REL14_BRTAKEN
5099           || r_type == R_PPC64_REL14_BRNTAKEN
5100           || r_type == R_PPC64_ADDR24
5101           || r_type == R_PPC64_ADDR14
5102           || r_type == R_PPC64_ADDR14_BRTAKEN
5103           || r_type == R_PPC64_ADDR14_BRNTAKEN);
5104 }
5105
5106 /* Look through the relocs for a section during the first phase, and
5107    calculate needed space in the global offset table, procedure
5108    linkage table, and dynamic reloc sections.  */
5109
5110 static bfd_boolean
5111 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5112                         asection *sec, const Elf_Internal_Rela *relocs)
5113 {
5114   struct ppc_link_hash_table *htab;
5115   Elf_Internal_Shdr *symtab_hdr;
5116   struct elf_link_hash_entry **sym_hashes;
5117   const Elf_Internal_Rela *rel;
5118   const Elf_Internal_Rela *rel_end;
5119   asection *sreloc;
5120   asection **opd_sym_map;
5121   struct elf_link_hash_entry *tga, *dottga;
5122
5123   if (info->relocatable)
5124     return TRUE;
5125
5126   /* Don't do anything special with non-loaded, non-alloced sections.
5127      In particular, any relocs in such sections should not affect GOT
5128      and PLT reference counting (ie. we don't allow them to create GOT
5129      or PLT entries), there's no possibility or desire to optimize TLS
5130      relocs, and there's not much point in propagating relocs to shared
5131      libs that the dynamic linker won't relocate.  */
5132   if ((sec->flags & SEC_ALLOC) == 0)
5133     return TRUE;
5134
5135   BFD_ASSERT (is_ppc64_elf (abfd));
5136
5137   htab = ppc_hash_table (info);
5138   if (htab == NULL)
5139     return FALSE;
5140
5141   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5142                               FALSE, FALSE, TRUE);
5143   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5144                                  FALSE, FALSE, TRUE);
5145   symtab_hdr = &elf_symtab_hdr (abfd);
5146   sym_hashes = elf_sym_hashes (abfd);
5147   sreloc = NULL;
5148   opd_sym_map = NULL;
5149   if (strcmp (sec->name, ".opd") == 0)
5150     {
5151       /* Garbage collection needs some extra help with .opd sections.
5152          We don't want to necessarily keep everything referenced by
5153          relocs in .opd, as that would keep all functions.  Instead,
5154          if we reference an .opd symbol (a function descriptor), we
5155          want to keep the function code symbol's section.  This is
5156          easy for global symbols, but for local syms we need to keep
5157          information about the associated function section.  */
5158       bfd_size_type amt;
5159
5160       if (abiversion (abfd) == 0)
5161         set_abiversion (abfd, 1);
5162       else if (abiversion (abfd) == 2)
5163         {
5164           info->callbacks->einfo (_("%P: .opd not allowed in ABI version %d\n"),
5165                                   abiversion (abfd));
5166           bfd_set_error (bfd_error_bad_value);
5167           return FALSE;
5168         }
5169       amt = sec->size * sizeof (*opd_sym_map) / 8;
5170       opd_sym_map = bfd_zalloc (abfd, amt);
5171       if (opd_sym_map == NULL)
5172         return FALSE;
5173       ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
5174       BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
5175       ppc64_elf_section_data (sec)->sec_type = sec_opd;
5176     }
5177
5178   rel_end = relocs + sec->reloc_count;
5179   for (rel = relocs; rel < rel_end; rel++)
5180     {
5181       unsigned long r_symndx;
5182       struct elf_link_hash_entry *h;
5183       enum elf_ppc64_reloc_type r_type;
5184       int tls_type;
5185       struct _ppc64_elf_section_data *ppc64_sec;
5186       struct plt_entry **ifunc;
5187
5188       r_symndx = ELF64_R_SYM (rel->r_info);
5189       if (r_symndx < symtab_hdr->sh_info)
5190         h = NULL;
5191       else
5192         {
5193           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5194           h = elf_follow_link (h);
5195
5196           /* PR15323, ref flags aren't set for references in the same
5197              object.  */
5198           h->root.non_ir_ref = 1;
5199
5200           if (h == htab->elf.hgot)
5201             sec->has_toc_reloc = 1;
5202         }
5203
5204       tls_type = 0;
5205       ifunc = NULL;
5206       if (h != NULL)
5207         {
5208           if (h->type == STT_GNU_IFUNC)
5209             {
5210               h->needs_plt = 1;
5211               ifunc = &h->plt.plist;
5212             }
5213         }
5214       else
5215         {
5216           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5217                                                           abfd, r_symndx);
5218           if (isym == NULL)
5219             return FALSE;
5220
5221           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5222             {
5223               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5224                                              rel->r_addend, PLT_IFUNC);
5225               if (ifunc == NULL)
5226                 return FALSE;
5227             }
5228         }
5229       r_type = ELF64_R_TYPE (rel->r_info);
5230       if (is_branch_reloc (r_type))
5231         {
5232           if (h != NULL && (h == tga || h == dottga))
5233             {
5234               if (rel != relocs
5235                   && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5236                       || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5237                 /* We have a new-style __tls_get_addr call with a marker
5238                    reloc.  */
5239                 ;
5240               else
5241                 /* Mark this section as having an old-style call.  */
5242                 sec->has_tls_get_addr_call = 1;
5243             }
5244
5245           /* STT_GNU_IFUNC symbols must have a PLT entry.  */
5246           if (ifunc != NULL
5247               && !update_plt_info (abfd, ifunc, rel->r_addend))
5248             return FALSE;
5249         }
5250
5251       switch (r_type)
5252         {
5253         case R_PPC64_TLSGD:
5254         case R_PPC64_TLSLD:
5255           /* These special tls relocs tie a call to __tls_get_addr with
5256              its parameter symbol.  */
5257           break;
5258
5259         case R_PPC64_GOT_TLSLD16:
5260         case R_PPC64_GOT_TLSLD16_LO:
5261         case R_PPC64_GOT_TLSLD16_HI:
5262         case R_PPC64_GOT_TLSLD16_HA:
5263           tls_type = TLS_TLS | TLS_LD;
5264           goto dogottls;
5265
5266         case R_PPC64_GOT_TLSGD16:
5267         case R_PPC64_GOT_TLSGD16_LO:
5268         case R_PPC64_GOT_TLSGD16_HI:
5269         case R_PPC64_GOT_TLSGD16_HA:
5270           tls_type = TLS_TLS | TLS_GD;
5271           goto dogottls;
5272
5273         case R_PPC64_GOT_TPREL16_DS:
5274         case R_PPC64_GOT_TPREL16_LO_DS:
5275         case R_PPC64_GOT_TPREL16_HI:
5276         case R_PPC64_GOT_TPREL16_HA:
5277           if (!info->executable)
5278             info->flags |= DF_STATIC_TLS;
5279           tls_type = TLS_TLS | TLS_TPREL;
5280           goto dogottls;
5281
5282         case R_PPC64_GOT_DTPREL16_DS:
5283         case R_PPC64_GOT_DTPREL16_LO_DS:
5284         case R_PPC64_GOT_DTPREL16_HI:
5285         case R_PPC64_GOT_DTPREL16_HA:
5286           tls_type = TLS_TLS | TLS_DTPREL;
5287         dogottls:
5288           sec->has_tls_reloc = 1;
5289           /* Fall thru */
5290
5291         case R_PPC64_GOT16:
5292         case R_PPC64_GOT16_DS:
5293         case R_PPC64_GOT16_HA:
5294         case R_PPC64_GOT16_HI:
5295         case R_PPC64_GOT16_LO:
5296         case R_PPC64_GOT16_LO_DS:
5297           /* This symbol requires a global offset table entry.  */
5298           sec->has_toc_reloc = 1;
5299           if (r_type == R_PPC64_GOT_TLSLD16
5300               || r_type == R_PPC64_GOT_TLSGD16
5301               || r_type == R_PPC64_GOT_TPREL16_DS
5302               || r_type == R_PPC64_GOT_DTPREL16_DS
5303               || r_type == R_PPC64_GOT16
5304               || r_type == R_PPC64_GOT16_DS)
5305             {
5306               htab->do_multi_toc = 1;
5307               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5308             }
5309
5310           if (ppc64_elf_tdata (abfd)->got == NULL
5311               && !create_got_section (abfd, info))
5312             return FALSE;
5313
5314           if (h != NULL)
5315             {
5316               struct ppc_link_hash_entry *eh;
5317               struct got_entry *ent;
5318
5319               eh = (struct ppc_link_hash_entry *) h;
5320               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5321                 if (ent->addend == rel->r_addend
5322                     && ent->owner == abfd
5323                     && ent->tls_type == tls_type)
5324                   break;
5325               if (ent == NULL)
5326                 {
5327                   bfd_size_type amt = sizeof (*ent);
5328                   ent = bfd_alloc (abfd, amt);
5329                   if (ent == NULL)
5330                     return FALSE;
5331                   ent->next = eh->elf.got.glist;
5332                   ent->addend = rel->r_addend;
5333                   ent->owner = abfd;
5334                   ent->tls_type = tls_type;
5335                   ent->is_indirect = FALSE;
5336                   ent->got.refcount = 0;
5337                   eh->elf.got.glist = ent;
5338                 }
5339               ent->got.refcount += 1;
5340               eh->tls_mask |= tls_type;
5341             }
5342           else
5343             /* This is a global offset table entry for a local symbol.  */
5344             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5345                                         rel->r_addend, tls_type))
5346               return FALSE;
5347           break;
5348
5349         case R_PPC64_PLT16_HA:
5350         case R_PPC64_PLT16_HI:
5351         case R_PPC64_PLT16_LO:
5352         case R_PPC64_PLT32:
5353         case R_PPC64_PLT64:
5354           /* This symbol requires a procedure linkage table entry.  We
5355              actually build the entry in adjust_dynamic_symbol,
5356              because this might be a case of linking PIC code without
5357              linking in any dynamic objects, in which case we don't
5358              need to generate a procedure linkage table after all.  */
5359           if (h == NULL)
5360             {
5361               /* It does not make sense to have a procedure linkage
5362                  table entry for a local symbol.  */
5363               bfd_set_error (bfd_error_bad_value);
5364               return FALSE;
5365             }
5366           else
5367             {
5368               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5369                 return FALSE;
5370               h->needs_plt = 1;
5371               if (h->root.root.string[0] == '.'
5372                   && h->root.root.string[1] != '\0')
5373                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5374             }
5375           break;
5376
5377           /* The following relocations don't need to propagate the
5378              relocation if linking a shared object since they are
5379              section relative.  */
5380         case R_PPC64_SECTOFF:
5381         case R_PPC64_SECTOFF_LO:
5382         case R_PPC64_SECTOFF_HI:
5383         case R_PPC64_SECTOFF_HA:
5384         case R_PPC64_SECTOFF_DS:
5385         case R_PPC64_SECTOFF_LO_DS:
5386         case R_PPC64_DTPREL16:
5387         case R_PPC64_DTPREL16_LO:
5388         case R_PPC64_DTPREL16_HI:
5389         case R_PPC64_DTPREL16_HA:
5390         case R_PPC64_DTPREL16_DS:
5391         case R_PPC64_DTPREL16_LO_DS:
5392         case R_PPC64_DTPREL16_HIGH:
5393         case R_PPC64_DTPREL16_HIGHA:
5394         case R_PPC64_DTPREL16_HIGHER:
5395         case R_PPC64_DTPREL16_HIGHERA:
5396         case R_PPC64_DTPREL16_HIGHEST:
5397         case R_PPC64_DTPREL16_HIGHESTA:
5398           break;
5399
5400           /* Nor do these.  */
5401         case R_PPC64_REL16:
5402         case R_PPC64_REL16_LO:
5403         case R_PPC64_REL16_HI:
5404         case R_PPC64_REL16_HA:
5405           break;
5406
5407         case R_PPC64_TOC16:
5408         case R_PPC64_TOC16_DS:
5409           htab->do_multi_toc = 1;
5410           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5411         case R_PPC64_TOC16_LO:
5412         case R_PPC64_TOC16_HI:
5413         case R_PPC64_TOC16_HA:
5414         case R_PPC64_TOC16_LO_DS:
5415           sec->has_toc_reloc = 1;
5416           break;
5417
5418           /* This relocation describes the C++ object vtable hierarchy.
5419              Reconstruct it for later use during GC.  */
5420         case R_PPC64_GNU_VTINHERIT:
5421           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5422             return FALSE;
5423           break;
5424
5425           /* This relocation describes which C++ vtable entries are actually
5426              used.  Record for later use during GC.  */
5427         case R_PPC64_GNU_VTENTRY:
5428           BFD_ASSERT (h != NULL);
5429           if (h != NULL
5430               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5431             return FALSE;
5432           break;
5433
5434         case R_PPC64_REL14:
5435         case R_PPC64_REL14_BRTAKEN:
5436         case R_PPC64_REL14_BRNTAKEN:
5437           {
5438             asection *dest = NULL;
5439
5440             /* Heuristic: If jumping outside our section, chances are
5441                we are going to need a stub.  */
5442             if (h != NULL)
5443               {
5444                 /* If the sym is weak it may be overridden later, so
5445                    don't assume we know where a weak sym lives.  */
5446                 if (h->root.type == bfd_link_hash_defined)
5447                   dest = h->root.u.def.section;
5448               }
5449             else
5450               {
5451                 Elf_Internal_Sym *isym;
5452
5453                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5454                                               abfd, r_symndx);
5455                 if (isym == NULL)
5456                   return FALSE;
5457
5458                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5459               }
5460
5461             if (dest != sec)
5462               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5463           }
5464           /* Fall through.  */
5465
5466         case R_PPC64_REL24:
5467           if (h != NULL && ifunc == NULL)
5468             {
5469               /* We may need a .plt entry if the function this reloc
5470                  refers to is in a shared lib.  */
5471               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5472                 return FALSE;
5473               h->needs_plt = 1;
5474               if (h->root.root.string[0] == '.'
5475                   && h->root.root.string[1] != '\0')
5476                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5477               if (h == tga || h == dottga)
5478                 sec->has_tls_reloc = 1;
5479             }
5480           break;
5481
5482         case R_PPC64_TPREL64:
5483           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5484           if (!info->executable)
5485             info->flags |= DF_STATIC_TLS;
5486           goto dotlstoc;
5487
5488         case R_PPC64_DTPMOD64:
5489           if (rel + 1 < rel_end
5490               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5491               && rel[1].r_offset == rel->r_offset + 8)
5492             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5493           else
5494             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5495           goto dotlstoc;
5496
5497         case R_PPC64_DTPREL64:
5498           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5499           if (rel != relocs
5500               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5501               && rel[-1].r_offset == rel->r_offset - 8)
5502             /* This is the second reloc of a dtpmod, dtprel pair.
5503                Don't mark with TLS_DTPREL.  */
5504             goto dodyn;
5505
5506         dotlstoc:
5507           sec->has_tls_reloc = 1;
5508           if (h != NULL)
5509             {
5510               struct ppc_link_hash_entry *eh;
5511               eh = (struct ppc_link_hash_entry *) h;
5512               eh->tls_mask |= tls_type;
5513             }
5514           else
5515             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5516                                         rel->r_addend, tls_type))
5517               return FALSE;
5518
5519           ppc64_sec = ppc64_elf_section_data (sec);
5520           if (ppc64_sec->sec_type != sec_toc)
5521             {
5522               bfd_size_type amt;
5523
5524               /* One extra to simplify get_tls_mask.  */
5525               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5526               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5527               if (ppc64_sec->u.toc.symndx == NULL)
5528                 return FALSE;
5529               amt = sec->size * sizeof (bfd_vma) / 8;
5530               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5531               if (ppc64_sec->u.toc.add == NULL)
5532                 return FALSE;
5533               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5534               ppc64_sec->sec_type = sec_toc;
5535             }
5536           BFD_ASSERT (rel->r_offset % 8 == 0);
5537           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5538           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5539
5540           /* Mark the second slot of a GD or LD entry.
5541              -1 to indicate GD and -2 to indicate LD.  */
5542           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5543             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5544           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5545             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5546           goto dodyn;
5547
5548         case R_PPC64_TPREL16:
5549         case R_PPC64_TPREL16_LO:
5550         case R_PPC64_TPREL16_HI:
5551         case R_PPC64_TPREL16_HA:
5552         case R_PPC64_TPREL16_DS:
5553         case R_PPC64_TPREL16_LO_DS:
5554         case R_PPC64_TPREL16_HIGH:
5555         case R_PPC64_TPREL16_HIGHA:
5556         case R_PPC64_TPREL16_HIGHER:
5557         case R_PPC64_TPREL16_HIGHERA:
5558         case R_PPC64_TPREL16_HIGHEST:
5559         case R_PPC64_TPREL16_HIGHESTA:
5560           if (info->shared)
5561             {
5562               if (!info->executable)
5563                 info->flags |= DF_STATIC_TLS;
5564               goto dodyn;
5565             }
5566           break;
5567
5568         case R_PPC64_ADDR64:
5569           if (opd_sym_map != NULL
5570               && rel + 1 < rel_end
5571               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5572             {
5573               if (h != NULL)
5574                 {
5575                   if (h->root.root.string[0] == '.'
5576                       && h->root.root.string[1] != 0
5577                       && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5578                     ;
5579                   else
5580                     ((struct ppc_link_hash_entry *) h)->is_func = 1;
5581                 }
5582               else
5583                 {
5584                   asection *s;
5585                   Elf_Internal_Sym *isym;
5586
5587                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5588                                                 abfd, r_symndx);
5589                   if (isym == NULL)
5590                     return FALSE;
5591
5592                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5593                   if (s != NULL && s != sec)
5594                     opd_sym_map[rel->r_offset / 8] = s;
5595                 }
5596             }
5597           /* Fall through.  */
5598
5599         case R_PPC64_REL30:
5600         case R_PPC64_REL32:
5601         case R_PPC64_REL64:
5602         case R_PPC64_ADDR14:
5603         case R_PPC64_ADDR14_BRNTAKEN:
5604         case R_PPC64_ADDR14_BRTAKEN:
5605         case R_PPC64_ADDR16:
5606         case R_PPC64_ADDR16_DS:
5607         case R_PPC64_ADDR16_HA:
5608         case R_PPC64_ADDR16_HI:
5609         case R_PPC64_ADDR16_HIGH:
5610         case R_PPC64_ADDR16_HIGHA:
5611         case R_PPC64_ADDR16_HIGHER:
5612         case R_PPC64_ADDR16_HIGHERA:
5613         case R_PPC64_ADDR16_HIGHEST:
5614         case R_PPC64_ADDR16_HIGHESTA:
5615         case R_PPC64_ADDR16_LO:
5616         case R_PPC64_ADDR16_LO_DS:
5617         case R_PPC64_ADDR24:
5618         case R_PPC64_ADDR32:
5619         case R_PPC64_UADDR16:
5620         case R_PPC64_UADDR32:
5621         case R_PPC64_UADDR64:
5622         case R_PPC64_TOC:
5623           if (h != NULL && !info->shared)
5624             /* We may need a copy reloc.  */
5625             h->non_got_ref = 1;
5626
5627           /* Don't propagate .opd relocs.  */
5628           if (NO_OPD_RELOCS && opd_sym_map != NULL)
5629             break;
5630
5631           /* If we are creating a shared library, and this is a reloc
5632              against a global symbol, or a non PC relative reloc
5633              against a local symbol, then we need to copy the reloc
5634              into the shared library.  However, if we are linking with
5635              -Bsymbolic, we do not need to copy a reloc against a
5636              global symbol which is defined in an object we are
5637              including in the link (i.e., DEF_REGULAR is set).  At
5638              this point we have not seen all the input files, so it is
5639              possible that DEF_REGULAR is not set now but will be set
5640              later (it is never cleared).  In case of a weak definition,
5641              DEF_REGULAR may be cleared later by a strong definition in
5642              a shared library.  We account for that possibility below by
5643              storing information in the dyn_relocs field of the hash
5644              table entry.  A similar situation occurs when creating
5645              shared libraries and symbol visibility changes render the
5646              symbol local.
5647
5648              If on the other hand, we are creating an executable, we
5649              may need to keep relocations for symbols satisfied by a
5650              dynamic library if we manage to avoid copy relocs for the
5651              symbol.  */
5652         dodyn:
5653           if ((info->shared
5654                && (must_be_dyn_reloc (info, r_type)
5655                    || (h != NULL
5656                        && (!SYMBOLIC_BIND (info, h)
5657                            || h->root.type == bfd_link_hash_defweak
5658                            || !h->def_regular))))
5659               || (ELIMINATE_COPY_RELOCS
5660                   && !info->shared
5661                   && h != NULL
5662                   && (h->root.type == bfd_link_hash_defweak
5663                       || !h->def_regular))
5664               || (!info->shared
5665                   && ifunc != NULL))
5666             {
5667               /* We must copy these reloc types into the output file.
5668                  Create a reloc section in dynobj and make room for
5669                  this reloc.  */
5670               if (sreloc == NULL)
5671                 {
5672                   sreloc = _bfd_elf_make_dynamic_reloc_section
5673                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5674
5675                   if (sreloc == NULL)
5676                     return FALSE;
5677                 }
5678
5679               /* If this is a global symbol, we count the number of
5680                  relocations we need for this symbol.  */
5681               if (h != NULL)
5682                 {
5683                   struct elf_dyn_relocs *p;
5684                   struct elf_dyn_relocs **head;
5685
5686                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5687                   p = *head;
5688                   if (p == NULL || p->sec != sec)
5689                     {
5690                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5691                       if (p == NULL)
5692                         return FALSE;
5693                       p->next = *head;
5694                       *head = p;
5695                       p->sec = sec;
5696                       p->count = 0;
5697                       p->pc_count = 0;
5698                     }
5699                   p->count += 1;
5700                   if (!must_be_dyn_reloc (info, r_type))
5701                     p->pc_count += 1;
5702                 }
5703               else
5704                 {
5705                   /* Track dynamic relocs needed for local syms too.
5706                      We really need local syms available to do this
5707                      easily.  Oh well.  */
5708                   struct ppc_dyn_relocs *p;
5709                   struct ppc_dyn_relocs **head;
5710                   bfd_boolean is_ifunc;
5711                   asection *s;
5712                   void *vpp;
5713                   Elf_Internal_Sym *isym;
5714
5715                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5716                                                 abfd, r_symndx);
5717                   if (isym == NULL)
5718                     return FALSE;
5719
5720                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5721                   if (s == NULL)
5722                     s = sec;
5723
5724                   vpp = &elf_section_data (s)->local_dynrel;
5725                   head = (struct ppc_dyn_relocs **) vpp;
5726                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5727                   p = *head;
5728                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5729                     p = p->next;
5730                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5731                     {
5732                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5733                       if (p == NULL)
5734                         return FALSE;
5735                       p->next = *head;
5736                       *head = p;
5737                       p->sec = sec;
5738                       p->ifunc = is_ifunc;
5739                       p->count = 0;
5740                     }
5741                   p->count += 1;
5742                 }
5743             }
5744           break;
5745
5746         default:
5747           break;
5748         }
5749     }
5750
5751   return TRUE;
5752 }
5753
5754 /* Merge backend specific data from an object file to the output
5755    object file when linking.  */
5756
5757 static bfd_boolean
5758 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5759 {
5760   unsigned long iflags, oflags;
5761
5762   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5763     return TRUE;
5764
5765   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5766     return TRUE;
5767
5768   if (!_bfd_generic_verify_endian_match (ibfd, obfd))
5769     return FALSE;
5770
5771   iflags = elf_elfheader (ibfd)->e_flags;
5772   oflags = elf_elfheader (obfd)->e_flags;
5773
5774   if (!elf_flags_init (obfd) || oflags == 0)
5775     {
5776       elf_flags_init (obfd) = TRUE;
5777       elf_elfheader (obfd)->e_flags = iflags;
5778     }
5779   else if (iflags == oflags || iflags == 0)
5780     ;
5781   else if (iflags & ~EF_PPC64_ABI)
5782     {
5783       (*_bfd_error_handler)
5784         (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
5785       bfd_set_error (bfd_error_bad_value);
5786       return FALSE;
5787     }
5788   else
5789     {
5790       (*_bfd_error_handler)
5791         (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
5792          ibfd, iflags, oflags);
5793       bfd_set_error (bfd_error_bad_value);
5794       return FALSE;
5795     }
5796
5797   /* Merge Tag_compatibility attributes and any common GNU ones.  */
5798   _bfd_elf_merge_object_attributes (ibfd, obfd);
5799
5800   return TRUE;
5801 }
5802
5803 static bfd_boolean
5804 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5805 {
5806   /* Print normal ELF private data.  */
5807   _bfd_elf_print_private_bfd_data (abfd, ptr);
5808
5809   if (elf_elfheader (abfd)->e_flags != 0)
5810     {
5811       FILE *file = ptr;
5812
5813       /* xgettext:c-format */
5814       fprintf (file, _("private flags = 0x%lx:"),
5815                elf_elfheader (abfd)->e_flags);
5816
5817       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5818         fprintf (file, _(" [abiv%ld]"),
5819                  elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5820       fputc ('\n', file);
5821     }
5822
5823   return TRUE;
5824 }
5825
5826 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
5827    of the code entry point, and its section.  */
5828
5829 static bfd_vma
5830 opd_entry_value (asection *opd_sec,
5831                  bfd_vma offset,
5832                  asection **code_sec,
5833                  bfd_vma *code_off,
5834                  bfd_boolean in_code_sec)
5835 {
5836   bfd *opd_bfd = opd_sec->owner;
5837   Elf_Internal_Rela *relocs;
5838   Elf_Internal_Rela *lo, *hi, *look;
5839   bfd_vma val;
5840
5841   /* No relocs implies we are linking a --just-symbols object, or looking
5842      at a final linked executable with addr2line or somesuch.  */
5843   if (opd_sec->reloc_count == 0)
5844     {
5845       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5846
5847       if (contents == NULL)
5848         {
5849           if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5850             return (bfd_vma) -1;
5851           ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5852         }
5853
5854       val = bfd_get_64 (opd_bfd, contents + offset);
5855       if (code_sec != NULL)
5856         {
5857           asection *sec, *likely = NULL;
5858
5859           if (in_code_sec)
5860             {
5861               sec = *code_sec;
5862               if (sec->vma <= val
5863                   && val < sec->vma + sec->size)
5864                 likely = sec;
5865               else
5866                 val = -1;
5867             }
5868           else
5869             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5870               if (sec->vma <= val
5871                   && (sec->flags & SEC_LOAD) != 0
5872                   && (sec->flags & SEC_ALLOC) != 0)
5873                 likely = sec;
5874           if (likely != NULL)
5875             {
5876               *code_sec = likely;
5877               if (code_off != NULL)
5878                 *code_off = val - likely->vma;
5879             }
5880         }
5881       return val;
5882     }
5883
5884   BFD_ASSERT (is_ppc64_elf (opd_bfd));
5885
5886   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5887   if (relocs == NULL)
5888     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5889
5890   /* Go find the opd reloc at the sym address.  */
5891   lo = relocs;
5892   BFD_ASSERT (lo != NULL);
5893   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5894   val = (bfd_vma) -1;
5895   while (lo < hi)
5896     {
5897       look = lo + (hi - lo) / 2;
5898       if (look->r_offset < offset)
5899         lo = look + 1;
5900       else if (look->r_offset > offset)
5901         hi = look;
5902       else
5903         {
5904           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5905
5906           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5907               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5908             {
5909               unsigned long symndx = ELF64_R_SYM (look->r_info);
5910               asection *sec;
5911
5912               if (symndx < symtab_hdr->sh_info
5913                   || elf_sym_hashes (opd_bfd) == NULL)
5914                 {
5915                   Elf_Internal_Sym *sym;
5916
5917                   sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5918                   if (sym == NULL)
5919                     {
5920                       size_t symcnt = symtab_hdr->sh_info;
5921                       if (elf_sym_hashes (opd_bfd) == NULL)
5922                         symcnt = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
5923                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, symcnt,
5924                                                   0, NULL, NULL, NULL);
5925                       if (sym == NULL)
5926                         break;
5927                       symtab_hdr->contents = (bfd_byte *) sym;
5928                     }
5929
5930                   sym += symndx;
5931                   val = sym->st_value;
5932                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5933                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5934                 }
5935               else
5936                 {
5937                   struct elf_link_hash_entry **sym_hashes;
5938                   struct elf_link_hash_entry *rh;
5939
5940                   sym_hashes = elf_sym_hashes (opd_bfd);
5941                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
5942                   if (rh != NULL)
5943                     {
5944                       rh = elf_follow_link (rh);
5945                       BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5946                                   || rh->root.type == bfd_link_hash_defweak);
5947                       val = rh->root.u.def.value;
5948                       sec = rh->root.u.def.section;
5949                     }
5950                   else
5951                     {
5952                       /* Handle the odd case where we can be called
5953                          during bfd_elf_link_add_symbols before the
5954                          symbol hashes have been fully populated.  */
5955                       Elf_Internal_Sym *sym;
5956
5957                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, 1,
5958                                                   symndx, NULL, NULL, NULL);
5959                       if (sym == NULL)
5960                         break;
5961
5962                       val = sym->st_value;
5963                       sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5964                       free (sym);
5965                     }
5966                 }
5967               val += look->r_addend;
5968               if (code_off != NULL)
5969                 *code_off = val;
5970               if (code_sec != NULL)
5971                 {
5972                   if (in_code_sec && *code_sec != sec)
5973                     return -1;
5974                   else
5975                     *code_sec = sec;
5976                 }
5977               if (sec != NULL && sec->output_section != NULL)
5978                 val += sec->output_section->vma + sec->output_offset;
5979             }
5980           break;
5981         }
5982     }
5983
5984   return val;
5985 }
5986
5987 /* If the ELF symbol SYM might be a function in SEC, return the
5988    function size and set *CODE_OFF to the function's entry point,
5989    otherwise return zero.  */
5990
5991 static bfd_size_type
5992 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5993                               bfd_vma *code_off)
5994 {
5995   bfd_size_type size;
5996
5997   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5998                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5999     return 0;
6000
6001   size = 0;
6002   if (!(sym->flags & BSF_SYNTHETIC))
6003     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6004
6005   if (strcmp (sym->section->name, ".opd") == 0)
6006     {
6007       if (opd_entry_value (sym->section, sym->value,
6008                            &sec, code_off, TRUE) == (bfd_vma) -1)
6009         return 0;
6010       /* An old ABI binary with dot-syms has a size of 24 on the .opd
6011          symbol.  This size has nothing to do with the code size of the
6012          function, which is what we're supposed to return, but the
6013          code size isn't available without looking up the dot-sym.
6014          However, doing that would be a waste of time particularly
6015          since elf_find_function will look at the dot-sym anyway.
6016          Now, elf_find_function will keep the largest size of any
6017          function sym found at the code address of interest, so return
6018          1 here to avoid it incorrectly caching a larger function size
6019          for a small function.  This does mean we return the wrong
6020          size for a new-ABI function of size 24, but all that does is
6021          disable caching for such functions.  */
6022       if (size == 24)
6023         size = 1;
6024     }
6025   else
6026     {
6027       if (sym->section != sec)
6028         return 0;
6029       *code_off = sym->value;
6030     }
6031   if (size == 0)
6032     size = 1;
6033   return size;
6034 }
6035
6036 /* Return true if symbol is defined in a regular object file.  */
6037
6038 static bfd_boolean
6039 is_static_defined (struct elf_link_hash_entry *h)
6040 {
6041   return ((h->root.type == bfd_link_hash_defined
6042            || h->root.type == bfd_link_hash_defweak)
6043           && h->root.u.def.section != NULL
6044           && h->root.u.def.section->output_section != NULL);
6045 }
6046
6047 /* If FDH is a function descriptor symbol, return the associated code
6048    entry symbol if it is defined.  Return NULL otherwise.  */
6049
6050 static struct ppc_link_hash_entry *
6051 defined_code_entry (struct ppc_link_hash_entry *fdh)
6052 {
6053   if (fdh->is_func_descriptor)
6054     {
6055       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6056       if (fh->elf.root.type == bfd_link_hash_defined
6057           || fh->elf.root.type == bfd_link_hash_defweak)
6058         return fh;
6059     }
6060   return NULL;
6061 }
6062
6063 /* If FH is a function code entry symbol, return the associated
6064    function descriptor symbol if it is defined.  Return NULL otherwise.  */
6065
6066 static struct ppc_link_hash_entry *
6067 defined_func_desc (struct ppc_link_hash_entry *fh)
6068 {
6069   if (fh->oh != NULL
6070       && fh->oh->is_func_descriptor)
6071     {
6072       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6073       if (fdh->elf.root.type == bfd_link_hash_defined
6074           || fdh->elf.root.type == bfd_link_hash_defweak)
6075         return fdh;
6076     }
6077   return NULL;
6078 }
6079
6080 /* Mark all our entry sym sections, both opd and code section.  */
6081
6082 static void
6083 ppc64_elf_gc_keep (struct bfd_link_info *info)
6084 {
6085   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6086   struct bfd_sym_chain *sym;
6087
6088   if (htab == NULL)
6089     return;
6090
6091   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6092     {
6093       struct ppc_link_hash_entry *eh, *fh;
6094       asection *sec;
6095
6096       eh = (struct ppc_link_hash_entry *)
6097         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6098       if (eh == NULL)
6099         continue;
6100       if (eh->elf.root.type != bfd_link_hash_defined
6101           && eh->elf.root.type != bfd_link_hash_defweak)
6102         continue;
6103
6104       fh = defined_code_entry (eh);
6105       if (fh != NULL)
6106         {
6107           sec = fh->elf.root.u.def.section;
6108           sec->flags |= SEC_KEEP;
6109         }
6110       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6111                && opd_entry_value (eh->elf.root.u.def.section,
6112                                    eh->elf.root.u.def.value,
6113                                    &sec, NULL, FALSE) != (bfd_vma) -1)
6114         sec->flags |= SEC_KEEP;
6115
6116       sec = eh->elf.root.u.def.section;
6117       sec->flags |= SEC_KEEP;
6118     }
6119 }
6120
6121 /* Mark sections containing dynamically referenced symbols.  When
6122    building shared libraries, we must assume that any visible symbol is
6123    referenced.  */
6124
6125 static bfd_boolean
6126 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6127 {
6128   struct bfd_link_info *info = (struct bfd_link_info *) inf;
6129   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6130   struct ppc_link_hash_entry *fdh;
6131
6132   /* Dynamic linking info is on the func descriptor sym.  */
6133   fdh = defined_func_desc (eh);
6134   if (fdh != NULL)
6135     eh = fdh;
6136
6137   if ((eh->elf.root.type == bfd_link_hash_defined
6138        || eh->elf.root.type == bfd_link_hash_defweak)
6139       && (eh->elf.ref_dynamic
6140           || (!info->executable
6141               && eh->elf.def_regular
6142               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6143               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6144               && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6145                   || !bfd_hide_sym_by_version (info->version_info,
6146                                                eh->elf.root.root.string)))))
6147     {
6148       asection *code_sec;
6149       struct ppc_link_hash_entry *fh;
6150
6151       eh->elf.root.u.def.section->flags |= SEC_KEEP;
6152
6153       /* Function descriptor syms cause the associated
6154          function code sym section to be marked.  */
6155       fh = defined_code_entry (eh);
6156       if (fh != NULL)
6157         {
6158           code_sec = fh->elf.root.u.def.section;
6159           code_sec->flags |= SEC_KEEP;
6160         }
6161       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6162                && opd_entry_value (eh->elf.root.u.def.section,
6163                                    eh->elf.root.u.def.value,
6164                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
6165         code_sec->flags |= SEC_KEEP;
6166     }
6167
6168   return TRUE;
6169 }
6170
6171 /* Return the section that should be marked against GC for a given
6172    relocation.  */
6173
6174 static asection *
6175 ppc64_elf_gc_mark_hook (asection *sec,
6176                         struct bfd_link_info *info,
6177                         Elf_Internal_Rela *rel,
6178                         struct elf_link_hash_entry *h,
6179                         Elf_Internal_Sym *sym)
6180 {
6181   asection *rsec;
6182
6183   /* Syms return NULL if we're marking .opd, so we avoid marking all
6184      function sections, as all functions are referenced in .opd.  */
6185   rsec = NULL;
6186   if (get_opd_info (sec) != NULL)
6187     return rsec;
6188
6189   if (h != NULL)
6190     {
6191       enum elf_ppc64_reloc_type r_type;
6192       struct ppc_link_hash_entry *eh, *fh, *fdh;
6193
6194       r_type = ELF64_R_TYPE (rel->r_info);
6195       switch (r_type)
6196         {
6197         case R_PPC64_GNU_VTINHERIT:
6198         case R_PPC64_GNU_VTENTRY:
6199           break;
6200
6201         default:
6202           switch (h->root.type)
6203             {
6204             case bfd_link_hash_defined:
6205             case bfd_link_hash_defweak:
6206               eh = (struct ppc_link_hash_entry *) h;
6207               fdh = defined_func_desc (eh);
6208               if (fdh != NULL)
6209                 eh = fdh;
6210
6211               /* Function descriptor syms cause the associated
6212                  function code sym section to be marked.  */
6213               fh = defined_code_entry (eh);
6214               if (fh != NULL)
6215                 {
6216                   /* They also mark their opd section.  */
6217                   eh->elf.root.u.def.section->gc_mark = 1;
6218
6219                   rsec = fh->elf.root.u.def.section;
6220                 }
6221               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6222                        && opd_entry_value (eh->elf.root.u.def.section,
6223                                            eh->elf.root.u.def.value,
6224                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
6225                 eh->elf.root.u.def.section->gc_mark = 1;
6226               else
6227                 rsec = h->root.u.def.section;
6228               break;
6229
6230             case bfd_link_hash_common:
6231               rsec = h->root.u.c.p->section;
6232               break;
6233
6234             default:
6235               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6236             }
6237         }
6238     }
6239   else
6240     {
6241       struct _opd_sec_data *opd;
6242
6243       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6244       opd = get_opd_info (rsec);
6245       if (opd != NULL && opd->func_sec != NULL)
6246         {
6247           rsec->gc_mark = 1;
6248
6249           rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
6250         }
6251     }
6252
6253   return rsec;
6254 }
6255
6256 /* Update the .got, .plt. and dynamic reloc reference counts for the
6257    section being removed.  */
6258
6259 static bfd_boolean
6260 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6261                          asection *sec, const Elf_Internal_Rela *relocs)
6262 {
6263   struct ppc_link_hash_table *htab;
6264   Elf_Internal_Shdr *symtab_hdr;
6265   struct elf_link_hash_entry **sym_hashes;
6266   struct got_entry **local_got_ents;
6267   const Elf_Internal_Rela *rel, *relend;
6268
6269   if (info->relocatable)
6270     return TRUE;
6271
6272   if ((sec->flags & SEC_ALLOC) == 0)
6273     return TRUE;
6274
6275   elf_section_data (sec)->local_dynrel = NULL;
6276
6277   htab = ppc_hash_table (info);
6278   if (htab == NULL)
6279     return FALSE;
6280
6281   symtab_hdr = &elf_symtab_hdr (abfd);
6282   sym_hashes = elf_sym_hashes (abfd);
6283   local_got_ents = elf_local_got_ents (abfd);
6284
6285   relend = relocs + sec->reloc_count;
6286   for (rel = relocs; rel < relend; rel++)
6287     {
6288       unsigned long r_symndx;
6289       enum elf_ppc64_reloc_type r_type;
6290       struct elf_link_hash_entry *h = NULL;
6291       unsigned char tls_type = 0;
6292
6293       r_symndx = ELF64_R_SYM (rel->r_info);
6294       r_type = ELF64_R_TYPE (rel->r_info);
6295       if (r_symndx >= symtab_hdr->sh_info)
6296         {
6297           struct ppc_link_hash_entry *eh;
6298           struct elf_dyn_relocs **pp;
6299           struct elf_dyn_relocs *p;
6300
6301           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6302           h = elf_follow_link (h);
6303           eh = (struct ppc_link_hash_entry *) h;
6304
6305           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6306             if (p->sec == sec)
6307               {
6308                 /* Everything must go for SEC.  */
6309                 *pp = p->next;
6310                 break;
6311               }
6312         }
6313
6314       if (is_branch_reloc (r_type))
6315         {
6316           struct plt_entry **ifunc = NULL;
6317           if (h != NULL)
6318             {
6319               if (h->type == STT_GNU_IFUNC)
6320                 ifunc = &h->plt.plist;
6321             }
6322           else if (local_got_ents != NULL)
6323             {
6324               struct plt_entry **local_plt = (struct plt_entry **)
6325                 (local_got_ents + symtab_hdr->sh_info);
6326               unsigned char *local_got_tls_masks = (unsigned char *)
6327                 (local_plt + symtab_hdr->sh_info);
6328               if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6329                 ifunc = local_plt + r_symndx;
6330             }
6331           if (ifunc != NULL)
6332             {
6333               struct plt_entry *ent;
6334
6335               for (ent = *ifunc; ent != NULL; ent = ent->next)
6336                 if (ent->addend == rel->r_addend)
6337                   break;
6338               if (ent == NULL)
6339                 abort ();
6340               if (ent->plt.refcount > 0)
6341                 ent->plt.refcount -= 1;
6342               continue;
6343             }
6344         }
6345
6346       switch (r_type)
6347         {
6348         case R_PPC64_GOT_TLSLD16:
6349         case R_PPC64_GOT_TLSLD16_LO:
6350         case R_PPC64_GOT_TLSLD16_HI:
6351         case R_PPC64_GOT_TLSLD16_HA:
6352           tls_type = TLS_TLS | TLS_LD;
6353           goto dogot;
6354
6355         case R_PPC64_GOT_TLSGD16:
6356         case R_PPC64_GOT_TLSGD16_LO:
6357         case R_PPC64_GOT_TLSGD16_HI:
6358         case R_PPC64_GOT_TLSGD16_HA:
6359           tls_type = TLS_TLS | TLS_GD;
6360           goto dogot;
6361
6362         case R_PPC64_GOT_TPREL16_DS:
6363         case R_PPC64_GOT_TPREL16_LO_DS:
6364         case R_PPC64_GOT_TPREL16_HI:
6365         case R_PPC64_GOT_TPREL16_HA:
6366           tls_type = TLS_TLS | TLS_TPREL;
6367           goto dogot;
6368
6369         case R_PPC64_GOT_DTPREL16_DS:
6370         case R_PPC64_GOT_DTPREL16_LO_DS:
6371         case R_PPC64_GOT_DTPREL16_HI:
6372         case R_PPC64_GOT_DTPREL16_HA:
6373           tls_type = TLS_TLS | TLS_DTPREL;
6374           goto dogot;
6375
6376         case R_PPC64_GOT16:
6377         case R_PPC64_GOT16_DS:
6378         case R_PPC64_GOT16_HA:
6379         case R_PPC64_GOT16_HI:
6380         case R_PPC64_GOT16_LO:
6381         case R_PPC64_GOT16_LO_DS:
6382         dogot:
6383           {
6384             struct got_entry *ent;
6385
6386             if (h != NULL)
6387               ent = h->got.glist;
6388             else
6389               ent = local_got_ents[r_symndx];
6390
6391             for (; ent != NULL; ent = ent->next)
6392               if (ent->addend == rel->r_addend
6393                   && ent->owner == abfd
6394                   && ent->tls_type == tls_type)
6395                 break;
6396             if (ent == NULL)
6397               abort ();
6398             if (ent->got.refcount > 0)
6399               ent->got.refcount -= 1;
6400           }
6401           break;
6402
6403         case R_PPC64_PLT16_HA:
6404         case R_PPC64_PLT16_HI:
6405         case R_PPC64_PLT16_LO:
6406         case R_PPC64_PLT32:
6407         case R_PPC64_PLT64:
6408         case R_PPC64_REL14:
6409         case R_PPC64_REL14_BRNTAKEN:
6410         case R_PPC64_REL14_BRTAKEN:
6411         case R_PPC64_REL24:
6412           if (h != NULL)
6413             {
6414               struct plt_entry *ent;
6415
6416               for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6417                 if (ent->addend == rel->r_addend)
6418                   break;
6419               if (ent != NULL && ent->plt.refcount > 0)
6420                 ent->plt.refcount -= 1;
6421             }
6422           break;
6423
6424         default:
6425           break;
6426         }
6427     }
6428   return TRUE;
6429 }
6430
6431 /* The maximum size of .sfpr.  */
6432 #define SFPR_MAX (218*4)
6433
6434 struct sfpr_def_parms
6435 {
6436   const char name[12];
6437   unsigned char lo, hi;
6438   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6439   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6440 };
6441
6442 /* Auto-generate _save*, _rest* functions in .sfpr.  */
6443
6444 static bfd_boolean
6445 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6446 {
6447   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6448   unsigned int i;
6449   size_t len = strlen (parm->name);
6450   bfd_boolean writing = FALSE;
6451   char sym[16];
6452
6453   if (htab == NULL)
6454     return FALSE;
6455
6456   memcpy (sym, parm->name, len);
6457   sym[len + 2] = 0;
6458
6459   for (i = parm->lo; i <= parm->hi; i++)
6460     {
6461       struct elf_link_hash_entry *h;
6462
6463       sym[len + 0] = i / 10 + '0';
6464       sym[len + 1] = i % 10 + '0';
6465       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6466       if (h != NULL
6467           && !h->def_regular)
6468         {
6469           h->root.type = bfd_link_hash_defined;
6470           h->root.u.def.section = htab->sfpr;
6471           h->root.u.def.value = htab->sfpr->size;
6472           h->type = STT_FUNC;
6473           h->def_regular = 1;
6474           _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6475           writing = TRUE;
6476           if (htab->sfpr->contents == NULL)
6477             {
6478               htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6479               if (htab->sfpr->contents == NULL)
6480                 return FALSE;
6481             }
6482         }
6483       if (writing)
6484         {
6485           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6486           if (i != parm->hi)
6487             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6488           else
6489             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6490           htab->sfpr->size = p - htab->sfpr->contents;
6491         }
6492     }
6493
6494   return TRUE;
6495 }
6496
6497 static bfd_byte *
6498 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6499 {
6500   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6501   return p + 4;
6502 }
6503
6504 static bfd_byte *
6505 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6506 {
6507   p = savegpr0 (abfd, p, r);
6508   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6509   p = p + 4;
6510   bfd_put_32 (abfd, BLR, p);
6511   return p + 4;
6512 }
6513
6514 static bfd_byte *
6515 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6516 {
6517   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6518   return p + 4;
6519 }
6520
6521 static bfd_byte *
6522 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6523 {
6524   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6525   p = p + 4;
6526   p = restgpr0 (abfd, p, r);
6527   bfd_put_32 (abfd, MTLR_R0, p);
6528   p = p + 4;
6529   if (r == 29)
6530     {
6531       p = restgpr0 (abfd, p, 30);
6532       p = restgpr0 (abfd, p, 31);
6533     }
6534   bfd_put_32 (abfd, BLR, p);
6535   return p + 4;
6536 }
6537
6538 static bfd_byte *
6539 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6540 {
6541   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6542   return p + 4;
6543 }
6544
6545 static bfd_byte *
6546 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6547 {
6548   p = savegpr1 (abfd, p, r);
6549   bfd_put_32 (abfd, BLR, p);
6550   return p + 4;
6551 }
6552
6553 static bfd_byte *
6554 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6555 {
6556   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6557   return p + 4;
6558 }
6559
6560 static bfd_byte *
6561 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6562 {
6563   p = restgpr1 (abfd, p, r);
6564   bfd_put_32 (abfd, BLR, p);
6565   return p + 4;
6566 }
6567
6568 static bfd_byte *
6569 savefpr (bfd *abfd, bfd_byte *p, int r)
6570 {
6571   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6572   return p + 4;
6573 }
6574
6575 static bfd_byte *
6576 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6577 {
6578   p = savefpr (abfd, p, r);
6579   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6580   p = p + 4;
6581   bfd_put_32 (abfd, BLR, p);
6582   return p + 4;
6583 }
6584
6585 static bfd_byte *
6586 restfpr (bfd *abfd, bfd_byte *p, int r)
6587 {
6588   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6589   return p + 4;
6590 }
6591
6592 static bfd_byte *
6593 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6594 {
6595   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6596   p = p + 4;
6597   p = restfpr (abfd, p, r);
6598   bfd_put_32 (abfd, MTLR_R0, p);
6599   p = p + 4;
6600   if (r == 29)
6601     {
6602       p = restfpr (abfd, p, 30);
6603       p = restfpr (abfd, p, 31);
6604     }
6605   bfd_put_32 (abfd, BLR, p);
6606   return p + 4;
6607 }
6608
6609 static bfd_byte *
6610 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6611 {
6612   p = savefpr (abfd, p, r);
6613   bfd_put_32 (abfd, BLR, p);
6614   return p + 4;
6615 }
6616
6617 static bfd_byte *
6618 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6619 {
6620   p = restfpr (abfd, p, r);
6621   bfd_put_32 (abfd, BLR, p);
6622   return p + 4;
6623 }
6624
6625 static bfd_byte *
6626 savevr (bfd *abfd, bfd_byte *p, int r)
6627 {
6628   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6629   p = p + 4;
6630   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6631   return p + 4;
6632 }
6633
6634 static bfd_byte *
6635 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6636 {
6637   p = savevr (abfd, p, r);
6638   bfd_put_32 (abfd, BLR, p);
6639   return p + 4;
6640 }
6641
6642 static bfd_byte *
6643 restvr (bfd *abfd, bfd_byte *p, int r)
6644 {
6645   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6646   p = p + 4;
6647   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6648   return p + 4;
6649 }
6650
6651 static bfd_byte *
6652 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6653 {
6654   p = restvr (abfd, p, r);
6655   bfd_put_32 (abfd, BLR, p);
6656   return p + 4;
6657 }
6658
6659 /* Called via elf_link_hash_traverse to transfer dynamic linking
6660    information on function code symbol entries to their corresponding
6661    function descriptor symbol entries.  */
6662
6663 static bfd_boolean
6664 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6665 {
6666   struct bfd_link_info *info;
6667   struct ppc_link_hash_table *htab;
6668   struct plt_entry *ent;
6669   struct ppc_link_hash_entry *fh;
6670   struct ppc_link_hash_entry *fdh;
6671   bfd_boolean force_local;
6672
6673   fh = (struct ppc_link_hash_entry *) h;
6674   if (fh->elf.root.type == bfd_link_hash_indirect)
6675     return TRUE;
6676
6677   info = inf;
6678   htab = ppc_hash_table (info);
6679   if (htab == NULL)
6680     return FALSE;
6681
6682   /* Resolve undefined references to dot-symbols as the value
6683      in the function descriptor, if we have one in a regular object.
6684      This is to satisfy cases like ".quad .foo".  Calls to functions
6685      in dynamic objects are handled elsewhere.  */
6686   if (fh->elf.root.type == bfd_link_hash_undefweak
6687       && fh->was_undefined
6688       && (fdh = defined_func_desc (fh)) != NULL
6689       && get_opd_info (fdh->elf.root.u.def.section) != NULL
6690       && opd_entry_value (fdh->elf.root.u.def.section,
6691                           fdh->elf.root.u.def.value,
6692                           &fh->elf.root.u.def.section,
6693                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6694     {
6695       fh->elf.root.type = fdh->elf.root.type;
6696       fh->elf.forced_local = 1;
6697       fh->elf.def_regular = fdh->elf.def_regular;
6698       fh->elf.def_dynamic = fdh->elf.def_dynamic;
6699     }
6700
6701   /* If this is a function code symbol, transfer dynamic linking
6702      information to the function descriptor symbol.  */
6703   if (!fh->is_func)
6704     return TRUE;
6705
6706   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6707     if (ent->plt.refcount > 0)
6708       break;
6709   if (ent == NULL
6710       || fh->elf.root.root.string[0] != '.'
6711       || fh->elf.root.root.string[1] == '\0')
6712     return TRUE;
6713
6714   /* Find the corresponding function descriptor symbol.  Create it
6715      as undefined if necessary.  */
6716
6717   fdh = lookup_fdh (fh, htab);
6718   if (fdh == NULL
6719       && !info->executable
6720       && (fh->elf.root.type == bfd_link_hash_undefined
6721           || fh->elf.root.type == bfd_link_hash_undefweak))
6722     {
6723       fdh = make_fdh (info, fh);
6724       if (fdh == NULL)
6725         return FALSE;
6726     }
6727
6728   /* Fake function descriptors are made undefweak.  If the function
6729      code symbol is strong undefined, make the fake sym the same.
6730      If the function code symbol is defined, then force the fake
6731      descriptor local;  We can't support overriding of symbols in a
6732      shared library on a fake descriptor.  */
6733
6734   if (fdh != NULL
6735       && fdh->fake
6736       && fdh->elf.root.type == bfd_link_hash_undefweak)
6737     {
6738       if (fh->elf.root.type == bfd_link_hash_undefined)
6739         {
6740           fdh->elf.root.type = bfd_link_hash_undefined;
6741           bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6742         }
6743       else if (fh->elf.root.type == bfd_link_hash_defined
6744                || fh->elf.root.type == bfd_link_hash_defweak)
6745         {
6746           _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6747         }
6748     }
6749
6750   if (fdh != NULL
6751       && !fdh->elf.forced_local
6752       && (!info->executable
6753           || fdh->elf.def_dynamic
6754           || fdh->elf.ref_dynamic
6755           || (fdh->elf.root.type == bfd_link_hash_undefweak
6756               && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6757     {
6758       if (fdh->elf.dynindx == -1)
6759         if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6760           return FALSE;
6761       fdh->elf.ref_regular |= fh->elf.ref_regular;
6762       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6763       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6764       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6765       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6766         {
6767           move_plt_plist (fh, fdh);
6768           fdh->elf.needs_plt = 1;
6769         }
6770       fdh->is_func_descriptor = 1;
6771       fdh->oh = fh;
6772       fh->oh = fdh;
6773     }
6774
6775   /* Now that the info is on the function descriptor, clear the
6776      function code sym info.  Any function code syms for which we
6777      don't have a definition in a regular file, we force local.
6778      This prevents a shared library from exporting syms that have
6779      been imported from another library.  Function code syms that
6780      are really in the library we must leave global to prevent the
6781      linker dragging in a definition from a static library.  */
6782   force_local = (!fh->elf.def_regular
6783                  || fdh == NULL
6784                  || !fdh->elf.def_regular
6785                  || fdh->elf.forced_local);
6786   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6787
6788   return TRUE;
6789 }
6790
6791 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
6792    this hook to a) provide some gcc support functions, and b) transfer
6793    dynamic linking information gathered so far on function code symbol
6794    entries, to their corresponding function descriptor symbol entries.  */
6795
6796 static bfd_boolean
6797 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6798                             struct bfd_link_info *info)
6799 {
6800   struct ppc_link_hash_table *htab;
6801   unsigned int i;
6802   static const struct sfpr_def_parms funcs[] =
6803     {
6804       { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6805       { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6806       { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6807       { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6808       { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6809       { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6810       { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6811       { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6812       { "._savef", 14, 31, savefpr, savefpr1_tail },
6813       { "._restf", 14, 31, restfpr, restfpr1_tail },
6814       { "_savevr_", 20, 31, savevr, savevr_tail },
6815       { "_restvr_", 20, 31, restvr, restvr_tail }
6816     };
6817
6818   htab = ppc_hash_table (info);
6819   if (htab == NULL)
6820     return FALSE;
6821
6822   if (!info->relocatable
6823       && htab->elf.hgot != NULL)
6824     _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6825
6826   if (htab->sfpr == NULL)
6827     /* We don't have any relocs.  */
6828     return TRUE;
6829
6830   /* Provide any missing _save* and _rest* functions.  */
6831   htab->sfpr->size = 0;
6832   if (!info->relocatable)
6833     for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6834       if (!sfpr_define (info, &funcs[i]))
6835         return FALSE;
6836
6837   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6838
6839   if (htab->sfpr->size == 0)
6840     htab->sfpr->flags |= SEC_EXCLUDE;
6841
6842   return TRUE;
6843 }
6844
6845 /* Adjust a symbol defined by a dynamic object and referenced by a
6846    regular object.  The current definition is in some section of the
6847    dynamic object, but we're not including those sections.  We have to
6848    change the definition to something the rest of the link can
6849    understand.  */
6850
6851 static bfd_boolean
6852 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6853                                  struct elf_link_hash_entry *h)
6854 {
6855   struct ppc_link_hash_table *htab;
6856   asection *s;
6857
6858   htab = ppc_hash_table (info);
6859   if (htab == NULL)
6860     return FALSE;
6861
6862   /* Deal with function syms.  */
6863   if (h->type == STT_FUNC
6864       || h->type == STT_GNU_IFUNC
6865       || h->needs_plt)
6866     {
6867       /* Clear procedure linkage table information for any symbol that
6868          won't need a .plt entry.  */
6869       struct plt_entry *ent;
6870       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6871         if (ent->plt.refcount > 0)
6872           break;
6873       if (ent == NULL
6874           || (h->type != STT_GNU_IFUNC
6875               && (SYMBOL_CALLS_LOCAL (info, h)
6876                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6877                       && h->root.type == bfd_link_hash_undefweak))))
6878         {
6879           h->plt.plist = NULL;
6880           h->needs_plt = 0;
6881         }
6882     }
6883   else
6884     h->plt.plist = NULL;
6885
6886   /* If this is a weak symbol, and there is a real definition, the
6887      processor independent code will have arranged for us to see the
6888      real definition first, and we can just use the same value.  */
6889   if (h->u.weakdef != NULL)
6890     {
6891       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6892                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
6893       h->root.u.def.section = h->u.weakdef->root.u.def.section;
6894       h->root.u.def.value = h->u.weakdef->root.u.def.value;
6895       if (ELIMINATE_COPY_RELOCS)
6896         h->non_got_ref = h->u.weakdef->non_got_ref;
6897       return TRUE;
6898     }
6899
6900   /* If we are creating a shared library, we must presume that the
6901      only references to the symbol are via the global offset table.
6902      For such cases we need not do anything here; the relocations will
6903      be handled correctly by relocate_section.  */
6904   if (info->shared)
6905     return TRUE;
6906
6907   /* If there are no references to this symbol that do not use the
6908      GOT, we don't need to generate a copy reloc.  */
6909   if (!h->non_got_ref)
6910     return TRUE;
6911
6912   /* Don't generate a copy reloc for symbols defined in the executable.  */
6913   if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6914     return TRUE;
6915
6916   if (ELIMINATE_COPY_RELOCS)
6917     {
6918       struct ppc_link_hash_entry * eh;
6919       struct elf_dyn_relocs *p;
6920
6921       eh = (struct ppc_link_hash_entry *) h;
6922       for (p = eh->dyn_relocs; p != NULL; p = p->next)
6923         {
6924           s = p->sec->output_section;
6925           if (s != NULL && (s->flags & SEC_READONLY) != 0)
6926             break;
6927         }
6928
6929       /* If we didn't find any dynamic relocs in read-only sections, then
6930          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
6931       if (p == NULL)
6932         {
6933           h->non_got_ref = 0;
6934           return TRUE;
6935         }
6936     }
6937
6938   if (h->plt.plist != NULL)
6939     {
6940       /* We should never get here, but unfortunately there are versions
6941          of gcc out there that improperly (for this ABI) put initialized
6942          function pointers, vtable refs and suchlike in read-only
6943          sections.  Allow them to proceed, but warn that this might
6944          break at runtime.  */
6945       info->callbacks->einfo
6946         (_("%P: copy reloc against `%T' requires lazy plt linking; "
6947            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6948          h->root.root.string);
6949     }
6950
6951   /* This is a reference to a symbol defined by a dynamic object which
6952      is not a function.  */
6953
6954   /* We must allocate the symbol in our .dynbss section, which will
6955      become part of the .bss section of the executable.  There will be
6956      an entry for this symbol in the .dynsym section.  The dynamic
6957      object will contain position independent code, so all references
6958      from the dynamic object to this symbol will go through the global
6959      offset table.  The dynamic linker will use the .dynsym entry to
6960      determine the address it must put in the global offset table, so
6961      both the dynamic object and the regular object will refer to the
6962      same memory location for the variable.  */
6963
6964   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6965      to copy the initial value out of the dynamic object and into the
6966      runtime process image.  We need to remember the offset into the
6967      .rela.bss section we are going to use.  */
6968   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6969     {
6970       htab->relbss->size += sizeof (Elf64_External_Rela);
6971       h->needs_copy = 1;
6972     }
6973
6974   s = htab->dynbss;
6975
6976   return _bfd_elf_adjust_dynamic_copy (h, s);
6977 }
6978
6979 /* If given a function descriptor symbol, hide both the function code
6980    sym and the descriptor.  */
6981 static void
6982 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6983                        struct elf_link_hash_entry *h,
6984                        bfd_boolean force_local)
6985 {
6986   struct ppc_link_hash_entry *eh;
6987   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6988
6989   eh = (struct ppc_link_hash_entry *) h;
6990   if (eh->is_func_descriptor)
6991     {
6992       struct ppc_link_hash_entry *fh = eh->oh;
6993
6994       if (fh == NULL)
6995         {
6996           const char *p, *q;
6997           struct ppc_link_hash_table *htab;
6998           char save;
6999
7000           /* We aren't supposed to use alloca in BFD because on
7001              systems which do not have alloca the version in libiberty
7002              calls xmalloc, which might cause the program to crash
7003              when it runs out of memory.  This function doesn't have a
7004              return status, so there's no way to gracefully return an
7005              error.  So cheat.  We know that string[-1] can be safely
7006              accessed;  It's either a string in an ELF string table,
7007              or allocated in an objalloc structure.  */
7008
7009           p = eh->elf.root.root.string - 1;
7010           save = *p;
7011           *(char *) p = '.';
7012           htab = ppc_hash_table (info);
7013           if (htab == NULL)
7014             return;
7015
7016           fh = (struct ppc_link_hash_entry *)
7017             elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7018           *(char *) p = save;
7019
7020           /* Unfortunately, if it so happens that the string we were
7021              looking for was allocated immediately before this string,
7022              then we overwrote the string terminator.  That's the only
7023              reason the lookup should fail.  */
7024           if (fh == NULL)
7025             {
7026               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7027               while (q >= eh->elf.root.root.string && *q == *p)
7028                 --q, --p;
7029               if (q < eh->elf.root.root.string && *p == '.')
7030                 fh = (struct ppc_link_hash_entry *)
7031                   elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7032             }
7033           if (fh != NULL)
7034             {
7035               eh->oh = fh;
7036               fh->oh = eh;
7037             }
7038         }
7039       if (fh != NULL)
7040         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7041     }
7042 }
7043
7044 static bfd_boolean
7045 get_sym_h (struct elf_link_hash_entry **hp,
7046            Elf_Internal_Sym **symp,
7047            asection **symsecp,
7048            unsigned char **tls_maskp,
7049            Elf_Internal_Sym **locsymsp,
7050            unsigned long r_symndx,
7051            bfd *ibfd)
7052 {
7053   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7054
7055   if (r_symndx >= symtab_hdr->sh_info)
7056     {
7057       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7058       struct elf_link_hash_entry *h;
7059
7060       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7061       h = elf_follow_link (h);
7062
7063       if (hp != NULL)
7064         *hp = h;
7065
7066       if (symp != NULL)
7067         *symp = NULL;
7068
7069       if (symsecp != NULL)
7070         {
7071           asection *symsec = NULL;
7072           if (h->root.type == bfd_link_hash_defined
7073               || h->root.type == bfd_link_hash_defweak)
7074             symsec = h->root.u.def.section;
7075           *symsecp = symsec;
7076         }
7077
7078       if (tls_maskp != NULL)
7079         {
7080           struct ppc_link_hash_entry *eh;
7081
7082           eh = (struct ppc_link_hash_entry *) h;
7083           *tls_maskp = &eh->tls_mask;
7084         }
7085     }
7086   else
7087     {
7088       Elf_Internal_Sym *sym;
7089       Elf_Internal_Sym *locsyms = *locsymsp;
7090
7091       if (locsyms == NULL)
7092         {
7093           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7094           if (locsyms == NULL)
7095             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7096                                             symtab_hdr->sh_info,
7097                                             0, NULL, NULL, NULL);
7098           if (locsyms == NULL)
7099             return FALSE;
7100           *locsymsp = locsyms;
7101         }
7102       sym = locsyms + r_symndx;
7103
7104       if (hp != NULL)
7105         *hp = NULL;
7106
7107       if (symp != NULL)
7108         *symp = sym;
7109
7110       if (symsecp != NULL)
7111         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7112
7113       if (tls_maskp != NULL)
7114         {
7115           struct got_entry **lgot_ents;
7116           unsigned char *tls_mask;
7117
7118           tls_mask = NULL;
7119           lgot_ents = elf_local_got_ents (ibfd);
7120           if (lgot_ents != NULL)
7121             {
7122               struct plt_entry **local_plt = (struct plt_entry **)
7123                 (lgot_ents + symtab_hdr->sh_info);
7124               unsigned char *lgot_masks = (unsigned char *)
7125                 (local_plt + symtab_hdr->sh_info);
7126               tls_mask = &lgot_masks[r_symndx];
7127             }
7128           *tls_maskp = tls_mask;
7129         }
7130     }
7131   return TRUE;
7132 }
7133
7134 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
7135    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7136    type suitable for optimization, and 1 otherwise.  */
7137
7138 static int
7139 get_tls_mask (unsigned char **tls_maskp,
7140               unsigned long *toc_symndx,
7141               bfd_vma *toc_addend,
7142               Elf_Internal_Sym **locsymsp,
7143               const Elf_Internal_Rela *rel,
7144               bfd *ibfd)
7145 {
7146   unsigned long r_symndx;
7147   int next_r;
7148   struct elf_link_hash_entry *h;
7149   Elf_Internal_Sym *sym;
7150   asection *sec;
7151   bfd_vma off;
7152
7153   r_symndx = ELF64_R_SYM (rel->r_info);
7154   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7155     return 0;
7156
7157   if ((*tls_maskp != NULL && **tls_maskp != 0)
7158       || sec == NULL
7159       || ppc64_elf_section_data (sec) == NULL
7160       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7161     return 1;
7162
7163   /* Look inside a TOC section too.  */
7164   if (h != NULL)
7165     {
7166       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7167       off = h->root.u.def.value;
7168     }
7169   else
7170     off = sym->st_value;
7171   off += rel->r_addend;
7172   BFD_ASSERT (off % 8 == 0);
7173   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7174   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7175   if (toc_symndx != NULL)
7176     *toc_symndx = r_symndx;
7177   if (toc_addend != NULL)
7178     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7179   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7180     return 0;
7181   if ((h == NULL || is_static_defined (h))
7182       && (next_r == -1 || next_r == -2))
7183     return 1 - next_r;
7184   return 1;
7185 }
7186
7187 /* Find (or create) an entry in the tocsave hash table.  */
7188
7189 static struct tocsave_entry *
7190 tocsave_find (struct ppc_link_hash_table *htab,
7191               enum insert_option insert,
7192               Elf_Internal_Sym **local_syms,
7193               const Elf_Internal_Rela *irela,
7194               bfd *ibfd)
7195 {
7196   unsigned long r_indx;
7197   struct elf_link_hash_entry *h;
7198   Elf_Internal_Sym *sym;
7199   struct tocsave_entry ent, *p;
7200   hashval_t hash;
7201   struct tocsave_entry **slot;
7202
7203   r_indx = ELF64_R_SYM (irela->r_info);
7204   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7205     return NULL;
7206   if (ent.sec == NULL || ent.sec->output_section == NULL)
7207     {
7208       (*_bfd_error_handler)
7209         (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7210       return NULL;
7211     }
7212
7213   if (h != NULL)
7214     ent.offset = h->root.u.def.value;
7215   else
7216     ent.offset = sym->st_value;
7217   ent.offset += irela->r_addend;
7218
7219   hash = tocsave_htab_hash (&ent);
7220   slot = ((struct tocsave_entry **)
7221           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7222   if (slot == NULL)
7223     return NULL;
7224
7225   if (*slot == NULL)
7226     {
7227       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7228       if (p == NULL)
7229         return NULL;
7230       *p = ent;
7231       *slot = p;
7232     }
7233   return *slot;
7234 }
7235
7236 /* Adjust all global syms defined in opd sections.  In gcc generated
7237    code for the old ABI, these will already have been done.  */
7238
7239 static bfd_boolean
7240 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7241 {
7242   struct ppc_link_hash_entry *eh;
7243   asection *sym_sec;
7244   struct _opd_sec_data *opd;
7245
7246   if (h->root.type == bfd_link_hash_indirect)
7247     return TRUE;
7248
7249   if (h->root.type != bfd_link_hash_defined
7250       && h->root.type != bfd_link_hash_defweak)
7251     return TRUE;
7252
7253   eh = (struct ppc_link_hash_entry *) h;
7254   if (eh->adjust_done)
7255     return TRUE;
7256
7257   sym_sec = eh->elf.root.u.def.section;
7258   opd = get_opd_info (sym_sec);
7259   if (opd != NULL && opd->adjust != NULL)
7260     {
7261       long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
7262       if (adjust == -1)
7263         {
7264           /* This entry has been deleted.  */
7265           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7266           if (dsec == NULL)
7267             {
7268               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7269                 if (discarded_section (dsec))
7270                   {
7271                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7272                     break;
7273                   }
7274             }
7275           eh->elf.root.u.def.value = 0;
7276           eh->elf.root.u.def.section = dsec;
7277         }
7278       else
7279         eh->elf.root.u.def.value += adjust;
7280       eh->adjust_done = 1;
7281     }
7282   return TRUE;
7283 }
7284
7285 /* Handles decrementing dynamic reloc counts for the reloc specified by
7286    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
7287    have already been determined.  */
7288
7289 static bfd_boolean
7290 dec_dynrel_count (bfd_vma r_info,
7291                   asection *sec,
7292                   struct bfd_link_info *info,
7293                   Elf_Internal_Sym **local_syms,
7294                   struct elf_link_hash_entry *h,
7295                   Elf_Internal_Sym *sym)
7296 {
7297   enum elf_ppc64_reloc_type r_type;
7298   asection *sym_sec = NULL;
7299
7300   /* Can this reloc be dynamic?  This switch, and later tests here
7301      should be kept in sync with the code in check_relocs.  */
7302   r_type = ELF64_R_TYPE (r_info);
7303   switch (r_type)
7304     {
7305     default:
7306       return TRUE;
7307
7308     case R_PPC64_TPREL16:
7309     case R_PPC64_TPREL16_LO:
7310     case R_PPC64_TPREL16_HI:
7311     case R_PPC64_TPREL16_HA:
7312     case R_PPC64_TPREL16_DS:
7313     case R_PPC64_TPREL16_LO_DS:
7314     case R_PPC64_TPREL16_HIGH:
7315     case R_PPC64_TPREL16_HIGHA:
7316     case R_PPC64_TPREL16_HIGHER:
7317     case R_PPC64_TPREL16_HIGHERA:
7318     case R_PPC64_TPREL16_HIGHEST:
7319     case R_PPC64_TPREL16_HIGHESTA:
7320       if (!info->shared)
7321         return TRUE;
7322
7323     case R_PPC64_TPREL64:
7324     case R_PPC64_DTPMOD64:
7325     case R_PPC64_DTPREL64:
7326     case R_PPC64_ADDR64:
7327     case R_PPC64_REL30:
7328     case R_PPC64_REL32:
7329     case R_PPC64_REL64:
7330     case R_PPC64_ADDR14:
7331     case R_PPC64_ADDR14_BRNTAKEN:
7332     case R_PPC64_ADDR14_BRTAKEN:
7333     case R_PPC64_ADDR16:
7334     case R_PPC64_ADDR16_DS:
7335     case R_PPC64_ADDR16_HA:
7336     case R_PPC64_ADDR16_HI:
7337     case R_PPC64_ADDR16_HIGH:
7338     case R_PPC64_ADDR16_HIGHA:
7339     case R_PPC64_ADDR16_HIGHER:
7340     case R_PPC64_ADDR16_HIGHERA:
7341     case R_PPC64_ADDR16_HIGHEST:
7342     case R_PPC64_ADDR16_HIGHESTA:
7343     case R_PPC64_ADDR16_LO:
7344     case R_PPC64_ADDR16_LO_DS:
7345     case R_PPC64_ADDR24:
7346     case R_PPC64_ADDR32:
7347     case R_PPC64_UADDR16:
7348     case R_PPC64_UADDR32:
7349     case R_PPC64_UADDR64:
7350     case R_PPC64_TOC:
7351       break;
7352     }
7353
7354   if (local_syms != NULL)
7355     {
7356       unsigned long r_symndx;
7357       bfd *ibfd = sec->owner;
7358
7359       r_symndx = ELF64_R_SYM (r_info);
7360       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7361         return FALSE;
7362     }
7363
7364   if ((info->shared
7365        && (must_be_dyn_reloc (info, r_type)
7366            || (h != NULL
7367                && (!SYMBOLIC_BIND (info, h)
7368                    || h->root.type == bfd_link_hash_defweak
7369                    || !h->def_regular))))
7370       || (ELIMINATE_COPY_RELOCS
7371           && !info->shared
7372           && h != NULL
7373           && (h->root.type == bfd_link_hash_defweak
7374               || !h->def_regular)))
7375     ;
7376   else
7377     return TRUE;
7378
7379   if (h != NULL)
7380     {
7381       struct elf_dyn_relocs *p;
7382       struct elf_dyn_relocs **pp;
7383       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7384
7385       /* elf_gc_sweep may have already removed all dyn relocs associated
7386          with local syms for a given section.  Also, symbol flags are
7387          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
7388          report a dynreloc miscount.  */
7389       if (*pp == NULL && info->gc_sections)
7390         return TRUE;
7391
7392       while ((p = *pp) != NULL)
7393         {
7394           if (p->sec == sec)
7395             {
7396               if (!must_be_dyn_reloc (info, r_type))
7397                 p->pc_count -= 1;
7398               p->count -= 1;
7399               if (p->count == 0)
7400                 *pp = p->next;
7401               return TRUE;
7402             }
7403           pp = &p->next;
7404         }
7405     }
7406   else
7407     {
7408       struct ppc_dyn_relocs *p;
7409       struct ppc_dyn_relocs **pp;
7410       void *vpp;
7411       bfd_boolean is_ifunc;
7412
7413       if (local_syms == NULL)
7414         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7415       if (sym_sec == NULL)
7416         sym_sec = sec;
7417
7418       vpp = &elf_section_data (sym_sec)->local_dynrel;
7419       pp = (struct ppc_dyn_relocs **) vpp;
7420
7421       if (*pp == NULL && info->gc_sections)
7422         return TRUE;
7423
7424       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7425       while ((p = *pp) != NULL)
7426         {
7427           if (p->sec == sec && p->ifunc == is_ifunc)
7428             {
7429               p->count -= 1;
7430               if (p->count == 0)
7431                 *pp = p->next;
7432               return TRUE;
7433             }
7434           pp = &p->next;
7435         }
7436     }
7437
7438   info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7439                           sec->owner, sec);
7440   bfd_set_error (bfd_error_bad_value);
7441   return FALSE;
7442 }
7443
7444 /* Remove unused Official Procedure Descriptor entries.  Currently we
7445    only remove those associated with functions in discarded link-once
7446    sections, or weakly defined functions that have been overridden.  It
7447    would be possible to remove many more entries for statically linked
7448    applications.  */
7449
7450 bfd_boolean
7451 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
7452 {
7453   bfd *ibfd;
7454   bfd_boolean some_edited = FALSE;
7455   asection *need_pad = NULL;
7456
7457   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7458     {
7459       asection *sec;
7460       Elf_Internal_Rela *relstart, *rel, *relend;
7461       Elf_Internal_Shdr *symtab_hdr;
7462       Elf_Internal_Sym *local_syms;
7463       bfd_vma offset;
7464       struct _opd_sec_data *opd;
7465       bfd_boolean need_edit, add_aux_fields;
7466       bfd_size_type cnt_16b = 0;
7467
7468       if (!is_ppc64_elf (ibfd))
7469         continue;
7470
7471       sec = bfd_get_section_by_name (ibfd, ".opd");
7472       if (sec == NULL || sec->size == 0)
7473         continue;
7474
7475       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7476         continue;
7477
7478       if (sec->output_section == bfd_abs_section_ptr)
7479         continue;
7480
7481       /* Look through the section relocs.  */
7482       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7483         continue;
7484
7485       local_syms = NULL;
7486       symtab_hdr = &elf_symtab_hdr (ibfd);
7487
7488       /* Read the relocations.  */
7489       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7490                                             info->keep_memory);
7491       if (relstart == NULL)
7492         return FALSE;
7493
7494       /* First run through the relocs to check they are sane, and to
7495          determine whether we need to edit this opd section.  */
7496       need_edit = FALSE;
7497       need_pad = sec;
7498       offset = 0;
7499       relend = relstart + sec->reloc_count;
7500       for (rel = relstart; rel < relend; )
7501         {
7502           enum elf_ppc64_reloc_type r_type;
7503           unsigned long r_symndx;
7504           asection *sym_sec;
7505           struct elf_link_hash_entry *h;
7506           Elf_Internal_Sym *sym;
7507
7508           /* .opd contains a regular array of 16 or 24 byte entries.  We're
7509              only interested in the reloc pointing to a function entry
7510              point.  */
7511           if (rel->r_offset != offset
7512               || rel + 1 >= relend
7513               || (rel + 1)->r_offset != offset + 8)
7514             {
7515               /* If someone messes with .opd alignment then after a
7516                  "ld -r" we might have padding in the middle of .opd.
7517                  Also, there's nothing to prevent someone putting
7518                  something silly in .opd with the assembler.  No .opd
7519                  optimization for them!  */
7520             broken_opd:
7521               (*_bfd_error_handler)
7522                 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7523               need_edit = FALSE;
7524               break;
7525             }
7526
7527           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7528               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7529             {
7530               (*_bfd_error_handler)
7531                 (_("%B: unexpected reloc type %u in .opd section"),
7532                  ibfd, r_type);
7533               need_edit = FALSE;
7534               break;
7535             }
7536
7537           r_symndx = ELF64_R_SYM (rel->r_info);
7538           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7539                           r_symndx, ibfd))
7540             goto error_ret;
7541
7542           if (sym_sec == NULL || sym_sec->owner == NULL)
7543             {
7544               const char *sym_name;
7545               if (h != NULL)
7546                 sym_name = h->root.root.string;
7547               else
7548                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7549                                              sym_sec);
7550
7551               (*_bfd_error_handler)
7552                 (_("%B: undefined sym `%s' in .opd section"),
7553                  ibfd, sym_name);
7554               need_edit = FALSE;
7555               break;
7556             }
7557
7558           /* opd entries are always for functions defined in the
7559              current input bfd.  If the symbol isn't defined in the
7560              input bfd, then we won't be using the function in this
7561              bfd;  It must be defined in a linkonce section in another
7562              bfd, or is weak.  It's also possible that we are
7563              discarding the function due to a linker script /DISCARD/,
7564              which we test for via the output_section.  */
7565           if (sym_sec->owner != ibfd
7566               || sym_sec->output_section == bfd_abs_section_ptr)
7567             need_edit = TRUE;
7568
7569           rel += 2;
7570           if (rel == relend
7571               || (rel + 1 == relend && rel->r_offset == offset + 16))
7572             {
7573               if (sec->size == offset + 24)
7574                 {
7575                   need_pad = NULL;
7576                   break;
7577                 }
7578               if (rel == relend && sec->size == offset + 16)
7579                 {
7580                   cnt_16b++;
7581                   break;
7582                 }
7583               goto broken_opd;
7584             }
7585
7586           if (rel->r_offset == offset + 24)
7587             offset += 24;
7588           else if (rel->r_offset != offset + 16)
7589             goto broken_opd;
7590           else if (rel + 1 < relend
7591                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7592                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7593             {
7594               offset += 16;
7595               cnt_16b++;
7596             }
7597           else if (rel + 2 < relend
7598                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7599                    && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7600             {
7601               offset += 24;
7602               rel += 1;
7603             }
7604           else
7605             goto broken_opd;
7606         }
7607
7608       add_aux_fields = non_overlapping && cnt_16b > 0;
7609
7610       if (need_edit || add_aux_fields)
7611         {
7612           Elf_Internal_Rela *write_rel;
7613           Elf_Internal_Shdr *rel_hdr;
7614           bfd_byte *rptr, *wptr;
7615           bfd_byte *new_contents;
7616           bfd_boolean skip;
7617           long opd_ent_size;
7618           bfd_size_type amt;
7619
7620           new_contents = NULL;
7621           amt = sec->size * sizeof (long) / 8;
7622           opd = &ppc64_elf_section_data (sec)->u.opd;
7623           opd->adjust = bfd_zalloc (sec->owner, amt);
7624           if (opd->adjust == NULL)
7625             return FALSE;
7626           ppc64_elf_section_data (sec)->sec_type = sec_opd;
7627
7628           /* This seems a waste of time as input .opd sections are all
7629              zeros as generated by gcc, but I suppose there's no reason
7630              this will always be so.  We might start putting something in
7631              the third word of .opd entries.  */
7632           if ((sec->flags & SEC_IN_MEMORY) == 0)
7633             {
7634               bfd_byte *loc;
7635               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7636                 {
7637                   if (loc != NULL)
7638                     free (loc);
7639                 error_ret:
7640                   if (local_syms != NULL
7641                       && symtab_hdr->contents != (unsigned char *) local_syms)
7642                     free (local_syms);
7643                   if (elf_section_data (sec)->relocs != relstart)
7644                     free (relstart);
7645                   return FALSE;
7646                 }
7647               sec->contents = loc;
7648               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7649             }
7650
7651           elf_section_data (sec)->relocs = relstart;
7652
7653           new_contents = sec->contents;
7654           if (add_aux_fields)
7655             {
7656               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7657               if (new_contents == NULL)
7658                 return FALSE;
7659               need_pad = FALSE;
7660             }
7661           wptr = new_contents;
7662           rptr = sec->contents;
7663
7664           write_rel = relstart;
7665           skip = FALSE;
7666           offset = 0;
7667           opd_ent_size = 0;
7668           for (rel = relstart; rel < relend; rel++)
7669             {
7670               unsigned long r_symndx;
7671               asection *sym_sec;
7672               struct elf_link_hash_entry *h;
7673               Elf_Internal_Sym *sym;
7674
7675               r_symndx = ELF64_R_SYM (rel->r_info);
7676               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7677                               r_symndx, ibfd))
7678                 goto error_ret;
7679
7680               if (rel->r_offset == offset)
7681                 {
7682                   struct ppc_link_hash_entry *fdh = NULL;
7683
7684                   /* See if the .opd entry is full 24 byte or
7685                      16 byte (with fd_aux entry overlapped with next
7686                      fd_func).  */
7687                   opd_ent_size = 24;
7688                   if ((rel + 2 == relend && sec->size == offset + 16)
7689                       || (rel + 3 < relend
7690                           && rel[2].r_offset == offset + 16
7691                           && rel[3].r_offset == offset + 24
7692                           && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7693                           && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7694                     opd_ent_size = 16;
7695
7696                   if (h != NULL
7697                       && h->root.root.string[0] == '.')
7698                     {
7699                       struct ppc_link_hash_table *htab;
7700
7701                       htab = ppc_hash_table (info);
7702                       if (htab != NULL)
7703                         fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7704                                           htab);
7705                       if (fdh != NULL
7706                           && fdh->elf.root.type != bfd_link_hash_defined
7707                           && fdh->elf.root.type != bfd_link_hash_defweak)
7708                         fdh = NULL;
7709                     }
7710
7711                   skip = (sym_sec->owner != ibfd
7712                           || sym_sec->output_section == bfd_abs_section_ptr);
7713                   if (skip)
7714                     {
7715                       if (fdh != NULL && sym_sec->owner == ibfd)
7716                         {
7717                           /* Arrange for the function descriptor sym
7718                              to be dropped.  */
7719                           fdh->elf.root.u.def.value = 0;
7720                           fdh->elf.root.u.def.section = sym_sec;
7721                         }
7722                       opd->adjust[rel->r_offset / 8] = -1;
7723                     }
7724                   else
7725                     {
7726                       /* We'll be keeping this opd entry.  */
7727
7728                       if (fdh != NULL)
7729                         {
7730                           /* Redefine the function descriptor symbol to
7731                              this location in the opd section.  It is
7732                              necessary to update the value here rather
7733                              than using an array of adjustments as we do
7734                              for local symbols, because various places
7735                              in the generic ELF code use the value
7736                              stored in u.def.value.  */
7737                           fdh->elf.root.u.def.value = wptr - new_contents;
7738                           fdh->adjust_done = 1;
7739                         }
7740
7741                       /* Local syms are a bit tricky.  We could
7742                          tweak them as they can be cached, but
7743                          we'd need to look through the local syms
7744                          for the function descriptor sym which we
7745                          don't have at the moment.  So keep an
7746                          array of adjustments.  */
7747                       opd->adjust[rel->r_offset / 8]
7748                         = (wptr - new_contents) - (rptr - sec->contents);
7749
7750                       if (wptr != rptr)
7751                         memcpy (wptr, rptr, opd_ent_size);
7752                       wptr += opd_ent_size;
7753                       if (add_aux_fields && opd_ent_size == 16)
7754                         {
7755                           memset (wptr, '\0', 8);
7756                           wptr += 8;
7757                         }
7758                     }
7759                   rptr += opd_ent_size;
7760                   offset += opd_ent_size;
7761                 }
7762
7763               if (skip)
7764                 {
7765                   if (!NO_OPD_RELOCS
7766                       && !info->relocatable
7767                       && !dec_dynrel_count (rel->r_info, sec, info,
7768                                             NULL, h, sym))
7769                     goto error_ret;
7770                 }
7771               else
7772                 {
7773                   /* We need to adjust any reloc offsets to point to the
7774                      new opd entries.  While we're at it, we may as well
7775                      remove redundant relocs.  */
7776                   rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
7777                   if (write_rel != rel)
7778                     memcpy (write_rel, rel, sizeof (*rel));
7779                   ++write_rel;
7780                 }
7781             }
7782
7783           sec->size = wptr - new_contents;
7784           sec->reloc_count = write_rel - relstart;
7785           if (add_aux_fields)
7786             {
7787               free (sec->contents);
7788               sec->contents = new_contents;
7789             }
7790
7791           /* Fudge the header size too, as this is used later in
7792              elf_bfd_final_link if we are emitting relocs.  */
7793           rel_hdr = _bfd_elf_single_rel_hdr (sec);
7794           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7795           some_edited = TRUE;
7796         }
7797       else if (elf_section_data (sec)->relocs != relstart)
7798         free (relstart);
7799
7800       if (local_syms != NULL
7801           && symtab_hdr->contents != (unsigned char *) local_syms)
7802         {
7803           if (!info->keep_memory)
7804             free (local_syms);
7805           else
7806             symtab_hdr->contents = (unsigned char *) local_syms;
7807         }
7808     }
7809
7810   if (some_edited)
7811     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7812
7813   /* If we are doing a final link and the last .opd entry is just 16 byte
7814      long, add a 8 byte padding after it.  */
7815   if (need_pad != NULL && !info->relocatable)
7816     {
7817       bfd_byte *p;
7818
7819       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7820         {
7821           BFD_ASSERT (need_pad->size > 0);
7822
7823           p = bfd_malloc (need_pad->size + 8);
7824           if (p == NULL)
7825             return FALSE;
7826
7827           if (! bfd_get_section_contents (need_pad->owner, need_pad,
7828                                           p, 0, need_pad->size))
7829             return FALSE;
7830
7831           need_pad->contents = p;
7832           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7833         }
7834       else
7835         {
7836           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7837           if (p == NULL)
7838             return FALSE;
7839
7840           need_pad->contents = p;
7841         }
7842
7843       memset (need_pad->contents + need_pad->size, 0, 8);
7844       need_pad->size += 8;
7845     }
7846
7847   return TRUE;
7848 }
7849
7850 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
7851
7852 asection *
7853 ppc64_elf_tls_setup (struct bfd_link_info *info,
7854                      int no_tls_get_addr_opt,
7855                      int *no_multi_toc)
7856 {
7857   struct ppc_link_hash_table *htab;
7858
7859   htab = ppc_hash_table (info);
7860   if (htab == NULL)
7861     return NULL;
7862
7863   if (abiversion (info->output_bfd) == 1)
7864     htab->opd_abi = 1;
7865
7866   if (*no_multi_toc)
7867     htab->do_multi_toc = 0;
7868   else if (!htab->do_multi_toc)
7869     *no_multi_toc = 1;
7870
7871   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7872                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7873                                               FALSE, FALSE, TRUE));
7874   /* Move dynamic linking info to the function descriptor sym.  */
7875   if (htab->tls_get_addr != NULL)
7876     func_desc_adjust (&htab->tls_get_addr->elf, info);
7877   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7878                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7879                                                  FALSE, FALSE, TRUE));
7880   if (!no_tls_get_addr_opt)
7881     {
7882       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7883
7884       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7885                                   FALSE, FALSE, TRUE);
7886       if (opt != NULL)
7887         func_desc_adjust (opt, info);
7888       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7889                                      FALSE, FALSE, TRUE);
7890       if (opt_fd != NULL
7891           && (opt_fd->root.type == bfd_link_hash_defined
7892               || opt_fd->root.type == bfd_link_hash_defweak))
7893         {
7894           /* If glibc supports an optimized __tls_get_addr call stub,
7895              signalled by the presence of __tls_get_addr_opt, and we'll
7896              be calling __tls_get_addr via a plt call stub, then
7897              make __tls_get_addr point to __tls_get_addr_opt.  */
7898           tga_fd = &htab->tls_get_addr_fd->elf;
7899           if (htab->elf.dynamic_sections_created
7900               && tga_fd != NULL
7901               && (tga_fd->type == STT_FUNC
7902                   || tga_fd->needs_plt)
7903               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7904                    || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7905                        && tga_fd->root.type == bfd_link_hash_undefweak)))
7906             {
7907               struct plt_entry *ent;
7908
7909               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7910                 if (ent->plt.refcount > 0)
7911                   break;
7912               if (ent != NULL)
7913                 {
7914                   tga_fd->root.type = bfd_link_hash_indirect;
7915                   tga_fd->root.u.i.link = &opt_fd->root;
7916                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7917                   if (opt_fd->dynindx != -1)
7918                     {
7919                       /* Use __tls_get_addr_opt in dynamic relocations.  */
7920                       opt_fd->dynindx = -1;
7921                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7922                                               opt_fd->dynstr_index);
7923                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7924                         return NULL;
7925                     }
7926                   htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7927                   tga = &htab->tls_get_addr->elf;
7928                   if (opt != NULL && tga != NULL)
7929                     {
7930                       tga->root.type = bfd_link_hash_indirect;
7931                       tga->root.u.i.link = &opt->root;
7932                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
7933                       _bfd_elf_link_hash_hide_symbol (info, opt,
7934                                                       tga->forced_local);
7935                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7936                     }
7937                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7938                   htab->tls_get_addr_fd->is_func_descriptor = 1;
7939                   if (htab->tls_get_addr != NULL)
7940                     {
7941                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7942                       htab->tls_get_addr->is_func = 1;
7943                     }
7944                 }
7945             }
7946         }
7947       else
7948         no_tls_get_addr_opt = TRUE;
7949     }
7950   htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
7951   return _bfd_elf_tls_setup (info->output_bfd, info);
7952 }
7953
7954 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7955    HASH1 or HASH2.  */
7956
7957 static bfd_boolean
7958 branch_reloc_hash_match (const bfd *ibfd,
7959                          const Elf_Internal_Rela *rel,
7960                          const struct ppc_link_hash_entry *hash1,
7961                          const struct ppc_link_hash_entry *hash2)
7962 {
7963   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7964   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7965   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7966
7967   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7968     {
7969       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7970       struct elf_link_hash_entry *h;
7971
7972       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7973       h = elf_follow_link (h);
7974       if (h == &hash1->elf || h == &hash2->elf)
7975         return TRUE;
7976     }
7977   return FALSE;
7978 }
7979
7980 /* Run through all the TLS relocs looking for optimization
7981    opportunities.  The linker has been hacked (see ppc64elf.em) to do
7982    a preliminary section layout so that we know the TLS segment
7983    offsets.  We can't optimize earlier because some optimizations need
7984    to know the tp offset, and we need to optimize before allocating
7985    dynamic relocations.  */
7986
7987 bfd_boolean
7988 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7989 {
7990   bfd *ibfd;
7991   asection *sec;
7992   struct ppc_link_hash_table *htab;
7993   unsigned char *toc_ref;
7994   int pass;
7995
7996   if (info->relocatable || !info->executable)
7997     return TRUE;
7998
7999   htab = ppc_hash_table (info);
8000   if (htab == NULL)
8001     return FALSE;
8002
8003   /* Make two passes over the relocs.  On the first pass, mark toc
8004      entries involved with tls relocs, and check that tls relocs
8005      involved in setting up a tls_get_addr call are indeed followed by
8006      such a call.  If they are not, we can't do any tls optimization.
8007      On the second pass twiddle tls_mask flags to notify
8008      relocate_section that optimization can be done, and adjust got
8009      and plt refcounts.  */
8010   toc_ref = NULL;
8011   for (pass = 0; pass < 2; ++pass)
8012     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8013       {
8014         Elf_Internal_Sym *locsyms = NULL;
8015         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8016
8017         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8018           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8019             {
8020               Elf_Internal_Rela *relstart, *rel, *relend;
8021               bfd_boolean found_tls_get_addr_arg = 0;
8022
8023               /* Read the relocations.  */
8024               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8025                                                     info->keep_memory);
8026               if (relstart == NULL)
8027                 return FALSE;
8028
8029               relend = relstart + sec->reloc_count;
8030               for (rel = relstart; rel < relend; rel++)
8031                 {
8032                   enum elf_ppc64_reloc_type r_type;
8033                   unsigned long r_symndx;
8034                   struct elf_link_hash_entry *h;
8035                   Elf_Internal_Sym *sym;
8036                   asection *sym_sec;
8037                   unsigned char *tls_mask;
8038                   unsigned char tls_set, tls_clear, tls_type = 0;
8039                   bfd_vma value;
8040                   bfd_boolean ok_tprel, is_local;
8041                   long toc_ref_index = 0;
8042                   int expecting_tls_get_addr = 0;
8043                   bfd_boolean ret = FALSE;
8044
8045                   r_symndx = ELF64_R_SYM (rel->r_info);
8046                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8047                                   r_symndx, ibfd))
8048                     {
8049                     err_free_rel:
8050                       if (elf_section_data (sec)->relocs != relstart)
8051                         free (relstart);
8052                       if (toc_ref != NULL)
8053                         free (toc_ref);
8054                       if (locsyms != NULL
8055                           && (elf_symtab_hdr (ibfd).contents
8056                               != (unsigned char *) locsyms))
8057                         free (locsyms);
8058                       return ret;
8059                     }
8060
8061                   if (h != NULL)
8062                     {
8063                       if (h->root.type == bfd_link_hash_defined
8064                           || h->root.type == bfd_link_hash_defweak)
8065                         value = h->root.u.def.value;
8066                       else if (h->root.type == bfd_link_hash_undefweak)
8067                         value = 0;
8068                       else
8069                         {
8070                           found_tls_get_addr_arg = 0;
8071                           continue;
8072                         }
8073                     }
8074                   else
8075                     /* Symbols referenced by TLS relocs must be of type
8076                        STT_TLS.  So no need for .opd local sym adjust.  */
8077                     value = sym->st_value;
8078
8079                   ok_tprel = FALSE;
8080                   is_local = FALSE;
8081                   if (h == NULL
8082                       || !h->def_dynamic)
8083                     {
8084                       is_local = TRUE;
8085                       if (h != NULL
8086                           && h->root.type == bfd_link_hash_undefweak)
8087                         ok_tprel = TRUE;
8088                       else
8089                         {
8090                           value += sym_sec->output_offset;
8091                           value += sym_sec->output_section->vma;
8092                           value -= htab->elf.tls_sec->vma;
8093                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8094                                       < (bfd_vma) 1 << 32);
8095                         }
8096                     }
8097
8098                   r_type = ELF64_R_TYPE (rel->r_info);
8099                   /* If this section has old-style __tls_get_addr calls
8100                      without marker relocs, then check that each
8101                      __tls_get_addr call reloc is preceded by a reloc
8102                      that conceivably belongs to the __tls_get_addr arg
8103                      setup insn.  If we don't find matching arg setup
8104                      relocs, don't do any tls optimization.  */
8105                   if (pass == 0
8106                       && sec->has_tls_get_addr_call
8107                       && h != NULL
8108                       && (h == &htab->tls_get_addr->elf
8109                           || h == &htab->tls_get_addr_fd->elf)
8110                       && !found_tls_get_addr_arg
8111                       && is_branch_reloc (r_type))
8112                     {
8113                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8114                                                 "TLS optimization disabled\n"),
8115                                               ibfd, sec, rel->r_offset);
8116                       ret = TRUE;
8117                       goto err_free_rel;
8118                     }
8119
8120                   found_tls_get_addr_arg = 0;
8121                   switch (r_type)
8122                     {
8123                     case R_PPC64_GOT_TLSLD16:
8124                     case R_PPC64_GOT_TLSLD16_LO:
8125                       expecting_tls_get_addr = 1;
8126                       found_tls_get_addr_arg = 1;
8127                       /* Fall thru */
8128
8129                     case R_PPC64_GOT_TLSLD16_HI:
8130                     case R_PPC64_GOT_TLSLD16_HA:
8131                       /* These relocs should never be against a symbol
8132                          defined in a shared lib.  Leave them alone if
8133                          that turns out to be the case.  */
8134                       if (!is_local)
8135                         continue;
8136
8137                       /* LD -> LE */
8138                       tls_set = 0;
8139                       tls_clear = TLS_LD;
8140                       tls_type = TLS_TLS | TLS_LD;
8141                       break;
8142
8143                     case R_PPC64_GOT_TLSGD16:
8144                     case R_PPC64_GOT_TLSGD16_LO:
8145                       expecting_tls_get_addr = 1;
8146                       found_tls_get_addr_arg = 1;
8147                       /* Fall thru */
8148
8149                     case R_PPC64_GOT_TLSGD16_HI:
8150                     case R_PPC64_GOT_TLSGD16_HA:
8151                       if (ok_tprel)
8152                         /* GD -> LE */
8153                         tls_set = 0;
8154                       else
8155                         /* GD -> IE */
8156                         tls_set = TLS_TLS | TLS_TPRELGD;
8157                       tls_clear = TLS_GD;
8158                       tls_type = TLS_TLS | TLS_GD;
8159                       break;
8160
8161                     case R_PPC64_GOT_TPREL16_DS:
8162                     case R_PPC64_GOT_TPREL16_LO_DS:
8163                     case R_PPC64_GOT_TPREL16_HI:
8164                     case R_PPC64_GOT_TPREL16_HA:
8165                       if (ok_tprel)
8166                         {
8167                           /* IE -> LE */
8168                           tls_set = 0;
8169                           tls_clear = TLS_TPREL;
8170                           tls_type = TLS_TLS | TLS_TPREL;
8171                           break;
8172                         }
8173                       continue;
8174
8175                     case R_PPC64_TLSGD:
8176                     case R_PPC64_TLSLD:
8177                       found_tls_get_addr_arg = 1;
8178                       /* Fall thru */
8179
8180                     case R_PPC64_TLS:
8181                     case R_PPC64_TOC16:
8182                     case R_PPC64_TOC16_LO:
8183                       if (sym_sec == NULL || sym_sec != toc)
8184                         continue;
8185
8186                       /* Mark this toc entry as referenced by a TLS
8187                          code sequence.  We can do that now in the
8188                          case of R_PPC64_TLS, and after checking for
8189                          tls_get_addr for the TOC16 relocs.  */
8190                       if (toc_ref == NULL)
8191                         toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8192                       if (toc_ref == NULL)
8193                         goto err_free_rel;
8194
8195                       if (h != NULL)
8196                         value = h->root.u.def.value;
8197                       else
8198                         value = sym->st_value;
8199                       value += rel->r_addend;
8200                       BFD_ASSERT (value < toc->size && value % 8 == 0);
8201                       toc_ref_index = (value + toc->output_offset) / 8;
8202                       if (r_type == R_PPC64_TLS
8203                           || r_type == R_PPC64_TLSGD
8204                           || r_type == R_PPC64_TLSLD)
8205                         {
8206                           toc_ref[toc_ref_index] = 1;
8207                           continue;
8208                         }
8209
8210                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
8211                         continue;
8212
8213                       tls_set = 0;
8214                       tls_clear = 0;
8215                       expecting_tls_get_addr = 2;
8216                       break;
8217
8218                     case R_PPC64_TPREL64:
8219                       if (pass == 0
8220                           || sec != toc
8221                           || toc_ref == NULL
8222                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8223                         continue;
8224                       if (ok_tprel)
8225                         {
8226                           /* IE -> LE */
8227                           tls_set = TLS_EXPLICIT;
8228                           tls_clear = TLS_TPREL;
8229                           break;
8230                         }
8231                       continue;
8232
8233                     case R_PPC64_DTPMOD64:
8234                       if (pass == 0
8235                           || sec != toc
8236                           || toc_ref == NULL
8237                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8238                         continue;
8239                       if (rel + 1 < relend
8240                           && (rel[1].r_info
8241                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8242                           && rel[1].r_offset == rel->r_offset + 8)
8243                         {
8244                           if (ok_tprel)
8245                             /* GD -> LE */
8246                             tls_set = TLS_EXPLICIT | TLS_GD;
8247                           else
8248                             /* GD -> IE */
8249                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8250                           tls_clear = TLS_GD;
8251                         }
8252                       else
8253                         {
8254                           if (!is_local)
8255                             continue;
8256
8257                           /* LD -> LE */
8258                           tls_set = TLS_EXPLICIT;
8259                           tls_clear = TLS_LD;
8260                         }
8261                       break;
8262
8263                     default:
8264                       continue;
8265                     }
8266
8267                   if (pass == 0)
8268                     {
8269                       if (!expecting_tls_get_addr
8270                           || !sec->has_tls_get_addr_call)
8271                         continue;
8272
8273                       if (rel + 1 < relend
8274                           && branch_reloc_hash_match (ibfd, rel + 1,
8275                                                       htab->tls_get_addr,
8276                                                       htab->tls_get_addr_fd))
8277                         {
8278                           if (expecting_tls_get_addr == 2)
8279                             {
8280                               /* Check for toc tls entries.  */
8281                               unsigned char *toc_tls;
8282                               int retval;
8283
8284                               retval = get_tls_mask (&toc_tls, NULL, NULL,
8285                                                      &locsyms,
8286                                                      rel, ibfd);
8287                               if (retval == 0)
8288                                 goto err_free_rel;
8289                               if (toc_tls != NULL)
8290                                 {
8291                                   if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8292                                     found_tls_get_addr_arg = 1;
8293                                   if (retval > 1)
8294                                     toc_ref[toc_ref_index] = 1;
8295                                 }
8296                             }
8297                           continue;
8298                         }
8299
8300                       if (expecting_tls_get_addr != 1)
8301                         continue;
8302
8303                       /* Uh oh, we didn't find the expected call.  We
8304                          could just mark this symbol to exclude it
8305                          from tls optimization but it's safer to skip
8306                          the entire optimization.  */
8307                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8308                                                 "TLS optimization disabled\n"),
8309                                               ibfd, sec, rel->r_offset);
8310                       ret = TRUE;
8311                       goto err_free_rel;
8312                     }
8313
8314                   if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8315                     {
8316                       struct plt_entry *ent;
8317                       for (ent = htab->tls_get_addr->elf.plt.plist;
8318                            ent != NULL;
8319                            ent = ent->next)
8320                         if (ent->addend == 0)
8321                           {
8322                             if (ent->plt.refcount > 0)
8323                               {
8324                                 ent->plt.refcount -= 1;
8325                                 expecting_tls_get_addr = 0;
8326                               }
8327                             break;
8328                           }
8329                     }
8330
8331                   if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8332                     {
8333                       struct plt_entry *ent;
8334                       for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8335                            ent != NULL;
8336                            ent = ent->next)
8337                         if (ent->addend == 0)
8338                           {
8339                             if (ent->plt.refcount > 0)
8340                               ent->plt.refcount -= 1;
8341                             break;
8342                           }
8343                     }
8344
8345                   if (tls_clear == 0)
8346                     continue;
8347
8348                   if ((tls_set & TLS_EXPLICIT) == 0)
8349                     {
8350                       struct got_entry *ent;
8351
8352                       /* Adjust got entry for this reloc.  */
8353                       if (h != NULL)
8354                         ent = h->got.glist;
8355                       else
8356                         ent = elf_local_got_ents (ibfd)[r_symndx];
8357
8358                       for (; ent != NULL; ent = ent->next)
8359                         if (ent->addend == rel->r_addend
8360                             && ent->owner == ibfd
8361                             && ent->tls_type == tls_type)
8362                           break;
8363                       if (ent == NULL)
8364                         abort ();
8365
8366                       if (tls_set == 0)
8367                         {
8368                           /* We managed to get rid of a got entry.  */
8369                           if (ent->got.refcount > 0)
8370                             ent->got.refcount -= 1;
8371                         }
8372                     }
8373                   else
8374                     {
8375                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
8376                          we'll lose one or two dyn relocs.  */
8377                       if (!dec_dynrel_count (rel->r_info, sec, info,
8378                                              NULL, h, sym))
8379                         return FALSE;
8380
8381                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
8382                         {
8383                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8384                                                  NULL, h, sym))
8385                             return FALSE;
8386                         }
8387                     }
8388
8389                   *tls_mask |= tls_set;
8390                   *tls_mask &= ~tls_clear;
8391                 }
8392
8393               if (elf_section_data (sec)->relocs != relstart)
8394                 free (relstart);
8395             }
8396
8397         if (locsyms != NULL
8398             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8399           {
8400             if (!info->keep_memory)
8401               free (locsyms);
8402             else
8403               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8404           }
8405       }
8406
8407   if (toc_ref != NULL)
8408     free (toc_ref);
8409   return TRUE;
8410 }
8411
8412 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8413    the values of any global symbols in a toc section that has been
8414    edited.  Globals in toc sections should be a rarity, so this function
8415    sets a flag if any are found in toc sections other than the one just
8416    edited, so that futher hash table traversals can be avoided.  */
8417
8418 struct adjust_toc_info
8419 {
8420   asection *toc;
8421   unsigned long *skip;
8422   bfd_boolean global_toc_syms;
8423 };
8424
8425 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8426
8427 static bfd_boolean
8428 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8429 {
8430   struct ppc_link_hash_entry *eh;
8431   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8432   unsigned long i;
8433
8434   if (h->root.type != bfd_link_hash_defined
8435       && h->root.type != bfd_link_hash_defweak)
8436     return TRUE;
8437
8438   eh = (struct ppc_link_hash_entry *) h;
8439   if (eh->adjust_done)
8440     return TRUE;
8441
8442   if (eh->elf.root.u.def.section == toc_inf->toc)
8443     {
8444       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8445         i = toc_inf->toc->rawsize >> 3;
8446       else
8447         i = eh->elf.root.u.def.value >> 3;
8448
8449       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8450         {
8451           (*_bfd_error_handler)
8452             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8453           do
8454             ++i;
8455           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8456           eh->elf.root.u.def.value = (bfd_vma) i << 3;
8457         }
8458
8459       eh->elf.root.u.def.value -= toc_inf->skip[i];
8460       eh->adjust_done = 1;
8461     }
8462   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8463     toc_inf->global_toc_syms = TRUE;
8464
8465   return TRUE;
8466 }
8467
8468 /* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc.  */
8469
8470 static bfd_boolean
8471 ok_lo_toc_insn (unsigned int insn)
8472 {
8473   return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8474           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8475           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8476           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8477           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8478           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8479           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8480           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8481           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8482           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8483           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8484           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8485           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8486           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8487           || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8488               && (insn & 3) != 1)
8489           || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8490               && ((insn & 3) == 0 || (insn & 3) == 3))
8491           || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8492 }
8493
8494 /* Examine all relocs referencing .toc sections in order to remove
8495    unused .toc entries.  */
8496
8497 bfd_boolean
8498 ppc64_elf_edit_toc (struct bfd_link_info *info)
8499 {
8500   bfd *ibfd;
8501   struct adjust_toc_info toc_inf;
8502   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8503
8504   htab->do_toc_opt = 1;
8505   toc_inf.global_toc_syms = TRUE;
8506   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8507     {
8508       asection *toc, *sec;
8509       Elf_Internal_Shdr *symtab_hdr;
8510       Elf_Internal_Sym *local_syms;
8511       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8512       unsigned long *skip, *drop;
8513       unsigned char *used;
8514       unsigned char *keep, last, some_unused;
8515
8516       if (!is_ppc64_elf (ibfd))
8517         continue;
8518
8519       toc = bfd_get_section_by_name (ibfd, ".toc");
8520       if (toc == NULL
8521           || toc->size == 0
8522           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8523           || discarded_section (toc))
8524         continue;
8525
8526       toc_relocs = NULL;
8527       local_syms = NULL;
8528       symtab_hdr = &elf_symtab_hdr (ibfd);
8529
8530       /* Look at sections dropped from the final link.  */
8531       skip = NULL;
8532       relstart = NULL;
8533       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8534         {
8535           if (sec->reloc_count == 0
8536               || !discarded_section (sec)
8537               || get_opd_info (sec)
8538               || (sec->flags & SEC_ALLOC) == 0
8539               || (sec->flags & SEC_DEBUGGING) != 0)
8540             continue;
8541
8542           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8543           if (relstart == NULL)
8544             goto error_ret;
8545
8546           /* Run through the relocs to see which toc entries might be
8547              unused.  */
8548           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8549             {
8550               enum elf_ppc64_reloc_type r_type;
8551               unsigned long r_symndx;
8552               asection *sym_sec;
8553               struct elf_link_hash_entry *h;
8554               Elf_Internal_Sym *sym;
8555               bfd_vma val;
8556
8557               r_type = ELF64_R_TYPE (rel->r_info);
8558               switch (r_type)
8559                 {
8560                 default:
8561                   continue;
8562
8563                 case R_PPC64_TOC16:
8564                 case R_PPC64_TOC16_LO:
8565                 case R_PPC64_TOC16_HI:
8566                 case R_PPC64_TOC16_HA:
8567                 case R_PPC64_TOC16_DS:
8568                 case R_PPC64_TOC16_LO_DS:
8569                   break;
8570                 }
8571
8572               r_symndx = ELF64_R_SYM (rel->r_info);
8573               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8574                               r_symndx, ibfd))
8575                 goto error_ret;
8576
8577               if (sym_sec != toc)
8578                 continue;
8579
8580               if (h != NULL)
8581                 val = h->root.u.def.value;
8582               else
8583                 val = sym->st_value;
8584               val += rel->r_addend;
8585
8586               if (val >= toc->size)
8587                 continue;
8588
8589               /* Anything in the toc ought to be aligned to 8 bytes.
8590                  If not, don't mark as unused.  */
8591               if (val & 7)
8592                 continue;
8593
8594               if (skip == NULL)
8595                 {
8596                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8597                   if (skip == NULL)
8598                     goto error_ret;
8599                 }
8600
8601               skip[val >> 3] = ref_from_discarded;
8602             }
8603
8604           if (elf_section_data (sec)->relocs != relstart)
8605             free (relstart);
8606         }
8607
8608       /* For largetoc loads of address constants, we can convert
8609          .  addis rx,2,addr@got@ha
8610          .  ld ry,addr@got@l(rx)
8611          to
8612          .  addis rx,2,addr@toc@ha
8613          .  addi ry,rx,addr@toc@l
8614          when addr is within 2G of the toc pointer.  This then means
8615          that the word storing "addr" in the toc is no longer needed.  */
8616
8617       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8618           && toc->output_section->rawsize < (bfd_vma) 1 << 31
8619           && toc->reloc_count != 0)
8620         {
8621           /* Read toc relocs.  */
8622           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8623                                                   info->keep_memory);
8624           if (toc_relocs == NULL)
8625             goto error_ret;
8626
8627           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8628             {
8629               enum elf_ppc64_reloc_type r_type;
8630               unsigned long r_symndx;
8631               asection *sym_sec;
8632               struct elf_link_hash_entry *h;
8633               Elf_Internal_Sym *sym;
8634               bfd_vma val, addr;
8635
8636               r_type = ELF64_R_TYPE (rel->r_info);
8637               if (r_type != R_PPC64_ADDR64)
8638                 continue;
8639
8640               r_symndx = ELF64_R_SYM (rel->r_info);
8641               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8642                               r_symndx, ibfd))
8643                 goto error_ret;
8644
8645               if (sym_sec == NULL
8646                   || discarded_section (sym_sec))
8647                 continue;
8648
8649               if (!SYMBOL_CALLS_LOCAL (info, h))
8650                 continue;
8651
8652               if (h != NULL)
8653                 {
8654                   if (h->type == STT_GNU_IFUNC)
8655                     continue;
8656                   val = h->root.u.def.value;
8657                 }
8658               else
8659                 {
8660                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8661                     continue;
8662                   val = sym->st_value;
8663                 }
8664               val += rel->r_addend;
8665               val += sym_sec->output_section->vma + sym_sec->output_offset;
8666
8667               /* We don't yet know the exact toc pointer value, but we
8668                  know it will be somewhere in the toc section.  Don't
8669                  optimize if the difference from any possible toc
8670                  pointer is outside [ff..f80008000, 7fff7fff].  */
8671               addr = toc->output_section->vma + TOC_BASE_OFF;
8672               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8673                 continue;
8674
8675               addr = toc->output_section->vma + toc->output_section->rawsize;
8676               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8677                 continue;
8678
8679               if (skip == NULL)
8680                 {
8681                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8682                   if (skip == NULL)
8683                     goto error_ret;
8684                 }
8685
8686               skip[rel->r_offset >> 3]
8687                 |= can_optimize | ((rel - toc_relocs) << 2);
8688             }
8689         }
8690
8691       if (skip == NULL)
8692         continue;
8693
8694       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8695       if (used == NULL)
8696         {
8697         error_ret:
8698           if (local_syms != NULL
8699               && symtab_hdr->contents != (unsigned char *) local_syms)
8700             free (local_syms);
8701           if (sec != NULL
8702               && relstart != NULL
8703               && elf_section_data (sec)->relocs != relstart)
8704             free (relstart);
8705           if (toc_relocs != NULL
8706               && elf_section_data (toc)->relocs != toc_relocs)
8707             free (toc_relocs);
8708           if (skip != NULL)
8709             free (skip);
8710           return FALSE;
8711         }
8712
8713       /* Now check all kept sections that might reference the toc.
8714          Check the toc itself last.  */
8715       for (sec = (ibfd->sections == toc && toc->next ? toc->next
8716                   : ibfd->sections);
8717            sec != NULL;
8718            sec = (sec == toc ? NULL
8719                   : sec->next == NULL ? toc
8720                   : sec->next == toc && toc->next ? toc->next
8721                   : sec->next))
8722         {
8723           int repeat;
8724
8725           if (sec->reloc_count == 0
8726               || discarded_section (sec)
8727               || get_opd_info (sec)
8728               || (sec->flags & SEC_ALLOC) == 0
8729               || (sec->flags & SEC_DEBUGGING) != 0)
8730             continue;
8731
8732           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8733                                                 info->keep_memory);
8734           if (relstart == NULL)
8735             goto error_ret;
8736
8737           /* Mark toc entries referenced as used.  */
8738           do
8739             {
8740               repeat = 0;
8741               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8742                 {
8743                   enum elf_ppc64_reloc_type r_type;
8744                   unsigned long r_symndx;
8745                   asection *sym_sec;
8746                   struct elf_link_hash_entry *h;
8747                   Elf_Internal_Sym *sym;
8748                   bfd_vma val;
8749                   enum {no_check, check_lo, check_ha} insn_check;
8750
8751                   r_type = ELF64_R_TYPE (rel->r_info);
8752                   switch (r_type)
8753                     {
8754                     default:
8755                       insn_check = no_check;
8756                       break;
8757
8758                     case R_PPC64_GOT_TLSLD16_HA:
8759                     case R_PPC64_GOT_TLSGD16_HA:
8760                     case R_PPC64_GOT_TPREL16_HA:
8761                     case R_PPC64_GOT_DTPREL16_HA:
8762                     case R_PPC64_GOT16_HA:
8763                     case R_PPC64_TOC16_HA:
8764                       insn_check = check_ha;
8765                       break;
8766
8767                     case R_PPC64_GOT_TLSLD16_LO:
8768                     case R_PPC64_GOT_TLSGD16_LO:
8769                     case R_PPC64_GOT_TPREL16_LO_DS:
8770                     case R_PPC64_GOT_DTPREL16_LO_DS:
8771                     case R_PPC64_GOT16_LO:
8772                     case R_PPC64_GOT16_LO_DS:
8773                     case R_PPC64_TOC16_LO:
8774                     case R_PPC64_TOC16_LO_DS:
8775                       insn_check = check_lo;
8776                       break;
8777                     }
8778
8779                   if (insn_check != no_check)
8780                     {
8781                       bfd_vma off = rel->r_offset & ~3;
8782                       unsigned char buf[4];
8783                       unsigned int insn;
8784
8785                       if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8786                         {
8787                           free (used);
8788                           goto error_ret;
8789                         }
8790                       insn = bfd_get_32 (ibfd, buf);
8791                       if (insn_check == check_lo
8792                           ? !ok_lo_toc_insn (insn)
8793                           : ((insn & ((0x3f << 26) | 0x1f << 16))
8794                              != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8795                         {
8796                           char str[12];
8797
8798                           ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8799                           sprintf (str, "%#08x", insn);
8800                           info->callbacks->einfo
8801                             (_("%P: %H: toc optimization is not supported for"
8802                                " %s instruction.\n"),
8803                              ibfd, sec, rel->r_offset & ~3, str);
8804                         }
8805                     }
8806
8807                   switch (r_type)
8808                     {
8809                     case R_PPC64_TOC16:
8810                     case R_PPC64_TOC16_LO:
8811                     case R_PPC64_TOC16_HI:
8812                     case R_PPC64_TOC16_HA:
8813                     case R_PPC64_TOC16_DS:
8814                     case R_PPC64_TOC16_LO_DS:
8815                       /* In case we're taking addresses of toc entries.  */
8816                     case R_PPC64_ADDR64:
8817                       break;
8818
8819                     default:
8820                       continue;
8821                     }
8822
8823                   r_symndx = ELF64_R_SYM (rel->r_info);
8824                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8825                                   r_symndx, ibfd))
8826                     {
8827                       free (used);
8828                       goto error_ret;
8829                     }
8830
8831                   if (sym_sec != toc)
8832                     continue;
8833
8834                   if (h != NULL)
8835                     val = h->root.u.def.value;
8836                   else
8837                     val = sym->st_value;
8838                   val += rel->r_addend;
8839
8840                   if (val >= toc->size)
8841                     continue;
8842
8843                   if ((skip[val >> 3] & can_optimize) != 0)
8844                     {
8845                       bfd_vma off;
8846                       unsigned char opc;
8847
8848                       switch (r_type)
8849                         {
8850                         case R_PPC64_TOC16_HA:
8851                           break;
8852
8853                         case R_PPC64_TOC16_LO_DS:
8854                           off = rel->r_offset;
8855                           off += (bfd_big_endian (ibfd) ? -2 : 3);
8856                           if (!bfd_get_section_contents (ibfd, sec, &opc,
8857                                                          off, 1))
8858                             {
8859                               free (used);
8860                               goto error_ret;
8861                             }
8862                           if ((opc & (0x3f << 2)) == (58u << 2))
8863                             break;
8864                           /* Fall thru */
8865
8866                         default:
8867                           /* Wrong sort of reloc, or not a ld.  We may
8868                              as well clear ref_from_discarded too.  */
8869                           skip[val >> 3] = 0;
8870                         }
8871                     }
8872
8873                   if (sec != toc)
8874                     used[val >> 3] = 1;
8875                   /* For the toc section, we only mark as used if this
8876                      entry itself isn't unused.  */
8877                   else if ((used[rel->r_offset >> 3]
8878                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8879                            && !used[val >> 3])
8880                     {
8881                       /* Do all the relocs again, to catch reference
8882                          chains.  */
8883                       repeat = 1;
8884                       used[val >> 3] = 1;
8885                     }
8886                 }
8887             }
8888           while (repeat);
8889
8890           if (elf_section_data (sec)->relocs != relstart)
8891             free (relstart);
8892         }
8893
8894       /* Merge the used and skip arrays.  Assume that TOC
8895          doublewords not appearing as either used or unused belong
8896          to to an entry more than one doubleword in size.  */
8897       for (drop = skip, keep = used, last = 0, some_unused = 0;
8898            drop < skip + (toc->size + 7) / 8;
8899            ++drop, ++keep)
8900         {
8901           if (*keep)
8902             {
8903               *drop &= ~ref_from_discarded;
8904               if ((*drop & can_optimize) != 0)
8905                 some_unused = 1;
8906               last = 0;
8907             }
8908           else if ((*drop & ref_from_discarded) != 0)
8909             {
8910               some_unused = 1;
8911               last = ref_from_discarded;
8912             }
8913           else
8914             *drop = last;
8915         }
8916
8917       free (used);
8918
8919       if (some_unused)
8920         {
8921           bfd_byte *contents, *src;
8922           unsigned long off;
8923           Elf_Internal_Sym *sym;
8924           bfd_boolean local_toc_syms = FALSE;
8925
8926           /* Shuffle the toc contents, and at the same time convert the
8927              skip array from booleans into offsets.  */
8928           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8929             goto error_ret;
8930
8931           elf_section_data (toc)->this_hdr.contents = contents;
8932
8933           for (src = contents, off = 0, drop = skip;
8934                src < contents + toc->size;
8935                src += 8, ++drop)
8936             {
8937               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8938                 off += 8;
8939               else if (off != 0)
8940                 {
8941                   *drop = off;
8942                   memcpy (src - off, src, 8);
8943                 }
8944             }
8945           *drop = off;
8946           toc->rawsize = toc->size;
8947           toc->size = src - contents - off;
8948
8949           /* Adjust addends for relocs against the toc section sym,
8950              and optimize any accesses we can.  */
8951           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8952             {
8953               if (sec->reloc_count == 0
8954                   || discarded_section (sec))
8955                 continue;
8956
8957               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8958                                                     info->keep_memory);
8959               if (relstart == NULL)
8960                 goto error_ret;
8961
8962               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8963                 {
8964                   enum elf_ppc64_reloc_type r_type;
8965                   unsigned long r_symndx;
8966                   asection *sym_sec;
8967                   struct elf_link_hash_entry *h;
8968                   bfd_vma val;
8969
8970                   r_type = ELF64_R_TYPE (rel->r_info);
8971                   switch (r_type)
8972                     {
8973                     default:
8974                       continue;
8975
8976                     case R_PPC64_TOC16:
8977                     case R_PPC64_TOC16_LO:
8978                     case R_PPC64_TOC16_HI:
8979                     case R_PPC64_TOC16_HA:
8980                     case R_PPC64_TOC16_DS:
8981                     case R_PPC64_TOC16_LO_DS:
8982                     case R_PPC64_ADDR64:
8983                       break;
8984                     }
8985
8986                   r_symndx = ELF64_R_SYM (rel->r_info);
8987                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8988                                   r_symndx, ibfd))
8989                     goto error_ret;
8990
8991                   if (sym_sec != toc)
8992                     continue;
8993
8994                   if (h != NULL)
8995                     val = h->root.u.def.value;
8996                   else
8997                     {
8998                       val = sym->st_value;
8999                       if (val != 0)
9000                         local_toc_syms = TRUE;
9001                     }
9002
9003                   val += rel->r_addend;
9004
9005                   if (val > toc->rawsize)
9006                     val = toc->rawsize;
9007                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
9008                     continue;
9009                   else if ((skip[val >> 3] & can_optimize) != 0)
9010                     {
9011                       Elf_Internal_Rela *tocrel
9012                         = toc_relocs + (skip[val >> 3] >> 2);
9013                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9014
9015                       switch (r_type)
9016                         {
9017                         case R_PPC64_TOC16_HA:
9018                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9019                           break;
9020
9021                         case R_PPC64_TOC16_LO_DS:
9022                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9023                           break;
9024
9025                         default:
9026                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9027                             ppc_howto_init ();
9028                           info->callbacks->einfo
9029                             (_("%P: %H: %s references "
9030                                "optimized away TOC entry\n"),
9031                              ibfd, sec, rel->r_offset,
9032                              ppc64_elf_howto_table[r_type]->name);
9033                           bfd_set_error (bfd_error_bad_value);
9034                           goto error_ret;
9035                         }
9036                       rel->r_addend = tocrel->r_addend;
9037                       elf_section_data (sec)->relocs = relstart;
9038                       continue;
9039                     }
9040
9041                   if (h != NULL || sym->st_value != 0)
9042                     continue;
9043
9044                   rel->r_addend -= skip[val >> 3];
9045                   elf_section_data (sec)->relocs = relstart;
9046                 }
9047
9048               if (elf_section_data (sec)->relocs != relstart)
9049                 free (relstart);
9050             }
9051
9052           /* We shouldn't have local or global symbols defined in the TOC,
9053              but handle them anyway.  */
9054           if (local_syms != NULL)
9055             for (sym = local_syms;
9056                  sym < local_syms + symtab_hdr->sh_info;
9057                  ++sym)
9058               if (sym->st_value != 0
9059                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9060                 {
9061                   unsigned long i;
9062
9063                   if (sym->st_value > toc->rawsize)
9064                     i = toc->rawsize >> 3;
9065                   else
9066                     i = sym->st_value >> 3;
9067
9068                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9069                     {
9070                       if (local_toc_syms)
9071                         (*_bfd_error_handler)
9072                           (_("%s defined on removed toc entry"),
9073                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9074                       do
9075                         ++i;
9076                       while ((skip[i] & (ref_from_discarded | can_optimize)));
9077                       sym->st_value = (bfd_vma) i << 3;
9078                     }
9079
9080                   sym->st_value -= skip[i];
9081                   symtab_hdr->contents = (unsigned char *) local_syms;
9082                 }
9083
9084           /* Adjust any global syms defined in this toc input section.  */
9085           if (toc_inf.global_toc_syms)
9086             {
9087               toc_inf.toc = toc;
9088               toc_inf.skip = skip;
9089               toc_inf.global_toc_syms = FALSE;
9090               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9091                                       &toc_inf);
9092             }
9093
9094           if (toc->reloc_count != 0)
9095             {
9096               Elf_Internal_Shdr *rel_hdr;
9097               Elf_Internal_Rela *wrel;
9098               bfd_size_type sz;
9099
9100               /* Remove unused toc relocs, and adjust those we keep.  */
9101               if (toc_relocs == NULL)
9102                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9103                                                         info->keep_memory);
9104               if (toc_relocs == NULL)
9105                 goto error_ret;
9106
9107               wrel = toc_relocs;
9108               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9109                 if ((skip[rel->r_offset >> 3]
9110                      & (ref_from_discarded | can_optimize)) == 0)
9111                   {
9112                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9113                     wrel->r_info = rel->r_info;
9114                     wrel->r_addend = rel->r_addend;
9115                     ++wrel;
9116                   }
9117                 else if (!dec_dynrel_count (rel->r_info, toc, info,
9118                                             &local_syms, NULL, NULL))
9119                   goto error_ret;
9120
9121               elf_section_data (toc)->relocs = toc_relocs;
9122               toc->reloc_count = wrel - toc_relocs;
9123               rel_hdr = _bfd_elf_single_rel_hdr (toc);
9124               sz = rel_hdr->sh_entsize;
9125               rel_hdr->sh_size = toc->reloc_count * sz;
9126             }
9127         }
9128       else if (toc_relocs != NULL
9129                && elf_section_data (toc)->relocs != toc_relocs)
9130         free (toc_relocs);
9131
9132       if (local_syms != NULL
9133           && symtab_hdr->contents != (unsigned char *) local_syms)
9134         {
9135           if (!info->keep_memory)
9136             free (local_syms);
9137           else
9138             symtab_hdr->contents = (unsigned char *) local_syms;
9139         }
9140       free (skip);
9141     }
9142
9143   return TRUE;
9144 }
9145
9146 /* Return true iff input section I references the TOC using
9147    instructions limited to +/-32k offsets.  */
9148
9149 bfd_boolean
9150 ppc64_elf_has_small_toc_reloc (asection *i)
9151 {
9152   return (is_ppc64_elf (i->owner)
9153           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9154 }
9155
9156 /* Allocate space for one GOT entry.  */
9157
9158 static void
9159 allocate_got (struct elf_link_hash_entry *h,
9160               struct bfd_link_info *info,
9161               struct got_entry *gent)
9162 {
9163   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9164   bfd_boolean dyn;
9165   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9166   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9167                  ? 16 : 8);
9168   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9169                   ? 2 : 1) * sizeof (Elf64_External_Rela);
9170   asection *got = ppc64_elf_tdata (gent->owner)->got;
9171
9172   gent->got.offset = got->size;
9173   got->size += entsize;
9174
9175   dyn = htab->elf.dynamic_sections_created;
9176   if (h->type == STT_GNU_IFUNC)
9177     {
9178       htab->reliplt->size += rentsize;
9179       htab->got_reli_size += rentsize;
9180     }
9181   else if ((info->shared
9182             || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9183            && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9184                || h->root.type != bfd_link_hash_undefweak))
9185     {
9186       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9187       relgot->size += rentsize;
9188     }
9189 }
9190
9191 /* This function merges got entries in the same toc group.  */
9192
9193 static void
9194 merge_got_entries (struct got_entry **pent)
9195 {
9196   struct got_entry *ent, *ent2;
9197
9198   for (ent = *pent; ent != NULL; ent = ent->next)
9199     if (!ent->is_indirect)
9200       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9201         if (!ent2->is_indirect
9202             && ent2->addend == ent->addend
9203             && ent2->tls_type == ent->tls_type
9204             && elf_gp (ent2->owner) == elf_gp (ent->owner))
9205           {
9206             ent2->is_indirect = TRUE;
9207             ent2->got.ent = ent;
9208           }
9209 }
9210
9211 /* Allocate space in .plt, .got and associated reloc sections for
9212    dynamic relocs.  */
9213
9214 static bfd_boolean
9215 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9216 {
9217   struct bfd_link_info *info;
9218   struct ppc_link_hash_table *htab;
9219   asection *s;
9220   struct ppc_link_hash_entry *eh;
9221   struct elf_dyn_relocs *p;
9222   struct got_entry **pgent, *gent;
9223
9224   if (h->root.type == bfd_link_hash_indirect)
9225     return TRUE;
9226
9227   info = (struct bfd_link_info *) inf;
9228   htab = ppc_hash_table (info);
9229   if (htab == NULL)
9230     return FALSE;
9231
9232   if ((htab->elf.dynamic_sections_created
9233        && h->dynindx != -1
9234        && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
9235       || h->type == STT_GNU_IFUNC)
9236     {
9237       struct plt_entry *pent;
9238       bfd_boolean doneone = FALSE;
9239       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9240         if (pent->plt.refcount > 0)
9241           {
9242             if (!htab->elf.dynamic_sections_created
9243                 || h->dynindx == -1)
9244               {
9245                 s = htab->iplt;
9246                 pent->plt.offset = s->size;
9247                 s->size += PLT_ENTRY_SIZE (htab);
9248                 s = htab->reliplt;
9249               }
9250             else
9251               {
9252                 /* If this is the first .plt entry, make room for the special
9253                    first entry.  */
9254                 s = htab->plt;
9255                 if (s->size == 0)
9256                   s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9257
9258                 pent->plt.offset = s->size;
9259
9260                 /* Make room for this entry.  */
9261                 s->size += PLT_ENTRY_SIZE (htab);
9262
9263                 /* Make room for the .glink code.  */
9264                 s = htab->glink;
9265                 if (s->size == 0)
9266                   s->size += GLINK_CALL_STUB_SIZE;
9267                 if (htab->opd_abi)
9268                   {
9269                     /* We need bigger stubs past index 32767.  */
9270                     if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9271                       s->size += 4;
9272                     s->size += 2*4;
9273                   }
9274                 else
9275                   s->size += 4;
9276
9277                 /* We also need to make an entry in the .rela.plt section.  */
9278                 s = htab->relplt;
9279               }
9280             s->size += sizeof (Elf64_External_Rela);
9281             doneone = TRUE;
9282           }
9283         else
9284           pent->plt.offset = (bfd_vma) -1;
9285       if (!doneone)
9286         {
9287           h->plt.plist = NULL;
9288           h->needs_plt = 0;
9289         }
9290     }
9291   else
9292     {
9293       h->plt.plist = NULL;
9294       h->needs_plt = 0;
9295     }
9296
9297   eh = (struct ppc_link_hash_entry *) h;
9298   /* Run through the TLS GD got entries first if we're changing them
9299      to TPREL.  */
9300   if ((eh->tls_mask & TLS_TPRELGD) != 0)
9301     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9302       if (gent->got.refcount > 0
9303           && (gent->tls_type & TLS_GD) != 0)
9304         {
9305           /* This was a GD entry that has been converted to TPREL.  If
9306              there happens to be a TPREL entry we can use that one.  */
9307           struct got_entry *ent;
9308           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9309             if (ent->got.refcount > 0
9310                 && (ent->tls_type & TLS_TPREL) != 0
9311                 && ent->addend == gent->addend
9312                 && ent->owner == gent->owner)
9313               {
9314                 gent->got.refcount = 0;
9315                 break;
9316               }
9317
9318           /* If not, then we'll be using our own TPREL entry.  */
9319           if (gent->got.refcount != 0)
9320             gent->tls_type = TLS_TLS | TLS_TPREL;
9321         }
9322
9323   /* Remove any list entry that won't generate a word in the GOT before
9324      we call merge_got_entries.  Otherwise we risk merging to empty
9325      entries.  */
9326   pgent = &h->got.glist;
9327   while ((gent = *pgent) != NULL)
9328     if (gent->got.refcount > 0)
9329       {
9330         if ((gent->tls_type & TLS_LD) != 0
9331             && !h->def_dynamic)
9332           {
9333             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9334             *pgent = gent->next;
9335           }
9336         else
9337           pgent = &gent->next;
9338       }
9339     else
9340       *pgent = gent->next;
9341
9342   if (!htab->do_multi_toc)
9343     merge_got_entries (&h->got.glist);
9344
9345   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9346     if (!gent->is_indirect)
9347       {
9348         /* Make sure this symbol is output as a dynamic symbol.
9349            Undefined weak syms won't yet be marked as dynamic,
9350            nor will all TLS symbols.  */
9351         if (h->dynindx == -1
9352             && !h->forced_local
9353             && h->type != STT_GNU_IFUNC
9354             && htab->elf.dynamic_sections_created)
9355           {
9356             if (! bfd_elf_link_record_dynamic_symbol (info, h))
9357               return FALSE;
9358           }
9359
9360         if (!is_ppc64_elf (gent->owner))
9361           abort ();
9362
9363         allocate_got (h, info, gent);
9364       }
9365
9366   if (eh->dyn_relocs == NULL
9367       || (!htab->elf.dynamic_sections_created
9368           && h->type != STT_GNU_IFUNC))
9369     return TRUE;
9370
9371   /* In the shared -Bsymbolic case, discard space allocated for
9372      dynamic pc-relative relocs against symbols which turn out to be
9373      defined in regular objects.  For the normal shared case, discard
9374      space for relocs that have become local due to symbol visibility
9375      changes.  */
9376
9377   if (info->shared)
9378     {
9379       /* Relocs that use pc_count are those that appear on a call insn,
9380          or certain REL relocs (see must_be_dyn_reloc) that can be
9381          generated via assembly.  We want calls to protected symbols to
9382          resolve directly to the function rather than going via the plt.
9383          If people want function pointer comparisons to work as expected
9384          then they should avoid writing weird assembly.  */
9385       if (SYMBOL_CALLS_LOCAL (info, h))
9386         {
9387           struct elf_dyn_relocs **pp;
9388
9389           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9390             {
9391               p->count -= p->pc_count;
9392               p->pc_count = 0;
9393               if (p->count == 0)
9394                 *pp = p->next;
9395               else
9396                 pp = &p->next;
9397             }
9398         }
9399
9400       /* Also discard relocs on undefined weak syms with non-default
9401          visibility.  */
9402       if (eh->dyn_relocs != NULL
9403           && h->root.type == bfd_link_hash_undefweak)
9404         {
9405           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9406             eh->dyn_relocs = NULL;
9407
9408           /* Make sure this symbol is output as a dynamic symbol.
9409              Undefined weak syms won't yet be marked as dynamic.  */
9410           else if (h->dynindx == -1
9411                    && !h->forced_local)
9412             {
9413               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9414                 return FALSE;
9415             }
9416         }
9417     }
9418   else if (h->type == STT_GNU_IFUNC)
9419     {
9420       if (!h->non_got_ref)
9421         eh->dyn_relocs = NULL;
9422     }
9423   else if (ELIMINATE_COPY_RELOCS)
9424     {
9425       /* For the non-shared case, discard space for relocs against
9426          symbols which turn out to need copy relocs or are not
9427          dynamic.  */
9428
9429       if (!h->non_got_ref
9430           && !h->def_regular)
9431         {
9432           /* Make sure this symbol is output as a dynamic symbol.
9433              Undefined weak syms won't yet be marked as dynamic.  */
9434           if (h->dynindx == -1
9435               && !h->forced_local)
9436             {
9437               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9438                 return FALSE;
9439             }
9440
9441           /* If that succeeded, we know we'll be keeping all the
9442              relocs.  */
9443           if (h->dynindx != -1)
9444             goto keep;
9445         }
9446
9447       eh->dyn_relocs = NULL;
9448
9449     keep: ;
9450     }
9451
9452   /* Finally, allocate space.  */
9453   for (p = eh->dyn_relocs; p != NULL; p = p->next)
9454     {
9455       asection *sreloc = elf_section_data (p->sec)->sreloc;
9456       if (eh->elf.type == STT_GNU_IFUNC)
9457         sreloc = htab->reliplt;
9458       sreloc->size += p->count * sizeof (Elf64_External_Rela);
9459     }
9460
9461   return TRUE;
9462 }
9463
9464 /* Find any dynamic relocs that apply to read-only sections.  */
9465
9466 static bfd_boolean
9467 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9468 {
9469   struct ppc_link_hash_entry *eh;
9470   struct elf_dyn_relocs *p;
9471
9472   eh = (struct ppc_link_hash_entry *) h;
9473   for (p = eh->dyn_relocs; p != NULL; p = p->next)
9474     {
9475       asection *s = p->sec->output_section;
9476
9477       if (s != NULL && (s->flags & SEC_READONLY) != 0)
9478         {
9479           struct bfd_link_info *info = inf;
9480
9481           info->flags |= DF_TEXTREL;
9482
9483           /* Not an error, just cut short the traversal.  */
9484           return FALSE;
9485         }
9486     }
9487   return TRUE;
9488 }
9489
9490 /* Set the sizes of the dynamic sections.  */
9491
9492 static bfd_boolean
9493 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9494                                  struct bfd_link_info *info)
9495 {
9496   struct ppc_link_hash_table *htab;
9497   bfd *dynobj;
9498   asection *s;
9499   bfd_boolean relocs;
9500   bfd *ibfd;
9501   struct got_entry *first_tlsld;
9502
9503   htab = ppc_hash_table (info);
9504   if (htab == NULL)
9505     return FALSE;
9506
9507   dynobj = htab->elf.dynobj;
9508   if (dynobj == NULL)
9509     abort ();
9510
9511   if (htab->elf.dynamic_sections_created)
9512     {
9513       /* Set the contents of the .interp section to the interpreter.  */
9514       if (info->executable)
9515         {
9516           s = bfd_get_linker_section (dynobj, ".interp");
9517           if (s == NULL)
9518             abort ();
9519           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9520           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9521         }
9522     }
9523
9524   /* Set up .got offsets for local syms, and space for local dynamic
9525      relocs.  */
9526   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9527     {
9528       struct got_entry **lgot_ents;
9529       struct got_entry **end_lgot_ents;
9530       struct plt_entry **local_plt;
9531       struct plt_entry **end_local_plt;
9532       unsigned char *lgot_masks;
9533       bfd_size_type locsymcount;
9534       Elf_Internal_Shdr *symtab_hdr;
9535
9536       if (!is_ppc64_elf (ibfd))
9537         continue;
9538
9539       for (s = ibfd->sections; s != NULL; s = s->next)
9540         {
9541           struct ppc_dyn_relocs *p;
9542
9543           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9544             {
9545               if (!bfd_is_abs_section (p->sec)
9546                   && bfd_is_abs_section (p->sec->output_section))
9547                 {
9548                   /* Input section has been discarded, either because
9549                      it is a copy of a linkonce section or due to
9550                      linker script /DISCARD/, so we'll be discarding
9551                      the relocs too.  */
9552                 }
9553               else if (p->count != 0)
9554                 {
9555                   asection *srel = elf_section_data (p->sec)->sreloc;
9556                   if (p->ifunc)
9557                     srel = htab->reliplt;
9558                   srel->size += p->count * sizeof (Elf64_External_Rela);
9559                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9560                     info->flags |= DF_TEXTREL;
9561                 }
9562             }
9563         }
9564
9565       lgot_ents = elf_local_got_ents (ibfd);
9566       if (!lgot_ents)
9567         continue;
9568
9569       symtab_hdr = &elf_symtab_hdr (ibfd);
9570       locsymcount = symtab_hdr->sh_info;
9571       end_lgot_ents = lgot_ents + locsymcount;
9572       local_plt = (struct plt_entry **) end_lgot_ents;
9573       end_local_plt = local_plt + locsymcount;
9574       lgot_masks = (unsigned char *) end_local_plt;
9575       s = ppc64_elf_tdata (ibfd)->got;
9576       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9577         {
9578           struct got_entry **pent, *ent;
9579
9580           pent = lgot_ents;
9581           while ((ent = *pent) != NULL)
9582             if (ent->got.refcount > 0)
9583               {
9584                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9585                   {
9586                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
9587                     *pent = ent->next;
9588                   }
9589                 else
9590                   {
9591                     unsigned int ent_size = 8;
9592                     unsigned int rel_size = sizeof (Elf64_External_Rela);
9593
9594                     ent->got.offset = s->size;
9595                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9596                       {
9597                         ent_size *= 2;
9598                         rel_size *= 2;
9599                       }
9600                     s->size += ent_size;
9601                     if ((*lgot_masks & PLT_IFUNC) != 0)
9602                       {
9603                         htab->reliplt->size += rel_size;
9604                         htab->got_reli_size += rel_size;
9605                       }
9606                     else if (info->shared)
9607                       {
9608                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9609                         srel->size += rel_size;
9610                       }
9611                     pent = &ent->next;
9612                   }
9613               }
9614             else
9615               *pent = ent->next;
9616         }
9617
9618       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
9619       for (; local_plt < end_local_plt; ++local_plt)
9620         {
9621           struct plt_entry *ent;
9622
9623           for (ent = *local_plt; ent != NULL; ent = ent->next)
9624             if (ent->plt.refcount > 0)
9625               {
9626                 s = htab->iplt;
9627                 ent->plt.offset = s->size;
9628                 s->size += PLT_ENTRY_SIZE (htab);
9629
9630                 htab->reliplt->size += sizeof (Elf64_External_Rela);
9631               }
9632             else
9633               ent->plt.offset = (bfd_vma) -1;
9634         }
9635     }
9636
9637   /* Allocate global sym .plt and .got entries, and space for global
9638      sym dynamic relocs.  */
9639   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9640
9641   first_tlsld = NULL;
9642   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9643     {
9644       struct got_entry *ent;
9645
9646       if (!is_ppc64_elf (ibfd))
9647         continue;
9648
9649       ent = ppc64_tlsld_got (ibfd);
9650       if (ent->got.refcount > 0)
9651         {
9652           if (!htab->do_multi_toc && first_tlsld != NULL)
9653             {
9654               ent->is_indirect = TRUE;
9655               ent->got.ent = first_tlsld;
9656             }
9657           else
9658             {
9659               if (first_tlsld == NULL)
9660                 first_tlsld = ent;
9661               s = ppc64_elf_tdata (ibfd)->got;
9662               ent->got.offset = s->size;
9663               ent->owner = ibfd;
9664               s->size += 16;
9665               if (info->shared)
9666                 {
9667                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9668                   srel->size += sizeof (Elf64_External_Rela);
9669                 }
9670             }
9671         }
9672       else
9673         ent->got.offset = (bfd_vma) -1;
9674     }
9675
9676   /* We now have determined the sizes of the various dynamic sections.
9677      Allocate memory for them.  */
9678   relocs = FALSE;
9679   for (s = dynobj->sections; s != NULL; s = s->next)
9680     {
9681       if ((s->flags & SEC_LINKER_CREATED) == 0)
9682         continue;
9683
9684       if (s == htab->brlt || s == htab->relbrlt)
9685         /* These haven't been allocated yet;  don't strip.  */
9686         continue;
9687       else if (s == htab->got
9688                || s == htab->plt
9689                || s == htab->iplt
9690                || s == htab->glink
9691                || s == htab->dynbss)
9692         {
9693           /* Strip this section if we don't need it; see the
9694              comment below.  */
9695         }
9696       else if (s == htab->glink_eh_frame)
9697         {
9698           if (!bfd_is_abs_section (s->output_section))
9699             /* Not sized yet.  */
9700             continue;
9701         }
9702       else if (CONST_STRNEQ (s->name, ".rela"))
9703         {
9704           if (s->size != 0)
9705             {
9706               if (s != htab->relplt)
9707                 relocs = TRUE;
9708
9709               /* We use the reloc_count field as a counter if we need
9710                  to copy relocs into the output file.  */
9711               s->reloc_count = 0;
9712             }
9713         }
9714       else
9715         {
9716           /* It's not one of our sections, so don't allocate space.  */
9717           continue;
9718         }
9719
9720       if (s->size == 0)
9721         {
9722           /* If we don't need this section, strip it from the
9723              output file.  This is mostly to handle .rela.bss and
9724              .rela.plt.  We must create both sections in
9725              create_dynamic_sections, because they must be created
9726              before the linker maps input sections to output
9727              sections.  The linker does that before
9728              adjust_dynamic_symbol is called, and it is that
9729              function which decides whether anything needs to go
9730              into these sections.  */
9731           s->flags |= SEC_EXCLUDE;
9732           continue;
9733         }
9734
9735       if ((s->flags & SEC_HAS_CONTENTS) == 0)
9736         continue;
9737
9738       /* Allocate memory for the section contents.  We use bfd_zalloc
9739          here in case unused entries are not reclaimed before the
9740          section's contents are written out.  This should not happen,
9741          but this way if it does we get a R_PPC64_NONE reloc in .rela
9742          sections instead of garbage.
9743          We also rely on the section contents being zero when writing
9744          the GOT.  */
9745       s->contents = bfd_zalloc (dynobj, s->size);
9746       if (s->contents == NULL)
9747         return FALSE;
9748     }
9749
9750   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9751     {
9752       if (!is_ppc64_elf (ibfd))
9753         continue;
9754
9755       s = ppc64_elf_tdata (ibfd)->got;
9756       if (s != NULL && s != htab->got)
9757         {
9758           if (s->size == 0)
9759             s->flags |= SEC_EXCLUDE;
9760           else
9761             {
9762               s->contents = bfd_zalloc (ibfd, s->size);
9763               if (s->contents == NULL)
9764                 return FALSE;
9765             }
9766         }
9767       s = ppc64_elf_tdata (ibfd)->relgot;
9768       if (s != NULL)
9769         {
9770           if (s->size == 0)
9771             s->flags |= SEC_EXCLUDE;
9772           else
9773             {
9774               s->contents = bfd_zalloc (ibfd, s->size);
9775               if (s->contents == NULL)
9776                 return FALSE;
9777               relocs = TRUE;
9778               s->reloc_count = 0;
9779             }
9780         }
9781     }
9782
9783   if (htab->elf.dynamic_sections_created)
9784     {
9785       /* Add some entries to the .dynamic section.  We fill in the
9786          values later, in ppc64_elf_finish_dynamic_sections, but we
9787          must add the entries now so that we get the correct size for
9788          the .dynamic section.  The DT_DEBUG entry is filled in by the
9789          dynamic linker and used by the debugger.  */
9790 #define add_dynamic_entry(TAG, VAL) \
9791   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9792
9793       if (info->executable)
9794         {
9795           if (!add_dynamic_entry (DT_DEBUG, 0))
9796             return FALSE;
9797         }
9798
9799       if (htab->plt != NULL && htab->plt->size != 0)
9800         {
9801           if (!add_dynamic_entry (DT_PLTGOT, 0)
9802               || !add_dynamic_entry (DT_PLTRELSZ, 0)
9803               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9804               || !add_dynamic_entry (DT_JMPREL, 0)
9805               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9806             return FALSE;
9807         }
9808
9809       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
9810         {
9811           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9812               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9813             return FALSE;
9814         }
9815
9816       if (!htab->no_tls_get_addr_opt
9817           && htab->tls_get_addr_fd != NULL
9818           && htab->tls_get_addr_fd->elf.plt.plist != NULL
9819           && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9820         return FALSE;
9821
9822       if (relocs)
9823         {
9824           if (!add_dynamic_entry (DT_RELA, 0)
9825               || !add_dynamic_entry (DT_RELASZ, 0)
9826               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9827             return FALSE;
9828
9829           /* If any dynamic relocs apply to a read-only section,
9830              then we need a DT_TEXTREL entry.  */
9831           if ((info->flags & DF_TEXTREL) == 0)
9832             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
9833
9834           if ((info->flags & DF_TEXTREL) != 0)
9835             {
9836               if (!add_dynamic_entry (DT_TEXTREL, 0))
9837                 return FALSE;
9838             }
9839         }
9840     }
9841 #undef add_dynamic_entry
9842
9843   return TRUE;
9844 }
9845
9846 /* Determine the type of stub needed, if any, for a call.  */
9847
9848 static inline enum ppc_stub_type
9849 ppc_type_of_stub (asection *input_sec,
9850                   const Elf_Internal_Rela *rel,
9851                   struct ppc_link_hash_entry **hash,
9852                   struct plt_entry **plt_ent,
9853                   bfd_vma destination,
9854                   unsigned long local_off)
9855 {
9856   struct ppc_link_hash_entry *h = *hash;
9857   bfd_vma location;
9858   bfd_vma branch_offset;
9859   bfd_vma max_branch_offset;
9860   enum elf_ppc64_reloc_type r_type;
9861
9862   if (h != NULL)
9863     {
9864       struct plt_entry *ent;
9865       struct ppc_link_hash_entry *fdh = h;
9866       if (h->oh != NULL
9867           && h->oh->is_func_descriptor)
9868         {
9869           fdh = ppc_follow_link (h->oh);
9870           *hash = fdh;
9871         }
9872
9873       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9874         if (ent->addend == rel->r_addend
9875             && ent->plt.offset != (bfd_vma) -1)
9876           {
9877             *plt_ent = ent;
9878             return ppc_stub_plt_call;
9879           }
9880
9881       /* Here, we know we don't have a plt entry.  If we don't have a
9882          either a defined function descriptor or a defined entry symbol
9883          in a regular object file, then it is pointless trying to make
9884          any other type of stub.  */
9885       if (!is_static_defined (&fdh->elf)
9886           && !is_static_defined (&h->elf))
9887         return ppc_stub_none;
9888     }
9889   else if (elf_local_got_ents (input_sec->owner) != NULL)
9890     {
9891       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9892       struct plt_entry **local_plt = (struct plt_entry **)
9893         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9894       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9895
9896       if (local_plt[r_symndx] != NULL)
9897         {
9898           struct plt_entry *ent;
9899
9900           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9901             if (ent->addend == rel->r_addend
9902                 && ent->plt.offset != (bfd_vma) -1)
9903               {
9904                 *plt_ent = ent;
9905                 return ppc_stub_plt_call;
9906               }
9907         }
9908     }
9909
9910   /* Determine where the call point is.  */
9911   location = (input_sec->output_offset
9912               + input_sec->output_section->vma
9913               + rel->r_offset);
9914
9915   branch_offset = destination - location;
9916   r_type = ELF64_R_TYPE (rel->r_info);
9917
9918   /* Determine if a long branch stub is needed.  */
9919   max_branch_offset = 1 << 25;
9920   if (r_type != R_PPC64_REL24)
9921     max_branch_offset = 1 << 15;
9922
9923   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
9924     /* We need a stub.  Figure out whether a long_branch or plt_branch
9925        is needed later.  */
9926     return ppc_stub_long_branch;
9927
9928   return ppc_stub_none;
9929 }
9930
9931 /* With power7 weakly ordered memory model, it is possible for ld.so
9932    to update a plt entry in one thread and have another thread see a
9933    stale zero toc entry.  To avoid this we need some sort of acquire
9934    barrier in the call stub.  One solution is to make the load of the
9935    toc word seem to appear to depend on the load of the function entry
9936    word.  Another solution is to test for r2 being zero, and branch to
9937    the appropriate glink entry if so.
9938
9939    .    fake dep barrier        compare
9940    .    ld 12,xxx(2)            ld 12,xxx(2)
9941    .    mtctr 12                mtctr 12
9942    .    xor 11,12,12            ld 2,xxx+8(2)
9943    .    add 2,2,11              cmpldi 2,0
9944    .    ld 2,xxx+8(2)           bnectr+
9945    .    bctr                    b <glink_entry>
9946
9947    The solution involving the compare turns out to be faster, so
9948    that's what we use unless the branch won't reach.  */
9949
9950 #define ALWAYS_USE_FAKE_DEP 0
9951 #define ALWAYS_EMIT_R2SAVE 0
9952
9953 #define PPC_LO(v) ((v) & 0xffff)
9954 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9955 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9956
9957 static inline unsigned int
9958 plt_stub_size (struct ppc_link_hash_table *htab,
9959                struct ppc_stub_hash_entry *stub_entry,
9960                bfd_vma off)
9961 {
9962   unsigned size = 12;
9963
9964   if (ALWAYS_EMIT_R2SAVE
9965       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9966     size += 4;
9967   if (PPC_HA (off) != 0)
9968     size += 4;
9969   if (htab->opd_abi)
9970     {
9971       size += 4;
9972       if (htab->plt_static_chain)
9973         size += 4;
9974       if (htab->plt_thread_safe)
9975         size += 8;
9976       if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
9977         size += 4;
9978     }
9979   if (stub_entry->h != NULL
9980       && (stub_entry->h == htab->tls_get_addr_fd
9981           || stub_entry->h == htab->tls_get_addr)
9982       && !htab->no_tls_get_addr_opt)
9983     size += 13 * 4;
9984   return size;
9985 }
9986
9987 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
9988    then return the padding needed to do so.  */
9989 static inline unsigned int
9990 plt_stub_pad (struct ppc_link_hash_table *htab,
9991               struct ppc_stub_hash_entry *stub_entry,
9992               bfd_vma plt_off)
9993 {
9994   int stub_align = 1 << htab->plt_stub_align;
9995   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
9996   bfd_vma stub_off = stub_entry->stub_sec->size;
9997
9998   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
9999       > (stub_size & -stub_align))
10000     return stub_align - (stub_off & (stub_align - 1));
10001   return 0;
10002 }
10003
10004 /* Build a .plt call stub.  */
10005
10006 static inline bfd_byte *
10007 build_plt_stub (struct ppc_link_hash_table *htab,
10008                 struct ppc_stub_hash_entry *stub_entry,
10009                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10010 {
10011   bfd *obfd = htab->stub_bfd;
10012   bfd_boolean plt_load_toc = htab->opd_abi;
10013   bfd_boolean plt_static_chain = htab->plt_static_chain;
10014   bfd_boolean plt_thread_safe = htab->plt_thread_safe;
10015   bfd_boolean use_fake_dep = plt_thread_safe;
10016   bfd_vma cmp_branch_off = 0;
10017
10018   if (!ALWAYS_USE_FAKE_DEP
10019       && plt_load_toc
10020       && plt_thread_safe
10021       && !(stub_entry->h != NULL
10022            && (stub_entry->h == htab->tls_get_addr_fd
10023                || stub_entry->h == htab->tls_get_addr)
10024            && !htab->no_tls_get_addr_opt))
10025     {
10026       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10027       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10028                           / PLT_ENTRY_SIZE (htab));
10029       bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10030       bfd_vma to, from;
10031
10032       if (pltindex > 32768)
10033         glinkoff += (pltindex - 32768) * 4;
10034       to = (glinkoff
10035             + htab->glink->output_offset
10036             + htab->glink->output_section->vma);
10037       from = (p - stub_entry->stub_sec->contents
10038               + 4 * (ALWAYS_EMIT_R2SAVE
10039                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10040               + 4 * (PPC_HA (offset) != 0)
10041               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10042                      != PPC_HA (offset))
10043               + 4 * (plt_static_chain != 0)
10044               + 20
10045               + stub_entry->stub_sec->output_offset
10046               + stub_entry->stub_sec->output_section->vma);
10047       cmp_branch_off = to - from;
10048       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10049     }
10050
10051   if (PPC_HA (offset) != 0)
10052     {
10053       if (r != NULL)
10054         {
10055           if (ALWAYS_EMIT_R2SAVE
10056               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10057             r[0].r_offset += 4;
10058           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10059           r[1].r_offset = r[0].r_offset + 4;
10060           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10061           r[1].r_addend = r[0].r_addend;
10062           if (plt_load_toc)
10063             {
10064               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10065                 {
10066                   r[2].r_offset = r[1].r_offset + 4;
10067                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10068                   r[2].r_addend = r[0].r_addend;
10069                 }
10070               else
10071                 {
10072                   r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10073                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10074                   r[2].r_addend = r[0].r_addend + 8;
10075                   if (plt_static_chain)
10076                     {
10077                       r[3].r_offset = r[2].r_offset + 4;
10078                       r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10079                       r[3].r_addend = r[0].r_addend + 16;
10080                     }
10081                 }
10082             }
10083         }
10084       if (ALWAYS_EMIT_R2SAVE
10085           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10086         bfd_put_32 (obfd, STD_R2_40R1, p),                      p += 4;
10087       bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p),     p += 4;
10088       bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),      p += 4;
10089       if (plt_load_toc
10090           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10091         {
10092           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10093           offset = 0;
10094         }
10095       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10096       if (plt_load_toc)
10097         {
10098           if (use_fake_dep)
10099             {
10100               bfd_put_32 (obfd, XOR_R2_R12_R12, p),             p += 4;
10101               bfd_put_32 (obfd, ADD_R11_R11_R2, p),             p += 4;
10102             }
10103           bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10104           if (plt_static_chain)
10105             bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10106         }
10107     }
10108   else
10109     {
10110       if (r != NULL)
10111         {
10112           if (ALWAYS_EMIT_R2SAVE
10113               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10114             r[0].r_offset += 4;
10115           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10116           if (plt_load_toc)
10117             {
10118               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10119                 {
10120                   r[1].r_offset = r[0].r_offset + 4;
10121                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10122                   r[1].r_addend = r[0].r_addend;
10123                 }
10124               else
10125                 {
10126                   r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10127                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10128                   r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10129                   if (plt_static_chain)
10130                     {
10131                       r[2].r_offset = r[1].r_offset + 4;
10132                       r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10133                       r[2].r_addend = r[0].r_addend + 8;
10134                     }
10135                 }
10136             }
10137         }
10138       if (ALWAYS_EMIT_R2SAVE
10139           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10140         bfd_put_32 (obfd, STD_R2_40R1, p),                      p += 4;
10141       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
10142       if (plt_load_toc
10143           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10144         {
10145           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
10146           offset = 0;
10147         }
10148       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10149       if (plt_load_toc)
10150         {
10151           if (use_fake_dep)
10152             {
10153               bfd_put_32 (obfd, XOR_R11_R12_R12, p),            p += 4;
10154               bfd_put_32 (obfd, ADD_R2_R2_R11, p),              p += 4;
10155             }
10156           if (plt_static_chain)
10157             bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10158           bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10159         }
10160     }
10161   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10162     {
10163       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
10164       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
10165       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10166     }
10167   else
10168     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
10169   return p;
10170 }
10171
10172 /* Build a special .plt call stub for __tls_get_addr.  */
10173
10174 #define LD_R11_0R3      0xe9630000
10175 #define LD_R12_0R3      0xe9830000
10176 #define MR_R0_R3        0x7c601b78
10177 #define CMPDI_R11_0     0x2c2b0000
10178 #define ADD_R3_R12_R13  0x7c6c6a14
10179 #define BEQLR           0x4d820020
10180 #define MR_R3_R0        0x7c030378
10181 #define MFLR_R11        0x7d6802a6
10182 #define STD_R11_0R1     0xf9610000
10183 #define BCTRL           0x4e800421
10184 #define LD_R11_0R1      0xe9610000
10185 #define LD_R2_0R1       0xe8410000
10186 #define MTLR_R11        0x7d6803a6
10187
10188 static inline bfd_byte *
10189 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10190                          struct ppc_stub_hash_entry *stub_entry,
10191                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10192 {
10193   bfd *obfd = htab->stub_bfd;
10194
10195   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
10196   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
10197   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
10198   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
10199   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
10200   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
10201   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
10202   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
10203   bfd_put_32 (obfd, STD_R11_0R1 + 32, p),       p += 4;
10204
10205   if (r != NULL)
10206     r[0].r_offset += 9 * 4;
10207   p = build_plt_stub (htab, stub_entry, p, offset, r);
10208   bfd_put_32 (obfd, BCTRL, p - 4);
10209
10210   bfd_put_32 (obfd, LD_R11_0R1 + 32, p),        p += 4;
10211   bfd_put_32 (obfd, LD_R2_0R1 + 40, p),         p += 4;
10212   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
10213   bfd_put_32 (obfd, BLR, p),                    p += 4;
10214
10215   return p;
10216 }
10217
10218 static Elf_Internal_Rela *
10219 get_relocs (asection *sec, int count)
10220 {
10221   Elf_Internal_Rela *relocs;
10222   struct bfd_elf_section_data *elfsec_data;
10223
10224   elfsec_data = elf_section_data (sec);
10225   relocs = elfsec_data->relocs;
10226   if (relocs == NULL)
10227     {
10228       bfd_size_type relsize;
10229       relsize = sec->reloc_count * sizeof (*relocs);
10230       relocs = bfd_alloc (sec->owner, relsize);
10231       if (relocs == NULL)
10232         return NULL;
10233       elfsec_data->relocs = relocs;
10234       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10235                                           sizeof (Elf_Internal_Shdr));
10236       if (elfsec_data->rela.hdr == NULL)
10237         return NULL;
10238       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10239                                         * sizeof (Elf64_External_Rela));
10240       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10241       sec->reloc_count = 0;
10242     }
10243   relocs += sec->reloc_count;
10244   sec->reloc_count += count;
10245   return relocs;
10246 }
10247
10248 static bfd_vma
10249 get_r2off (struct bfd_link_info *info,
10250            struct ppc_stub_hash_entry *stub_entry)
10251 {
10252   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10253   bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
10254
10255   if (r2off == 0)
10256     {
10257       /* Support linking -R objects.  Get the toc pointer from the
10258          opd entry.  */
10259       char buf[8];
10260       if (!htab->opd_abi)
10261         return r2off;
10262       asection *opd = stub_entry->h->elf.root.u.def.section;
10263       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10264
10265       if (strcmp (opd->name, ".opd") != 0
10266           || opd->reloc_count != 0)
10267         {
10268           info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10269                                   stub_entry->h->elf.root.root.string);
10270           bfd_set_error (bfd_error_bad_value);
10271           return 0;
10272         }
10273       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10274         return 0;
10275       r2off = bfd_get_64 (opd->owner, buf);
10276       r2off -= elf_gp (info->output_bfd);
10277     }
10278   r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
10279   return r2off;
10280 }
10281
10282 static bfd_boolean
10283 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10284 {
10285   struct ppc_stub_hash_entry *stub_entry;
10286   struct ppc_branch_hash_entry *br_entry;
10287   struct bfd_link_info *info;
10288   struct ppc_link_hash_table *htab;
10289   bfd_byte *loc;
10290   bfd_byte *p;
10291   bfd_vma dest, off;
10292   int size;
10293   Elf_Internal_Rela *r;
10294   asection *plt;
10295
10296   /* Massage our args to the form they really have.  */
10297   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10298   info = in_arg;
10299
10300   htab = ppc_hash_table (info);
10301   if (htab == NULL)
10302     return FALSE;
10303
10304   /* Make a note of the offset within the stubs for this entry.  */
10305   stub_entry->stub_offset = stub_entry->stub_sec->size;
10306   loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
10307
10308   htab->stub_count[stub_entry->stub_type - 1] += 1;
10309   switch (stub_entry->stub_type)
10310     {
10311     case ppc_stub_long_branch:
10312     case ppc_stub_long_branch_r2off:
10313       /* Branches are relative.  This is where we are going to.  */
10314       dest = (stub_entry->target_value
10315               + stub_entry->target_section->output_offset
10316               + stub_entry->target_section->output_section->vma);
10317       dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10318       off = dest;
10319
10320       /* And this is where we are coming from.  */
10321       off -= (stub_entry->stub_offset
10322               + stub_entry->stub_sec->output_offset
10323               + stub_entry->stub_sec->output_section->vma);
10324
10325       size = 4;
10326       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10327         {
10328           bfd_vma r2off = get_r2off (info, stub_entry);
10329
10330           if (r2off == 0)
10331             {
10332               htab->stub_error = TRUE;
10333               return FALSE;
10334             }
10335           bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
10336           loc += 4;
10337           size = 12;
10338           if (PPC_HA (r2off) != 0)
10339             {
10340               size = 16;
10341               bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10342               loc += 4;
10343             }
10344           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
10345           loc += 4;
10346           off -= size - 4;
10347         }
10348       bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
10349
10350       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10351         {
10352           info->callbacks->einfo
10353             (_("%P: long branch stub `%s' offset overflow\n"),
10354              stub_entry->root.string);
10355           htab->stub_error = TRUE;
10356           return FALSE;
10357         }
10358
10359       if (info->emitrelocations)
10360         {
10361           r = get_relocs (stub_entry->stub_sec, 1);
10362           if (r == NULL)
10363             return FALSE;
10364           r->r_offset = loc - stub_entry->stub_sec->contents;
10365           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10366           r->r_addend = dest;
10367           if (stub_entry->h != NULL)
10368             {
10369               struct elf_link_hash_entry **hashes;
10370               unsigned long symndx;
10371               struct ppc_link_hash_entry *h;
10372
10373               hashes = elf_sym_hashes (htab->stub_bfd);
10374               if (hashes == NULL)
10375                 {
10376                   bfd_size_type hsize;
10377
10378                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10379                   hashes = bfd_zalloc (htab->stub_bfd, hsize);
10380                   if (hashes == NULL)
10381                     return FALSE;
10382                   elf_sym_hashes (htab->stub_bfd) = hashes;
10383                   htab->stub_globals = 1;
10384                 }
10385               symndx = htab->stub_globals++;
10386               h = stub_entry->h;
10387               hashes[symndx] = &h->elf;
10388               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10389               if (h->oh != NULL && h->oh->is_func)
10390                 h = ppc_follow_link (h->oh);
10391               if (h->elf.root.u.def.section != stub_entry->target_section)
10392                 /* H is an opd symbol.  The addend must be zero.  */
10393                 r->r_addend = 0;
10394               else
10395                 {
10396                   off = (h->elf.root.u.def.value
10397                          + h->elf.root.u.def.section->output_offset
10398                          + h->elf.root.u.def.section->output_section->vma);
10399                   r->r_addend -= off;
10400                 }
10401             }
10402         }
10403       break;
10404
10405     case ppc_stub_plt_branch:
10406     case ppc_stub_plt_branch_r2off:
10407       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10408                                          stub_entry->root.string + 9,
10409                                          FALSE, FALSE);
10410       if (br_entry == NULL)
10411         {
10412           info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10413                                   stub_entry->root.string);
10414           htab->stub_error = TRUE;
10415           return FALSE;
10416         }
10417
10418       dest = (stub_entry->target_value
10419               + stub_entry->target_section->output_offset
10420               + stub_entry->target_section->output_section->vma);
10421       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10422         dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10423
10424       bfd_put_64 (htab->brlt->owner, dest,
10425                   htab->brlt->contents + br_entry->offset);
10426
10427       if (br_entry->iter == htab->stub_iteration)
10428         {
10429           br_entry->iter = 0;
10430
10431           if (htab->relbrlt != NULL)
10432             {
10433               /* Create a reloc for the branch lookup table entry.  */
10434               Elf_Internal_Rela rela;
10435               bfd_byte *rl;
10436
10437               rela.r_offset = (br_entry->offset
10438                                + htab->brlt->output_offset
10439                                + htab->brlt->output_section->vma);
10440               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10441               rela.r_addend = dest;
10442
10443               rl = htab->relbrlt->contents;
10444               rl += (htab->relbrlt->reloc_count++
10445                      * sizeof (Elf64_External_Rela));
10446               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10447             }
10448           else if (info->emitrelocations)
10449             {
10450               r = get_relocs (htab->brlt, 1);
10451               if (r == NULL)
10452                 return FALSE;
10453               /* brlt, being SEC_LINKER_CREATED does not go through the
10454                  normal reloc processing.  Symbols and offsets are not
10455                  translated from input file to output file form, so
10456                  set up the offset per the output file.  */
10457               r->r_offset = (br_entry->offset
10458                              + htab->brlt->output_offset
10459                              + htab->brlt->output_section->vma);
10460               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10461               r->r_addend = dest;
10462             }
10463         }
10464
10465       dest = (br_entry->offset
10466               + htab->brlt->output_offset
10467               + htab->brlt->output_section->vma);
10468
10469       off = (dest
10470              - elf_gp (htab->brlt->output_section->owner)
10471              - htab->stub_group[stub_entry->id_sec->id].toc_off);
10472
10473       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10474         {
10475           info->callbacks->einfo
10476             (_("%P: linkage table error against `%T'\n"),
10477              stub_entry->root.string);
10478           bfd_set_error (bfd_error_bad_value);
10479           htab->stub_error = TRUE;
10480           return FALSE;
10481         }
10482
10483       if (info->emitrelocations)
10484         {
10485           r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
10486           if (r == NULL)
10487             return FALSE;
10488           r[0].r_offset = loc - stub_entry->stub_sec->contents;
10489           if (bfd_big_endian (info->output_bfd))
10490             r[0].r_offset += 2;
10491           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off
10492               && htab->opd_abi)
10493             r[0].r_offset += 4;
10494           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10495           r[0].r_addend = dest;
10496           if (PPC_HA (off) != 0)
10497             {
10498               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10499               r[1].r_offset = r[0].r_offset + 4;
10500               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10501               r[1].r_addend = r[0].r_addend;
10502             }
10503         }
10504
10505       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off
10506           || !htab->opd_abi)
10507         {
10508           if (PPC_HA (off) != 0)
10509             {
10510               size = 16;
10511               bfd_put_32 (htab->stub_bfd, ADDIS_R11_R2 | PPC_HA (off), loc);
10512               loc += 4;
10513               bfd_put_32 (htab->stub_bfd, LD_R12_0R11 | PPC_LO (off), loc);
10514             }
10515           else
10516             {
10517               size = 12;
10518               bfd_put_32 (htab->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
10519             }
10520         }
10521       else
10522         {
10523           bfd_vma r2off = get_r2off (info, stub_entry);
10524
10525           if (r2off == 0)
10526             {
10527               htab->stub_error = TRUE;
10528               return FALSE;
10529             }
10530
10531           bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
10532           loc += 4;
10533           size = 20;
10534           if (PPC_HA (off) != 0)
10535             {
10536               size += 4;
10537               bfd_put_32 (htab->stub_bfd, ADDIS_R11_R2 | PPC_HA (off), loc);
10538               loc += 4;
10539               bfd_put_32 (htab->stub_bfd, LD_R12_0R11 | PPC_LO (off), loc);
10540               loc += 4;
10541             }
10542           else
10543             {
10544               bfd_put_32 (htab->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
10545               loc += 4;
10546             }
10547
10548           if (PPC_HA (r2off) != 0)
10549             {
10550               size += 4;
10551               bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10552               loc += 4;
10553             }
10554           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
10555         }
10556       loc += 4;
10557       bfd_put_32 (htab->stub_bfd, MTCTR_R12, loc);
10558       loc += 4;
10559       bfd_put_32 (htab->stub_bfd, BCTR, loc);
10560       break;
10561
10562     case ppc_stub_plt_call:
10563     case ppc_stub_plt_call_r2save:
10564       if (stub_entry->h != NULL
10565           && stub_entry->h->is_func_descriptor
10566           && stub_entry->h->oh != NULL)
10567         {
10568           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10569
10570           /* If the old-ABI "dot-symbol" is undefined make it weak so
10571              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
10572              FIXME: We used to define the symbol on one of the call
10573              stubs instead, which is why we test symbol section id
10574              against htab->top_id in various places.  Likely all
10575              these checks could now disappear.  */
10576           if (fh->elf.root.type == bfd_link_hash_undefined)
10577             fh->elf.root.type = bfd_link_hash_undefweak;
10578           /* Stop undo_symbol_twiddle changing it back to undefined.  */
10579           fh->was_undefined = 0;
10580         }
10581
10582       /* Now build the stub.  */
10583       dest = stub_entry->plt_ent->plt.offset & ~1;
10584       if (dest >= (bfd_vma) -2)
10585         abort ();
10586
10587       plt = htab->plt;
10588       if (!htab->elf.dynamic_sections_created
10589           || stub_entry->h == NULL
10590           || stub_entry->h->elf.dynindx == -1)
10591         plt = htab->iplt;
10592
10593       dest += plt->output_offset + plt->output_section->vma;
10594
10595       if (stub_entry->h == NULL
10596           && (stub_entry->plt_ent->plt.offset & 1) == 0)
10597         {
10598           Elf_Internal_Rela rela;
10599           bfd_byte *rl;
10600
10601           rela.r_offset = dest;
10602           if (htab->opd_abi)
10603             rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
10604           else
10605             rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
10606           rela.r_addend = (stub_entry->target_value
10607                            + stub_entry->target_section->output_offset
10608                            + stub_entry->target_section->output_section->vma);
10609
10610           rl = (htab->reliplt->contents
10611                 + (htab->reliplt->reloc_count++
10612                    * sizeof (Elf64_External_Rela)));
10613           bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
10614           stub_entry->plt_ent->plt.offset |= 1;
10615         }
10616
10617       off = (dest
10618              - elf_gp (plt->output_section->owner)
10619              - htab->stub_group[stub_entry->id_sec->id].toc_off);
10620
10621       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10622         {
10623           info->callbacks->einfo
10624             (_("%P: linkage table error against `%T'\n"),
10625              stub_entry->h != NULL
10626              ? stub_entry->h->elf.root.root.string
10627              : "<local sym>");
10628           bfd_set_error (bfd_error_bad_value);
10629           htab->stub_error = TRUE;
10630           return FALSE;
10631         }
10632
10633       if (htab->plt_stub_align != 0)
10634         {
10635           unsigned pad = plt_stub_pad (htab, stub_entry, off);
10636
10637           stub_entry->stub_sec->size += pad;
10638           stub_entry->stub_offset = stub_entry->stub_sec->size;
10639           loc += pad;
10640         }
10641
10642       r = NULL;
10643       if (info->emitrelocations)
10644         {
10645           r = get_relocs (stub_entry->stub_sec,
10646                           (2
10647                            + (PPC_HA (off) != 0)
10648                            + (htab->plt_static_chain
10649                               && PPC_HA (off + 16) == PPC_HA (off))));
10650           if (r == NULL)
10651             return FALSE;
10652           r[0].r_offset = loc - stub_entry->stub_sec->contents;
10653           if (bfd_big_endian (info->output_bfd))
10654             r[0].r_offset += 2;
10655           r[0].r_addend = dest;
10656         }
10657       if (stub_entry->h != NULL
10658           && (stub_entry->h == htab->tls_get_addr_fd
10659               || stub_entry->h == htab->tls_get_addr)
10660           && !htab->no_tls_get_addr_opt)
10661         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
10662       else
10663         p = build_plt_stub (htab, stub_entry, loc, off, r);
10664       size = p - loc;
10665       break;
10666
10667     default:
10668       BFD_FAIL ();
10669       return FALSE;
10670     }
10671
10672   stub_entry->stub_sec->size += size;
10673
10674   if (htab->emit_stub_syms)
10675     {
10676       struct elf_link_hash_entry *h;
10677       size_t len1, len2;
10678       char *name;
10679       const char *const stub_str[] = { "long_branch",
10680                                        "long_branch_r2off",
10681                                        "plt_branch",
10682                                        "plt_branch_r2off",
10683                                        "plt_call",
10684                                        "plt_call" };
10685
10686       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10687       len2 = strlen (stub_entry->root.string);
10688       name = bfd_malloc (len1 + len2 + 2);
10689       if (name == NULL)
10690         return FALSE;
10691       memcpy (name, stub_entry->root.string, 9);
10692       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10693       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10694       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
10695       if (h == NULL)
10696         return FALSE;
10697       if (h->root.type == bfd_link_hash_new)
10698         {
10699           h->root.type = bfd_link_hash_defined;
10700           h->root.u.def.section = stub_entry->stub_sec;
10701           h->root.u.def.value = stub_entry->stub_offset;
10702           h->ref_regular = 1;
10703           h->def_regular = 1;
10704           h->ref_regular_nonweak = 1;
10705           h->forced_local = 1;
10706           h->non_elf = 0;
10707         }
10708     }
10709
10710   return TRUE;
10711 }
10712
10713 /* As above, but don't actually build the stub.  Just bump offset so
10714    we know stub section sizes, and select plt_branch stubs where
10715    long_branch stubs won't do.  */
10716
10717 static bfd_boolean
10718 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10719 {
10720   struct ppc_stub_hash_entry *stub_entry;
10721   struct bfd_link_info *info;
10722   struct ppc_link_hash_table *htab;
10723   bfd_vma off;
10724   int size;
10725
10726   /* Massage our args to the form they really have.  */
10727   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10728   info = in_arg;
10729
10730   htab = ppc_hash_table (info);
10731   if (htab == NULL)
10732     return FALSE;
10733
10734   if (stub_entry->stub_type == ppc_stub_plt_call
10735       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10736     {
10737       asection *plt;
10738       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
10739       if (off >= (bfd_vma) -2)
10740         abort ();
10741       plt = htab->plt;
10742       if (!htab->elf.dynamic_sections_created
10743           || stub_entry->h == NULL
10744           || stub_entry->h->elf.dynindx == -1)
10745         plt = htab->iplt;
10746       off += (plt->output_offset
10747               + plt->output_section->vma
10748               - elf_gp (plt->output_section->owner)
10749               - htab->stub_group[stub_entry->id_sec->id].toc_off);
10750
10751       size = plt_stub_size (htab, stub_entry, off);
10752       if (htab->plt_stub_align)
10753         size += plt_stub_pad (htab, stub_entry, off);
10754       if (info->emitrelocations)
10755         {
10756           stub_entry->stub_sec->reloc_count
10757             += ((PPC_HA (off) != 0)
10758                 + (htab->opd_abi
10759                    ? 2 + (htab->plt_static_chain
10760                           && PPC_HA (off + 16) == PPC_HA (off))
10761                    : 1));
10762           stub_entry->stub_sec->flags |= SEC_RELOC;
10763         }
10764     }
10765   else
10766     {
10767       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10768          variants.  */
10769       bfd_vma r2off = 0;
10770       bfd_vma local_off = 0;
10771
10772       off = (stub_entry->target_value
10773              + stub_entry->target_section->output_offset
10774              + stub_entry->target_section->output_section->vma);
10775       off -= (stub_entry->stub_sec->size
10776               + stub_entry->stub_sec->output_offset
10777               + stub_entry->stub_sec->output_section->vma);
10778
10779       /* Reset the stub type from the plt variant in case we now
10780          can reach with a shorter stub.  */
10781       if (stub_entry->stub_type >= ppc_stub_plt_branch)
10782         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10783
10784       size = 4;
10785       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10786         {
10787           r2off = get_r2off (info, stub_entry);
10788           if (r2off == 0 && htab->opd_abi)
10789             {
10790               htab->stub_error = TRUE;
10791               return FALSE;
10792             }
10793           size = 12;
10794           if (PPC_HA (r2off) != 0)
10795             size = 16;
10796           off -= size - 4;
10797         }
10798
10799       local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10800
10801       /* If the branch offset if too big, use a ppc_stub_plt_branch.
10802          Do the same for -R objects without function descriptors.  */
10803       if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
10804           || (stub_entry->stub_type == ppc_stub_long_branch_r2off
10805               && r2off == 0))
10806         {
10807           struct ppc_branch_hash_entry *br_entry;
10808
10809           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10810                                              stub_entry->root.string + 9,
10811                                              TRUE, FALSE);
10812           if (br_entry == NULL)
10813             {
10814               info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
10815                                       stub_entry->root.string);
10816               htab->stub_error = TRUE;
10817               return FALSE;
10818             }
10819
10820           if (br_entry->iter != htab->stub_iteration)
10821             {
10822               br_entry->iter = htab->stub_iteration;
10823               br_entry->offset = htab->brlt->size;
10824               htab->brlt->size += 8;
10825
10826               if (htab->relbrlt != NULL)
10827                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10828               else if (info->emitrelocations)
10829                 {
10830                   htab->brlt->reloc_count += 1;
10831                   htab->brlt->flags |= SEC_RELOC;
10832                 }
10833             }
10834
10835           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
10836           off = (br_entry->offset
10837                  + htab->brlt->output_offset
10838                  + htab->brlt->output_section->vma
10839                  - elf_gp (htab->brlt->output_section->owner)
10840                  - htab->stub_group[stub_entry->id_sec->id].toc_off);
10841
10842           if (info->emitrelocations)
10843             {
10844               stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10845               stub_entry->stub_sec->flags |= SEC_RELOC;
10846             }
10847
10848           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off
10849               || !htab->opd_abi)
10850             {
10851               size = 12;
10852               if (PPC_HA (off) != 0)
10853                 size = 16;
10854             }
10855           else
10856             {
10857               size = 20;
10858               if (PPC_HA (off) != 0)
10859                 size += 4;
10860
10861               if (PPC_HA (r2off) != 0)
10862                 size += 4;
10863             }
10864         }
10865       else if (info->emitrelocations)
10866         {
10867           stub_entry->stub_sec->reloc_count += 1;
10868           stub_entry->stub_sec->flags |= SEC_RELOC;
10869         }
10870     }
10871
10872   stub_entry->stub_sec->size += size;
10873   return TRUE;
10874 }
10875
10876 /* Set up various things so that we can make a list of input sections
10877    for each output section included in the link.  Returns -1 on error,
10878    0 when no stubs will be needed, and 1 on success.  */
10879
10880 int
10881 ppc64_elf_setup_section_lists
10882   (struct bfd_link_info *info,
10883    asection *(*add_stub_section) (const char *, asection *),
10884    void (*layout_sections_again) (void))
10885 {
10886   bfd *input_bfd;
10887   int top_id, top_index, id;
10888   asection *section;
10889   asection **input_list;
10890   bfd_size_type amt;
10891   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10892
10893   if (htab == NULL)
10894     return -1;
10895   /* Stash our params away.  */
10896   htab->add_stub_section = add_stub_section;
10897   htab->layout_sections_again = layout_sections_again;
10898
10899   /* Find the top input section id.  */
10900   for (input_bfd = info->input_bfds, top_id = 3;
10901        input_bfd != NULL;
10902        input_bfd = input_bfd->link_next)
10903     {
10904       for (section = input_bfd->sections;
10905            section != NULL;
10906            section = section->next)
10907         {
10908           if (top_id < section->id)
10909             top_id = section->id;
10910         }
10911     }
10912
10913   htab->top_id = top_id;
10914   amt = sizeof (struct map_stub) * (top_id + 1);
10915   htab->stub_group = bfd_zmalloc (amt);
10916   if (htab->stub_group == NULL)
10917     return -1;
10918
10919   /* Set toc_off for com, und, abs and ind sections.  */
10920   for (id = 0; id < 3; id++)
10921     htab->stub_group[id].toc_off = TOC_BASE_OFF;
10922
10923   /* We can't use output_bfd->section_count here to find the top output
10924      section index as some sections may have been removed, and
10925      strip_excluded_output_sections doesn't renumber the indices.  */
10926   for (section = info->output_bfd->sections, top_index = 0;
10927        section != NULL;
10928        section = section->next)
10929     {
10930       if (top_index < section->index)
10931         top_index = section->index;
10932     }
10933
10934   htab->top_index = top_index;
10935   amt = sizeof (asection *) * (top_index + 1);
10936   input_list = bfd_zmalloc (amt);
10937   htab->input_list = input_list;
10938   if (input_list == NULL)
10939     return -1;
10940
10941   return 1;
10942 }
10943
10944 /* Set up for first pass at multitoc partitioning.  */
10945
10946 void
10947 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10948 {
10949   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10950
10951   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
10952   htab->toc_bfd = NULL;
10953   htab->toc_first_sec = NULL;
10954 }
10955
10956 /* The linker repeatedly calls this function for each TOC input section
10957    and linker generated GOT section.  Group input bfds such that the toc
10958    within a group is less than 64k in size.  */
10959
10960 bfd_boolean
10961 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10962 {
10963   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10964   bfd_vma addr, off, limit;
10965
10966   if (htab == NULL)
10967     return FALSE;
10968
10969   if (!htab->second_toc_pass)
10970     {
10971       /* Keep track of the first .toc or .got section for this input bfd.  */
10972       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
10973
10974       if (new_bfd)
10975         {
10976           htab->toc_bfd = isec->owner;
10977           htab->toc_first_sec = isec;
10978         }
10979
10980       addr = isec->output_offset + isec->output_section->vma;
10981       off = addr - htab->toc_curr;
10982       limit = 0x80008000;
10983       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10984         limit = 0x10000;
10985       if (off + isec->size > limit)
10986         {
10987           addr = (htab->toc_first_sec->output_offset
10988                   + htab->toc_first_sec->output_section->vma);
10989           htab->toc_curr = addr;
10990         }
10991
10992       /* toc_curr is the base address of this toc group.  Set elf_gp
10993          for the input section to be the offset relative to the
10994          output toc base plus 0x8000.  Making the input elf_gp an
10995          offset allows us to move the toc as a whole without
10996          recalculating input elf_gp.  */
10997       off = htab->toc_curr - elf_gp (isec->output_section->owner);
10998       off += TOC_BASE_OFF;
10999
11000       /* Die if someone uses a linker script that doesn't keep input
11001          file .toc and .got together.  */
11002       if (new_bfd
11003           && elf_gp (isec->owner) != 0
11004           && elf_gp (isec->owner) != off)
11005         return FALSE;
11006
11007       elf_gp (isec->owner) = off;
11008       return TRUE;
11009     }
11010
11011   /* During the second pass toc_first_sec points to the start of
11012      a toc group, and toc_curr is used to track the old elf_gp.
11013      We use toc_bfd to ensure we only look at each bfd once.  */
11014   if (htab->toc_bfd == isec->owner)
11015     return TRUE;
11016   htab->toc_bfd = isec->owner;
11017
11018   if (htab->toc_first_sec == NULL
11019       || htab->toc_curr != elf_gp (isec->owner))
11020     {
11021       htab->toc_curr = elf_gp (isec->owner);
11022       htab->toc_first_sec = isec;
11023     }
11024   addr = (htab->toc_first_sec->output_offset
11025           + htab->toc_first_sec->output_section->vma);
11026   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11027   elf_gp (isec->owner) = off;
11028
11029   return TRUE;
11030 }
11031
11032 /* Called via elf_link_hash_traverse to merge GOT entries for global
11033    symbol H.  */
11034
11035 static bfd_boolean
11036 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11037 {
11038   if (h->root.type == bfd_link_hash_indirect)
11039     return TRUE;
11040
11041   merge_got_entries (&h->got.glist);
11042
11043   return TRUE;
11044 }
11045
11046 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11047    symbol H.  */
11048
11049 static bfd_boolean
11050 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11051 {
11052   struct got_entry *gent;
11053
11054   if (h->root.type == bfd_link_hash_indirect)
11055     return TRUE;
11056
11057   for (gent = h->got.glist; gent != NULL; gent = gent->next)
11058     if (!gent->is_indirect)
11059       allocate_got (h, (struct bfd_link_info *) inf, gent);
11060   return TRUE;
11061 }
11062
11063 /* Called on the first multitoc pass after the last call to
11064    ppc64_elf_next_toc_section.  This function removes duplicate GOT
11065    entries.  */
11066
11067 bfd_boolean
11068 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11069 {
11070   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11071   struct bfd *ibfd, *ibfd2;
11072   bfd_boolean done_something;
11073
11074   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11075
11076   if (!htab->do_multi_toc)
11077     return FALSE;
11078
11079   /* Merge global sym got entries within a toc group.  */
11080   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11081
11082   /* And tlsld_got.  */
11083   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11084     {
11085       struct got_entry *ent, *ent2;
11086
11087       if (!is_ppc64_elf (ibfd))
11088         continue;
11089
11090       ent = ppc64_tlsld_got (ibfd);
11091       if (!ent->is_indirect
11092           && ent->got.offset != (bfd_vma) -1)
11093         {
11094           for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
11095             {
11096               if (!is_ppc64_elf (ibfd2))
11097                 continue;
11098
11099               ent2 = ppc64_tlsld_got (ibfd2);
11100               if (!ent2->is_indirect
11101                   && ent2->got.offset != (bfd_vma) -1
11102                   && elf_gp (ibfd2) == elf_gp (ibfd))
11103                 {
11104                   ent2->is_indirect = TRUE;
11105                   ent2->got.ent = ent;
11106                 }
11107             }
11108         }
11109     }
11110
11111   /* Zap sizes of got sections.  */
11112   htab->reliplt->rawsize = htab->reliplt->size;
11113   htab->reliplt->size -= htab->got_reli_size;
11114   htab->got_reli_size = 0;
11115
11116   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11117     {
11118       asection *got, *relgot;
11119
11120       if (!is_ppc64_elf (ibfd))
11121         continue;
11122
11123       got = ppc64_elf_tdata (ibfd)->got;
11124       if (got != NULL)
11125         {
11126           got->rawsize = got->size;
11127           got->size = 0;
11128           relgot = ppc64_elf_tdata (ibfd)->relgot;
11129           relgot->rawsize = relgot->size;
11130           relgot->size = 0;
11131         }
11132     }
11133
11134   /* Now reallocate the got, local syms first.  We don't need to
11135      allocate section contents again since we never increase size.  */
11136   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11137     {
11138       struct got_entry **lgot_ents;
11139       struct got_entry **end_lgot_ents;
11140       struct plt_entry **local_plt;
11141       struct plt_entry **end_local_plt;
11142       unsigned char *lgot_masks;
11143       bfd_size_type locsymcount;
11144       Elf_Internal_Shdr *symtab_hdr;
11145       asection *s;
11146
11147       if (!is_ppc64_elf (ibfd))
11148         continue;
11149
11150       lgot_ents = elf_local_got_ents (ibfd);
11151       if (!lgot_ents)
11152         continue;
11153
11154       symtab_hdr = &elf_symtab_hdr (ibfd);
11155       locsymcount = symtab_hdr->sh_info;
11156       end_lgot_ents = lgot_ents + locsymcount;
11157       local_plt = (struct plt_entry **) end_lgot_ents;
11158       end_local_plt = local_plt + locsymcount;
11159       lgot_masks = (unsigned char *) end_local_plt;
11160       s = ppc64_elf_tdata (ibfd)->got;
11161       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11162         {
11163           struct got_entry *ent;
11164
11165           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11166             {
11167               unsigned int ent_size = 8;
11168               unsigned int rel_size = sizeof (Elf64_External_Rela);
11169
11170               ent->got.offset = s->size;
11171               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11172                 {
11173                   ent_size *= 2;
11174                   rel_size *= 2;
11175                 }
11176               s->size += ent_size;
11177               if ((*lgot_masks & PLT_IFUNC) != 0)
11178                 {
11179                   htab->reliplt->size += rel_size;
11180                   htab->got_reli_size += rel_size;
11181                 }
11182               else if (info->shared)
11183                 {
11184                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11185                   srel->size += rel_size;
11186                 }
11187             }
11188         }
11189     }
11190
11191   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11192
11193   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11194     {
11195       struct got_entry *ent;
11196
11197       if (!is_ppc64_elf (ibfd))
11198         continue;
11199
11200       ent = ppc64_tlsld_got (ibfd);
11201       if (!ent->is_indirect
11202           && ent->got.offset != (bfd_vma) -1)
11203         {
11204           asection *s = ppc64_elf_tdata (ibfd)->got;
11205           ent->got.offset = s->size;
11206           s->size += 16;
11207           if (info->shared)
11208             {
11209               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11210               srel->size += sizeof (Elf64_External_Rela);
11211             }
11212         }
11213     }
11214
11215   done_something = htab->reliplt->rawsize != htab->reliplt->size;
11216   if (!done_something)
11217     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11218       {
11219         asection *got;
11220
11221         if (!is_ppc64_elf (ibfd))
11222           continue;
11223
11224         got = ppc64_elf_tdata (ibfd)->got;
11225         if (got != NULL)
11226           {
11227             done_something = got->rawsize != got->size;
11228             if (done_something)
11229               break;
11230           }
11231       }
11232
11233   if (done_something)
11234     (*htab->layout_sections_again) ();
11235
11236   /* Set up for second pass over toc sections to recalculate elf_gp
11237      on input sections.  */
11238   htab->toc_bfd = NULL;
11239   htab->toc_first_sec = NULL;
11240   htab->second_toc_pass = TRUE;
11241   return done_something;
11242 }
11243
11244 /* Called after second pass of multitoc partitioning.  */
11245
11246 void
11247 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11248 {
11249   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11250
11251   /* After the second pass, toc_curr tracks the TOC offset used
11252      for code sections below in ppc64_elf_next_input_section.  */
11253   htab->toc_curr = TOC_BASE_OFF;
11254 }
11255
11256 /* No toc references were found in ISEC.  If the code in ISEC makes no
11257    calls, then there's no need to use toc adjusting stubs when branching
11258    into ISEC.  Actually, indirect calls from ISEC are OK as they will
11259    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
11260    needed, and 2 if a cyclical call-graph was found but no other reason
11261    for a stub was detected.  If called from the top level, a return of
11262    2 means the same as a return of 0.  */
11263
11264 static int
11265 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11266 {
11267   int ret;
11268
11269   /* Mark this section as checked.  */
11270   isec->call_check_done = 1;
11271
11272   /* We know none of our code bearing sections will need toc stubs.  */
11273   if ((isec->flags & SEC_LINKER_CREATED) != 0)
11274     return 0;
11275
11276   if (isec->size == 0)
11277     return 0;
11278
11279   if (isec->output_section == NULL)
11280     return 0;
11281
11282   ret = 0;
11283   if (isec->reloc_count != 0)
11284     {
11285       Elf_Internal_Rela *relstart, *rel;
11286       Elf_Internal_Sym *local_syms;
11287       struct ppc_link_hash_table *htab;
11288
11289       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11290                                             info->keep_memory);
11291       if (relstart == NULL)
11292         return -1;
11293
11294       /* Look for branches to outside of this section.  */
11295       local_syms = NULL;
11296       htab = ppc_hash_table (info);
11297       if (htab == NULL)
11298         return -1;
11299
11300       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11301         {
11302           enum elf_ppc64_reloc_type r_type;
11303           unsigned long r_symndx;
11304           struct elf_link_hash_entry *h;
11305           struct ppc_link_hash_entry *eh;
11306           Elf_Internal_Sym *sym;
11307           asection *sym_sec;
11308           struct _opd_sec_data *opd;
11309           bfd_vma sym_value;
11310           bfd_vma dest;
11311
11312           r_type = ELF64_R_TYPE (rel->r_info);
11313           if (r_type != R_PPC64_REL24
11314               && r_type != R_PPC64_REL14
11315               && r_type != R_PPC64_REL14_BRTAKEN
11316               && r_type != R_PPC64_REL14_BRNTAKEN)
11317             continue;
11318
11319           r_symndx = ELF64_R_SYM (rel->r_info);
11320           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11321                           isec->owner))
11322             {
11323               ret = -1;
11324               break;
11325             }
11326
11327           /* Calls to dynamic lib functions go through a plt call stub
11328              that uses r2.  */
11329           eh = (struct ppc_link_hash_entry *) h;
11330           if (eh != NULL
11331               && (eh->elf.plt.plist != NULL
11332                   || (eh->oh != NULL
11333                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11334             {
11335               ret = 1;
11336               break;
11337             }
11338
11339           if (sym_sec == NULL)
11340             /* Ignore other undefined symbols.  */
11341             continue;
11342
11343           /* Assume branches to other sections not included in the
11344              link need stubs too, to cover -R and absolute syms.  */
11345           if (sym_sec->output_section == NULL)
11346             {
11347               ret = 1;
11348               break;
11349             }
11350
11351           if (h == NULL)
11352             sym_value = sym->st_value;
11353           else
11354             {
11355               if (h->root.type != bfd_link_hash_defined
11356                   && h->root.type != bfd_link_hash_defweak)
11357                 abort ();
11358               sym_value = h->root.u.def.value;
11359             }
11360           sym_value += rel->r_addend;
11361
11362           /* If this branch reloc uses an opd sym, find the code section.  */
11363           opd = get_opd_info (sym_sec);
11364           if (opd != NULL)
11365             {
11366               if (h == NULL && opd->adjust != NULL)
11367                 {
11368                   long adjust;
11369
11370                   adjust = opd->adjust[sym->st_value / 8];
11371                   if (adjust == -1)
11372                     /* Assume deleted functions won't ever be called.  */
11373                     continue;
11374                   sym_value += adjust;
11375                 }
11376
11377               dest = opd_entry_value (sym_sec, sym_value,
11378                                       &sym_sec, NULL, FALSE);
11379               if (dest == (bfd_vma) -1)
11380                 continue;
11381             }
11382           else
11383             dest = (sym_value
11384                     + sym_sec->output_offset
11385                     + sym_sec->output_section->vma);
11386
11387           /* Ignore branch to self.  */
11388           if (sym_sec == isec)
11389             continue;
11390
11391           /* If the called function uses the toc, we need a stub.  */
11392           if (sym_sec->has_toc_reloc
11393               || sym_sec->makes_toc_func_call)
11394             {
11395               ret = 1;
11396               break;
11397             }
11398
11399           /* Assume any branch that needs a long branch stub might in fact
11400              need a plt_branch stub.  A plt_branch stub uses r2.  */
11401           else if (dest - (isec->output_offset
11402                            + isec->output_section->vma
11403                            + rel->r_offset) + (1 << 25)
11404                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11405                                                              ? h->other
11406                                                              : sym->st_other))
11407             {
11408               ret = 1;
11409               break;
11410             }
11411
11412           /* If calling back to a section in the process of being
11413              tested, we can't say for sure that no toc adjusting stubs
11414              are needed, so don't return zero.  */
11415           else if (sym_sec->call_check_in_progress)
11416             ret = 2;
11417
11418           /* Branches to another section that itself doesn't have any TOC
11419              references are OK.  Recursively call ourselves to check.  */
11420           else if (!sym_sec->call_check_done)
11421             {
11422               int recur;
11423
11424               /* Mark current section as indeterminate, so that other
11425                  sections that call back to current won't be marked as
11426                  known.  */
11427               isec->call_check_in_progress = 1;
11428               recur = toc_adjusting_stub_needed (info, sym_sec);
11429               isec->call_check_in_progress = 0;
11430
11431               if (recur != 0)
11432                 {
11433                   ret = recur;
11434                   if (recur != 2)
11435                     break;
11436                 }
11437             }
11438         }
11439
11440       if (local_syms != NULL
11441           && (elf_symtab_hdr (isec->owner).contents
11442               != (unsigned char *) local_syms))
11443         free (local_syms);
11444       if (elf_section_data (isec)->relocs != relstart)
11445         free (relstart);
11446     }
11447
11448   if ((ret & 1) == 0
11449       && isec->map_head.s != NULL
11450       && (strcmp (isec->output_section->name, ".init") == 0
11451           || strcmp (isec->output_section->name, ".fini") == 0))
11452     {
11453       if (isec->map_head.s->has_toc_reloc
11454           || isec->map_head.s->makes_toc_func_call)
11455         ret = 1;
11456       else if (!isec->map_head.s->call_check_done)
11457         {
11458           int recur;
11459           isec->call_check_in_progress = 1;
11460           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11461           isec->call_check_in_progress = 0;
11462           if (recur != 0)
11463             ret = recur;
11464         }
11465     }
11466
11467   if (ret == 1)
11468     isec->makes_toc_func_call = 1;
11469
11470   return ret;
11471 }
11472
11473 /* The linker repeatedly calls this function for each input section,
11474    in the order that input sections are linked into output sections.
11475    Build lists of input sections to determine groupings between which
11476    we may insert linker stubs.  */
11477
11478 bfd_boolean
11479 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11480 {
11481   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11482
11483   if (htab == NULL)
11484     return FALSE;
11485
11486   if ((isec->output_section->flags & SEC_CODE) != 0
11487       && isec->output_section->index <= htab->top_index)
11488     {
11489       asection **list = htab->input_list + isec->output_section->index;
11490       /* Steal the link_sec pointer for our list.  */
11491 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
11492       /* This happens to make the list in reverse order,
11493          which is what we want.  */
11494       PREV_SEC (isec) = *list;
11495       *list = isec;
11496     }
11497
11498   if (htab->multi_toc_needed)
11499     {
11500       /* If a code section has a function that uses the TOC then we need
11501          to use the right TOC (obviously).  Also, make sure that .opd gets
11502          the correct TOC value for R_PPC64_TOC relocs that don't have or
11503          can't find their function symbol (shouldn't ever happen now).
11504          Also specially treat .fixup for the linux kernel.  .fixup
11505          contains branches, but only back to the function that hit an
11506          exception.  */
11507       if (isec->has_toc_reloc
11508           || (isec->flags & SEC_CODE) == 0
11509           || strcmp (isec->name, ".fixup") == 0)
11510         {
11511           if (elf_gp (isec->owner) != 0)
11512             htab->toc_curr = elf_gp (isec->owner);
11513         }
11514       else
11515         {
11516           if (!isec->call_check_done
11517               && toc_adjusting_stub_needed (info, isec) < 0)
11518             return FALSE;
11519           /* If we make a local call from this section, ie. a branch
11520              without a following nop, then we have no place to put a
11521              toc restoring insn.  We must use the same toc group as
11522              the callee.
11523              Testing makes_toc_func_call actually tests for *any*
11524              calls to functions that need a good toc pointer.  A more
11525              precise test would be better, as this one will set
11526              incorrect values for pasted .init/.fini fragments.
11527              (Fixed later in check_pasted_section.)  */
11528           if (isec->makes_toc_func_call
11529               && elf_gp (isec->owner) != 0)
11530             htab->toc_curr = elf_gp (isec->owner);
11531         }
11532     }
11533
11534   /* Functions that don't use the TOC can belong in any TOC group.
11535      Use the last TOC base.  */
11536   htab->stub_group[isec->id].toc_off = htab->toc_curr;
11537   return TRUE;
11538 }
11539
11540 /* Check that all .init and .fini sections use the same toc, if they
11541    have toc relocs.  */
11542
11543 static bfd_boolean
11544 check_pasted_section (struct bfd_link_info *info, const char *name)
11545 {
11546   asection *o = bfd_get_section_by_name (info->output_bfd, name);
11547
11548   if (o != NULL)
11549     {
11550       struct ppc_link_hash_table *htab = ppc_hash_table (info);
11551       bfd_vma toc_off = 0;
11552       asection *i;
11553
11554       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11555         if (i->has_toc_reloc)
11556           {
11557             if (toc_off == 0)
11558               toc_off = htab->stub_group[i->id].toc_off;
11559             else if (toc_off != htab->stub_group[i->id].toc_off)
11560               return FALSE;
11561           }
11562
11563       if (toc_off == 0)
11564         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11565           if (i->makes_toc_func_call)
11566             {
11567               toc_off = htab->stub_group[i->id].toc_off;
11568               break;
11569             }
11570
11571       /* Make sure the whole pasted function uses the same toc offset.  */
11572       if (toc_off != 0)
11573         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11574           htab->stub_group[i->id].toc_off = toc_off;
11575     }
11576   return TRUE;
11577 }
11578
11579 bfd_boolean
11580 ppc64_elf_check_init_fini (struct bfd_link_info *info)
11581 {
11582   return (check_pasted_section (info, ".init")
11583           & check_pasted_section (info, ".fini"));
11584 }
11585
11586 /* See whether we can group stub sections together.  Grouping stub
11587    sections may result in fewer stubs.  More importantly, we need to
11588    put all .init* and .fini* stubs at the beginning of the .init or
11589    .fini output sections respectively, because glibc splits the
11590    _init and _fini functions into multiple parts.  Putting a stub in
11591    the middle of a function is not a good idea.  */
11592
11593 static void
11594 group_sections (struct ppc_link_hash_table *htab,
11595                 bfd_size_type stub_group_size,
11596                 bfd_boolean stubs_always_before_branch)
11597 {
11598   asection **list;
11599   bfd_size_type stub14_group_size;
11600   bfd_boolean suppress_size_errors;
11601
11602   suppress_size_errors = FALSE;
11603   stub14_group_size = stub_group_size;
11604   if (stub_group_size == 1)
11605     {
11606       /* Default values.  */
11607       if (stubs_always_before_branch)
11608         {
11609           stub_group_size = 0x1e00000;
11610           stub14_group_size = 0x7800;
11611         }
11612       else
11613         {
11614           stub_group_size = 0x1c00000;
11615           stub14_group_size = 0x7000;
11616         }
11617       suppress_size_errors = TRUE;
11618     }
11619
11620   list = htab->input_list + htab->top_index;
11621   do
11622     {
11623       asection *tail = *list;
11624       while (tail != NULL)
11625         {
11626           asection *curr;
11627           asection *prev;
11628           bfd_size_type total;
11629           bfd_boolean big_sec;
11630           bfd_vma curr_toc;
11631
11632           curr = tail;
11633           total = tail->size;
11634           big_sec = total > (ppc64_elf_section_data (tail) != NULL
11635                              && ppc64_elf_section_data (tail)->has_14bit_branch
11636                              ? stub14_group_size : stub_group_size);
11637           if (big_sec && !suppress_size_errors)
11638             (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
11639                                      tail->owner, tail);
11640           curr_toc = htab->stub_group[tail->id].toc_off;
11641
11642           while ((prev = PREV_SEC (curr)) != NULL
11643                  && ((total += curr->output_offset - prev->output_offset)
11644                      < (ppc64_elf_section_data (prev) != NULL
11645                         && ppc64_elf_section_data (prev)->has_14bit_branch
11646                         ? stub14_group_size : stub_group_size))
11647                  && htab->stub_group[prev->id].toc_off == curr_toc)
11648             curr = prev;
11649
11650           /* OK, the size from the start of CURR to the end is less
11651              than stub_group_size and thus can be handled by one stub
11652              section.  (or the tail section is itself larger than
11653              stub_group_size, in which case we may be toast.)  We
11654              should really be keeping track of the total size of stubs
11655              added here, as stubs contribute to the final output
11656              section size.  That's a little tricky, and this way will
11657              only break if stubs added make the total size more than
11658              2^25, ie. for the default stub_group_size, if stubs total
11659              more than 2097152 bytes, or nearly 75000 plt call stubs.  */
11660           do
11661             {
11662               prev = PREV_SEC (tail);
11663               /* Set up this stub group.  */
11664               htab->stub_group[tail->id].link_sec = curr;
11665             }
11666           while (tail != curr && (tail = prev) != NULL);
11667
11668           /* But wait, there's more!  Input sections up to stub_group_size
11669              bytes before the stub section can be handled by it too.
11670              Don't do this if we have a really large section after the
11671              stubs, as adding more stubs increases the chance that
11672              branches may not reach into the stub section.  */
11673           if (!stubs_always_before_branch && !big_sec)
11674             {
11675               total = 0;
11676               while (prev != NULL
11677                      && ((total += tail->output_offset - prev->output_offset)
11678                          < (ppc64_elf_section_data (prev) != NULL
11679                             && ppc64_elf_section_data (prev)->has_14bit_branch
11680                             ? stub14_group_size : stub_group_size))
11681                      && htab->stub_group[prev->id].toc_off == curr_toc)
11682                 {
11683                   tail = prev;
11684                   prev = PREV_SEC (tail);
11685                   htab->stub_group[tail->id].link_sec = curr;
11686                 }
11687             }
11688           tail = prev;
11689         }
11690     }
11691   while (list-- != htab->input_list);
11692   free (htab->input_list);
11693 #undef PREV_SEC
11694 }
11695
11696 static const unsigned char glink_eh_frame_cie[] =
11697 {
11698   0, 0, 0, 16,                          /* length.  */
11699   0, 0, 0, 0,                           /* id.  */
11700   1,                                    /* CIE version.  */
11701   'z', 'R', 0,                          /* Augmentation string.  */
11702   4,                                    /* Code alignment.  */
11703   0x78,                                 /* Data alignment.  */
11704   65,                                   /* RA reg.  */
11705   1,                                    /* Augmentation size.  */
11706   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
11707   DW_CFA_def_cfa, 1, 0                  /* def_cfa: r1 offset 0.  */
11708 };
11709
11710 /* Stripping output sections is normally done before dynamic section
11711    symbols have been allocated.  This function is called later, and
11712    handles cases like htab->brlt which is mapped to its own output
11713    section.  */
11714
11715 static void
11716 maybe_strip_output (struct bfd_link_info *info, asection *isec)
11717 {
11718   if (isec->size == 0
11719       && isec->output_section->size == 0
11720       && !(isec->output_section->flags & SEC_KEEP)
11721       && !bfd_section_removed_from_list (info->output_bfd,
11722                                          isec->output_section)
11723       && elf_section_data (isec->output_section)->dynindx == 0)
11724     {
11725       isec->output_section->flags |= SEC_EXCLUDE;
11726       bfd_section_list_remove (info->output_bfd, isec->output_section);
11727       info->output_bfd->section_count--;
11728     }
11729 }
11730
11731 /* Determine and set the size of the stub section for a final link.
11732
11733    The basic idea here is to examine all the relocations looking for
11734    PC-relative calls to a target that is unreachable with a "bl"
11735    instruction.  */
11736
11737 bfd_boolean
11738 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
11739                       bfd_boolean plt_static_chain, int plt_thread_safe,
11740                       int plt_stub_align)
11741 {
11742   bfd_size_type stub_group_size;
11743   bfd_boolean stubs_always_before_branch;
11744   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11745
11746   if (htab == NULL)
11747     return FALSE;
11748
11749   htab->plt_static_chain = plt_static_chain;
11750   htab->plt_stub_align = plt_stub_align;
11751   if (plt_thread_safe == -1 && !info->executable)
11752     plt_thread_safe = 1;
11753   if (!htab->opd_abi)
11754     plt_thread_safe = 0;
11755   else if (plt_thread_safe == -1)
11756     {
11757       static const char *const thread_starter[] =
11758         {
11759           "pthread_create",
11760           /* libstdc++ */
11761           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11762           /* librt */
11763           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11764           "mq_notify", "create_timer",
11765           /* libanl */
11766           "getaddrinfo_a",
11767           /* libgomp */
11768           "GOMP_parallel_start",
11769           "GOMP_parallel_loop_static_start",
11770           "GOMP_parallel_loop_dynamic_start",
11771           "GOMP_parallel_loop_guided_start",
11772           "GOMP_parallel_loop_runtime_start",
11773           "GOMP_parallel_sections_start",
11774         };
11775       unsigned i;
11776
11777       for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
11778         {
11779           struct elf_link_hash_entry *h;
11780           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11781                                     FALSE, FALSE, TRUE);
11782           plt_thread_safe = h != NULL && h->ref_regular;
11783           if (plt_thread_safe)
11784             break;
11785         }
11786     }
11787   htab->plt_thread_safe = plt_thread_safe;
11788   stubs_always_before_branch = group_size < 0;
11789   if (group_size < 0)
11790     stub_group_size = -group_size;
11791   else
11792     stub_group_size = group_size;
11793
11794   group_sections (htab, stub_group_size, stubs_always_before_branch);
11795
11796   while (1)
11797     {
11798       bfd *input_bfd;
11799       unsigned int bfd_indx;
11800       asection *stub_sec;
11801
11802       htab->stub_iteration += 1;
11803
11804       for (input_bfd = info->input_bfds, bfd_indx = 0;
11805            input_bfd != NULL;
11806            input_bfd = input_bfd->link_next, bfd_indx++)
11807         {
11808           Elf_Internal_Shdr *symtab_hdr;
11809           asection *section;
11810           Elf_Internal_Sym *local_syms = NULL;
11811
11812           if (!is_ppc64_elf (input_bfd))
11813             continue;
11814
11815           /* We'll need the symbol table in a second.  */
11816           symtab_hdr = &elf_symtab_hdr (input_bfd);
11817           if (symtab_hdr->sh_info == 0)
11818             continue;
11819
11820           /* Walk over each section attached to the input bfd.  */
11821           for (section = input_bfd->sections;
11822                section != NULL;
11823                section = section->next)
11824             {
11825               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
11826
11827               /* If there aren't any relocs, then there's nothing more
11828                  to do.  */
11829               if ((section->flags & SEC_RELOC) == 0
11830                   || (section->flags & SEC_ALLOC) == 0
11831                   || (section->flags & SEC_LOAD) == 0
11832                   || (section->flags & SEC_CODE) == 0
11833                   || section->reloc_count == 0)
11834                 continue;
11835
11836               /* If this section is a link-once section that will be
11837                  discarded, then don't create any stubs.  */
11838               if (section->output_section == NULL
11839                   || section->output_section->owner != info->output_bfd)
11840                 continue;
11841
11842               /* Get the relocs.  */
11843               internal_relocs
11844                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
11845                                              info->keep_memory);
11846               if (internal_relocs == NULL)
11847                 goto error_ret_free_local;
11848
11849               /* Now examine each relocation.  */
11850               irela = internal_relocs;
11851               irelaend = irela + section->reloc_count;
11852               for (; irela < irelaend; irela++)
11853                 {
11854                   enum elf_ppc64_reloc_type r_type;
11855                   unsigned int r_indx;
11856                   enum ppc_stub_type stub_type;
11857                   struct ppc_stub_hash_entry *stub_entry;
11858                   asection *sym_sec, *code_sec;
11859                   bfd_vma sym_value, code_value;
11860                   bfd_vma destination;
11861                   unsigned long local_off;
11862                   bfd_boolean ok_dest;
11863                   struct ppc_link_hash_entry *hash;
11864                   struct ppc_link_hash_entry *fdh;
11865                   struct elf_link_hash_entry *h;
11866                   Elf_Internal_Sym *sym;
11867                   char *stub_name;
11868                   const asection *id_sec;
11869                   struct _opd_sec_data *opd;
11870                   struct plt_entry *plt_ent;
11871
11872                   r_type = ELF64_R_TYPE (irela->r_info);
11873                   r_indx = ELF64_R_SYM (irela->r_info);
11874
11875                   if (r_type >= R_PPC64_max)
11876                     {
11877                       bfd_set_error (bfd_error_bad_value);
11878                       goto error_ret_free_internal;
11879                     }
11880
11881                   /* Only look for stubs on branch instructions.  */
11882                   if (r_type != R_PPC64_REL24
11883                       && r_type != R_PPC64_REL14
11884                       && r_type != R_PPC64_REL14_BRTAKEN
11885                       && r_type != R_PPC64_REL14_BRNTAKEN)
11886                     continue;
11887
11888                   /* Now determine the call target, its name, value,
11889                      section.  */
11890                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11891                                   r_indx, input_bfd))
11892                     goto error_ret_free_internal;
11893                   hash = (struct ppc_link_hash_entry *) h;
11894
11895                   ok_dest = FALSE;
11896                   fdh = NULL;
11897                   sym_value = 0;
11898                   if (hash == NULL)
11899                     {
11900                       sym_value = sym->st_value;
11901                       ok_dest = TRUE;
11902                     }
11903                   else if (hash->elf.root.type == bfd_link_hash_defined
11904                            || hash->elf.root.type == bfd_link_hash_defweak)
11905                     {
11906                       sym_value = hash->elf.root.u.def.value;
11907                       if (sym_sec->output_section != NULL)
11908                         ok_dest = TRUE;
11909                     }
11910                   else if (hash->elf.root.type == bfd_link_hash_undefweak
11911                            || hash->elf.root.type == bfd_link_hash_undefined)
11912                     {
11913                       /* Recognise an old ABI func code entry sym, and
11914                          use the func descriptor sym instead if it is
11915                          defined.  */
11916                       if (hash->elf.root.root.string[0] == '.'
11917                           && (fdh = lookup_fdh (hash, htab)) != NULL)
11918                         {
11919                           if (fdh->elf.root.type == bfd_link_hash_defined
11920                               || fdh->elf.root.type == bfd_link_hash_defweak)
11921                             {
11922                               sym_sec = fdh->elf.root.u.def.section;
11923                               sym_value = fdh->elf.root.u.def.value;
11924                               if (sym_sec->output_section != NULL)
11925                                 ok_dest = TRUE;
11926                             }
11927                           else
11928                             fdh = NULL;
11929                         }
11930                     }
11931                   else
11932                     {
11933                       bfd_set_error (bfd_error_bad_value);
11934                       goto error_ret_free_internal;
11935                     }
11936
11937                   destination = 0;
11938                   local_off = 0;
11939                   if (ok_dest)
11940                     {
11941                       sym_value += irela->r_addend;
11942                       destination = (sym_value
11943                                      + sym_sec->output_offset
11944                                      + sym_sec->output_section->vma);
11945                       local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
11946                                                             ? hash->elf.other
11947                                                             : sym->st_other);
11948                     }
11949
11950                   code_sec = sym_sec;
11951                   code_value = sym_value;
11952                   opd = get_opd_info (sym_sec);
11953                   if (opd != NULL)
11954                     {
11955                       bfd_vma dest;
11956
11957                       if (hash == NULL && opd->adjust != NULL)
11958                         {
11959                           long adjust = opd->adjust[sym_value / 8];
11960                           if (adjust == -1)
11961                             continue;
11962                           code_value += adjust;
11963                           sym_value += adjust;
11964                         }
11965                       dest = opd_entry_value (sym_sec, sym_value,
11966                                               &code_sec, &code_value, FALSE);
11967                       if (dest != (bfd_vma) -1)
11968                         {
11969                           destination = dest;
11970                           if (fdh != NULL)
11971                             {
11972                               /* Fixup old ABI sym to point at code
11973                                  entry.  */
11974                               hash->elf.root.type = bfd_link_hash_defweak;
11975                               hash->elf.root.u.def.section = code_sec;
11976                               hash->elf.root.u.def.value = code_value;
11977                             }
11978                         }
11979                     }
11980
11981                   /* Determine what (if any) linker stub is needed.  */
11982                   plt_ent = NULL;
11983                   stub_type = ppc_type_of_stub (section, irela, &hash,
11984                                                 &plt_ent, destination,
11985                                                 local_off);
11986
11987                   if (stub_type != ppc_stub_plt_call)
11988                     {
11989                       /* Check whether we need a TOC adjusting stub.
11990                          Since the linker pastes together pieces from
11991                          different object files when creating the
11992                          _init and _fini functions, it may be that a
11993                          call to what looks like a local sym is in
11994                          fact a call needing a TOC adjustment.  */
11995                       if (code_sec != NULL
11996                           && code_sec->output_section != NULL
11997                           && (htab->stub_group[code_sec->id].toc_off
11998                               != htab->stub_group[section->id].toc_off)
11999                           && (code_sec->has_toc_reloc
12000                               || code_sec->makes_toc_func_call))
12001                         stub_type = ppc_stub_long_branch_r2off;
12002                     }
12003
12004                   if (stub_type == ppc_stub_none)
12005                     continue;
12006
12007                   /* __tls_get_addr calls might be eliminated.  */
12008                   if (stub_type != ppc_stub_plt_call
12009                       && hash != NULL
12010                       && (hash == htab->tls_get_addr
12011                           || hash == htab->tls_get_addr_fd)
12012                       && section->has_tls_reloc
12013                       && irela != internal_relocs)
12014                     {
12015                       /* Get tls info.  */
12016                       unsigned char *tls_mask;
12017
12018                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12019                                          irela - 1, input_bfd))
12020                         goto error_ret_free_internal;
12021                       if (*tls_mask != 0)
12022                         continue;
12023                     }
12024
12025                   if (stub_type == ppc_stub_plt_call
12026                       && irela + 1 < irelaend
12027                       && irela[1].r_offset == irela->r_offset + 4
12028                       && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12029                     {
12030                       if (!tocsave_find (htab, INSERT,
12031                                          &local_syms, irela + 1, input_bfd))
12032                         goto error_ret_free_internal;
12033                     }
12034                   else if (stub_type == ppc_stub_plt_call)
12035                     stub_type = ppc_stub_plt_call_r2save;
12036
12037                   /* Support for grouping stub sections.  */
12038                   id_sec = htab->stub_group[section->id].link_sec;
12039
12040                   /* Get the name of this stub.  */
12041                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12042                   if (!stub_name)
12043                     goto error_ret_free_internal;
12044
12045                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12046                                                      stub_name, FALSE, FALSE);
12047                   if (stub_entry != NULL)
12048                     {
12049                       /* The proper stub has already been created.  */
12050                       free (stub_name);
12051                       if (stub_type == ppc_stub_plt_call_r2save)
12052                         stub_entry->stub_type = stub_type;
12053                       continue;
12054                     }
12055
12056                   stub_entry = ppc_add_stub (stub_name, section, info);
12057                   if (stub_entry == NULL)
12058                     {
12059                       free (stub_name);
12060                     error_ret_free_internal:
12061                       if (elf_section_data (section)->relocs == NULL)
12062                         free (internal_relocs);
12063                     error_ret_free_local:
12064                       if (local_syms != NULL
12065                           && (symtab_hdr->contents
12066                               != (unsigned char *) local_syms))
12067                         free (local_syms);
12068                       return FALSE;
12069                     }
12070
12071                   stub_entry->stub_type = stub_type;
12072                   if (stub_type != ppc_stub_plt_call
12073                       && stub_type != ppc_stub_plt_call_r2save)
12074                     {
12075                       stub_entry->target_value = code_value;
12076                       stub_entry->target_section = code_sec;
12077                     }
12078                   else
12079                     {
12080                       stub_entry->target_value = sym_value;
12081                       stub_entry->target_section = sym_sec;
12082                     }
12083                   stub_entry->h = hash;
12084                   stub_entry->plt_ent = plt_ent;
12085                   stub_entry->other = hash ? hash->elf.other : sym->st_other;
12086
12087                   if (stub_entry->h != NULL)
12088                     htab->stub_globals += 1;
12089                 }
12090
12091               /* We're done with the internal relocs, free them.  */
12092               if (elf_section_data (section)->relocs != internal_relocs)
12093                 free (internal_relocs);
12094             }
12095
12096           if (local_syms != NULL
12097               && symtab_hdr->contents != (unsigned char *) local_syms)
12098             {
12099               if (!info->keep_memory)
12100                 free (local_syms);
12101               else
12102                 symtab_hdr->contents = (unsigned char *) local_syms;
12103             }
12104         }
12105
12106       /* We may have added some stubs.  Find out the new size of the
12107          stub sections.  */
12108       for (stub_sec = htab->stub_bfd->sections;
12109            stub_sec != NULL;
12110            stub_sec = stub_sec->next)
12111         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12112           {
12113             stub_sec->rawsize = stub_sec->size;
12114             stub_sec->size = 0;
12115             stub_sec->reloc_count = 0;
12116             stub_sec->flags &= ~SEC_RELOC;
12117           }
12118
12119       htab->brlt->size = 0;
12120       htab->brlt->reloc_count = 0;
12121       htab->brlt->flags &= ~SEC_RELOC;
12122       if (htab->relbrlt != NULL)
12123         htab->relbrlt->size = 0;
12124
12125       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12126
12127       if (info->emitrelocations
12128           && htab->glink != NULL && htab->glink->size != 0)
12129         {
12130           htab->glink->reloc_count = 1;
12131           htab->glink->flags |= SEC_RELOC;
12132         }
12133
12134       if (htab->glink_eh_frame != NULL
12135           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12136           && htab->glink_eh_frame->output_section->size != 0)
12137         {
12138           size_t size = 0, align;
12139
12140           for (stub_sec = htab->stub_bfd->sections;
12141                stub_sec != NULL;
12142                stub_sec = stub_sec->next)
12143             if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12144               size += 20;
12145           if (htab->glink != NULL && htab->glink->size != 0)
12146             size += 24;
12147           if (size != 0)
12148             size += sizeof (glink_eh_frame_cie);
12149           align = 1;
12150           align <<= htab->glink_eh_frame->output_section->alignment_power;
12151           align -= 1;
12152           size = (size + align) & ~align;
12153           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12154           htab->glink_eh_frame->size = size;
12155         }
12156
12157       if (htab->plt_stub_align != 0)
12158         for (stub_sec = htab->stub_bfd->sections;
12159              stub_sec != NULL;
12160              stub_sec = stub_sec->next)
12161           if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12162             stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
12163                               & (-1 << htab->plt_stub_align));
12164
12165       for (stub_sec = htab->stub_bfd->sections;
12166            stub_sec != NULL;
12167            stub_sec = stub_sec->next)
12168         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12169             && stub_sec->rawsize != stub_sec->size)
12170           break;
12171
12172       /* Exit from this loop when no stubs have been added, and no stubs
12173          have changed size.  */
12174       if (stub_sec == NULL
12175           && (htab->glink_eh_frame == NULL
12176               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12177         break;
12178
12179       /* Ask the linker to do its stuff.  */
12180       (*htab->layout_sections_again) ();
12181     }
12182
12183   maybe_strip_output (info, htab->brlt);
12184   if (htab->glink_eh_frame != NULL)
12185     maybe_strip_output (info, htab->glink_eh_frame);
12186
12187   return TRUE;
12188 }
12189
12190 /* Called after we have determined section placement.  If sections
12191    move, we'll be called again.  Provide a value for TOCstart.  */
12192
12193 bfd_vma
12194 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12195 {
12196   asection *s;
12197   bfd_vma TOCstart;
12198
12199   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12200      order.  The TOC starts where the first of these sections starts.  */
12201   s = bfd_get_section_by_name (obfd, ".got");
12202   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12203     s = bfd_get_section_by_name (obfd, ".toc");
12204   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12205     s = bfd_get_section_by_name (obfd, ".tocbss");
12206   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12207     s = bfd_get_section_by_name (obfd, ".plt");
12208   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12209     {
12210       /* This may happen for
12211          o  references to TOC base (SYM@toc / TOC[tc0]) without a
12212          .toc directive
12213          o  bad linker script
12214          o --gc-sections and empty TOC sections
12215
12216          FIXME: Warn user?  */
12217
12218       /* Look for a likely section.  We probably won't even be
12219          using TOCstart.  */
12220       for (s = obfd->sections; s != NULL; s = s->next)
12221         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12222                          | SEC_EXCLUDE))
12223             == (SEC_ALLOC | SEC_SMALL_DATA))
12224           break;
12225       if (s == NULL)
12226         for (s = obfd->sections; s != NULL; s = s->next)
12227           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12228               == (SEC_ALLOC | SEC_SMALL_DATA))
12229             break;
12230       if (s == NULL)
12231         for (s = obfd->sections; s != NULL; s = s->next)
12232           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12233               == SEC_ALLOC)
12234             break;
12235       if (s == NULL)
12236         for (s = obfd->sections; s != NULL; s = s->next)
12237           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12238             break;
12239     }
12240
12241   TOCstart = 0;
12242   if (s != NULL)
12243     TOCstart = s->output_section->vma + s->output_offset;
12244
12245   _bfd_set_gp_value (obfd, TOCstart);
12246
12247   if (info != NULL && s != NULL && is_ppc64_elf (obfd))
12248     {
12249       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12250
12251       if (htab != NULL
12252           && htab->elf.hgot != NULL)
12253         {
12254           htab->elf.hgot->type = STT_OBJECT;
12255           htab->elf.hgot->root.type = bfd_link_hash_defined;
12256           htab->elf.hgot->root.u.def.value = TOC_BASE_OFF;
12257           htab->elf.hgot->root.u.def.section = s;
12258         }
12259     }
12260   return TOCstart;
12261 }
12262
12263 /* Build all the stubs associated with the current output file.
12264    The stubs are kept in a hash table attached to the main linker
12265    hash table.  This function is called via gldelf64ppc_finish.  */
12266
12267 bfd_boolean
12268 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
12269                        struct bfd_link_info *info,
12270                        char **stats)
12271 {
12272   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12273   asection *stub_sec;
12274   bfd_byte *p;
12275   int stub_sec_count = 0;
12276
12277   if (htab == NULL)
12278     return FALSE;
12279
12280   htab->emit_stub_syms = emit_stub_syms;
12281
12282   /* Allocate memory to hold the linker stubs.  */
12283   for (stub_sec = htab->stub_bfd->sections;
12284        stub_sec != NULL;
12285        stub_sec = stub_sec->next)
12286     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12287         && stub_sec->size != 0)
12288       {
12289         stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
12290         if (stub_sec->contents == NULL)
12291           return FALSE;
12292         /* We want to check that built size is the same as calculated
12293            size.  rawsize is a convenient location to use.  */
12294         stub_sec->rawsize = stub_sec->size;
12295         stub_sec->size = 0;
12296       }
12297
12298   if (htab->glink != NULL && htab->glink->size != 0)
12299     {
12300       unsigned int indx;
12301       bfd_vma plt0;
12302
12303       /* Build the .glink plt call stub.  */
12304       if (htab->emit_stub_syms)
12305         {
12306           struct elf_link_hash_entry *h;
12307           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12308                                     TRUE, FALSE, FALSE);
12309           if (h == NULL)
12310             return FALSE;
12311           if (h->root.type == bfd_link_hash_new)
12312             {
12313               h->root.type = bfd_link_hash_defined;
12314               h->root.u.def.section = htab->glink;
12315               h->root.u.def.value = 8;
12316               h->ref_regular = 1;
12317               h->def_regular = 1;
12318               h->ref_regular_nonweak = 1;
12319               h->forced_local = 1;
12320               h->non_elf = 0;
12321             }
12322         }
12323       plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
12324       if (info->emitrelocations)
12325         {
12326           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12327           if (r == NULL)
12328             return FALSE;
12329           r->r_offset = (htab->glink->output_offset
12330                          + htab->glink->output_section->vma);
12331           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12332           r->r_addend = plt0;
12333         }
12334       p = htab->glink->contents;
12335       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
12336       bfd_put_64 (htab->glink->owner, plt0, p);
12337       p += 8;
12338       if (htab->opd_abi)
12339         {
12340           bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12341           p += 4;
12342           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12343           p += 4;
12344           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12345           p += 4;
12346           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12347           p += 4;
12348           bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12349           p += 4;
12350           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12351           p += 4;
12352           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12353           p += 4;
12354           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12355           p += 4;
12356           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12357           p += 4;
12358           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12359           p += 4;
12360         }
12361       else
12362         {
12363           bfd_put_32 (htab->glink->owner, MFLR_R0, p);
12364           p += 4;
12365           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12366           p += 4;
12367           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12368           p += 4;
12369           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12370           p += 4;
12371           bfd_put_32 (htab->glink->owner, MTLR_R0, p);
12372           p += 4;
12373           bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
12374           p += 4;
12375           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12376           p += 4;
12377           bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
12378           p += 4;
12379           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12380           p += 4;
12381           bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
12382           p += 4;
12383           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12384           p += 4;
12385           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
12386           p += 4;
12387         }
12388       bfd_put_32 (htab->glink->owner, BCTR, p);
12389       p += 4;
12390       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
12391         {
12392           bfd_put_32 (htab->glink->owner, NOP, p);
12393           p += 4;
12394         }
12395
12396       /* Build the .glink lazy link call stubs.  */
12397       indx = 0;
12398       while (p < htab->glink->contents + htab->glink->size)
12399         {
12400           if (htab->opd_abi)
12401             {
12402               if (indx < 0x8000)
12403                 {
12404                   bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
12405                   p += 4;
12406                 }
12407               else
12408                 {
12409                   bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
12410                   p += 4;
12411                   bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
12412                               p);
12413                   p += 4;
12414                 }
12415             }
12416           bfd_put_32 (htab->glink->owner,
12417                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
12418           indx++;
12419           p += 4;
12420         }
12421       htab->glink->rawsize = p - htab->glink->contents;
12422     }
12423
12424   if (htab->brlt->size != 0)
12425     {
12426       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
12427                                          htab->brlt->size);
12428       if (htab->brlt->contents == NULL)
12429         return FALSE;
12430     }
12431   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
12432     {
12433       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
12434                                             htab->relbrlt->size);
12435       if (htab->relbrlt->contents == NULL)
12436         return FALSE;
12437     }
12438
12439   if (htab->glink_eh_frame != NULL
12440       && htab->glink_eh_frame->size != 0)
12441     {
12442       bfd_vma val;
12443       bfd_byte *last_fde;
12444       size_t last_fde_len, size, align, pad;
12445
12446       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12447       if (p == NULL)
12448         return FALSE;
12449       htab->glink_eh_frame->contents = p;
12450       last_fde = p;
12451
12452       htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12453
12454       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12455       /* CIE length (rewrite in case little-endian).  */
12456       last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12457       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12458       p += sizeof (glink_eh_frame_cie);
12459
12460       for (stub_sec = htab->stub_bfd->sections;
12461            stub_sec != NULL;
12462            stub_sec = stub_sec->next)
12463         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12464           {
12465             last_fde = p;
12466             last_fde_len = 16;
12467             /* FDE length.  */
12468             bfd_put_32 (htab->elf.dynobj, 16, p);
12469             p += 4;
12470             /* CIE pointer.  */
12471             val = p - htab->glink_eh_frame->contents;
12472             bfd_put_32 (htab->elf.dynobj, val, p);
12473             p += 4;
12474             /* Offset to stub section.  */
12475             val = (stub_sec->output_section->vma
12476                    + stub_sec->output_offset);
12477             val -= (htab->glink_eh_frame->output_section->vma
12478                     + htab->glink_eh_frame->output_offset);
12479             val -= p - htab->glink_eh_frame->contents;
12480             if (val + 0x80000000 > 0xffffffff)
12481               {
12482                 info->callbacks->einfo
12483                   (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
12484                    stub_sec->name);
12485                 return FALSE;
12486               }
12487             bfd_put_32 (htab->elf.dynobj, val, p);
12488             p += 4;
12489             /* stub section size.  */
12490             bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
12491             p += 4;
12492             /* Augmentation.  */
12493             p += 1;
12494             /* Pad.  */
12495             p += 3;
12496           }
12497       if (htab->glink != NULL && htab->glink->size != 0)
12498         {
12499           last_fde = p;
12500           last_fde_len = 20;
12501           /* FDE length.  */
12502           bfd_put_32 (htab->elf.dynobj, 20, p);
12503           p += 4;
12504           /* CIE pointer.  */
12505           val = p - htab->glink_eh_frame->contents;
12506           bfd_put_32 (htab->elf.dynobj, val, p);
12507           p += 4;
12508           /* Offset to .glink.  */
12509           val = (htab->glink->output_section->vma
12510                  + htab->glink->output_offset
12511                  + 8);
12512           val -= (htab->glink_eh_frame->output_section->vma
12513                   + htab->glink_eh_frame->output_offset);
12514           val -= p - htab->glink_eh_frame->contents;
12515           if (val + 0x80000000 > 0xffffffff)
12516             {
12517               info->callbacks->einfo
12518                 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
12519                  htab->glink->name);
12520               return FALSE;
12521             }
12522           bfd_put_32 (htab->elf.dynobj, val, p);
12523           p += 4;
12524           /* .glink size.  */
12525           bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
12526           p += 4;
12527           /* Augmentation.  */
12528           p += 1;
12529
12530           *p++ = DW_CFA_advance_loc + 1;
12531           *p++ = DW_CFA_register;
12532           *p++ = 65;
12533           *p++ = 12;
12534           *p++ = DW_CFA_advance_loc + 4;
12535           *p++ = DW_CFA_restore_extended;
12536           *p++ = 65;
12537         }
12538       /* Subsume any padding into the last FDE if user .eh_frame
12539          sections are aligned more than glink_eh_frame.  Otherwise any
12540          zero padding will be seen as a terminator.  */
12541       size = p - htab->glink_eh_frame->contents;
12542       align = 1;
12543       align <<= htab->glink_eh_frame->output_section->alignment_power;
12544       align -= 1;
12545       pad = ((size + align) & ~align) - size;
12546       htab->glink_eh_frame->size = size + pad;
12547       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12548     }
12549
12550   /* Build the stubs as directed by the stub hash table.  */
12551   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
12552
12553   if (htab->relbrlt != NULL)
12554     htab->relbrlt->reloc_count = 0;
12555
12556   if (htab->plt_stub_align != 0)
12557     for (stub_sec = htab->stub_bfd->sections;
12558          stub_sec != NULL;
12559          stub_sec = stub_sec->next)
12560       if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12561         stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
12562                           & (-1 << htab->plt_stub_align));
12563
12564   for (stub_sec = htab->stub_bfd->sections;
12565        stub_sec != NULL;
12566        stub_sec = stub_sec->next)
12567     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12568       {
12569         stub_sec_count += 1;
12570         if (stub_sec->rawsize != stub_sec->size)
12571           break;
12572       }
12573
12574   if (stub_sec != NULL
12575       || htab->glink->rawsize != htab->glink->size
12576       || (htab->glink_eh_frame != NULL
12577           && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
12578     {
12579       htab->stub_error = TRUE;
12580       info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
12581     }
12582
12583   if (htab->stub_error)
12584     return FALSE;
12585
12586   if (stats != NULL)
12587     {
12588       *stats = bfd_malloc (500);
12589       if (*stats == NULL)
12590         return FALSE;
12591
12592       sprintf (*stats, _("linker stubs in %u group%s\n"
12593                          "  branch       %lu\n"
12594                          "  toc adjust   %lu\n"
12595                          "  long branch  %lu\n"
12596                          "  long toc adj %lu\n"
12597                          "  plt call     %lu\n"
12598                          "  plt call toc %lu"),
12599                stub_sec_count,
12600                stub_sec_count == 1 ? "" : "s",
12601                htab->stub_count[ppc_stub_long_branch - 1],
12602                htab->stub_count[ppc_stub_long_branch_r2off - 1],
12603                htab->stub_count[ppc_stub_plt_branch - 1],
12604                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
12605                htab->stub_count[ppc_stub_plt_call - 1],
12606                htab->stub_count[ppc_stub_plt_call_r2save - 1]);
12607     }
12608   return TRUE;
12609 }
12610
12611 /* This function undoes the changes made by add_symbol_adjust.  */
12612
12613 static bfd_boolean
12614 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12615 {
12616   struct ppc_link_hash_entry *eh;
12617
12618   if (h->root.type == bfd_link_hash_indirect)
12619     return TRUE;
12620
12621   eh = (struct ppc_link_hash_entry *) h;
12622   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
12623     return TRUE;
12624
12625   eh->elf.root.type = bfd_link_hash_undefined;
12626   return TRUE;
12627 }
12628
12629 void
12630 ppc64_elf_restore_symbols (struct bfd_link_info *info)
12631 {
12632   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12633
12634   if (htab != NULL)
12635     elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
12636 }
12637
12638 /* What to do when ld finds relocations against symbols defined in
12639    discarded sections.  */
12640
12641 static unsigned int
12642 ppc64_elf_action_discarded (asection *sec)
12643 {
12644   if (strcmp (".opd", sec->name) == 0)
12645     return 0;
12646
12647   if (strcmp (".toc", sec->name) == 0)
12648     return 0;
12649
12650   if (strcmp (".toc1", sec->name) == 0)
12651     return 0;
12652
12653   return _bfd_elf_default_action_discarded (sec);
12654 }
12655
12656 /* The RELOCATE_SECTION function is called by the ELF backend linker
12657    to handle the relocations for a section.
12658
12659    The relocs are always passed as Rela structures; if the section
12660    actually uses Rel structures, the r_addend field will always be
12661    zero.
12662
12663    This function is responsible for adjust the section contents as
12664    necessary, and (if using Rela relocs and generating a
12665    relocatable output file) adjusting the reloc addend as
12666    necessary.
12667
12668    This function does not have to worry about setting the reloc
12669    address or the reloc symbol index.
12670
12671    LOCAL_SYMS is a pointer to the swapped in local symbols.
12672
12673    LOCAL_SECTIONS is an array giving the section in the input file
12674    corresponding to the st_shndx field of each local symbol.
12675
12676    The global hash table entry for the global symbols can be found
12677    via elf_sym_hashes (input_bfd).
12678
12679    When generating relocatable output, this function must handle
12680    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
12681    going to be the section symbol corresponding to the output
12682    section, which means that the addend must be adjusted
12683    accordingly.  */
12684
12685 static bfd_boolean
12686 ppc64_elf_relocate_section (bfd *output_bfd,
12687                             struct bfd_link_info *info,
12688                             bfd *input_bfd,
12689                             asection *input_section,
12690                             bfd_byte *contents,
12691                             Elf_Internal_Rela *relocs,
12692                             Elf_Internal_Sym *local_syms,
12693                             asection **local_sections)
12694 {
12695   struct ppc_link_hash_table *htab;
12696   Elf_Internal_Shdr *symtab_hdr;
12697   struct elf_link_hash_entry **sym_hashes;
12698   Elf_Internal_Rela *rel;
12699   Elf_Internal_Rela *relend;
12700   Elf_Internal_Rela outrel;
12701   bfd_byte *loc;
12702   struct got_entry **local_got_ents;
12703   bfd_vma TOCstart;
12704   bfd_boolean ret = TRUE;
12705   bfd_boolean is_opd;
12706   /* Assume 'at' branch hints.  */
12707   bfd_boolean is_isa_v2 = TRUE;
12708   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
12709
12710   /* Initialize howto table if needed.  */
12711   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
12712     ppc_howto_init ();
12713
12714   htab = ppc_hash_table (info);
12715   if (htab == NULL)
12716     return FALSE;
12717
12718   /* Don't relocate stub sections.  */
12719   if (input_section->owner == htab->stub_bfd)
12720     return TRUE;
12721
12722   BFD_ASSERT (is_ppc64_elf (input_bfd));
12723
12724   local_got_ents = elf_local_got_ents (input_bfd);
12725   TOCstart = elf_gp (output_bfd);
12726   symtab_hdr = &elf_symtab_hdr (input_bfd);
12727   sym_hashes = elf_sym_hashes (input_bfd);
12728   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
12729
12730   rel = relocs;
12731   relend = relocs + input_section->reloc_count;
12732   for (; rel < relend; rel++)
12733     {
12734       enum elf_ppc64_reloc_type r_type;
12735       bfd_vma addend;
12736       bfd_reloc_status_type r;
12737       Elf_Internal_Sym *sym;
12738       asection *sec;
12739       struct elf_link_hash_entry *h_elf;
12740       struct ppc_link_hash_entry *h;
12741       struct ppc_link_hash_entry *fdh;
12742       const char *sym_name;
12743       unsigned long r_symndx, toc_symndx;
12744       bfd_vma toc_addend;
12745       unsigned char tls_mask, tls_gd, tls_type;
12746       unsigned char sym_type;
12747       bfd_vma relocation;
12748       bfd_boolean unresolved_reloc;
12749       bfd_boolean warned;
12750       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
12751       unsigned int insn;
12752       unsigned int mask;
12753       struct ppc_stub_hash_entry *stub_entry;
12754       bfd_vma max_br_offset;
12755       bfd_vma from;
12756       const Elf_Internal_Rela orig_rel = *rel;
12757
12758       r_type = ELF64_R_TYPE (rel->r_info);
12759       r_symndx = ELF64_R_SYM (rel->r_info);
12760
12761       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
12762          symbol of the previous ADDR64 reloc.  The symbol gives us the
12763          proper TOC base to use.  */
12764       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
12765           && rel != relocs
12766           && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
12767           && is_opd)
12768         r_symndx = ELF64_R_SYM (rel[-1].r_info);
12769
12770       sym = NULL;
12771       sec = NULL;
12772       h_elf = NULL;
12773       sym_name = NULL;
12774       unresolved_reloc = FALSE;
12775       warned = FALSE;
12776
12777       if (r_symndx < symtab_hdr->sh_info)
12778         {
12779           /* It's a local symbol.  */
12780           struct _opd_sec_data *opd;
12781
12782           sym = local_syms + r_symndx;
12783           sec = local_sections[r_symndx];
12784           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
12785           sym_type = ELF64_ST_TYPE (sym->st_info);
12786           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
12787           opd = get_opd_info (sec);
12788           if (opd != NULL && opd->adjust != NULL)
12789             {
12790               long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
12791               if (adjust == -1)
12792                 relocation = 0;
12793               else
12794                 {
12795                   /* If this is a relocation against the opd section sym
12796                      and we have edited .opd, adjust the reloc addend so
12797                      that ld -r and ld --emit-relocs output is correct.
12798                      If it is a reloc against some other .opd symbol,
12799                      then the symbol value will be adjusted later.  */
12800                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
12801                     rel->r_addend += adjust;
12802                   else
12803                     relocation += adjust;
12804                 }
12805             }
12806         }
12807       else
12808         {
12809           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
12810                                    r_symndx, symtab_hdr, sym_hashes,
12811                                    h_elf, sec, relocation,
12812                                    unresolved_reloc, warned);
12813           sym_name = h_elf->root.root.string;
12814           sym_type = h_elf->type;
12815           if (sec != NULL
12816               && sec->owner == output_bfd
12817               && strcmp (sec->name, ".opd") == 0)
12818             {
12819               /* This is a symbol defined in a linker script.  All
12820                  such are defined in output sections, even those
12821                  defined by simple assignment from a symbol defined in
12822                  an input section.  Transfer the symbol to an
12823                  appropriate input .opd section, so that a branch to
12824                  this symbol will be mapped to the location specified
12825                  by the opd entry.  */
12826               struct bfd_link_order *lo;
12827               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
12828                 if (lo->type == bfd_indirect_link_order)
12829                   {
12830                     asection *isec = lo->u.indirect.section;
12831                     if (h_elf->root.u.def.value >= isec->output_offset
12832                         && h_elf->root.u.def.value < (isec->output_offset
12833                                                       + isec->size))
12834                       {
12835                         h_elf->root.u.def.value -= isec->output_offset;
12836                         h_elf->root.u.def.section = isec;
12837                         sec = isec;
12838                         break;
12839                       }
12840                   }
12841             }
12842         }
12843       h = (struct ppc_link_hash_entry *) h_elf;
12844
12845       if (sec != NULL && discarded_section (sec))
12846         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
12847                                          rel, 1, relend,
12848                                          ppc64_elf_howto_table[r_type], 0,
12849                                          contents);
12850
12851       if (info->relocatable)
12852         continue;
12853
12854       if (h != NULL && &h->elf == htab->elf.hgot)
12855         {
12856           relocation = (TOCstart
12857                         + htab->stub_group[input_section->id].toc_off);
12858           sec = bfd_abs_section_ptr;
12859           unresolved_reloc = FALSE;
12860         }
12861
12862       /* TLS optimizations.  Replace instruction sequences and relocs
12863          based on information we collected in tls_optimize.  We edit
12864          RELOCS so that --emit-relocs will output something sensible
12865          for the final instruction stream.  */
12866       tls_mask = 0;
12867       tls_gd = 0;
12868       toc_symndx = 0;
12869       if (h != NULL)
12870         tls_mask = h->tls_mask;
12871       else if (local_got_ents != NULL)
12872         {
12873           struct plt_entry **local_plt = (struct plt_entry **)
12874             (local_got_ents + symtab_hdr->sh_info);
12875           unsigned char *lgot_masks = (unsigned char *)
12876             (local_plt + symtab_hdr->sh_info);
12877           tls_mask = lgot_masks[r_symndx];
12878         }
12879       if (tls_mask == 0
12880           && (r_type == R_PPC64_TLS
12881               || r_type == R_PPC64_TLSGD
12882               || r_type == R_PPC64_TLSLD))
12883         {
12884           /* Check for toc tls entries.  */
12885           unsigned char *toc_tls;
12886
12887           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12888                              &local_syms, rel, input_bfd))
12889             return FALSE;
12890
12891           if (toc_tls)
12892             tls_mask = *toc_tls;
12893         }
12894
12895       /* Check that tls relocs are used with tls syms, and non-tls
12896          relocs are used with non-tls syms.  */
12897       if (r_symndx != STN_UNDEF
12898           && r_type != R_PPC64_NONE
12899           && (h == NULL
12900               || h->elf.root.type == bfd_link_hash_defined
12901               || h->elf.root.type == bfd_link_hash_defweak)
12902           && (IS_PPC64_TLS_RELOC (r_type)
12903               != (sym_type == STT_TLS
12904                   || (sym_type == STT_SECTION
12905                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
12906         {
12907           if (tls_mask != 0
12908               && (r_type == R_PPC64_TLS
12909                   || r_type == R_PPC64_TLSGD
12910                   || r_type == R_PPC64_TLSLD))
12911             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
12912             ;
12913           else
12914             info->callbacks->einfo
12915               (!IS_PPC64_TLS_RELOC (r_type)
12916                ? _("%P: %H: %s used with TLS symbol `%T'\n")
12917                : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
12918                input_bfd, input_section, rel->r_offset,
12919                ppc64_elf_howto_table[r_type]->name,
12920                sym_name);
12921         }
12922
12923       /* Ensure reloc mapping code below stays sane.  */
12924       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
12925           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
12926           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
12927           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
12928           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
12929           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
12930           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
12931           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
12932           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
12933           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
12934         abort ();
12935
12936       switch (r_type)
12937         {
12938         default:
12939           break;
12940
12941         case R_PPC64_LO_DS_OPT:
12942           insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12943           if ((insn & (0x3f << 26)) != 58u << 26)
12944             abort ();
12945           insn += (14u << 26) - (58u << 26);
12946           bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12947           r_type = R_PPC64_TOC16_LO;
12948           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12949           break;
12950
12951         case R_PPC64_TOC16:
12952         case R_PPC64_TOC16_LO:
12953         case R_PPC64_TOC16_DS:
12954         case R_PPC64_TOC16_LO_DS:
12955           {
12956             /* Check for toc tls entries.  */
12957             unsigned char *toc_tls;
12958             int retval;
12959
12960             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12961                                    &local_syms, rel, input_bfd);
12962             if (retval == 0)
12963               return FALSE;
12964
12965             if (toc_tls)
12966               {
12967                 tls_mask = *toc_tls;
12968                 if (r_type == R_PPC64_TOC16_DS
12969                     || r_type == R_PPC64_TOC16_LO_DS)
12970                   {
12971                     if (tls_mask != 0
12972                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
12973                       goto toctprel;
12974                   }
12975                 else
12976                   {
12977                     /* If we found a GD reloc pair, then we might be
12978                        doing a GD->IE transition.  */
12979                     if (retval == 2)
12980                       {
12981                         tls_gd = TLS_TPRELGD;
12982                         if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12983                           goto tls_ldgd_opt;
12984                       }
12985                     else if (retval == 3)
12986                       {
12987                         if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12988                           goto tls_ldgd_opt;
12989                       }
12990                   }
12991               }
12992           }
12993           break;
12994
12995         case R_PPC64_GOT_TPREL16_HI:
12996         case R_PPC64_GOT_TPREL16_HA:
12997           if (tls_mask != 0
12998               && (tls_mask & TLS_TPREL) == 0)
12999             {
13000               rel->r_offset -= d_offset;
13001               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13002               r_type = R_PPC64_NONE;
13003               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13004             }
13005           break;
13006
13007         case R_PPC64_GOT_TPREL16_DS:
13008         case R_PPC64_GOT_TPREL16_LO_DS:
13009           if (tls_mask != 0
13010               && (tls_mask & TLS_TPREL) == 0)
13011             {
13012             toctprel:
13013               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
13014               insn &= 31 << 21;
13015               insn |= 0x3c0d0000;       /* addis 0,13,0 */
13016               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
13017               r_type = R_PPC64_TPREL16_HA;
13018               if (toc_symndx != 0)
13019                 {
13020                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13021                   rel->r_addend = toc_addend;
13022                   /* We changed the symbol.  Start over in order to
13023                      get h, sym, sec etc. right.  */
13024                   rel--;
13025                   continue;
13026                 }
13027               else
13028                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13029             }
13030           break;
13031
13032         case R_PPC64_TLS:
13033           if (tls_mask != 0
13034               && (tls_mask & TLS_TPREL) == 0)
13035             {
13036               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
13037               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13038               if (insn == 0)
13039                 abort ();
13040               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
13041               /* Was PPC64_TLS which sits on insn boundary, now
13042                  PPC64_TPREL16_LO which is at low-order half-word.  */
13043               rel->r_offset += d_offset;
13044               r_type = R_PPC64_TPREL16_LO;
13045               if (toc_symndx != 0)
13046                 {
13047                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13048                   rel->r_addend = toc_addend;
13049                   /* We changed the symbol.  Start over in order to
13050                      get h, sym, sec etc. right.  */
13051                   rel--;
13052                   continue;
13053                 }
13054               else
13055                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13056             }
13057           break;
13058
13059         case R_PPC64_GOT_TLSGD16_HI:
13060         case R_PPC64_GOT_TLSGD16_HA:
13061           tls_gd = TLS_TPRELGD;
13062           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13063             goto tls_gdld_hi;
13064           break;
13065
13066         case R_PPC64_GOT_TLSLD16_HI:
13067         case R_PPC64_GOT_TLSLD16_HA:
13068           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13069             {
13070             tls_gdld_hi:
13071               if ((tls_mask & tls_gd) != 0)
13072                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13073                           + R_PPC64_GOT_TPREL16_DS);
13074               else
13075                 {
13076                   rel->r_offset -= d_offset;
13077                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13078                   r_type = R_PPC64_NONE;
13079                 }
13080               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13081             }
13082           break;
13083
13084         case R_PPC64_GOT_TLSGD16:
13085         case R_PPC64_GOT_TLSGD16_LO:
13086           tls_gd = TLS_TPRELGD;
13087           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13088             goto tls_ldgd_opt;
13089           break;
13090
13091         case R_PPC64_GOT_TLSLD16:
13092         case R_PPC64_GOT_TLSLD16_LO:
13093           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13094             {
13095               unsigned int insn1, insn2, insn3;
13096               bfd_vma offset;
13097
13098             tls_ldgd_opt:
13099               offset = (bfd_vma) -1;
13100               /* If not using the newer R_PPC64_TLSGD/LD to mark
13101                  __tls_get_addr calls, we must trust that the call
13102                  stays with its arg setup insns, ie. that the next
13103                  reloc is the __tls_get_addr call associated with
13104                  the current reloc.  Edit both insns.  */
13105               if (input_section->has_tls_get_addr_call
13106                   && rel + 1 < relend
13107                   && branch_reloc_hash_match (input_bfd, rel + 1,
13108                                               htab->tls_get_addr,
13109                                               htab->tls_get_addr_fd))
13110                 offset = rel[1].r_offset;
13111               if ((tls_mask & tls_gd) != 0)
13112                 {
13113                   /* IE */
13114                   insn1 = bfd_get_32 (output_bfd,
13115                                       contents + rel->r_offset - d_offset);
13116                   insn1 &= (1 << 26) - (1 << 2);
13117                   insn1 |= 58 << 26;    /* ld */
13118                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13119                   if (offset != (bfd_vma) -1)
13120                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13121                   if ((tls_mask & TLS_EXPLICIT) == 0)
13122                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13123                               + R_PPC64_GOT_TPREL16_DS);
13124                   else
13125                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13126                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13127                 }
13128               else
13129                 {
13130                   /* LE */
13131                   insn1 = 0x3c6d0000;   /* addis 3,13,0 */
13132                   insn2 = 0x38630000;   /* addi 3,3,0 */
13133                   if (tls_gd == 0)
13134                     {
13135                       /* Was an LD reloc.  */
13136                       if (toc_symndx)
13137                         sec = local_sections[toc_symndx];
13138                       for (r_symndx = 0;
13139                            r_symndx < symtab_hdr->sh_info;
13140                            r_symndx++)
13141                         if (local_sections[r_symndx] == sec)
13142                           break;
13143                       if (r_symndx >= symtab_hdr->sh_info)
13144                         r_symndx = STN_UNDEF;
13145                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13146                       if (r_symndx != STN_UNDEF)
13147                         rel->r_addend -= (local_syms[r_symndx].st_value
13148                                           + sec->output_offset
13149                                           + sec->output_section->vma);
13150                     }
13151                   else if (toc_symndx != 0)
13152                     {
13153                       r_symndx = toc_symndx;
13154                       rel->r_addend = toc_addend;
13155                     }
13156                   r_type = R_PPC64_TPREL16_HA;
13157                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13158                   if (offset != (bfd_vma) -1)
13159                     {
13160                       rel[1].r_info = ELF64_R_INFO (r_symndx,
13161                                                     R_PPC64_TPREL16_LO);
13162                       rel[1].r_offset = offset + d_offset;
13163                       rel[1].r_addend = rel->r_addend;
13164                     }
13165                 }
13166               bfd_put_32 (output_bfd, insn1,
13167                           contents + rel->r_offset - d_offset);
13168               if (offset != (bfd_vma) -1)
13169                 {
13170                   insn3 = bfd_get_32 (output_bfd,
13171                                       contents + offset + 4);
13172                   if (insn3 == NOP
13173                       || insn3 == CROR_151515 || insn3 == CROR_313131)
13174                     {
13175                       rel[1].r_offset += 4;
13176                       bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13177                       insn2 = NOP;
13178                     }
13179                   bfd_put_32 (output_bfd, insn2, contents + offset);
13180                 }
13181               if ((tls_mask & tls_gd) == 0
13182                   && (tls_gd == 0 || toc_symndx != 0))
13183                 {
13184                   /* We changed the symbol.  Start over in order
13185                      to get h, sym, sec etc. right.  */
13186                   rel--;
13187                   continue;
13188                 }
13189             }
13190           break;
13191
13192         case R_PPC64_TLSGD:
13193           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13194             {
13195               unsigned int insn2, insn3;
13196               bfd_vma offset = rel->r_offset;
13197
13198               if ((tls_mask & TLS_TPRELGD) != 0)
13199                 {
13200                   /* IE */
13201                   r_type = R_PPC64_NONE;
13202                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13203                 }
13204               else
13205                 {
13206                   /* LE */
13207                   if (toc_symndx != 0)
13208                     {
13209                       r_symndx = toc_symndx;
13210                       rel->r_addend = toc_addend;
13211                     }
13212                   r_type = R_PPC64_TPREL16_LO;
13213                   rel->r_offset = offset + d_offset;
13214                   insn2 = 0x38630000;   /* addi 3,3,0 */
13215                 }
13216               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13217               /* Zap the reloc on the _tls_get_addr call too.  */
13218               BFD_ASSERT (offset == rel[1].r_offset);
13219               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13220               insn3 = bfd_get_32 (output_bfd,
13221                                   contents + offset + 4);
13222               if (insn3 == NOP
13223                   || insn3 == CROR_151515 || insn3 == CROR_313131)
13224                 {
13225                   rel->r_offset += 4;
13226                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13227                   insn2 = NOP;
13228                 }
13229               bfd_put_32 (output_bfd, insn2, contents + offset);
13230               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13231                 {
13232                   rel--;
13233                   continue;
13234                 }
13235             }
13236           break;
13237
13238         case R_PPC64_TLSLD:
13239           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13240             {
13241               unsigned int insn2, insn3;
13242               bfd_vma offset = rel->r_offset;
13243
13244               if (toc_symndx)
13245                 sec = local_sections[toc_symndx];
13246               for (r_symndx = 0;
13247                    r_symndx < symtab_hdr->sh_info;
13248                    r_symndx++)
13249                 if (local_sections[r_symndx] == sec)
13250                   break;
13251               if (r_symndx >= symtab_hdr->sh_info)
13252                 r_symndx = STN_UNDEF;
13253               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13254               if (r_symndx != STN_UNDEF)
13255                 rel->r_addend -= (local_syms[r_symndx].st_value
13256                                   + sec->output_offset
13257                                   + sec->output_section->vma);
13258
13259               r_type = R_PPC64_TPREL16_LO;
13260               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13261               rel->r_offset = offset + d_offset;
13262               /* Zap the reloc on the _tls_get_addr call too.  */
13263               BFD_ASSERT (offset == rel[1].r_offset);
13264               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13265               insn2 = 0x38630000;       /* addi 3,3,0 */
13266               insn3 = bfd_get_32 (output_bfd,
13267                                   contents + offset + 4);
13268               if (insn3 == NOP
13269                   || insn3 == CROR_151515 || insn3 == CROR_313131)
13270                 {
13271                   rel->r_offset += 4;
13272                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13273                   insn2 = NOP;
13274                 }
13275               bfd_put_32 (output_bfd, insn2, contents + offset);
13276               rel--;
13277               continue;
13278             }
13279           break;
13280
13281         case R_PPC64_DTPMOD64:
13282           if (rel + 1 < relend
13283               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13284               && rel[1].r_offset == rel->r_offset + 8)
13285             {
13286               if ((tls_mask & TLS_GD) == 0)
13287                 {
13288                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13289                   if ((tls_mask & TLS_TPRELGD) != 0)
13290                     r_type = R_PPC64_TPREL64;
13291                   else
13292                     {
13293                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13294                       r_type = R_PPC64_NONE;
13295                     }
13296                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13297                 }
13298             }
13299           else
13300             {
13301               if ((tls_mask & TLS_LD) == 0)
13302                 {
13303                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13304                   r_type = R_PPC64_NONE;
13305                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13306                 }
13307             }
13308           break;
13309
13310         case R_PPC64_TPREL64:
13311           if ((tls_mask & TLS_TPREL) == 0)
13312             {
13313               r_type = R_PPC64_NONE;
13314               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13315             }
13316           break;
13317         }
13318
13319       /* Handle other relocations that tweak non-addend part of insn.  */
13320       insn = 0;
13321       max_br_offset = 1 << 25;
13322       addend = rel->r_addend;
13323       reloc_dest = DEST_NORMAL;
13324       switch (r_type)
13325         {
13326         default:
13327           break;
13328
13329         case R_PPC64_TOCSAVE:
13330           if (relocation + addend == (rel->r_offset
13331                                       + input_section->output_offset
13332                                       + input_section->output_section->vma)
13333               && tocsave_find (htab, NO_INSERT,
13334                                &local_syms, rel, input_bfd))
13335             {
13336               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13337               if (insn == NOP
13338                   || insn == CROR_151515 || insn == CROR_313131)
13339                 bfd_put_32 (input_bfd, STD_R2_40R1,
13340                             contents + rel->r_offset);
13341             }
13342           break;
13343
13344           /* Branch taken prediction relocations.  */
13345         case R_PPC64_ADDR14_BRTAKEN:
13346         case R_PPC64_REL14_BRTAKEN:
13347           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
13348           /* Fall thru.  */
13349
13350           /* Branch not taken prediction relocations.  */
13351         case R_PPC64_ADDR14_BRNTAKEN:
13352         case R_PPC64_REL14_BRNTAKEN:
13353           insn |= bfd_get_32 (output_bfd,
13354                               contents + rel->r_offset) & ~(0x01 << 21);
13355           /* Fall thru.  */
13356
13357         case R_PPC64_REL14:
13358           max_br_offset = 1 << 15;
13359           /* Fall thru.  */
13360
13361         case R_PPC64_REL24:
13362           /* Calls to functions with a different TOC, such as calls to
13363              shared objects, need to alter the TOC pointer.  This is
13364              done using a linkage stub.  A REL24 branching to these
13365              linkage stubs needs to be followed by a nop, as the nop
13366              will be replaced with an instruction to restore the TOC
13367              base pointer.  */
13368           fdh = h;
13369           if (h != NULL
13370               && h->oh != NULL
13371               && h->oh->is_func_descriptor)
13372             fdh = ppc_follow_link (h->oh);
13373           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13374                                            htab);
13375           if (stub_entry != NULL
13376               && (stub_entry->stub_type == ppc_stub_plt_call
13377                   || stub_entry->stub_type == ppc_stub_plt_call_r2save
13378                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13379                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
13380             {
13381               bfd_boolean can_plt_call = FALSE;
13382
13383               /* All of these stubs will modify r2, so there must be a
13384                  branch and link followed by a nop.  The nop is
13385                  replaced by an insn to restore r2.  */
13386               if (rel->r_offset + 8 <= input_section->size)
13387                 {
13388                   unsigned long br;
13389
13390                   br = bfd_get_32 (input_bfd,
13391                                    contents + rel->r_offset);
13392                   if ((br & 1) != 0)
13393                     {
13394                       unsigned long nop;
13395
13396                       nop = bfd_get_32 (input_bfd,
13397                                         contents + rel->r_offset + 4);
13398                       if (nop == NOP
13399                           || nop == CROR_151515 || nop == CROR_313131)
13400                         {
13401                           if (h != NULL
13402                               && (h == htab->tls_get_addr_fd
13403                                   || h == htab->tls_get_addr)
13404                               && !htab->no_tls_get_addr_opt)
13405                             {
13406                               /* Special stub used, leave nop alone.  */
13407                             }
13408                           else
13409                             bfd_put_32 (input_bfd, LD_R2_40R1,
13410                                         contents + rel->r_offset + 4);
13411                           can_plt_call = TRUE;
13412                         }
13413                     }
13414                 }
13415
13416               if (!can_plt_call && h != NULL)
13417                 {
13418                   const char *name = h->elf.root.root.string;
13419
13420                   if (*name == '.')
13421                     ++name;
13422
13423                   if (strncmp (name, "__libc_start_main", 17) == 0
13424                       && (name[17] == 0 || name[17] == '@'))
13425                     {
13426                       /* Allow crt1 branch to go via a toc adjusting
13427                          stub.  Other calls that never return could do
13428                          the same, if we could detect such.  */
13429                       can_plt_call = TRUE;
13430                     }
13431                 }
13432
13433               if (!can_plt_call)
13434                 {
13435                   /* g++ as of 20130507 emits self-calls without a
13436                      following nop.  This is arguably wrong since we
13437                      have conflicting information.  On the one hand a
13438                      global symbol and on the other a local call
13439                      sequence, but don't error for this special case.
13440                      It isn't possible to cheaply verify we have
13441                      exactly such a call.  Allow all calls to the same
13442                      section.  */
13443                   asection *code_sec = sec;
13444
13445                   if (get_opd_info (sec) != NULL)
13446                     {
13447                       bfd_vma off = (relocation + addend
13448                                      - sec->output_section->vma
13449                                      - sec->output_offset);
13450
13451                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
13452                     }
13453                   if (code_sec == input_section)
13454                     can_plt_call = TRUE;
13455                 }
13456
13457               if (!can_plt_call)
13458                 {
13459                   info->callbacks->einfo
13460                     (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13461                        "recompile with -fPIC"),
13462                      input_bfd, input_section, rel->r_offset, sym_name);
13463
13464                   bfd_set_error (bfd_error_bad_value);
13465                   ret = FALSE;
13466                 }
13467
13468               if (can_plt_call
13469                   && (stub_entry->stub_type == ppc_stub_plt_call
13470                       || stub_entry->stub_type == ppc_stub_plt_call_r2save))
13471                 unresolved_reloc = FALSE;
13472             }
13473
13474           if ((stub_entry == NULL
13475                || stub_entry->stub_type == ppc_stub_long_branch
13476                || stub_entry->stub_type == ppc_stub_plt_branch)
13477               && get_opd_info (sec) != NULL)
13478             {
13479               /* The branch destination is the value of the opd entry. */
13480               bfd_vma off = (relocation + addend
13481                              - sec->output_section->vma
13482                              - sec->output_offset);
13483               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
13484               if (dest != (bfd_vma) -1)
13485                 {
13486                   relocation = dest;
13487                   addend = 0;
13488                   reloc_dest = DEST_OPD;
13489                 }
13490             }
13491
13492           /* If the branch is out of reach we ought to have a long
13493              branch stub.  */
13494           from = (rel->r_offset
13495                   + input_section->output_offset
13496                   + input_section->output_section->vma);
13497
13498           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
13499                                                   ? fdh->elf.other
13500                                                   : sym->st_other);
13501
13502           if (stub_entry != NULL
13503               && (stub_entry->stub_type == ppc_stub_long_branch
13504                   || stub_entry->stub_type == ppc_stub_plt_branch)
13505               && (r_type == R_PPC64_ADDR14_BRTAKEN
13506                   || r_type == R_PPC64_ADDR14_BRNTAKEN
13507                   || (relocation + addend - from + max_br_offset
13508                       < 2 * max_br_offset)))
13509             /* Don't use the stub if this branch is in range.  */
13510             stub_entry = NULL;
13511
13512           if (stub_entry != NULL)
13513             {
13514               /* Munge up the value and addend so that we call the stub
13515                  rather than the procedure directly.  */
13516               relocation = (stub_entry->stub_offset
13517                             + stub_entry->stub_sec->output_offset
13518                             + stub_entry->stub_sec->output_section->vma);
13519               addend = 0;
13520               reloc_dest = DEST_STUB;
13521
13522               if ((stub_entry->stub_type == ppc_stub_plt_call
13523                    || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13524                   && (ALWAYS_EMIT_R2SAVE
13525                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13526                   && rel + 1 < relend
13527                   && rel[1].r_offset == rel->r_offset + 4
13528                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
13529                 relocation += 4;
13530             }
13531
13532           if (insn != 0)
13533             {
13534               if (is_isa_v2)
13535                 {
13536                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
13537                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
13538                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
13539                   if ((insn & (0x14 << 21)) == (0x04 << 21))
13540                     insn |= 0x02 << 21;
13541                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
13542                     insn |= 0x08 << 21;
13543                   else
13544                     break;
13545                 }
13546               else
13547                 {
13548                   /* Invert 'y' bit if not the default.  */
13549                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
13550                     insn ^= 0x01 << 21;
13551                 }
13552
13553               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
13554             }
13555
13556           /* NOP out calls to undefined weak functions.
13557              We can thus call a weak function without first
13558              checking whether the function is defined.  */
13559           else if (h != NULL
13560                    && h->elf.root.type == bfd_link_hash_undefweak
13561                    && h->elf.dynindx == -1
13562                    && r_type == R_PPC64_REL24
13563                    && relocation == 0
13564                    && addend == 0)
13565             {
13566               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13567               continue;
13568             }
13569           break;
13570         }
13571
13572       /* Set `addend'.  */
13573       tls_type = 0;
13574       switch (r_type)
13575         {
13576         default:
13577           info->callbacks->einfo
13578             (_("%P: %B: unknown relocation type %d for `%T'\n"),
13579              input_bfd, (int) r_type, sym_name);
13580
13581           bfd_set_error (bfd_error_bad_value);
13582           ret = FALSE;
13583           continue;
13584
13585         case R_PPC64_NONE:
13586         case R_PPC64_TLS:
13587         case R_PPC64_TLSGD:
13588         case R_PPC64_TLSLD:
13589         case R_PPC64_TOCSAVE:
13590         case R_PPC64_GNU_VTINHERIT:
13591         case R_PPC64_GNU_VTENTRY:
13592           continue;
13593
13594           /* GOT16 relocations.  Like an ADDR16 using the symbol's
13595              address in the GOT as relocation value instead of the
13596              symbol's value itself.  Also, create a GOT entry for the
13597              symbol and put the symbol value there.  */
13598         case R_PPC64_GOT_TLSGD16:
13599         case R_PPC64_GOT_TLSGD16_LO:
13600         case R_PPC64_GOT_TLSGD16_HI:
13601         case R_PPC64_GOT_TLSGD16_HA:
13602           tls_type = TLS_TLS | TLS_GD;
13603           goto dogot;
13604
13605         case R_PPC64_GOT_TLSLD16:
13606         case R_PPC64_GOT_TLSLD16_LO:
13607         case R_PPC64_GOT_TLSLD16_HI:
13608         case R_PPC64_GOT_TLSLD16_HA:
13609           tls_type = TLS_TLS | TLS_LD;
13610           goto dogot;
13611
13612         case R_PPC64_GOT_TPREL16_DS:
13613         case R_PPC64_GOT_TPREL16_LO_DS:
13614         case R_PPC64_GOT_TPREL16_HI:
13615         case R_PPC64_GOT_TPREL16_HA:
13616           tls_type = TLS_TLS | TLS_TPREL;
13617           goto dogot;
13618
13619         case R_PPC64_GOT_DTPREL16_DS:
13620         case R_PPC64_GOT_DTPREL16_LO_DS:
13621         case R_PPC64_GOT_DTPREL16_HI:
13622         case R_PPC64_GOT_DTPREL16_HA:
13623           tls_type = TLS_TLS | TLS_DTPREL;
13624           goto dogot;
13625
13626         case R_PPC64_GOT16:
13627         case R_PPC64_GOT16_LO:
13628         case R_PPC64_GOT16_HI:
13629         case R_PPC64_GOT16_HA:
13630         case R_PPC64_GOT16_DS:
13631         case R_PPC64_GOT16_LO_DS:
13632         dogot:
13633           {
13634             /* Relocation is to the entry for this symbol in the global
13635                offset table.  */
13636             asection *got;
13637             bfd_vma *offp;
13638             bfd_vma off;
13639             unsigned long indx = 0;
13640             struct got_entry *ent;
13641
13642             if (tls_type == (TLS_TLS | TLS_LD)
13643                 && (h == NULL
13644                     || !h->elf.def_dynamic))
13645               ent = ppc64_tlsld_got (input_bfd);
13646             else
13647               {
13648
13649                 if (h != NULL)
13650                   {
13651                     bfd_boolean dyn = htab->elf.dynamic_sections_created;
13652                     if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
13653                                                           &h->elf)
13654                         || (info->shared
13655                             && SYMBOL_CALLS_LOCAL (info, &h->elf)))
13656                       /* This is actually a static link, or it is a
13657                          -Bsymbolic link and the symbol is defined
13658                          locally, or the symbol was forced to be local
13659                          because of a version file.  */
13660                       ;
13661                     else
13662                       {
13663                         BFD_ASSERT (h->elf.dynindx != -1);
13664                         indx = h->elf.dynindx;
13665                         unresolved_reloc = FALSE;
13666                       }
13667                     ent = h->elf.got.glist;
13668                   }
13669                 else
13670                   {
13671                     if (local_got_ents == NULL)
13672                       abort ();
13673                     ent = local_got_ents[r_symndx];
13674                   }
13675
13676                 for (; ent != NULL; ent = ent->next)
13677                   if (ent->addend == orig_rel.r_addend
13678                       && ent->owner == input_bfd
13679                       && ent->tls_type == tls_type)
13680                     break;
13681               }
13682
13683             if (ent == NULL)
13684               abort ();
13685             if (ent->is_indirect)
13686               ent = ent->got.ent;
13687             offp = &ent->got.offset;
13688             got = ppc64_elf_tdata (ent->owner)->got;
13689             if (got == NULL)
13690               abort ();
13691
13692             /* The offset must always be a multiple of 8.  We use the
13693                least significant bit to record whether we have already
13694                processed this entry.  */
13695             off = *offp;
13696             if ((off & 1) != 0)
13697               off &= ~1;
13698             else
13699               {
13700                 /* Generate relocs for the dynamic linker, except in
13701                    the case of TLSLD where we'll use one entry per
13702                    module.  */
13703                 asection *relgot;
13704                 bfd_boolean ifunc;
13705
13706                 *offp = off | 1;
13707                 relgot = NULL;
13708                 ifunc = (h != NULL
13709                          ? h->elf.type == STT_GNU_IFUNC
13710                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
13711                 if (ifunc)
13712                   relgot = htab->reliplt;
13713                 else if ((info->shared || indx != 0)
13714                          && (h == NULL
13715                              || (tls_type == (TLS_TLS | TLS_LD)
13716                                  && !h->elf.def_dynamic)
13717                              || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13718                              || h->elf.root.type != bfd_link_hash_undefweak))
13719                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
13720                 if (relgot != NULL)
13721                   {
13722                     outrel.r_offset = (got->output_section->vma
13723                                        + got->output_offset
13724                                        + off);
13725                     outrel.r_addend = addend;
13726                     if (tls_type & (TLS_LD | TLS_GD))
13727                       {
13728                         outrel.r_addend = 0;
13729                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
13730                         if (tls_type == (TLS_TLS | TLS_GD))
13731                           {
13732                             loc = relgot->contents;
13733                             loc += (relgot->reloc_count++
13734                                     * sizeof (Elf64_External_Rela));
13735                             bfd_elf64_swap_reloca_out (output_bfd,
13736                                                        &outrel, loc);
13737                             outrel.r_offset += 8;
13738                             outrel.r_addend = addend;
13739                             outrel.r_info
13740                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
13741                           }
13742                       }
13743                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
13744                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
13745                     else if (tls_type == (TLS_TLS | TLS_TPREL))
13746                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
13747                     else if (indx != 0)
13748                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
13749                     else
13750                       {
13751                         if (ifunc)
13752                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13753                         else
13754                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13755
13756                         /* Write the .got section contents for the sake
13757                            of prelink.  */
13758                         loc = got->contents + off;
13759                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
13760                                     loc);
13761                       }
13762
13763                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
13764                       {
13765                         outrel.r_addend += relocation;
13766                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
13767                           outrel.r_addend -= htab->elf.tls_sec->vma;
13768                       }
13769                     loc = relgot->contents;
13770                     loc += (relgot->reloc_count++
13771                             * sizeof (Elf64_External_Rela));
13772                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13773                   }
13774
13775                 /* Init the .got section contents here if we're not
13776                    emitting a reloc.  */
13777                 else
13778                   {
13779                     relocation += addend;
13780                     if (tls_type == (TLS_TLS | TLS_LD))
13781                       relocation = 1;
13782                     else if (tls_type != 0)
13783                       {
13784                         relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
13785                         if (tls_type == (TLS_TLS | TLS_TPREL))
13786                           relocation += DTP_OFFSET - TP_OFFSET;
13787
13788                         if (tls_type == (TLS_TLS | TLS_GD))
13789                           {
13790                             bfd_put_64 (output_bfd, relocation,
13791                                         got->contents + off + 8);
13792                             relocation = 1;
13793                           }
13794                       }
13795
13796                     bfd_put_64 (output_bfd, relocation,
13797                                 got->contents + off);
13798                   }
13799               }
13800
13801             if (off >= (bfd_vma) -2)
13802               abort ();
13803
13804             relocation = got->output_section->vma + got->output_offset + off;
13805             addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
13806           }
13807           break;
13808
13809         case R_PPC64_PLT16_HA:
13810         case R_PPC64_PLT16_HI:
13811         case R_PPC64_PLT16_LO:
13812         case R_PPC64_PLT32:
13813         case R_PPC64_PLT64:
13814           /* Relocation is to the entry for this symbol in the
13815              procedure linkage table.  */
13816
13817           /* Resolve a PLT reloc against a local symbol directly,
13818              without using the procedure linkage table.  */
13819           if (h == NULL)
13820             break;
13821
13822           /* It's possible that we didn't make a PLT entry for this
13823              symbol.  This happens when statically linking PIC code,
13824              or when using -Bsymbolic.  Go find a match if there is a
13825              PLT entry.  */
13826           if (htab->plt != NULL)
13827             {
13828               struct plt_entry *ent;
13829               for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
13830                 if (ent->addend == orig_rel.r_addend
13831                     && ent->plt.offset != (bfd_vma) -1)
13832                   {
13833                     relocation = (htab->plt->output_section->vma
13834                                   + htab->plt->output_offset
13835                                   + ent->plt.offset);
13836                     unresolved_reloc = FALSE;
13837                   }
13838             }
13839           break;
13840
13841         case R_PPC64_TOC:
13842           /* Relocation value is TOC base.  */
13843           relocation = TOCstart;
13844           if (r_symndx == STN_UNDEF)
13845             relocation += htab->stub_group[input_section->id].toc_off;
13846           else if (unresolved_reloc)
13847             ;
13848           else if (sec != NULL && sec->id <= htab->top_id)
13849             relocation += htab->stub_group[sec->id].toc_off;
13850           else
13851             unresolved_reloc = TRUE;
13852           goto dodyn;
13853
13854           /* TOC16 relocs.  We want the offset relative to the TOC base,
13855              which is the address of the start of the TOC plus 0x8000.
13856              The TOC consists of sections .got, .toc, .tocbss, and .plt,
13857              in this order.  */
13858         case R_PPC64_TOC16:
13859         case R_PPC64_TOC16_LO:
13860         case R_PPC64_TOC16_HI:
13861         case R_PPC64_TOC16_DS:
13862         case R_PPC64_TOC16_LO_DS:
13863         case R_PPC64_TOC16_HA:
13864           addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
13865           break;
13866
13867           /* Relocate against the beginning of the section.  */
13868         case R_PPC64_SECTOFF:
13869         case R_PPC64_SECTOFF_LO:
13870         case R_PPC64_SECTOFF_HI:
13871         case R_PPC64_SECTOFF_DS:
13872         case R_PPC64_SECTOFF_LO_DS:
13873         case R_PPC64_SECTOFF_HA:
13874           if (sec != NULL)
13875             addend -= sec->output_section->vma;
13876           break;
13877
13878         case R_PPC64_REL16:
13879         case R_PPC64_REL16_LO:
13880         case R_PPC64_REL16_HI:
13881         case R_PPC64_REL16_HA:
13882           break;
13883
13884         case R_PPC64_REL14:
13885         case R_PPC64_REL14_BRNTAKEN:
13886         case R_PPC64_REL14_BRTAKEN:
13887         case R_PPC64_REL24:
13888           break;
13889
13890         case R_PPC64_TPREL16:
13891         case R_PPC64_TPREL16_LO:
13892         case R_PPC64_TPREL16_HI:
13893         case R_PPC64_TPREL16_HA:
13894         case R_PPC64_TPREL16_DS:
13895         case R_PPC64_TPREL16_LO_DS:
13896         case R_PPC64_TPREL16_HIGH:
13897         case R_PPC64_TPREL16_HIGHA:
13898         case R_PPC64_TPREL16_HIGHER:
13899         case R_PPC64_TPREL16_HIGHERA:
13900         case R_PPC64_TPREL16_HIGHEST:
13901         case R_PPC64_TPREL16_HIGHESTA:
13902           if (h != NULL
13903               && h->elf.root.type == bfd_link_hash_undefweak
13904               && h->elf.dynindx == -1)
13905             {
13906               /* Make this relocation against an undefined weak symbol
13907                  resolve to zero.  This is really just a tweak, since
13908                  code using weak externs ought to check that they are
13909                  defined before using them.  */
13910               bfd_byte *p = contents + rel->r_offset - d_offset;
13911
13912               insn = bfd_get_32 (output_bfd, p);
13913               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
13914               if (insn != 0)
13915                 bfd_put_32 (output_bfd, insn, p);
13916               break;
13917             }
13918           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13919           if (info->shared)
13920             /* The TPREL16 relocs shouldn't really be used in shared
13921                libs as they will result in DT_TEXTREL being set, but
13922                support them anyway.  */
13923             goto dodyn;
13924           break;
13925
13926         case R_PPC64_DTPREL16:
13927         case R_PPC64_DTPREL16_LO:
13928         case R_PPC64_DTPREL16_HI:
13929         case R_PPC64_DTPREL16_HA:
13930         case R_PPC64_DTPREL16_DS:
13931         case R_PPC64_DTPREL16_LO_DS:
13932         case R_PPC64_DTPREL16_HIGH:
13933         case R_PPC64_DTPREL16_HIGHA:
13934         case R_PPC64_DTPREL16_HIGHER:
13935         case R_PPC64_DTPREL16_HIGHERA:
13936         case R_PPC64_DTPREL16_HIGHEST:
13937         case R_PPC64_DTPREL16_HIGHESTA:
13938           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13939           break;
13940
13941         case R_PPC64_DTPMOD64:
13942           relocation = 1;
13943           addend = 0;
13944           goto dodyn;
13945
13946         case R_PPC64_TPREL64:
13947           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13948           goto dodyn;
13949
13950         case R_PPC64_DTPREL64:
13951           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13952           /* Fall thru */
13953
13954           /* Relocations that may need to be propagated if this is a
13955              dynamic object.  */
13956         case R_PPC64_REL30:
13957         case R_PPC64_REL32:
13958         case R_PPC64_REL64:
13959         case R_PPC64_ADDR14:
13960         case R_PPC64_ADDR14_BRNTAKEN:
13961         case R_PPC64_ADDR14_BRTAKEN:
13962         case R_PPC64_ADDR16:
13963         case R_PPC64_ADDR16_DS:
13964         case R_PPC64_ADDR16_HA:
13965         case R_PPC64_ADDR16_HI:
13966         case R_PPC64_ADDR16_HIGH:
13967         case R_PPC64_ADDR16_HIGHA:
13968         case R_PPC64_ADDR16_HIGHER:
13969         case R_PPC64_ADDR16_HIGHERA:
13970         case R_PPC64_ADDR16_HIGHEST:
13971         case R_PPC64_ADDR16_HIGHESTA:
13972         case R_PPC64_ADDR16_LO:
13973         case R_PPC64_ADDR16_LO_DS:
13974         case R_PPC64_ADDR24:
13975         case R_PPC64_ADDR32:
13976         case R_PPC64_ADDR64:
13977         case R_PPC64_UADDR16:
13978         case R_PPC64_UADDR32:
13979         case R_PPC64_UADDR64:
13980         dodyn:
13981           if ((input_section->flags & SEC_ALLOC) == 0)
13982             break;
13983
13984           if (NO_OPD_RELOCS && is_opd)
13985             break;
13986
13987           if ((info->shared
13988                && (h == NULL
13989                    || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13990                    || h->elf.root.type != bfd_link_hash_undefweak)
13991                && (must_be_dyn_reloc (info, r_type)
13992                    || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
13993               || (ELIMINATE_COPY_RELOCS
13994                   && !info->shared
13995                   && h != NULL
13996                   && h->elf.dynindx != -1
13997                   && !h->elf.non_got_ref
13998                   && !h->elf.def_regular)
13999               || (!info->shared
14000                   && (h != NULL
14001                       ? h->elf.type == STT_GNU_IFUNC
14002                       : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
14003             {
14004               bfd_boolean skip, relocate;
14005               asection *sreloc;
14006               bfd_vma out_off;
14007
14008               /* When generating a dynamic object, these relocations
14009                  are copied into the output file to be resolved at run
14010                  time.  */
14011
14012               skip = FALSE;
14013               relocate = FALSE;
14014
14015               out_off = _bfd_elf_section_offset (output_bfd, info,
14016                                                  input_section, rel->r_offset);
14017               if (out_off == (bfd_vma) -1)
14018                 skip = TRUE;
14019               else if (out_off == (bfd_vma) -2)
14020                 skip = TRUE, relocate = TRUE;
14021               out_off += (input_section->output_section->vma
14022                           + input_section->output_offset);
14023               outrel.r_offset = out_off;
14024               outrel.r_addend = rel->r_addend;
14025
14026               /* Optimize unaligned reloc use.  */
14027               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14028                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14029                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14030               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14031                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14032                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14033               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14034                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14035                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14036
14037               if (skip)
14038                 memset (&outrel, 0, sizeof outrel);
14039               else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
14040                        && !is_opd
14041                        && r_type != R_PPC64_TOC)
14042                 {
14043                   BFD_ASSERT (h->elf.dynindx != -1);
14044                   outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14045                 }
14046               else
14047                 {
14048                   /* This symbol is local, or marked to become local,
14049                      or this is an opd section reloc which must point
14050                      at a local function.  */
14051                   outrel.r_addend += relocation;
14052                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14053                     {
14054                       if (is_opd && h != NULL)
14055                         {
14056                           /* Lie about opd entries.  This case occurs
14057                              when building shared libraries and we
14058                              reference a function in another shared
14059                              lib.  The same thing happens for a weak
14060                              definition in an application that's
14061                              overridden by a strong definition in a
14062                              shared lib.  (I believe this is a generic
14063                              bug in binutils handling of weak syms.)
14064                              In these cases we won't use the opd
14065                              entry in this lib.  */
14066                           unresolved_reloc = FALSE;
14067                         }
14068                       if (!is_opd
14069                           && r_type == R_PPC64_ADDR64
14070                           && (h != NULL
14071                               ? h->elf.type == STT_GNU_IFUNC
14072                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14073                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14074                       else
14075                         {
14076                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14077
14078                           /* We need to relocate .opd contents for ld.so.
14079                              Prelink also wants simple and consistent rules
14080                              for relocs.  This make all RELATIVE relocs have
14081                              *r_offset equal to r_addend.  */
14082                           relocate = TRUE;
14083                         }
14084                     }
14085                   else
14086                     {
14087                       long indx = 0;
14088
14089                       if (h != NULL
14090                           ? h->elf.type == STT_GNU_IFUNC
14091                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14092                         {
14093                           info->callbacks->einfo
14094                             (_("%P: %H: %s for indirect "
14095                                "function `%T' unsupported\n"),
14096                              input_bfd, input_section, rel->r_offset,
14097                              ppc64_elf_howto_table[r_type]->name,
14098                              sym_name);
14099                           ret = FALSE;
14100                         }
14101                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14102                         ;
14103                       else if (sec == NULL || sec->owner == NULL)
14104                         {
14105                           bfd_set_error (bfd_error_bad_value);
14106                           return FALSE;
14107                         }
14108                       else
14109                         {
14110                           asection *osec;
14111
14112                           osec = sec->output_section;
14113                           indx = elf_section_data (osec)->dynindx;
14114
14115                           if (indx == 0)
14116                             {
14117                               if ((osec->flags & SEC_READONLY) == 0
14118                                   && htab->elf.data_index_section != NULL)
14119                                 osec = htab->elf.data_index_section;
14120                               else
14121                                 osec = htab->elf.text_index_section;
14122                               indx = elf_section_data (osec)->dynindx;
14123                             }
14124                           BFD_ASSERT (indx != 0);
14125
14126                           /* We are turning this relocation into one
14127                              against a section symbol, so subtract out
14128                              the output section's address but not the
14129                              offset of the input section in the output
14130                              section.  */
14131                           outrel.r_addend -= osec->vma;
14132                         }
14133
14134                       outrel.r_info = ELF64_R_INFO (indx, r_type);
14135                     }
14136                 }
14137
14138               sreloc = elf_section_data (input_section)->sreloc;
14139               if (h != NULL
14140                   ? h->elf.type == STT_GNU_IFUNC
14141                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14142                 sreloc = htab->reliplt;
14143               if (sreloc == NULL)
14144                 abort ();
14145
14146               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14147                   >= sreloc->size)
14148                 abort ();
14149               loc = sreloc->contents;
14150               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14151               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14152
14153               /* If this reloc is against an external symbol, it will
14154                  be computed at runtime, so there's no need to do
14155                  anything now.  However, for the sake of prelink ensure
14156                  that the section contents are a known value.  */
14157               if (! relocate)
14158                 {
14159                   unresolved_reloc = FALSE;
14160                   /* The value chosen here is quite arbitrary as ld.so
14161                      ignores section contents except for the special
14162                      case of .opd where the contents might be accessed
14163                      before relocation.  Choose zero, as that won't
14164                      cause reloc overflow.  */
14165                   relocation = 0;
14166                   addend = 0;
14167                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14168                      to improve backward compatibility with older
14169                      versions of ld.  */
14170                   if (r_type == R_PPC64_ADDR64)
14171                     addend = outrel.r_addend;
14172                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
14173                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
14174                     addend = (input_section->output_section->vma
14175                               + input_section->output_offset
14176                               + rel->r_offset);
14177                 }
14178             }
14179           break;
14180
14181         case R_PPC64_COPY:
14182         case R_PPC64_GLOB_DAT:
14183         case R_PPC64_JMP_SLOT:
14184         case R_PPC64_JMP_IREL:
14185         case R_PPC64_RELATIVE:
14186           /* We shouldn't ever see these dynamic relocs in relocatable
14187              files.  */
14188           /* Fall through.  */
14189
14190         case R_PPC64_PLTGOT16:
14191         case R_PPC64_PLTGOT16_DS:
14192         case R_PPC64_PLTGOT16_HA:
14193         case R_PPC64_PLTGOT16_HI:
14194         case R_PPC64_PLTGOT16_LO:
14195         case R_PPC64_PLTGOT16_LO_DS:
14196         case R_PPC64_PLTREL32:
14197         case R_PPC64_PLTREL64:
14198           /* These ones haven't been implemented yet.  */
14199
14200           info->callbacks->einfo
14201             (_("%P: %B: %s is not supported for `%T'\n"),
14202              input_bfd,
14203              ppc64_elf_howto_table[r_type]->name, sym_name);
14204
14205           bfd_set_error (bfd_error_invalid_operation);
14206           ret = FALSE;
14207           continue;
14208         }
14209
14210       /* Multi-instruction sequences that access the TOC can be
14211          optimized, eg. addis ra,r2,0; addi rb,ra,x;
14212          to             nop;           addi rb,r2,x;  */
14213       switch (r_type)
14214         {
14215         default:
14216           break;
14217
14218         case R_PPC64_GOT_TLSLD16_HI:
14219         case R_PPC64_GOT_TLSGD16_HI:
14220         case R_PPC64_GOT_TPREL16_HI:
14221         case R_PPC64_GOT_DTPREL16_HI:
14222         case R_PPC64_GOT16_HI:
14223         case R_PPC64_TOC16_HI:
14224           /* These relocs would only be useful if building up an
14225              offset to later add to r2, perhaps in an indexed
14226              addressing mode instruction.  Don't try to optimize.
14227              Unfortunately, the possibility of someone building up an
14228              offset like this or even with the HA relocs, means that
14229              we need to check the high insn when optimizing the low
14230              insn.  */
14231           break;
14232
14233         case R_PPC64_GOT_TLSLD16_HA:
14234         case R_PPC64_GOT_TLSGD16_HA:
14235         case R_PPC64_GOT_TPREL16_HA:
14236         case R_PPC64_GOT_DTPREL16_HA:
14237         case R_PPC64_GOT16_HA:
14238         case R_PPC64_TOC16_HA:
14239           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14240               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14241             {
14242               bfd_byte *p = contents + (rel->r_offset & ~3);
14243               bfd_put_32 (input_bfd, NOP, p);
14244             }
14245           break;
14246
14247         case R_PPC64_GOT_TLSLD16_LO:
14248         case R_PPC64_GOT_TLSGD16_LO:
14249         case R_PPC64_GOT_TPREL16_LO_DS:
14250         case R_PPC64_GOT_DTPREL16_LO_DS:
14251         case R_PPC64_GOT16_LO:
14252         case R_PPC64_GOT16_LO_DS:
14253         case R_PPC64_TOC16_LO:
14254         case R_PPC64_TOC16_LO_DS:
14255           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14256               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14257             {
14258               bfd_byte *p = contents + (rel->r_offset & ~3);
14259               insn = bfd_get_32 (input_bfd, p);
14260               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14261                 {
14262                   /* Transform addic to addi when we change reg.  */
14263                   insn &= ~((0x3f << 26) | (0x1f << 16));
14264                   insn |= (14u << 26) | (2 << 16);
14265                 }
14266               else
14267                 {
14268                   insn &= ~(0x1f << 16);
14269                   insn |= 2 << 16;
14270                 }
14271               bfd_put_32 (input_bfd, insn, p);
14272             }
14273           break;
14274         }
14275
14276       /* Do any further special processing.  */
14277       switch (r_type)
14278         {
14279         default:
14280           break;
14281
14282         case R_PPC64_REL16_HA:
14283         case R_PPC64_ADDR16_HA:
14284         case R_PPC64_ADDR16_HIGHA:
14285         case R_PPC64_ADDR16_HIGHERA:
14286         case R_PPC64_ADDR16_HIGHESTA:
14287         case R_PPC64_TOC16_HA:
14288         case R_PPC64_SECTOFF_HA:
14289         case R_PPC64_TPREL16_HA:
14290         case R_PPC64_TPREL16_HIGHA:
14291         case R_PPC64_TPREL16_HIGHERA:
14292         case R_PPC64_TPREL16_HIGHESTA:
14293         case R_PPC64_DTPREL16_HA:
14294         case R_PPC64_DTPREL16_HIGHA:
14295         case R_PPC64_DTPREL16_HIGHERA:
14296         case R_PPC64_DTPREL16_HIGHESTA:
14297           /* It's just possible that this symbol is a weak symbol
14298              that's not actually defined anywhere. In that case,
14299              'sec' would be NULL, and we should leave the symbol
14300              alone (it will be set to zero elsewhere in the link).  */
14301           if (sec == NULL)
14302             break;
14303           /* Fall thru */
14304
14305         case R_PPC64_GOT16_HA:
14306         case R_PPC64_PLTGOT16_HA:
14307         case R_PPC64_PLT16_HA:
14308         case R_PPC64_GOT_TLSGD16_HA:
14309         case R_PPC64_GOT_TLSLD16_HA:
14310         case R_PPC64_GOT_TPREL16_HA:
14311         case R_PPC64_GOT_DTPREL16_HA:
14312           /* Add 0x10000 if sign bit in 0:15 is set.
14313              Bits 0:15 are not used.  */
14314           addend += 0x8000;
14315           break;
14316
14317         case R_PPC64_ADDR16_DS:
14318         case R_PPC64_ADDR16_LO_DS:
14319         case R_PPC64_GOT16_DS:
14320         case R_PPC64_GOT16_LO_DS:
14321         case R_PPC64_PLT16_LO_DS:
14322         case R_PPC64_SECTOFF_DS:
14323         case R_PPC64_SECTOFF_LO_DS:
14324         case R_PPC64_TOC16_DS:
14325         case R_PPC64_TOC16_LO_DS:
14326         case R_PPC64_PLTGOT16_DS:
14327         case R_PPC64_PLTGOT16_LO_DS:
14328         case R_PPC64_GOT_TPREL16_DS:
14329         case R_PPC64_GOT_TPREL16_LO_DS:
14330         case R_PPC64_GOT_DTPREL16_DS:
14331         case R_PPC64_GOT_DTPREL16_LO_DS:
14332         case R_PPC64_TPREL16_DS:
14333         case R_PPC64_TPREL16_LO_DS:
14334         case R_PPC64_DTPREL16_DS:
14335         case R_PPC64_DTPREL16_LO_DS:
14336           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14337           mask = 3;
14338           /* If this reloc is against an lq insn, then the value must be
14339              a multiple of 16.  This is somewhat of a hack, but the
14340              "correct" way to do this by defining _DQ forms of all the
14341              _DS relocs bloats all reloc switches in this file.  It
14342              doesn't seem to make much sense to use any of these relocs
14343              in data, so testing the insn should be safe.  */
14344           if ((insn & (0x3f << 26)) == (56u << 26))
14345             mask = 15;
14346           if (((relocation + addend) & mask) != 0)
14347             {
14348               info->callbacks->einfo
14349                 (_("%P: %H: error: %s not a multiple of %u\n"),
14350                  input_bfd, input_section, rel->r_offset,
14351                  ppc64_elf_howto_table[r_type]->name,
14352                  mask + 1);
14353               bfd_set_error (bfd_error_bad_value);
14354               ret = FALSE;
14355               continue;
14356             }
14357           break;
14358         }
14359
14360       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
14361          because such sections are not SEC_ALLOC and thus ld.so will
14362          not process them.  */
14363       if (unresolved_reloc
14364           && !((input_section->flags & SEC_DEBUGGING) != 0
14365                && h->elf.def_dynamic)
14366           && _bfd_elf_section_offset (output_bfd, info, input_section,
14367                                       rel->r_offset) != (bfd_vma) -1)
14368         {
14369           info->callbacks->einfo
14370             (_("%P: %H: unresolvable %s against `%T'\n"),
14371              input_bfd, input_section, rel->r_offset,
14372              ppc64_elf_howto_table[(int) r_type]->name,
14373              h->elf.root.root.string);
14374           ret = FALSE;
14375         }
14376
14377       r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
14378                                     input_bfd,
14379                                     input_section,
14380                                     contents,
14381                                     rel->r_offset,
14382                                     relocation,
14383                                     addend);
14384
14385       if (r != bfd_reloc_ok)
14386         {
14387           char *more_info = NULL;
14388           const char *reloc_name = ppc64_elf_howto_table[r_type]->name;
14389
14390           if (reloc_dest != DEST_NORMAL)
14391             {
14392               more_info = bfd_malloc (strlen (reloc_name) + 8);
14393               if (more_info != NULL)
14394                 {
14395                   strcpy (more_info, reloc_name);
14396                   strcat (more_info, (reloc_dest == DEST_OPD
14397                                       ? " (OPD)" : " (stub)"));
14398                   reloc_name = more_info;
14399                 }
14400             }
14401
14402           if (r == bfd_reloc_overflow)
14403             {
14404               if (warned)
14405                 continue;
14406               if (h != NULL
14407                   && h->elf.root.type == bfd_link_hash_undefweak
14408                   && ppc64_elf_howto_table[r_type]->pc_relative)
14409                 {
14410                   /* Assume this is a call protected by other code that
14411                      detects the symbol is undefined.  If this is the case,
14412                      we can safely ignore the overflow.  If not, the
14413                      program is hosed anyway, and a little warning isn't
14414                      going to help.  */
14415
14416                   continue;
14417                 }
14418
14419               if (!((*info->callbacks->reloc_overflow)
14420                     (info, &h->elf.root, sym_name,
14421                      reloc_name, orig_rel.r_addend,
14422                      input_bfd, input_section, rel->r_offset)))
14423                 return FALSE;
14424             }
14425           else
14426             {
14427               info->callbacks->einfo
14428                 (_("%P: %H: %s against `%T': error %d\n"),
14429                  input_bfd, input_section, rel->r_offset,
14430                  reloc_name, sym_name, (int) r);
14431               ret = FALSE;
14432             }
14433           if (more_info != NULL)
14434             free (more_info);
14435         }
14436     }
14437
14438   /* If we're emitting relocations, then shortly after this function
14439      returns, reloc offsets and addends for this section will be
14440      adjusted.  Worse, reloc symbol indices will be for the output
14441      file rather than the input.  Save a copy of the relocs for
14442      opd_entry_value.  */
14443   if (is_opd && (info->emitrelocations || info->relocatable))
14444     {
14445       bfd_size_type amt;
14446       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
14447       rel = bfd_alloc (input_bfd, amt);
14448       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
14449       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
14450       if (rel == NULL)
14451         return FALSE;
14452       memcpy (rel, relocs, amt);
14453     }
14454   return ret;
14455 }
14456
14457 /* Adjust the value of any local symbols in opd sections.  */
14458
14459 static int
14460 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
14461                               const char *name ATTRIBUTE_UNUSED,
14462                               Elf_Internal_Sym *elfsym,
14463                               asection *input_sec,
14464                               struct elf_link_hash_entry *h)
14465 {
14466   struct _opd_sec_data *opd;
14467   long adjust;
14468   bfd_vma value;
14469
14470   if (h != NULL)
14471     return 1;
14472
14473   opd = get_opd_info (input_sec);
14474   if (opd == NULL || opd->adjust == NULL)
14475     return 1;
14476
14477   value = elfsym->st_value - input_sec->output_offset;
14478   if (!info->relocatable)
14479     value -= input_sec->output_section->vma;
14480
14481   adjust = opd->adjust[value / 8];
14482   if (adjust == -1)
14483     return 2;
14484
14485   elfsym->st_value += adjust;
14486   return 1;
14487 }
14488
14489 /* Finish up dynamic symbol handling.  We set the contents of various
14490    dynamic sections here.  */
14491
14492 static bfd_boolean
14493 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
14494                                  struct bfd_link_info *info,
14495                                  struct elf_link_hash_entry *h,
14496                                  Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
14497 {
14498   struct ppc_link_hash_table *htab;
14499   struct plt_entry *ent;
14500   Elf_Internal_Rela rela;
14501   bfd_byte *loc;
14502
14503   htab = ppc_hash_table (info);
14504   if (htab == NULL)
14505     return FALSE;
14506
14507   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14508     if (ent->plt.offset != (bfd_vma) -1)
14509       {
14510         /* This symbol has an entry in the procedure linkage
14511            table.  Set it up.  */
14512         if (!htab->elf.dynamic_sections_created
14513             || h->dynindx == -1)
14514           {
14515             BFD_ASSERT (h->type == STT_GNU_IFUNC
14516                         && h->def_regular
14517                         && (h->root.type == bfd_link_hash_defined
14518                             || h->root.type == bfd_link_hash_defweak));
14519             rela.r_offset = (htab->iplt->output_section->vma
14520                              + htab->iplt->output_offset
14521                              + ent->plt.offset);
14522             if (htab->opd_abi)
14523               rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14524             else
14525               rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14526             rela.r_addend = (h->root.u.def.value
14527                              + h->root.u.def.section->output_offset
14528                              + h->root.u.def.section->output_section->vma
14529                              + ent->addend);
14530             loc = (htab->reliplt->contents
14531                    + (htab->reliplt->reloc_count++
14532                       * sizeof (Elf64_External_Rela)));
14533           }
14534         else
14535           {
14536             rela.r_offset = (htab->plt->output_section->vma
14537                              + htab->plt->output_offset
14538                              + ent->plt.offset);
14539             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14540             rela.r_addend = ent->addend;
14541             loc = (htab->relplt->contents
14542                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14543                       / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14544           }
14545         bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
14546       }
14547
14548   if (h->needs_copy)
14549     {
14550       /* This symbol needs a copy reloc.  Set it up.  */
14551
14552       if (h->dynindx == -1
14553           || (h->root.type != bfd_link_hash_defined
14554               && h->root.type != bfd_link_hash_defweak)
14555           || htab->relbss == NULL)
14556         abort ();
14557
14558       rela.r_offset = (h->root.u.def.value
14559                        + h->root.u.def.section->output_section->vma
14560                        + h->root.u.def.section->output_offset);
14561       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
14562       rela.r_addend = 0;
14563       loc = htab->relbss->contents;
14564       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
14565       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
14566     }
14567
14568   return TRUE;
14569 }
14570
14571 /* Used to decide how to sort relocs in an optimal manner for the
14572    dynamic linker, before writing them out.  */
14573
14574 static enum elf_reloc_type_class
14575 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
14576                             const asection *rel_sec,
14577                             const Elf_Internal_Rela *rela)
14578 {
14579   enum elf_ppc64_reloc_type r_type;
14580   struct ppc_link_hash_table *htab = ppc_hash_table (info);
14581
14582   if (rel_sec == htab->reliplt)
14583     return reloc_class_ifunc;
14584
14585   r_type = ELF64_R_TYPE (rela->r_info);
14586   switch (r_type)
14587     {
14588     case R_PPC64_RELATIVE:
14589       return reloc_class_relative;
14590     case R_PPC64_JMP_SLOT:
14591       return reloc_class_plt;
14592     case R_PPC64_COPY:
14593       return reloc_class_copy;
14594     default:
14595       return reloc_class_normal;
14596     }
14597 }
14598
14599 /* Finish up the dynamic sections.  */
14600
14601 static bfd_boolean
14602 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
14603                                    struct bfd_link_info *info)
14604 {
14605   struct ppc_link_hash_table *htab;
14606   bfd *dynobj;
14607   asection *sdyn;
14608
14609   htab = ppc_hash_table (info);
14610   if (htab == NULL)
14611     return FALSE;
14612
14613   dynobj = htab->elf.dynobj;
14614   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
14615
14616   if (htab->elf.dynamic_sections_created)
14617     {
14618       Elf64_External_Dyn *dyncon, *dynconend;
14619
14620       if (sdyn == NULL || htab->got == NULL)
14621         abort ();
14622
14623       dyncon = (Elf64_External_Dyn *) sdyn->contents;
14624       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
14625       for (; dyncon < dynconend; dyncon++)
14626         {
14627           Elf_Internal_Dyn dyn;
14628           asection *s;
14629
14630           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
14631
14632           switch (dyn.d_tag)
14633             {
14634             default:
14635               continue;
14636
14637             case DT_PPC64_GLINK:
14638               s = htab->glink;
14639               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14640               /* We stupidly defined DT_PPC64_GLINK to be the start
14641                  of glink rather than the first entry point, which is
14642                  what ld.so needs, and now have a bigger stub to
14643                  support automatic multiple TOCs.  */
14644               dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
14645               break;
14646
14647             case DT_PPC64_OPD:
14648               s = bfd_get_section_by_name (output_bfd, ".opd");
14649               if (s == NULL)
14650                 continue;
14651               dyn.d_un.d_ptr = s->vma;
14652               break;
14653
14654             case DT_PPC64_OPDSZ:
14655               s = bfd_get_section_by_name (output_bfd, ".opd");
14656               if (s == NULL)
14657                 continue;
14658               dyn.d_un.d_val = s->size;
14659               break;
14660
14661             case DT_PLTGOT:
14662               s = htab->plt;
14663               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14664               break;
14665
14666             case DT_JMPREL:
14667               s = htab->relplt;
14668               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14669               break;
14670
14671             case DT_PLTRELSZ:
14672               dyn.d_un.d_val = htab->relplt->size;
14673               break;
14674
14675             case DT_RELASZ:
14676               /* Don't count procedure linkage table relocs in the
14677                  overall reloc count.  */
14678               s = htab->relplt;
14679               if (s == NULL)
14680                 continue;
14681               dyn.d_un.d_val -= s->size;
14682               break;
14683
14684             case DT_RELA:
14685               /* We may not be using the standard ELF linker script.
14686                  If .rela.plt is the first .rela section, we adjust
14687                  DT_RELA to not include it.  */
14688               s = htab->relplt;
14689               if (s == NULL)
14690                 continue;
14691               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
14692                 continue;
14693               dyn.d_un.d_ptr += s->size;
14694               break;
14695             }
14696
14697           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
14698         }
14699     }
14700
14701   if (htab->got != NULL && htab->got->size != 0)
14702     {
14703       /* Fill in the first entry in the global offset table.
14704          We use it to hold the link-time TOCbase.  */
14705       bfd_put_64 (output_bfd,
14706                   elf_gp (output_bfd) + TOC_BASE_OFF,
14707                   htab->got->contents);
14708
14709       /* Set .got entry size.  */
14710       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
14711     }
14712
14713   if (htab->plt != NULL && htab->plt->size != 0)
14714     {
14715       /* Set .plt entry size.  */
14716       elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
14717         = PLT_ENTRY_SIZE (htab);
14718     }
14719
14720   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
14721      brlt ourselves if emitrelocations.  */
14722   if (htab->brlt != NULL
14723       && htab->brlt->reloc_count != 0
14724       && !_bfd_elf_link_output_relocs (output_bfd,
14725                                        htab->brlt,
14726                                        elf_section_data (htab->brlt)->rela.hdr,
14727                                        elf_section_data (htab->brlt)->relocs,
14728                                        NULL))
14729     return FALSE;
14730
14731   if (htab->glink != NULL
14732       && htab->glink->reloc_count != 0
14733       && !_bfd_elf_link_output_relocs (output_bfd,
14734                                        htab->glink,
14735                                        elf_section_data (htab->glink)->rela.hdr,
14736                                        elf_section_data (htab->glink)->relocs,
14737                                        NULL))
14738     return FALSE;
14739
14740
14741   if (htab->glink_eh_frame != NULL
14742       && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
14743       && !_bfd_elf_write_section_eh_frame (output_bfd, info,
14744                                            htab->glink_eh_frame,
14745                                            htab->glink_eh_frame->contents))
14746     return FALSE;
14747
14748   /* We need to handle writing out multiple GOT sections ourselves,
14749      since we didn't add them to DYNOBJ.  We know dynobj is the first
14750      bfd.  */
14751   while ((dynobj = dynobj->link_next) != NULL)
14752     {
14753       asection *s;
14754
14755       if (!is_ppc64_elf (dynobj))
14756         continue;
14757
14758       s = ppc64_elf_tdata (dynobj)->got;
14759       if (s != NULL
14760           && s->size != 0
14761           && s->output_section != bfd_abs_section_ptr
14762           && !bfd_set_section_contents (output_bfd, s->output_section,
14763                                         s->contents, s->output_offset,
14764                                         s->size))
14765         return FALSE;
14766       s = ppc64_elf_tdata (dynobj)->relgot;
14767       if (s != NULL
14768           && s->size != 0
14769           && s->output_section != bfd_abs_section_ptr
14770           && !bfd_set_section_contents (output_bfd, s->output_section,
14771                                         s->contents, s->output_offset,
14772                                         s->size))
14773         return FALSE;
14774     }
14775
14776   return TRUE;
14777 }
14778
14779 #include "elf64-target.h"
14780
14781 /* FreeBSD support */
14782
14783 #undef  TARGET_LITTLE_SYM
14784 #undef  TARGET_LITTLE_NAME
14785
14786 #undef  TARGET_BIG_SYM
14787 #define TARGET_BIG_SYM  bfd_elf64_powerpc_freebsd_vec
14788 #undef  TARGET_BIG_NAME
14789 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
14790
14791 #undef  ELF_OSABI
14792 #define ELF_OSABI       ELFOSABI_FREEBSD
14793
14794 #undef  elf64_bed
14795 #define elf64_bed       elf64_powerpc_fbsd_bed
14796
14797 #include "elf64-target.h"
14798