Support ELFv2 stack frame.
[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 /* Offsets to some stack save slots.  */
134 #define STK_LR 16
135 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
136 /* This one is dodgy.  ABIv2 does not have a linker word, so use the
137    CR save slot.  Used only by optimised __tls_get_addr call stub,
138    relying on __tls_get_addr_opt not saving CR..  */
139 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
140
141 /* TOC base pointers offset from start of TOC.  */
142 #define TOC_BASE_OFF    0x8000
143
144 /* Offset of tp and dtp pointers from start of TLS block.  */
145 #define TP_OFFSET       0x7000
146 #define DTP_OFFSET      0x8000
147
148 /* .plt call stub instructions.  The normal stub is like this, but
149    sometimes the .plt entry crosses a 64k boundary and we need to
150    insert an addi to adjust r11.  */
151 #define STD_R2_0R1      0xf8410000      /* std   %r2,0+40(%r1)       */
152 #define ADDIS_R11_R2    0x3d620000      /* addis %r11,%r2,xxx@ha     */
153 #define LD_R12_0R11     0xe98b0000      /* ld    %r12,xxx+0@l(%r11)  */
154 #define MTCTR_R12       0x7d8903a6      /* mtctr %r12                */
155 #define LD_R2_0R11      0xe84b0000      /* ld    %r2,xxx+8@l(%r11)   */
156 #define LD_R11_0R11     0xe96b0000      /* ld    %r11,xxx+16@l(%r11) */
157 #define BCTR            0x4e800420      /* bctr                      */
158
159 #define ADDI_R11_R11    0x396b0000      /* addi %r11,%r11,off@l  */
160 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
161 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
162
163 #define XOR_R2_R12_R12  0x7d826278      /* xor   %r2,%r12,%r12   */
164 #define ADD_R11_R11_R2  0x7d6b1214      /* add   %r11,%r11,%r2   */
165 #define XOR_R11_R12_R12 0x7d8b6278      /* xor   %r11,%r12,%r12  */
166 #define ADD_R2_R2_R11   0x7c425a14      /* add   %r2,%r2,%r11    */
167 #define CMPLDI_R2_0     0x28220000      /* cmpldi %r2,0          */
168 #define BNECTR          0x4ca20420      /* bnectr+               */
169 #define BNECTR_P4       0x4ce20420      /* bnectr+               */
170
171 #define LD_R12_0R2      0xe9820000      /* ld    %r12,xxx+0(%r2) */
172 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
173 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
174
175 #define LD_R2_0R1       0xe8410000      /* ld    %r2,0(%r1)      */
176
177 /* glink call stub instructions.  We enter with the index in R0.  */
178 #define GLINK_CALL_STUB_SIZE (16*4)
179                                         /* 0:                           */
180                                         /*  .quad plt0-1f               */
181                                         /* __glink:                     */
182 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
183 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
184                                         /* 1:                           */
185 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
186                                         /*  ld %2,(0b-1b)(%11)          */
187 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
188 #define ADD_R11_R2_R11  0x7d625a14      /*  add %11,%2,%11              */
189                                         /*  ld %12,0(%11)               */
190                                         /*  ld %2,8(%11)                */
191                                         /*  mtctr %12                   */
192                                         /*  ld %11,16(%11)              */
193                                         /*  bctr                        */
194 #define MFLR_R0         0x7c0802a6      /*  mflr %r0                    */
195 #define MTLR_R0         0x7c0803a6      /*  mtlr %r0                    */
196 #define SUB_R12_R12_R11 0x7d8b6050      /*  subf %r12,%r11,%r12         */
197 #define ADDI_R0_R12     0x380c0000      /*  addi %r0,%r12,0             */
198 #define SRDI_R0_R0_2    0x7800f082      /*  rldicl %r0,%r0,62,2         */
199
200 /* Pad with this.  */
201 #define NOP             0x60000000
202
203 /* Some other nops.  */
204 #define CROR_151515     0x4def7b82
205 #define CROR_313131     0x4ffffb82
206
207 /* .glink entries for the first 32k functions are two instructions.  */
208 #define LI_R0_0         0x38000000      /* li    %r0,0          */
209 #define B_DOT           0x48000000      /* b     .              */
210
211 /* After that, we need two instructions to load the index, followed by
212    a branch.  */
213 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
214 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
215
216 /* Instructions used by the save and restore reg functions.  */
217 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
218 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
219 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
220 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
221 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
222 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
223 #define LI_R12_0        0x39800000      /* li    %r12,0         */
224 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
225 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
226 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
227 #define BLR             0x4e800020      /* blr                  */
228
229 /* Since .opd is an array of descriptors and each entry will end up
230    with identical R_PPC64_RELATIVE relocs, there is really no need to
231    propagate .opd relocs;  The dynamic linker should be taught to
232    relocate .opd without reloc entries.  */
233 #ifndef NO_OPD_RELOCS
234 #define NO_OPD_RELOCS 0
235 #endif
236 \f
237 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
238
239 /* Relocation HOWTO's.  */
240 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
241
242 static reloc_howto_type ppc64_elf_howto_raw[] = {
243   /* This reloc does nothing.  */
244   HOWTO (R_PPC64_NONE,          /* type */
245          0,                     /* rightshift */
246          2,                     /* size (0 = byte, 1 = short, 2 = long) */
247          32,                    /* bitsize */
248          FALSE,                 /* pc_relative */
249          0,                     /* bitpos */
250          complain_overflow_dont, /* complain_on_overflow */
251          bfd_elf_generic_reloc, /* special_function */
252          "R_PPC64_NONE",        /* name */
253          FALSE,                 /* partial_inplace */
254          0,                     /* src_mask */
255          0,                     /* dst_mask */
256          FALSE),                /* pcrel_offset */
257
258   /* A standard 32 bit relocation.  */
259   HOWTO (R_PPC64_ADDR32,        /* type */
260          0,                     /* rightshift */
261          2,                     /* size (0 = byte, 1 = short, 2 = long) */
262          32,                    /* bitsize */
263          FALSE,                 /* pc_relative */
264          0,                     /* bitpos */
265          complain_overflow_bitfield, /* complain_on_overflow */
266          bfd_elf_generic_reloc, /* special_function */
267          "R_PPC64_ADDR32",      /* name */
268          FALSE,                 /* partial_inplace */
269          0,                     /* src_mask */
270          0xffffffff,            /* dst_mask */
271          FALSE),                /* pcrel_offset */
272
273   /* An absolute 26 bit branch; the lower two bits must be zero.
274      FIXME: we don't check that, we just clear them.  */
275   HOWTO (R_PPC64_ADDR24,        /* type */
276          0,                     /* rightshift */
277          2,                     /* size (0 = byte, 1 = short, 2 = long) */
278          26,                    /* bitsize */
279          FALSE,                 /* pc_relative */
280          0,                     /* bitpos */
281          complain_overflow_bitfield, /* complain_on_overflow */
282          bfd_elf_generic_reloc, /* special_function */
283          "R_PPC64_ADDR24",      /* name */
284          FALSE,                 /* partial_inplace */
285          0,                     /* src_mask */
286          0x03fffffc,            /* dst_mask */
287          FALSE),                /* pcrel_offset */
288
289   /* A standard 16 bit relocation.  */
290   HOWTO (R_PPC64_ADDR16,        /* type */
291          0,                     /* rightshift */
292          1,                     /* size (0 = byte, 1 = short, 2 = long) */
293          16,                    /* bitsize */
294          FALSE,                 /* pc_relative */
295          0,                     /* bitpos */
296          complain_overflow_bitfield, /* complain_on_overflow */
297          bfd_elf_generic_reloc, /* special_function */
298          "R_PPC64_ADDR16",      /* name */
299          FALSE,                 /* partial_inplace */
300          0,                     /* src_mask */
301          0xffff,                /* dst_mask */
302          FALSE),                /* pcrel_offset */
303
304   /* A 16 bit relocation without overflow.  */
305   HOWTO (R_PPC64_ADDR16_LO,     /* type */
306          0,                     /* rightshift */
307          1,                     /* size (0 = byte, 1 = short, 2 = long) */
308          16,                    /* bitsize */
309          FALSE,                 /* pc_relative */
310          0,                     /* bitpos */
311          complain_overflow_dont,/* complain_on_overflow */
312          bfd_elf_generic_reloc, /* special_function */
313          "R_PPC64_ADDR16_LO",   /* name */
314          FALSE,                 /* partial_inplace */
315          0,                     /* src_mask */
316          0xffff,                /* dst_mask */
317          FALSE),                /* pcrel_offset */
318
319   /* Bits 16-31 of an address.  */
320   HOWTO (R_PPC64_ADDR16_HI,     /* type */
321          16,                    /* rightshift */
322          1,                     /* size (0 = byte, 1 = short, 2 = long) */
323          16,                    /* bitsize */
324          FALSE,                 /* pc_relative */
325          0,                     /* bitpos */
326          complain_overflow_signed, /* complain_on_overflow */
327          bfd_elf_generic_reloc, /* special_function */
328          "R_PPC64_ADDR16_HI",   /* name */
329          FALSE,                 /* partial_inplace */
330          0,                     /* src_mask */
331          0xffff,                /* dst_mask */
332          FALSE),                /* pcrel_offset */
333
334   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
335      bits, treated as a signed number, is negative.  */
336   HOWTO (R_PPC64_ADDR16_HA,     /* type */
337          16,                    /* rightshift */
338          1,                     /* size (0 = byte, 1 = short, 2 = long) */
339          16,                    /* bitsize */
340          FALSE,                 /* pc_relative */
341          0,                     /* bitpos */
342          complain_overflow_signed, /* complain_on_overflow */
343          ppc64_elf_ha_reloc,    /* special_function */
344          "R_PPC64_ADDR16_HA",   /* name */
345          FALSE,                 /* partial_inplace */
346          0,                     /* src_mask */
347          0xffff,                /* dst_mask */
348          FALSE),                /* pcrel_offset */
349
350   /* An absolute 16 bit branch; the lower two bits must be zero.
351      FIXME: we don't check that, we just clear them.  */
352   HOWTO (R_PPC64_ADDR14,        /* type */
353          0,                     /* rightshift */
354          2,                     /* size (0 = byte, 1 = short, 2 = long) */
355          16,                    /* bitsize */
356          FALSE,                 /* pc_relative */
357          0,                     /* bitpos */
358          complain_overflow_bitfield, /* complain_on_overflow */
359          ppc64_elf_branch_reloc, /* special_function */
360          "R_PPC64_ADDR14",      /* name */
361          FALSE,                 /* partial_inplace */
362          0,                     /* src_mask */
363          0x0000fffc,            /* dst_mask */
364          FALSE),                /* pcrel_offset */
365
366   /* An absolute 16 bit branch, for which bit 10 should be set to
367      indicate that the branch is expected to be taken.  The lower two
368      bits must be zero.  */
369   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
370          0,                     /* rightshift */
371          2,                     /* size (0 = byte, 1 = short, 2 = long) */
372          16,                    /* bitsize */
373          FALSE,                 /* pc_relative */
374          0,                     /* bitpos */
375          complain_overflow_bitfield, /* complain_on_overflow */
376          ppc64_elf_brtaken_reloc, /* special_function */
377          "R_PPC64_ADDR14_BRTAKEN",/* name */
378          FALSE,                 /* partial_inplace */
379          0,                     /* src_mask */
380          0x0000fffc,            /* dst_mask */
381          FALSE),                /* pcrel_offset */
382
383   /* An absolute 16 bit branch, for which bit 10 should be set to
384      indicate that the branch is not expected to be taken.  The lower
385      two bits must be zero.  */
386   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
387          0,                     /* rightshift */
388          2,                     /* size (0 = byte, 1 = short, 2 = long) */
389          16,                    /* bitsize */
390          FALSE,                 /* pc_relative */
391          0,                     /* bitpos */
392          complain_overflow_bitfield, /* complain_on_overflow */
393          ppc64_elf_brtaken_reloc, /* special_function */
394          "R_PPC64_ADDR14_BRNTAKEN",/* name */
395          FALSE,                 /* partial_inplace */
396          0,                     /* src_mask */
397          0x0000fffc,            /* dst_mask */
398          FALSE),                /* pcrel_offset */
399
400   /* A relative 26 bit branch; the lower two bits must be zero.  */
401   HOWTO (R_PPC64_REL24,         /* type */
402          0,                     /* rightshift */
403          2,                     /* size (0 = byte, 1 = short, 2 = long) */
404          26,                    /* bitsize */
405          TRUE,                  /* pc_relative */
406          0,                     /* bitpos */
407          complain_overflow_signed, /* complain_on_overflow */
408          ppc64_elf_branch_reloc, /* special_function */
409          "R_PPC64_REL24",       /* name */
410          FALSE,                 /* partial_inplace */
411          0,                     /* src_mask */
412          0x03fffffc,            /* dst_mask */
413          TRUE),                 /* pcrel_offset */
414
415   /* A relative 16 bit branch; the lower two bits must be zero.  */
416   HOWTO (R_PPC64_REL14,         /* type */
417          0,                     /* rightshift */
418          2,                     /* size (0 = byte, 1 = short, 2 = long) */
419          16,                    /* bitsize */
420          TRUE,                  /* pc_relative */
421          0,                     /* bitpos */
422          complain_overflow_signed, /* complain_on_overflow */
423          ppc64_elf_branch_reloc, /* special_function */
424          "R_PPC64_REL14",       /* name */
425          FALSE,                 /* partial_inplace */
426          0,                     /* src_mask */
427          0x0000fffc,            /* dst_mask */
428          TRUE),                 /* pcrel_offset */
429
430   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
431      the branch is expected to be taken.  The lower two bits must be
432      zero.  */
433   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
434          0,                     /* rightshift */
435          2,                     /* size (0 = byte, 1 = short, 2 = long) */
436          16,                    /* bitsize */
437          TRUE,                  /* pc_relative */
438          0,                     /* bitpos */
439          complain_overflow_signed, /* complain_on_overflow */
440          ppc64_elf_brtaken_reloc, /* special_function */
441          "R_PPC64_REL14_BRTAKEN", /* name */
442          FALSE,                 /* partial_inplace */
443          0,                     /* src_mask */
444          0x0000fffc,            /* dst_mask */
445          TRUE),                 /* pcrel_offset */
446
447   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
448      the branch is not expected to be taken.  The lower two bits must
449      be zero.  */
450   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
451          0,                     /* rightshift */
452          2,                     /* size (0 = byte, 1 = short, 2 = long) */
453          16,                    /* bitsize */
454          TRUE,                  /* pc_relative */
455          0,                     /* bitpos */
456          complain_overflow_signed, /* complain_on_overflow */
457          ppc64_elf_brtaken_reloc, /* special_function */
458          "R_PPC64_REL14_BRNTAKEN",/* name */
459          FALSE,                 /* partial_inplace */
460          0,                     /* src_mask */
461          0x0000fffc,            /* dst_mask */
462          TRUE),                 /* pcrel_offset */
463
464   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
465      symbol.  */
466   HOWTO (R_PPC64_GOT16,         /* type */
467          0,                     /* rightshift */
468          1,                     /* size (0 = byte, 1 = short, 2 = long) */
469          16,                    /* bitsize */
470          FALSE,                 /* pc_relative */
471          0,                     /* bitpos */
472          complain_overflow_signed, /* complain_on_overflow */
473          ppc64_elf_unhandled_reloc, /* special_function */
474          "R_PPC64_GOT16",       /* name */
475          FALSE,                 /* partial_inplace */
476          0,                     /* src_mask */
477          0xffff,                /* dst_mask */
478          FALSE),                /* pcrel_offset */
479
480   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
481      the symbol.  */
482   HOWTO (R_PPC64_GOT16_LO,      /* type */
483          0,                     /* rightshift */
484          1,                     /* size (0 = byte, 1 = short, 2 = long) */
485          16,                    /* bitsize */
486          FALSE,                 /* pc_relative */
487          0,                     /* bitpos */
488          complain_overflow_dont, /* complain_on_overflow */
489          ppc64_elf_unhandled_reloc, /* special_function */
490          "R_PPC64_GOT16_LO",    /* name */
491          FALSE,                 /* partial_inplace */
492          0,                     /* src_mask */
493          0xffff,                /* dst_mask */
494          FALSE),                /* pcrel_offset */
495
496   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
497      the symbol.  */
498   HOWTO (R_PPC64_GOT16_HI,      /* type */
499          16,                    /* rightshift */
500          1,                     /* size (0 = byte, 1 = short, 2 = long) */
501          16,                    /* bitsize */
502          FALSE,                 /* pc_relative */
503          0,                     /* bitpos */
504          complain_overflow_signed,/* complain_on_overflow */
505          ppc64_elf_unhandled_reloc, /* special_function */
506          "R_PPC64_GOT16_HI",    /* name */
507          FALSE,                 /* partial_inplace */
508          0,                     /* src_mask */
509          0xffff,                /* dst_mask */
510          FALSE),                /* pcrel_offset */
511
512   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
513      the symbol.  */
514   HOWTO (R_PPC64_GOT16_HA,      /* type */
515          16,                    /* rightshift */
516          1,                     /* size (0 = byte, 1 = short, 2 = long) */
517          16,                    /* bitsize */
518          FALSE,                 /* pc_relative */
519          0,                     /* bitpos */
520          complain_overflow_signed,/* complain_on_overflow */
521          ppc64_elf_unhandled_reloc, /* special_function */
522          "R_PPC64_GOT16_HA",    /* name */
523          FALSE,                 /* partial_inplace */
524          0,                     /* src_mask */
525          0xffff,                /* dst_mask */
526          FALSE),                /* pcrel_offset */
527
528   /* This is used only by the dynamic linker.  The symbol should exist
529      both in the object being run and in some shared library.  The
530      dynamic linker copies the data addressed by the symbol from the
531      shared library into the object, because the object being
532      run has to have the data at some particular address.  */
533   HOWTO (R_PPC64_COPY,          /* type */
534          0,                     /* rightshift */
535          0,                     /* this one is variable size */
536          0,                     /* bitsize */
537          FALSE,                 /* pc_relative */
538          0,                     /* bitpos */
539          complain_overflow_dont, /* complain_on_overflow */
540          ppc64_elf_unhandled_reloc, /* special_function */
541          "R_PPC64_COPY",        /* name */
542          FALSE,                 /* partial_inplace */
543          0,                     /* src_mask */
544          0,                     /* dst_mask */
545          FALSE),                /* pcrel_offset */
546
547   /* Like R_PPC64_ADDR64, but used when setting global offset table
548      entries.  */
549   HOWTO (R_PPC64_GLOB_DAT,      /* type */
550          0,                     /* rightshift */
551          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
552          64,                    /* bitsize */
553          FALSE,                 /* pc_relative */
554          0,                     /* bitpos */
555          complain_overflow_dont, /* complain_on_overflow */
556          ppc64_elf_unhandled_reloc,  /* special_function */
557          "R_PPC64_GLOB_DAT",    /* name */
558          FALSE,                 /* partial_inplace */
559          0,                     /* src_mask */
560          ONES (64),             /* dst_mask */
561          FALSE),                /* pcrel_offset */
562
563   /* Created by the link editor.  Marks a procedure linkage table
564      entry for a symbol.  */
565   HOWTO (R_PPC64_JMP_SLOT,      /* type */
566          0,                     /* rightshift */
567          0,                     /* size (0 = byte, 1 = short, 2 = long) */
568          0,                     /* bitsize */
569          FALSE,                 /* pc_relative */
570          0,                     /* bitpos */
571          complain_overflow_dont, /* complain_on_overflow */
572          ppc64_elf_unhandled_reloc, /* special_function */
573          "R_PPC64_JMP_SLOT",    /* name */
574          FALSE,                 /* partial_inplace */
575          0,                     /* src_mask */
576          0,                     /* dst_mask */
577          FALSE),                /* pcrel_offset */
578
579   /* Used only by the dynamic linker.  When the object is run, this
580      doubleword64 is set to the load address of the object, plus the
581      addend.  */
582   HOWTO (R_PPC64_RELATIVE,      /* type */
583          0,                     /* rightshift */
584          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
585          64,                    /* bitsize */
586          FALSE,                 /* pc_relative */
587          0,                     /* bitpos */
588          complain_overflow_dont, /* complain_on_overflow */
589          bfd_elf_generic_reloc, /* special_function */
590          "R_PPC64_RELATIVE",    /* name */
591          FALSE,                 /* partial_inplace */
592          0,                     /* src_mask */
593          ONES (64),             /* dst_mask */
594          FALSE),                /* pcrel_offset */
595
596   /* Like R_PPC64_ADDR32, but may be unaligned.  */
597   HOWTO (R_PPC64_UADDR32,       /* type */
598          0,                     /* rightshift */
599          2,                     /* size (0 = byte, 1 = short, 2 = long) */
600          32,                    /* bitsize */
601          FALSE,                 /* pc_relative */
602          0,                     /* bitpos */
603          complain_overflow_bitfield, /* complain_on_overflow */
604          bfd_elf_generic_reloc, /* special_function */
605          "R_PPC64_UADDR32",     /* name */
606          FALSE,                 /* partial_inplace */
607          0,                     /* src_mask */
608          0xffffffff,            /* dst_mask */
609          FALSE),                /* pcrel_offset */
610
611   /* Like R_PPC64_ADDR16, but may be unaligned.  */
612   HOWTO (R_PPC64_UADDR16,       /* type */
613          0,                     /* rightshift */
614          1,                     /* size (0 = byte, 1 = short, 2 = long) */
615          16,                    /* bitsize */
616          FALSE,                 /* pc_relative */
617          0,                     /* bitpos */
618          complain_overflow_bitfield, /* complain_on_overflow */
619          bfd_elf_generic_reloc, /* special_function */
620          "R_PPC64_UADDR16",     /* name */
621          FALSE,                 /* partial_inplace */
622          0,                     /* src_mask */
623          0xffff,                /* dst_mask */
624          FALSE),                /* pcrel_offset */
625
626   /* 32-bit PC relative.  */
627   HOWTO (R_PPC64_REL32,         /* type */
628          0,                     /* rightshift */
629          2,                     /* size (0 = byte, 1 = short, 2 = long) */
630          32,                    /* bitsize */
631          TRUE,                  /* pc_relative */
632          0,                     /* bitpos */
633          /* FIXME: Verify.  Was complain_overflow_bitfield.  */
634          complain_overflow_signed, /* complain_on_overflow */
635          bfd_elf_generic_reloc, /* special_function */
636          "R_PPC64_REL32",       /* name */
637          FALSE,                 /* partial_inplace */
638          0,                     /* src_mask */
639          0xffffffff,            /* dst_mask */
640          TRUE),                 /* pcrel_offset */
641
642   /* 32-bit relocation to the symbol's procedure linkage table.  */
643   HOWTO (R_PPC64_PLT32,         /* type */
644          0,                     /* rightshift */
645          2,                     /* size (0 = byte, 1 = short, 2 = long) */
646          32,                    /* bitsize */
647          FALSE,                 /* pc_relative */
648          0,                     /* bitpos */
649          complain_overflow_bitfield, /* complain_on_overflow */
650          ppc64_elf_unhandled_reloc, /* special_function */
651          "R_PPC64_PLT32",       /* name */
652          FALSE,                 /* partial_inplace */
653          0,                     /* src_mask */
654          0xffffffff,            /* dst_mask */
655          FALSE),                /* pcrel_offset */
656
657   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
658      FIXME: R_PPC64_PLTREL32 not supported.  */
659   HOWTO (R_PPC64_PLTREL32,      /* type */
660          0,                     /* rightshift */
661          2,                     /* size (0 = byte, 1 = short, 2 = long) */
662          32,                    /* bitsize */
663          TRUE,                  /* pc_relative */
664          0,                     /* bitpos */
665          complain_overflow_signed, /* complain_on_overflow */
666          bfd_elf_generic_reloc, /* special_function */
667          "R_PPC64_PLTREL32",    /* name */
668          FALSE,                 /* partial_inplace */
669          0,                     /* src_mask */
670          0xffffffff,            /* dst_mask */
671          TRUE),                 /* pcrel_offset */
672
673   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
674      the symbol.  */
675   HOWTO (R_PPC64_PLT16_LO,      /* type */
676          0,                     /* rightshift */
677          1,                     /* size (0 = byte, 1 = short, 2 = long) */
678          16,                    /* bitsize */
679          FALSE,                 /* pc_relative */
680          0,                     /* bitpos */
681          complain_overflow_dont, /* complain_on_overflow */
682          ppc64_elf_unhandled_reloc, /* special_function */
683          "R_PPC64_PLT16_LO",    /* name */
684          FALSE,                 /* partial_inplace */
685          0,                     /* src_mask */
686          0xffff,                /* dst_mask */
687          FALSE),                /* pcrel_offset */
688
689   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
690      the symbol.  */
691   HOWTO (R_PPC64_PLT16_HI,      /* type */
692          16,                    /* rightshift */
693          1,                     /* size (0 = byte, 1 = short, 2 = long) */
694          16,                    /* bitsize */
695          FALSE,                 /* pc_relative */
696          0,                     /* bitpos */
697          complain_overflow_signed, /* complain_on_overflow */
698          ppc64_elf_unhandled_reloc, /* special_function */
699          "R_PPC64_PLT16_HI",    /* name */
700          FALSE,                 /* partial_inplace */
701          0,                     /* src_mask */
702          0xffff,                /* dst_mask */
703          FALSE),                /* pcrel_offset */
704
705   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
706      the symbol.  */
707   HOWTO (R_PPC64_PLT16_HA,      /* type */
708          16,                    /* rightshift */
709          1,                     /* size (0 = byte, 1 = short, 2 = long) */
710          16,                    /* bitsize */
711          FALSE,                 /* pc_relative */
712          0,                     /* bitpos */
713          complain_overflow_signed, /* complain_on_overflow */
714          ppc64_elf_unhandled_reloc, /* special_function */
715          "R_PPC64_PLT16_HA",    /* name */
716          FALSE,                 /* partial_inplace */
717          0,                     /* src_mask */
718          0xffff,                /* dst_mask */
719          FALSE),                /* pcrel_offset */
720
721   /* 16-bit section relative relocation.  */
722   HOWTO (R_PPC64_SECTOFF,       /* type */
723          0,                     /* rightshift */
724          1,                     /* size (0 = byte, 1 = short, 2 = long) */
725          16,                    /* bitsize */
726          FALSE,                 /* pc_relative */
727          0,                     /* bitpos */
728          complain_overflow_bitfield, /* complain_on_overflow */
729          ppc64_elf_sectoff_reloc, /* special_function */
730          "R_PPC64_SECTOFF",     /* name */
731          FALSE,                 /* partial_inplace */
732          0,                     /* src_mask */
733          0xffff,                /* dst_mask */
734          FALSE),                /* pcrel_offset */
735
736   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
737   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
738          0,                     /* rightshift */
739          1,                     /* size (0 = byte, 1 = short, 2 = long) */
740          16,                    /* bitsize */
741          FALSE,                 /* pc_relative */
742          0,                     /* bitpos */
743          complain_overflow_dont, /* complain_on_overflow */
744          ppc64_elf_sectoff_reloc, /* special_function */
745          "R_PPC64_SECTOFF_LO",  /* name */
746          FALSE,                 /* partial_inplace */
747          0,                     /* src_mask */
748          0xffff,                /* dst_mask */
749          FALSE),                /* pcrel_offset */
750
751   /* 16-bit upper half section relative relocation.  */
752   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
753          16,                    /* rightshift */
754          1,                     /* size (0 = byte, 1 = short, 2 = long) */
755          16,                    /* bitsize */
756          FALSE,                 /* pc_relative */
757          0,                     /* bitpos */
758          complain_overflow_signed, /* complain_on_overflow */
759          ppc64_elf_sectoff_reloc, /* special_function */
760          "R_PPC64_SECTOFF_HI",  /* name */
761          FALSE,                 /* partial_inplace */
762          0,                     /* src_mask */
763          0xffff,                /* dst_mask */
764          FALSE),                /* pcrel_offset */
765
766   /* 16-bit upper half adjusted section relative relocation.  */
767   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
768          16,                    /* rightshift */
769          1,                     /* size (0 = byte, 1 = short, 2 = long) */
770          16,                    /* bitsize */
771          FALSE,                 /* pc_relative */
772          0,                     /* bitpos */
773          complain_overflow_signed, /* complain_on_overflow */
774          ppc64_elf_sectoff_ha_reloc, /* special_function */
775          "R_PPC64_SECTOFF_HA",  /* name */
776          FALSE,                 /* partial_inplace */
777          0,                     /* src_mask */
778          0xffff,                /* dst_mask */
779          FALSE),                /* pcrel_offset */
780
781   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
782   HOWTO (R_PPC64_REL30,         /* type */
783          2,                     /* rightshift */
784          2,                     /* size (0 = byte, 1 = short, 2 = long) */
785          30,                    /* bitsize */
786          TRUE,                  /* pc_relative */
787          0,                     /* bitpos */
788          complain_overflow_dont, /* complain_on_overflow */
789          bfd_elf_generic_reloc, /* special_function */
790          "R_PPC64_REL30",       /* name */
791          FALSE,                 /* partial_inplace */
792          0,                     /* src_mask */
793          0xfffffffc,            /* dst_mask */
794          TRUE),                 /* pcrel_offset */
795
796   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
797
798   /* A standard 64-bit relocation.  */
799   HOWTO (R_PPC64_ADDR64,        /* type */
800          0,                     /* rightshift */
801          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
802          64,                    /* bitsize */
803          FALSE,                 /* pc_relative */
804          0,                     /* bitpos */
805          complain_overflow_dont, /* complain_on_overflow */
806          bfd_elf_generic_reloc, /* special_function */
807          "R_PPC64_ADDR64",      /* name */
808          FALSE,                 /* partial_inplace */
809          0,                     /* src_mask */
810          ONES (64),             /* dst_mask */
811          FALSE),                /* pcrel_offset */
812
813   /* The bits 32-47 of an address.  */
814   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
815          32,                    /* rightshift */
816          1,                     /* size (0 = byte, 1 = short, 2 = long) */
817          16,                    /* bitsize */
818          FALSE,                 /* pc_relative */
819          0,                     /* bitpos */
820          complain_overflow_dont, /* complain_on_overflow */
821          bfd_elf_generic_reloc, /* special_function */
822          "R_PPC64_ADDR16_HIGHER", /* name */
823          FALSE,                 /* partial_inplace */
824          0,                     /* src_mask */
825          0xffff,                /* dst_mask */
826          FALSE),                /* pcrel_offset */
827
828   /* The bits 32-47 of an address, plus 1 if the contents of the low
829      16 bits, treated as a signed number, is negative.  */
830   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
831          32,                    /* rightshift */
832          1,                     /* size (0 = byte, 1 = short, 2 = long) */
833          16,                    /* bitsize */
834          FALSE,                 /* pc_relative */
835          0,                     /* bitpos */
836          complain_overflow_dont, /* complain_on_overflow */
837          ppc64_elf_ha_reloc,    /* special_function */
838          "R_PPC64_ADDR16_HIGHERA", /* name */
839          FALSE,                 /* partial_inplace */
840          0,                     /* src_mask */
841          0xffff,                /* dst_mask */
842          FALSE),                /* pcrel_offset */
843
844   /* The bits 48-63 of an address.  */
845   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
846          48,                    /* rightshift */
847          1,                     /* size (0 = byte, 1 = short, 2 = long) */
848          16,                    /* bitsize */
849          FALSE,                 /* pc_relative */
850          0,                     /* bitpos */
851          complain_overflow_dont, /* complain_on_overflow */
852          bfd_elf_generic_reloc, /* special_function */
853          "R_PPC64_ADDR16_HIGHEST", /* name */
854          FALSE,                 /* partial_inplace */
855          0,                     /* src_mask */
856          0xffff,                /* dst_mask */
857          FALSE),                /* pcrel_offset */
858
859   /* The bits 48-63 of an address, plus 1 if the contents of the low
860      16 bits, treated as a signed number, is negative.  */
861   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
862          48,                    /* rightshift */
863          1,                     /* size (0 = byte, 1 = short, 2 = long) */
864          16,                    /* bitsize */
865          FALSE,                 /* pc_relative */
866          0,                     /* bitpos */
867          complain_overflow_dont, /* complain_on_overflow */
868          ppc64_elf_ha_reloc,    /* special_function */
869          "R_PPC64_ADDR16_HIGHESTA", /* name */
870          FALSE,                 /* partial_inplace */
871          0,                     /* src_mask */
872          0xffff,                /* dst_mask */
873          FALSE),                /* pcrel_offset */
874
875   /* Like ADDR64, but may be unaligned.  */
876   HOWTO (R_PPC64_UADDR64,       /* type */
877          0,                     /* rightshift */
878          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
879          64,                    /* bitsize */
880          FALSE,                 /* pc_relative */
881          0,                     /* bitpos */
882          complain_overflow_dont, /* complain_on_overflow */
883          bfd_elf_generic_reloc, /* special_function */
884          "R_PPC64_UADDR64",     /* name */
885          FALSE,                 /* partial_inplace */
886          0,                     /* src_mask */
887          ONES (64),             /* dst_mask */
888          FALSE),                /* pcrel_offset */
889
890   /* 64-bit relative relocation.  */
891   HOWTO (R_PPC64_REL64,         /* type */
892          0,                     /* rightshift */
893          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
894          64,                    /* bitsize */
895          TRUE,                  /* pc_relative */
896          0,                     /* bitpos */
897          complain_overflow_dont, /* complain_on_overflow */
898          bfd_elf_generic_reloc, /* special_function */
899          "R_PPC64_REL64",       /* name */
900          FALSE,                 /* partial_inplace */
901          0,                     /* src_mask */
902          ONES (64),             /* dst_mask */
903          TRUE),                 /* pcrel_offset */
904
905   /* 64-bit relocation to the symbol's procedure linkage table.  */
906   HOWTO (R_PPC64_PLT64,         /* type */
907          0,                     /* rightshift */
908          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
909          64,                    /* bitsize */
910          FALSE,                 /* pc_relative */
911          0,                     /* bitpos */
912          complain_overflow_dont, /* complain_on_overflow */
913          ppc64_elf_unhandled_reloc, /* special_function */
914          "R_PPC64_PLT64",       /* name */
915          FALSE,                 /* partial_inplace */
916          0,                     /* src_mask */
917          ONES (64),             /* dst_mask */
918          FALSE),                /* pcrel_offset */
919
920   /* 64-bit PC relative relocation to the symbol's procedure linkage
921      table.  */
922   /* FIXME: R_PPC64_PLTREL64 not supported.  */
923   HOWTO (R_PPC64_PLTREL64,      /* type */
924          0,                     /* rightshift */
925          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
926          64,                    /* bitsize */
927          TRUE,                  /* pc_relative */
928          0,                     /* bitpos */
929          complain_overflow_dont, /* complain_on_overflow */
930          ppc64_elf_unhandled_reloc, /* special_function */
931          "R_PPC64_PLTREL64",    /* name */
932          FALSE,                 /* partial_inplace */
933          0,                     /* src_mask */
934          ONES (64),             /* dst_mask */
935          TRUE),                 /* pcrel_offset */
936
937   /* 16 bit TOC-relative relocation.  */
938
939   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
940   HOWTO (R_PPC64_TOC16,         /* type */
941          0,                     /* rightshift */
942          1,                     /* size (0 = byte, 1 = short, 2 = long) */
943          16,                    /* bitsize */
944          FALSE,                 /* pc_relative */
945          0,                     /* bitpos */
946          complain_overflow_signed, /* complain_on_overflow */
947          ppc64_elf_toc_reloc,   /* special_function */
948          "R_PPC64_TOC16",       /* name */
949          FALSE,                 /* partial_inplace */
950          0,                     /* src_mask */
951          0xffff,                /* dst_mask */
952          FALSE),                /* pcrel_offset */
953
954   /* 16 bit TOC-relative relocation without overflow.  */
955
956   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
957   HOWTO (R_PPC64_TOC16_LO,      /* type */
958          0,                     /* rightshift */
959          1,                     /* size (0 = byte, 1 = short, 2 = long) */
960          16,                    /* bitsize */
961          FALSE,                 /* pc_relative */
962          0,                     /* bitpos */
963          complain_overflow_dont, /* complain_on_overflow */
964          ppc64_elf_toc_reloc,   /* special_function */
965          "R_PPC64_TOC16_LO",    /* name */
966          FALSE,                 /* partial_inplace */
967          0,                     /* src_mask */
968          0xffff,                /* dst_mask */
969          FALSE),                /* pcrel_offset */
970
971   /* 16 bit TOC-relative relocation, high 16 bits.  */
972
973   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
974   HOWTO (R_PPC64_TOC16_HI,      /* type */
975          16,                    /* rightshift */
976          1,                     /* size (0 = byte, 1 = short, 2 = long) */
977          16,                    /* bitsize */
978          FALSE,                 /* pc_relative */
979          0,                     /* bitpos */
980          complain_overflow_signed, /* complain_on_overflow */
981          ppc64_elf_toc_reloc,   /* special_function */
982          "R_PPC64_TOC16_HI",    /* name */
983          FALSE,                 /* partial_inplace */
984          0,                     /* src_mask */
985          0xffff,                /* dst_mask */
986          FALSE),                /* pcrel_offset */
987
988   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
989      contents of the low 16 bits, treated as a signed number, is
990      negative.  */
991
992   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
993   HOWTO (R_PPC64_TOC16_HA,      /* type */
994          16,                    /* rightshift */
995          1,                     /* size (0 = byte, 1 = short, 2 = long) */
996          16,                    /* bitsize */
997          FALSE,                 /* pc_relative */
998          0,                     /* bitpos */
999          complain_overflow_signed, /* complain_on_overflow */
1000          ppc64_elf_toc_ha_reloc, /* special_function */
1001          "R_PPC64_TOC16_HA",    /* name */
1002          FALSE,                 /* partial_inplace */
1003          0,                     /* src_mask */
1004          0xffff,                /* dst_mask */
1005          FALSE),                /* pcrel_offset */
1006
1007   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
1008
1009   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
1010   HOWTO (R_PPC64_TOC,           /* type */
1011          0,                     /* rightshift */
1012          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1013          64,                    /* bitsize */
1014          FALSE,                 /* pc_relative */
1015          0,                     /* bitpos */
1016          complain_overflow_bitfield, /* complain_on_overflow */
1017          ppc64_elf_toc64_reloc, /* special_function */
1018          "R_PPC64_TOC",         /* name */
1019          FALSE,                 /* partial_inplace */
1020          0,                     /* src_mask */
1021          ONES (64),             /* dst_mask */
1022          FALSE),                /* pcrel_offset */
1023
1024   /* Like R_PPC64_GOT16, but also informs the link editor that the
1025      value to relocate may (!) refer to a PLT entry which the link
1026      editor (a) may replace with the symbol value.  If the link editor
1027      is unable to fully resolve the symbol, it may (b) create a PLT
1028      entry and store the address to the new PLT entry in the GOT.
1029      This permits lazy resolution of function symbols at run time.
1030      The link editor may also skip all of this and just (c) emit a
1031      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
1032   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
1033     HOWTO (R_PPC64_PLTGOT16,    /* type */
1034          0,                     /* rightshift */
1035          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1036          16,                    /* bitsize */
1037          FALSE,                 /* pc_relative */
1038          0,                     /* bitpos */
1039          complain_overflow_signed, /* complain_on_overflow */
1040          ppc64_elf_unhandled_reloc, /* special_function */
1041          "R_PPC64_PLTGOT16",    /* name */
1042          FALSE,                 /* partial_inplace */
1043          0,                     /* src_mask */
1044          0xffff,                /* dst_mask */
1045          FALSE),                /* pcrel_offset */
1046
1047   /* Like R_PPC64_PLTGOT16, but without overflow.  */
1048   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1049   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1050          0,                     /* rightshift */
1051          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1052          16,                    /* bitsize */
1053          FALSE,                 /* pc_relative */
1054          0,                     /* bitpos */
1055          complain_overflow_dont, /* complain_on_overflow */
1056          ppc64_elf_unhandled_reloc, /* special_function */
1057          "R_PPC64_PLTGOT16_LO", /* name */
1058          FALSE,                 /* partial_inplace */
1059          0,                     /* src_mask */
1060          0xffff,                /* dst_mask */
1061          FALSE),                /* pcrel_offset */
1062
1063   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1064   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1065   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1066          16,                    /* rightshift */
1067          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1068          16,                    /* bitsize */
1069          FALSE,                 /* pc_relative */
1070          0,                     /* bitpos */
1071          complain_overflow_signed, /* complain_on_overflow */
1072          ppc64_elf_unhandled_reloc, /* special_function */
1073          "R_PPC64_PLTGOT16_HI", /* name */
1074          FALSE,                 /* partial_inplace */
1075          0,                     /* src_mask */
1076          0xffff,                /* dst_mask */
1077          FALSE),                /* pcrel_offset */
1078
1079   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1080      1 if the contents of the low 16 bits, treated as a signed number,
1081      is negative.  */
1082   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1083   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1084          16,                    /* rightshift */
1085          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1086          16,                    /* bitsize */
1087          FALSE,                 /* pc_relative */
1088          0,                     /* bitpos */
1089          complain_overflow_signed, /* complain_on_overflow */
1090          ppc64_elf_unhandled_reloc, /* special_function */
1091          "R_PPC64_PLTGOT16_HA", /* name */
1092          FALSE,                 /* partial_inplace */
1093          0,                     /* src_mask */
1094          0xffff,                /* dst_mask */
1095          FALSE),                /* pcrel_offset */
1096
1097   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1098   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1099          0,                     /* rightshift */
1100          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1101          16,                    /* bitsize */
1102          FALSE,                 /* pc_relative */
1103          0,                     /* bitpos */
1104          complain_overflow_bitfield, /* complain_on_overflow */
1105          bfd_elf_generic_reloc, /* special_function */
1106          "R_PPC64_ADDR16_DS",   /* name */
1107          FALSE,                 /* partial_inplace */
1108          0,                     /* src_mask */
1109          0xfffc,                /* dst_mask */
1110          FALSE),                /* pcrel_offset */
1111
1112   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1113   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1114          0,                     /* rightshift */
1115          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1116          16,                    /* bitsize */
1117          FALSE,                 /* pc_relative */
1118          0,                     /* bitpos */
1119          complain_overflow_dont,/* complain_on_overflow */
1120          bfd_elf_generic_reloc, /* special_function */
1121          "R_PPC64_ADDR16_LO_DS",/* name */
1122          FALSE,                 /* partial_inplace */
1123          0,                     /* src_mask */
1124          0xfffc,                /* dst_mask */
1125          FALSE),                /* pcrel_offset */
1126
1127   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1128   HOWTO (R_PPC64_GOT16_DS,      /* type */
1129          0,                     /* rightshift */
1130          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1131          16,                    /* bitsize */
1132          FALSE,                 /* pc_relative */
1133          0,                     /* bitpos */
1134          complain_overflow_signed, /* complain_on_overflow */
1135          ppc64_elf_unhandled_reloc, /* special_function */
1136          "R_PPC64_GOT16_DS",    /* name */
1137          FALSE,                 /* partial_inplace */
1138          0,                     /* src_mask */
1139          0xfffc,                /* dst_mask */
1140          FALSE),                /* pcrel_offset */
1141
1142   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1143   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1144          0,                     /* rightshift */
1145          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1146          16,                    /* bitsize */
1147          FALSE,                 /* pc_relative */
1148          0,                     /* bitpos */
1149          complain_overflow_dont, /* complain_on_overflow */
1150          ppc64_elf_unhandled_reloc, /* special_function */
1151          "R_PPC64_GOT16_LO_DS", /* name */
1152          FALSE,                 /* partial_inplace */
1153          0,                     /* src_mask */
1154          0xfffc,                /* dst_mask */
1155          FALSE),                /* pcrel_offset */
1156
1157   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1158   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1159          0,                     /* rightshift */
1160          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1161          16,                    /* bitsize */
1162          FALSE,                 /* pc_relative */
1163          0,                     /* bitpos */
1164          complain_overflow_dont, /* complain_on_overflow */
1165          ppc64_elf_unhandled_reloc, /* special_function */
1166          "R_PPC64_PLT16_LO_DS", /* name */
1167          FALSE,                 /* partial_inplace */
1168          0,                     /* src_mask */
1169          0xfffc,                /* dst_mask */
1170          FALSE),                /* pcrel_offset */
1171
1172   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1173   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1174          0,                     /* rightshift */
1175          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1176          16,                    /* bitsize */
1177          FALSE,                 /* pc_relative */
1178          0,                     /* bitpos */
1179          complain_overflow_bitfield, /* complain_on_overflow */
1180          ppc64_elf_sectoff_reloc, /* special_function */
1181          "R_PPC64_SECTOFF_DS",  /* name */
1182          FALSE,                 /* partial_inplace */
1183          0,                     /* src_mask */
1184          0xfffc,                /* dst_mask */
1185          FALSE),                /* pcrel_offset */
1186
1187   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1188   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1189          0,                     /* rightshift */
1190          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1191          16,                    /* bitsize */
1192          FALSE,                 /* pc_relative */
1193          0,                     /* bitpos */
1194          complain_overflow_dont, /* complain_on_overflow */
1195          ppc64_elf_sectoff_reloc, /* special_function */
1196          "R_PPC64_SECTOFF_LO_DS",/* name */
1197          FALSE,                 /* partial_inplace */
1198          0,                     /* src_mask */
1199          0xfffc,                /* dst_mask */
1200          FALSE),                /* pcrel_offset */
1201
1202   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1203   HOWTO (R_PPC64_TOC16_DS,      /* type */
1204          0,                     /* rightshift */
1205          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1206          16,                    /* bitsize */
1207          FALSE,                 /* pc_relative */
1208          0,                     /* bitpos */
1209          complain_overflow_signed, /* complain_on_overflow */
1210          ppc64_elf_toc_reloc,   /* special_function */
1211          "R_PPC64_TOC16_DS",    /* name */
1212          FALSE,                 /* partial_inplace */
1213          0,                     /* src_mask */
1214          0xfffc,                /* dst_mask */
1215          FALSE),                /* pcrel_offset */
1216
1217   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1218   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1219          0,                     /* rightshift */
1220          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1221          16,                    /* bitsize */
1222          FALSE,                 /* pc_relative */
1223          0,                     /* bitpos */
1224          complain_overflow_dont, /* complain_on_overflow */
1225          ppc64_elf_toc_reloc,   /* special_function */
1226          "R_PPC64_TOC16_LO_DS", /* name */
1227          FALSE,                 /* partial_inplace */
1228          0,                     /* src_mask */
1229          0xfffc,                /* dst_mask */
1230          FALSE),                /* pcrel_offset */
1231
1232   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1233   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1234   HOWTO (R_PPC64_PLTGOT16_DS,   /* type */
1235          0,                     /* rightshift */
1236          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1237          16,                    /* bitsize */
1238          FALSE,                 /* pc_relative */
1239          0,                     /* bitpos */
1240          complain_overflow_signed, /* complain_on_overflow */
1241          ppc64_elf_unhandled_reloc, /* special_function */
1242          "R_PPC64_PLTGOT16_DS", /* name */
1243          FALSE,                 /* partial_inplace */
1244          0,                     /* src_mask */
1245          0xfffc,                /* dst_mask */
1246          FALSE),                /* pcrel_offset */
1247
1248   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1249   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1250   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1251          0,                     /* rightshift */
1252          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1253          16,                    /* bitsize */
1254          FALSE,                 /* pc_relative */
1255          0,                     /* bitpos */
1256          complain_overflow_dont, /* complain_on_overflow */
1257          ppc64_elf_unhandled_reloc, /* special_function */
1258          "R_PPC64_PLTGOT16_LO_DS",/* name */
1259          FALSE,                 /* partial_inplace */
1260          0,                     /* src_mask */
1261          0xfffc,                /* dst_mask */
1262          FALSE),                /* pcrel_offset */
1263
1264   /* Marker relocs for TLS.  */
1265   HOWTO (R_PPC64_TLS,
1266          0,                     /* rightshift */
1267          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1268          32,                    /* bitsize */
1269          FALSE,                 /* pc_relative */
1270          0,                     /* bitpos */
1271          complain_overflow_dont, /* complain_on_overflow */
1272          bfd_elf_generic_reloc, /* special_function */
1273          "R_PPC64_TLS",         /* name */
1274          FALSE,                 /* partial_inplace */
1275          0,                     /* src_mask */
1276          0,                     /* dst_mask */
1277          FALSE),                /* pcrel_offset */
1278
1279   HOWTO (R_PPC64_TLSGD,
1280          0,                     /* rightshift */
1281          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1282          32,                    /* bitsize */
1283          FALSE,                 /* pc_relative */
1284          0,                     /* bitpos */
1285          complain_overflow_dont, /* complain_on_overflow */
1286          bfd_elf_generic_reloc, /* special_function */
1287          "R_PPC64_TLSGD",       /* name */
1288          FALSE,                 /* partial_inplace */
1289          0,                     /* src_mask */
1290          0,                     /* dst_mask */
1291          FALSE),                /* pcrel_offset */
1292
1293   HOWTO (R_PPC64_TLSLD,
1294          0,                     /* rightshift */
1295          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1296          32,                    /* bitsize */
1297          FALSE,                 /* pc_relative */
1298          0,                     /* bitpos */
1299          complain_overflow_dont, /* complain_on_overflow */
1300          bfd_elf_generic_reloc, /* special_function */
1301          "R_PPC64_TLSLD",       /* name */
1302          FALSE,                 /* partial_inplace */
1303          0,                     /* src_mask */
1304          0,                     /* dst_mask */
1305          FALSE),                /* pcrel_offset */
1306
1307   HOWTO (R_PPC64_TOCSAVE,
1308          0,                     /* rightshift */
1309          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1310          32,                    /* bitsize */
1311          FALSE,                 /* pc_relative */
1312          0,                     /* bitpos */
1313          complain_overflow_dont, /* complain_on_overflow */
1314          bfd_elf_generic_reloc, /* special_function */
1315          "R_PPC64_TOCSAVE",     /* name */
1316          FALSE,                 /* partial_inplace */
1317          0,                     /* src_mask */
1318          0,                     /* dst_mask */
1319          FALSE),                /* pcrel_offset */
1320
1321   /* Computes the load module index of the load module that contains the
1322      definition of its TLS sym.  */
1323   HOWTO (R_PPC64_DTPMOD64,
1324          0,                     /* rightshift */
1325          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1326          64,                    /* bitsize */
1327          FALSE,                 /* pc_relative */
1328          0,                     /* bitpos */
1329          complain_overflow_dont, /* complain_on_overflow */
1330          ppc64_elf_unhandled_reloc, /* special_function */
1331          "R_PPC64_DTPMOD64",    /* name */
1332          FALSE,                 /* partial_inplace */
1333          0,                     /* src_mask */
1334          ONES (64),             /* dst_mask */
1335          FALSE),                /* pcrel_offset */
1336
1337   /* Computes a dtv-relative displacement, the difference between the value
1338      of sym+add and the base address of the thread-local storage block that
1339      contains the definition of sym, minus 0x8000.  */
1340   HOWTO (R_PPC64_DTPREL64,
1341          0,                     /* rightshift */
1342          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1343          64,                    /* bitsize */
1344          FALSE,                 /* pc_relative */
1345          0,                     /* bitpos */
1346          complain_overflow_dont, /* complain_on_overflow */
1347          ppc64_elf_unhandled_reloc, /* special_function */
1348          "R_PPC64_DTPREL64",    /* name */
1349          FALSE,                 /* partial_inplace */
1350          0,                     /* src_mask */
1351          ONES (64),             /* dst_mask */
1352          FALSE),                /* pcrel_offset */
1353
1354   /* A 16 bit dtprel reloc.  */
1355   HOWTO (R_PPC64_DTPREL16,
1356          0,                     /* rightshift */
1357          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1358          16,                    /* bitsize */
1359          FALSE,                 /* pc_relative */
1360          0,                     /* bitpos */
1361          complain_overflow_signed, /* complain_on_overflow */
1362          ppc64_elf_unhandled_reloc, /* special_function */
1363          "R_PPC64_DTPREL16",    /* name */
1364          FALSE,                 /* partial_inplace */
1365          0,                     /* src_mask */
1366          0xffff,                /* dst_mask */
1367          FALSE),                /* pcrel_offset */
1368
1369   /* Like DTPREL16, but no overflow.  */
1370   HOWTO (R_PPC64_DTPREL16_LO,
1371          0,                     /* rightshift */
1372          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1373          16,                    /* bitsize */
1374          FALSE,                 /* pc_relative */
1375          0,                     /* bitpos */
1376          complain_overflow_dont, /* complain_on_overflow */
1377          ppc64_elf_unhandled_reloc, /* special_function */
1378          "R_PPC64_DTPREL16_LO", /* name */
1379          FALSE,                 /* partial_inplace */
1380          0,                     /* src_mask */
1381          0xffff,                /* dst_mask */
1382          FALSE),                /* pcrel_offset */
1383
1384   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1385   HOWTO (R_PPC64_DTPREL16_HI,
1386          16,                    /* rightshift */
1387          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1388          16,                    /* bitsize */
1389          FALSE,                 /* pc_relative */
1390          0,                     /* bitpos */
1391          complain_overflow_signed, /* complain_on_overflow */
1392          ppc64_elf_unhandled_reloc, /* special_function */
1393          "R_PPC64_DTPREL16_HI", /* name */
1394          FALSE,                 /* partial_inplace */
1395          0,                     /* src_mask */
1396          0xffff,                /* dst_mask */
1397          FALSE),                /* pcrel_offset */
1398
1399   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1400   HOWTO (R_PPC64_DTPREL16_HA,
1401          16,                    /* rightshift */
1402          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1403          16,                    /* bitsize */
1404          FALSE,                 /* pc_relative */
1405          0,                     /* bitpos */
1406          complain_overflow_signed, /* complain_on_overflow */
1407          ppc64_elf_unhandled_reloc, /* special_function */
1408          "R_PPC64_DTPREL16_HA", /* name */
1409          FALSE,                 /* partial_inplace */
1410          0,                     /* src_mask */
1411          0xffff,                /* dst_mask */
1412          FALSE),                /* pcrel_offset */
1413
1414   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1415   HOWTO (R_PPC64_DTPREL16_HIGHER,
1416          32,                    /* rightshift */
1417          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1418          16,                    /* bitsize */
1419          FALSE,                 /* pc_relative */
1420          0,                     /* bitpos */
1421          complain_overflow_dont, /* complain_on_overflow */
1422          ppc64_elf_unhandled_reloc, /* special_function */
1423          "R_PPC64_DTPREL16_HIGHER", /* name */
1424          FALSE,                 /* partial_inplace */
1425          0,                     /* src_mask */
1426          0xffff,                /* dst_mask */
1427          FALSE),                /* pcrel_offset */
1428
1429   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1430   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1431          32,                    /* rightshift */
1432          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1433          16,                    /* bitsize */
1434          FALSE,                 /* pc_relative */
1435          0,                     /* bitpos */
1436          complain_overflow_dont, /* complain_on_overflow */
1437          ppc64_elf_unhandled_reloc, /* special_function */
1438          "R_PPC64_DTPREL16_HIGHERA", /* name */
1439          FALSE,                 /* partial_inplace */
1440          0,                     /* src_mask */
1441          0xffff,                /* dst_mask */
1442          FALSE),                /* pcrel_offset */
1443
1444   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1445   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1446          48,                    /* rightshift */
1447          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1448          16,                    /* bitsize */
1449          FALSE,                 /* pc_relative */
1450          0,                     /* bitpos */
1451          complain_overflow_dont, /* complain_on_overflow */
1452          ppc64_elf_unhandled_reloc, /* special_function */
1453          "R_PPC64_DTPREL16_HIGHEST", /* name */
1454          FALSE,                 /* partial_inplace */
1455          0,                     /* src_mask */
1456          0xffff,                /* dst_mask */
1457          FALSE),                /* pcrel_offset */
1458
1459   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1460   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1461          48,                    /* rightshift */
1462          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1463          16,                    /* bitsize */
1464          FALSE,                 /* pc_relative */
1465          0,                     /* bitpos */
1466          complain_overflow_dont, /* complain_on_overflow */
1467          ppc64_elf_unhandled_reloc, /* special_function */
1468          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1469          FALSE,                 /* partial_inplace */
1470          0,                     /* src_mask */
1471          0xffff,                /* dst_mask */
1472          FALSE),                /* pcrel_offset */
1473
1474   /* Like DTPREL16, but for insns with a DS field.  */
1475   HOWTO (R_PPC64_DTPREL16_DS,
1476          0,                     /* rightshift */
1477          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1478          16,                    /* bitsize */
1479          FALSE,                 /* pc_relative */
1480          0,                     /* bitpos */
1481          complain_overflow_signed, /* complain_on_overflow */
1482          ppc64_elf_unhandled_reloc, /* special_function */
1483          "R_PPC64_DTPREL16_DS", /* name */
1484          FALSE,                 /* partial_inplace */
1485          0,                     /* src_mask */
1486          0xfffc,                /* dst_mask */
1487          FALSE),                /* pcrel_offset */
1488
1489   /* Like DTPREL16_DS, but no overflow.  */
1490   HOWTO (R_PPC64_DTPREL16_LO_DS,
1491          0,                     /* rightshift */
1492          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1493          16,                    /* bitsize */
1494          FALSE,                 /* pc_relative */
1495          0,                     /* bitpos */
1496          complain_overflow_dont, /* complain_on_overflow */
1497          ppc64_elf_unhandled_reloc, /* special_function */
1498          "R_PPC64_DTPREL16_LO_DS", /* name */
1499          FALSE,                 /* partial_inplace */
1500          0,                     /* src_mask */
1501          0xfffc,                /* dst_mask */
1502          FALSE),                /* pcrel_offset */
1503
1504   /* Computes a tp-relative displacement, the difference between the value of
1505      sym+add and the value of the thread pointer (r13).  */
1506   HOWTO (R_PPC64_TPREL64,
1507          0,                     /* rightshift */
1508          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1509          64,                    /* bitsize */
1510          FALSE,                 /* pc_relative */
1511          0,                     /* bitpos */
1512          complain_overflow_dont, /* complain_on_overflow */
1513          ppc64_elf_unhandled_reloc, /* special_function */
1514          "R_PPC64_TPREL64",     /* name */
1515          FALSE,                 /* partial_inplace */
1516          0,                     /* src_mask */
1517          ONES (64),             /* dst_mask */
1518          FALSE),                /* pcrel_offset */
1519
1520   /* A 16 bit tprel reloc.  */
1521   HOWTO (R_PPC64_TPREL16,
1522          0,                     /* rightshift */
1523          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1524          16,                    /* bitsize */
1525          FALSE,                 /* pc_relative */
1526          0,                     /* bitpos */
1527          complain_overflow_signed, /* complain_on_overflow */
1528          ppc64_elf_unhandled_reloc, /* special_function */
1529          "R_PPC64_TPREL16",     /* name */
1530          FALSE,                 /* partial_inplace */
1531          0,                     /* src_mask */
1532          0xffff,                /* dst_mask */
1533          FALSE),                /* pcrel_offset */
1534
1535   /* Like TPREL16, but no overflow.  */
1536   HOWTO (R_PPC64_TPREL16_LO,
1537          0,                     /* rightshift */
1538          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1539          16,                    /* bitsize */
1540          FALSE,                 /* pc_relative */
1541          0,                     /* bitpos */
1542          complain_overflow_dont, /* complain_on_overflow */
1543          ppc64_elf_unhandled_reloc, /* special_function */
1544          "R_PPC64_TPREL16_LO",  /* name */
1545          FALSE,                 /* partial_inplace */
1546          0,                     /* src_mask */
1547          0xffff,                /* dst_mask */
1548          FALSE),                /* pcrel_offset */
1549
1550   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1551   HOWTO (R_PPC64_TPREL16_HI,
1552          16,                    /* rightshift */
1553          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1554          16,                    /* bitsize */
1555          FALSE,                 /* pc_relative */
1556          0,                     /* bitpos */
1557          complain_overflow_signed, /* complain_on_overflow */
1558          ppc64_elf_unhandled_reloc, /* special_function */
1559          "R_PPC64_TPREL16_HI",  /* name */
1560          FALSE,                 /* partial_inplace */
1561          0,                     /* src_mask */
1562          0xffff,                /* dst_mask */
1563          FALSE),                /* pcrel_offset */
1564
1565   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1566   HOWTO (R_PPC64_TPREL16_HA,
1567          16,                    /* rightshift */
1568          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1569          16,                    /* bitsize */
1570          FALSE,                 /* pc_relative */
1571          0,                     /* bitpos */
1572          complain_overflow_signed, /* complain_on_overflow */
1573          ppc64_elf_unhandled_reloc, /* special_function */
1574          "R_PPC64_TPREL16_HA",  /* name */
1575          FALSE,                 /* partial_inplace */
1576          0,                     /* src_mask */
1577          0xffff,                /* dst_mask */
1578          FALSE),                /* pcrel_offset */
1579
1580   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1581   HOWTO (R_PPC64_TPREL16_HIGHER,
1582          32,                    /* rightshift */
1583          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1584          16,                    /* bitsize */
1585          FALSE,                 /* pc_relative */
1586          0,                     /* bitpos */
1587          complain_overflow_dont, /* complain_on_overflow */
1588          ppc64_elf_unhandled_reloc, /* special_function */
1589          "R_PPC64_TPREL16_HIGHER",      /* name */
1590          FALSE,                 /* partial_inplace */
1591          0,                     /* src_mask */
1592          0xffff,                /* dst_mask */
1593          FALSE),                /* pcrel_offset */
1594
1595   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1596   HOWTO (R_PPC64_TPREL16_HIGHERA,
1597          32,                    /* rightshift */
1598          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1599          16,                    /* bitsize */
1600          FALSE,                 /* pc_relative */
1601          0,                     /* bitpos */
1602          complain_overflow_dont, /* complain_on_overflow */
1603          ppc64_elf_unhandled_reloc, /* special_function */
1604          "R_PPC64_TPREL16_HIGHERA", /* name */
1605          FALSE,                 /* partial_inplace */
1606          0,                     /* src_mask */
1607          0xffff,                /* dst_mask */
1608          FALSE),                /* pcrel_offset */
1609
1610   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1611   HOWTO (R_PPC64_TPREL16_HIGHEST,
1612          48,                    /* rightshift */
1613          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1614          16,                    /* bitsize */
1615          FALSE,                 /* pc_relative */
1616          0,                     /* bitpos */
1617          complain_overflow_dont, /* complain_on_overflow */
1618          ppc64_elf_unhandled_reloc, /* special_function */
1619          "R_PPC64_TPREL16_HIGHEST", /* name */
1620          FALSE,                 /* partial_inplace */
1621          0,                     /* src_mask */
1622          0xffff,                /* dst_mask */
1623          FALSE),                /* pcrel_offset */
1624
1625   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1626   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1627          48,                    /* rightshift */
1628          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1629          16,                    /* bitsize */
1630          FALSE,                 /* pc_relative */
1631          0,                     /* bitpos */
1632          complain_overflow_dont, /* complain_on_overflow */
1633          ppc64_elf_unhandled_reloc, /* special_function */
1634          "R_PPC64_TPREL16_HIGHESTA", /* name */
1635          FALSE,                 /* partial_inplace */
1636          0,                     /* src_mask */
1637          0xffff,                /* dst_mask */
1638          FALSE),                /* pcrel_offset */
1639
1640   /* Like TPREL16, but for insns with a DS field.  */
1641   HOWTO (R_PPC64_TPREL16_DS,
1642          0,                     /* rightshift */
1643          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1644          16,                    /* bitsize */
1645          FALSE,                 /* pc_relative */
1646          0,                     /* bitpos */
1647          complain_overflow_signed, /* complain_on_overflow */
1648          ppc64_elf_unhandled_reloc, /* special_function */
1649          "R_PPC64_TPREL16_DS",  /* name */
1650          FALSE,                 /* partial_inplace */
1651          0,                     /* src_mask */
1652          0xfffc,                /* dst_mask */
1653          FALSE),                /* pcrel_offset */
1654
1655   /* Like TPREL16_DS, but no overflow.  */
1656   HOWTO (R_PPC64_TPREL16_LO_DS,
1657          0,                     /* rightshift */
1658          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1659          16,                    /* bitsize */
1660          FALSE,                 /* pc_relative */
1661          0,                     /* bitpos */
1662          complain_overflow_dont, /* complain_on_overflow */
1663          ppc64_elf_unhandled_reloc, /* special_function */
1664          "R_PPC64_TPREL16_LO_DS", /* name */
1665          FALSE,                 /* partial_inplace */
1666          0,                     /* src_mask */
1667          0xfffc,                /* dst_mask */
1668          FALSE),                /* pcrel_offset */
1669
1670   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1671      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1672      to the first entry relative to the TOC base (r2).  */
1673   HOWTO (R_PPC64_GOT_TLSGD16,
1674          0,                     /* rightshift */
1675          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1676          16,                    /* bitsize */
1677          FALSE,                 /* pc_relative */
1678          0,                     /* bitpos */
1679          complain_overflow_signed, /* complain_on_overflow */
1680          ppc64_elf_unhandled_reloc, /* special_function */
1681          "R_PPC64_GOT_TLSGD16", /* name */
1682          FALSE,                 /* partial_inplace */
1683          0,                     /* src_mask */
1684          0xffff,                /* dst_mask */
1685          FALSE),                /* pcrel_offset */
1686
1687   /* Like GOT_TLSGD16, but no overflow.  */
1688   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1689          0,                     /* rightshift */
1690          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1691          16,                    /* bitsize */
1692          FALSE,                 /* pc_relative */
1693          0,                     /* bitpos */
1694          complain_overflow_dont, /* complain_on_overflow */
1695          ppc64_elf_unhandled_reloc, /* special_function */
1696          "R_PPC64_GOT_TLSGD16_LO", /* name */
1697          FALSE,                 /* partial_inplace */
1698          0,                     /* src_mask */
1699          0xffff,                /* dst_mask */
1700          FALSE),                /* pcrel_offset */
1701
1702   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1703   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1704          16,                    /* rightshift */
1705          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1706          16,                    /* bitsize */
1707          FALSE,                 /* pc_relative */
1708          0,                     /* bitpos */
1709          complain_overflow_signed, /* complain_on_overflow */
1710          ppc64_elf_unhandled_reloc, /* special_function */
1711          "R_PPC64_GOT_TLSGD16_HI", /* name */
1712          FALSE,                 /* partial_inplace */
1713          0,                     /* src_mask */
1714          0xffff,                /* dst_mask */
1715          FALSE),                /* pcrel_offset */
1716
1717   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1718   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1719          16,                    /* rightshift */
1720          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1721          16,                    /* bitsize */
1722          FALSE,                 /* pc_relative */
1723          0,                     /* bitpos */
1724          complain_overflow_signed, /* complain_on_overflow */
1725          ppc64_elf_unhandled_reloc, /* special_function */
1726          "R_PPC64_GOT_TLSGD16_HA", /* name */
1727          FALSE,                 /* partial_inplace */
1728          0,                     /* src_mask */
1729          0xffff,                /* dst_mask */
1730          FALSE),                /* pcrel_offset */
1731
1732   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1733      with values (sym+add)@dtpmod and zero, and computes the offset to the
1734      first entry relative to the TOC base (r2).  */
1735   HOWTO (R_PPC64_GOT_TLSLD16,
1736          0,                     /* rightshift */
1737          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1738          16,                    /* bitsize */
1739          FALSE,                 /* pc_relative */
1740          0,                     /* bitpos */
1741          complain_overflow_signed, /* complain_on_overflow */
1742          ppc64_elf_unhandled_reloc, /* special_function */
1743          "R_PPC64_GOT_TLSLD16", /* name */
1744          FALSE,                 /* partial_inplace */
1745          0,                     /* src_mask */
1746          0xffff,                /* dst_mask */
1747          FALSE),                /* pcrel_offset */
1748
1749   /* Like GOT_TLSLD16, but no overflow.  */
1750   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1751          0,                     /* rightshift */
1752          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1753          16,                    /* bitsize */
1754          FALSE,                 /* pc_relative */
1755          0,                     /* bitpos */
1756          complain_overflow_dont, /* complain_on_overflow */
1757          ppc64_elf_unhandled_reloc, /* special_function */
1758          "R_PPC64_GOT_TLSLD16_LO", /* name */
1759          FALSE,                 /* partial_inplace */
1760          0,                     /* src_mask */
1761          0xffff,                /* dst_mask */
1762          FALSE),                /* pcrel_offset */
1763
1764   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1765   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1766          16,                    /* rightshift */
1767          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1768          16,                    /* bitsize */
1769          FALSE,                 /* pc_relative */
1770          0,                     /* bitpos */
1771          complain_overflow_signed, /* complain_on_overflow */
1772          ppc64_elf_unhandled_reloc, /* special_function */
1773          "R_PPC64_GOT_TLSLD16_HI", /* name */
1774          FALSE,                 /* partial_inplace */
1775          0,                     /* src_mask */
1776          0xffff,                /* dst_mask */
1777          FALSE),                /* pcrel_offset */
1778
1779   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1780   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1781          16,                    /* rightshift */
1782          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1783          16,                    /* bitsize */
1784          FALSE,                 /* pc_relative */
1785          0,                     /* bitpos */
1786          complain_overflow_signed, /* complain_on_overflow */
1787          ppc64_elf_unhandled_reloc, /* special_function */
1788          "R_PPC64_GOT_TLSLD16_HA", /* name */
1789          FALSE,                 /* partial_inplace */
1790          0,                     /* src_mask */
1791          0xffff,                /* dst_mask */
1792          FALSE),                /* pcrel_offset */
1793
1794   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1795      the offset to the entry relative to the TOC base (r2).  */
1796   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1797          0,                     /* rightshift */
1798          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1799          16,                    /* bitsize */
1800          FALSE,                 /* pc_relative */
1801          0,                     /* bitpos */
1802          complain_overflow_signed, /* complain_on_overflow */
1803          ppc64_elf_unhandled_reloc, /* special_function */
1804          "R_PPC64_GOT_DTPREL16_DS", /* name */
1805          FALSE,                 /* partial_inplace */
1806          0,                     /* src_mask */
1807          0xfffc,                /* dst_mask */
1808          FALSE),                /* pcrel_offset */
1809
1810   /* Like GOT_DTPREL16_DS, but no overflow.  */
1811   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1812          0,                     /* rightshift */
1813          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1814          16,                    /* bitsize */
1815          FALSE,                 /* pc_relative */
1816          0,                     /* bitpos */
1817          complain_overflow_dont, /* complain_on_overflow */
1818          ppc64_elf_unhandled_reloc, /* special_function */
1819          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1820          FALSE,                 /* partial_inplace */
1821          0,                     /* src_mask */
1822          0xfffc,                /* dst_mask */
1823          FALSE),                /* pcrel_offset */
1824
1825   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1826   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1827          16,                    /* rightshift */
1828          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1829          16,                    /* bitsize */
1830          FALSE,                 /* pc_relative */
1831          0,                     /* bitpos */
1832          complain_overflow_signed, /* complain_on_overflow */
1833          ppc64_elf_unhandled_reloc, /* special_function */
1834          "R_PPC64_GOT_DTPREL16_HI", /* name */
1835          FALSE,                 /* partial_inplace */
1836          0,                     /* src_mask */
1837          0xffff,                /* dst_mask */
1838          FALSE),                /* pcrel_offset */
1839
1840   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1841   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1842          16,                    /* rightshift */
1843          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1844          16,                    /* bitsize */
1845          FALSE,                 /* pc_relative */
1846          0,                     /* bitpos */
1847          complain_overflow_signed, /* complain_on_overflow */
1848          ppc64_elf_unhandled_reloc, /* special_function */
1849          "R_PPC64_GOT_DTPREL16_HA", /* name */
1850          FALSE,                 /* partial_inplace */
1851          0,                     /* src_mask */
1852          0xffff,                /* dst_mask */
1853          FALSE),                /* pcrel_offset */
1854
1855   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1856      offset to the entry relative to the TOC base (r2).  */
1857   HOWTO (R_PPC64_GOT_TPREL16_DS,
1858          0,                     /* rightshift */
1859          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1860          16,                    /* bitsize */
1861          FALSE,                 /* pc_relative */
1862          0,                     /* bitpos */
1863          complain_overflow_signed, /* complain_on_overflow */
1864          ppc64_elf_unhandled_reloc, /* special_function */
1865          "R_PPC64_GOT_TPREL16_DS", /* name */
1866          FALSE,                 /* partial_inplace */
1867          0,                     /* src_mask */
1868          0xfffc,                /* dst_mask */
1869          FALSE),                /* pcrel_offset */
1870
1871   /* Like GOT_TPREL16_DS, but no overflow.  */
1872   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1873          0,                     /* rightshift */
1874          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1875          16,                    /* bitsize */
1876          FALSE,                 /* pc_relative */
1877          0,                     /* bitpos */
1878          complain_overflow_dont, /* complain_on_overflow */
1879          ppc64_elf_unhandled_reloc, /* special_function */
1880          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1881          FALSE,                 /* partial_inplace */
1882          0,                     /* src_mask */
1883          0xfffc,                /* dst_mask */
1884          FALSE),                /* pcrel_offset */
1885
1886   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1887   HOWTO (R_PPC64_GOT_TPREL16_HI,
1888          16,                    /* rightshift */
1889          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1890          16,                    /* bitsize */
1891          FALSE,                 /* pc_relative */
1892          0,                     /* bitpos */
1893          complain_overflow_signed, /* complain_on_overflow */
1894          ppc64_elf_unhandled_reloc, /* special_function */
1895          "R_PPC64_GOT_TPREL16_HI", /* name */
1896          FALSE,                 /* partial_inplace */
1897          0,                     /* src_mask */
1898          0xffff,                /* dst_mask */
1899          FALSE),                /* pcrel_offset */
1900
1901   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1902   HOWTO (R_PPC64_GOT_TPREL16_HA,
1903          16,                    /* rightshift */
1904          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1905          16,                    /* bitsize */
1906          FALSE,                 /* pc_relative */
1907          0,                     /* bitpos */
1908          complain_overflow_signed, /* complain_on_overflow */
1909          ppc64_elf_unhandled_reloc, /* special_function */
1910          "R_PPC64_GOT_TPREL16_HA", /* name */
1911          FALSE,                 /* partial_inplace */
1912          0,                     /* src_mask */
1913          0xffff,                /* dst_mask */
1914          FALSE),                /* pcrel_offset */
1915
1916   HOWTO (R_PPC64_JMP_IREL,      /* type */
1917          0,                     /* rightshift */
1918          0,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1919          0,                     /* bitsize */
1920          FALSE,                 /* pc_relative */
1921          0,                     /* bitpos */
1922          complain_overflow_dont, /* complain_on_overflow */
1923          ppc64_elf_unhandled_reloc, /* special_function */
1924          "R_PPC64_JMP_IREL",    /* name */
1925          FALSE,                 /* partial_inplace */
1926          0,                     /* src_mask */
1927          0,                     /* dst_mask */
1928          FALSE),                /* pcrel_offset */
1929
1930   HOWTO (R_PPC64_IRELATIVE,     /* type */
1931          0,                     /* rightshift */
1932          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1933          64,                    /* bitsize */
1934          FALSE,                 /* pc_relative */
1935          0,                     /* bitpos */
1936          complain_overflow_dont, /* complain_on_overflow */
1937          bfd_elf_generic_reloc, /* special_function */
1938          "R_PPC64_IRELATIVE",   /* name */
1939          FALSE,                 /* partial_inplace */
1940          0,                     /* src_mask */
1941          ONES (64),             /* dst_mask */
1942          FALSE),                /* pcrel_offset */
1943
1944   /* A 16 bit relative relocation.  */
1945   HOWTO (R_PPC64_REL16,         /* type */
1946          0,                     /* rightshift */
1947          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1948          16,                    /* bitsize */
1949          TRUE,                  /* pc_relative */
1950          0,                     /* bitpos */
1951          complain_overflow_bitfield, /* complain_on_overflow */
1952          bfd_elf_generic_reloc, /* special_function */
1953          "R_PPC64_REL16",       /* name */
1954          FALSE,                 /* partial_inplace */
1955          0,                     /* src_mask */
1956          0xffff,                /* dst_mask */
1957          TRUE),                 /* pcrel_offset */
1958
1959   /* A 16 bit relative relocation without overflow.  */
1960   HOWTO (R_PPC64_REL16_LO,      /* type */
1961          0,                     /* rightshift */
1962          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1963          16,                    /* bitsize */
1964          TRUE,                  /* pc_relative */
1965          0,                     /* bitpos */
1966          complain_overflow_dont,/* complain_on_overflow */
1967          bfd_elf_generic_reloc, /* special_function */
1968          "R_PPC64_REL16_LO",    /* name */
1969          FALSE,                 /* partial_inplace */
1970          0,                     /* src_mask */
1971          0xffff,                /* dst_mask */
1972          TRUE),                 /* pcrel_offset */
1973
1974   /* The high order 16 bits of a relative address.  */
1975   HOWTO (R_PPC64_REL16_HI,      /* type */
1976          16,                    /* rightshift */
1977          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1978          16,                    /* bitsize */
1979          TRUE,                  /* pc_relative */
1980          0,                     /* bitpos */
1981          complain_overflow_signed, /* complain_on_overflow */
1982          bfd_elf_generic_reloc, /* special_function */
1983          "R_PPC64_REL16_HI",    /* name */
1984          FALSE,                 /* partial_inplace */
1985          0,                     /* src_mask */
1986          0xffff,                /* dst_mask */
1987          TRUE),                 /* pcrel_offset */
1988
1989   /* The high order 16 bits of a relative address, plus 1 if the contents of
1990      the low 16 bits, treated as a signed number, is negative.  */
1991   HOWTO (R_PPC64_REL16_HA,      /* type */
1992          16,                    /* rightshift */
1993          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1994          16,                    /* bitsize */
1995          TRUE,                  /* pc_relative */
1996          0,                     /* bitpos */
1997          complain_overflow_signed, /* complain_on_overflow */
1998          ppc64_elf_ha_reloc,    /* special_function */
1999          "R_PPC64_REL16_HA",    /* name */
2000          FALSE,                 /* partial_inplace */
2001          0,                     /* src_mask */
2002          0xffff,                /* dst_mask */
2003          TRUE),                 /* pcrel_offset */
2004
2005   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
2006   HOWTO (R_PPC64_ADDR16_HIGH,   /* type */
2007          16,                    /* rightshift */
2008          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2009          16,                    /* bitsize */
2010          FALSE,                 /* pc_relative */
2011          0,                     /* bitpos */
2012          complain_overflow_dont, /* complain_on_overflow */
2013          bfd_elf_generic_reloc, /* special_function */
2014          "R_PPC64_ADDR16_HIGH", /* name */
2015          FALSE,                 /* partial_inplace */
2016          0,                     /* src_mask */
2017          0xffff,                /* dst_mask */
2018          FALSE),                /* pcrel_offset */
2019
2020   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
2021   HOWTO (R_PPC64_ADDR16_HIGHA,  /* type */
2022          16,                    /* rightshift */
2023          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2024          16,                    /* bitsize */
2025          FALSE,                 /* pc_relative */
2026          0,                     /* bitpos */
2027          complain_overflow_dont, /* complain_on_overflow */
2028          ppc64_elf_ha_reloc,    /* special_function */
2029          "R_PPC64_ADDR16_HIGHA",        /* name */
2030          FALSE,                 /* partial_inplace */
2031          0,                     /* src_mask */
2032          0xffff,                /* dst_mask */
2033          FALSE),                /* pcrel_offset */
2034
2035   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
2036   HOWTO (R_PPC64_DTPREL16_HIGH,
2037          16,                    /* rightshift */
2038          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2039          16,                    /* bitsize */
2040          FALSE,                 /* pc_relative */
2041          0,                     /* bitpos */
2042          complain_overflow_dont, /* complain_on_overflow */
2043          ppc64_elf_unhandled_reloc, /* special_function */
2044          "R_PPC64_DTPREL16_HIGH", /* name */
2045          FALSE,                 /* partial_inplace */
2046          0,                     /* src_mask */
2047          0xffff,                /* dst_mask */
2048          FALSE),                /* pcrel_offset */
2049
2050   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
2051   HOWTO (R_PPC64_DTPREL16_HIGHA,
2052          16,                    /* rightshift */
2053          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2054          16,                    /* bitsize */
2055          FALSE,                 /* pc_relative */
2056          0,                     /* bitpos */
2057          complain_overflow_dont, /* complain_on_overflow */
2058          ppc64_elf_unhandled_reloc, /* special_function */
2059          "R_PPC64_DTPREL16_HIGHA", /* name */
2060          FALSE,                 /* partial_inplace */
2061          0,                     /* src_mask */
2062          0xffff,                /* dst_mask */
2063          FALSE),                /* pcrel_offset */
2064
2065   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
2066   HOWTO (R_PPC64_TPREL16_HIGH,
2067          16,                    /* rightshift */
2068          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2069          16,                    /* bitsize */
2070          FALSE,                 /* pc_relative */
2071          0,                     /* bitpos */
2072          complain_overflow_dont, /* complain_on_overflow */
2073          ppc64_elf_unhandled_reloc, /* special_function */
2074          "R_PPC64_TPREL16_HIGH",        /* name */
2075          FALSE,                 /* partial_inplace */
2076          0,                     /* src_mask */
2077          0xffff,                /* dst_mask */
2078          FALSE),                /* pcrel_offset */
2079
2080   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
2081   HOWTO (R_PPC64_TPREL16_HIGHA,
2082          16,                    /* rightshift */
2083          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2084          16,                    /* bitsize */
2085          FALSE,                 /* pc_relative */
2086          0,                     /* bitpos */
2087          complain_overflow_dont, /* complain_on_overflow */
2088          ppc64_elf_unhandled_reloc, /* special_function */
2089          "R_PPC64_TPREL16_HIGHA",       /* name */
2090          FALSE,                 /* partial_inplace */
2091          0,                     /* src_mask */
2092          0xffff,                /* dst_mask */
2093          FALSE),                /* pcrel_offset */
2094
2095   /* GNU extension to record C++ vtable hierarchy.  */
2096   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2097          0,                     /* rightshift */
2098          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2099          0,                     /* bitsize */
2100          FALSE,                 /* pc_relative */
2101          0,                     /* bitpos */
2102          complain_overflow_dont, /* complain_on_overflow */
2103          NULL,                  /* special_function */
2104          "R_PPC64_GNU_VTINHERIT", /* name */
2105          FALSE,                 /* partial_inplace */
2106          0,                     /* src_mask */
2107          0,                     /* dst_mask */
2108          FALSE),                /* pcrel_offset */
2109
2110   /* GNU extension to record C++ vtable member usage.  */
2111   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
2112          0,                     /* rightshift */
2113          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2114          0,                     /* bitsize */
2115          FALSE,                 /* pc_relative */
2116          0,                     /* bitpos */
2117          complain_overflow_dont, /* complain_on_overflow */
2118          NULL,                  /* special_function */
2119          "R_PPC64_GNU_VTENTRY", /* name */
2120          FALSE,                 /* partial_inplace */
2121          0,                     /* src_mask */
2122          0,                     /* dst_mask */
2123          FALSE),                /* pcrel_offset */
2124 };
2125
2126 \f
2127 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2128    be done.  */
2129
2130 static void
2131 ppc_howto_init (void)
2132 {
2133   unsigned int i, type;
2134
2135   for (i = 0;
2136        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2137        i++)
2138     {
2139       type = ppc64_elf_howto_raw[i].type;
2140       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2141                           / sizeof (ppc64_elf_howto_table[0])));
2142       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2143     }
2144 }
2145
2146 static reloc_howto_type *
2147 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2148                              bfd_reloc_code_real_type code)
2149 {
2150   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2151
2152   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2153     /* Initialize howto table if needed.  */
2154     ppc_howto_init ();
2155
2156   switch (code)
2157     {
2158     default:
2159       return NULL;
2160
2161     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
2162       break;
2163     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
2164       break;
2165     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
2166       break;
2167     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
2168       break;
2169     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
2170       break;
2171     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
2172       break;
2173     case BFD_RELOC_PPC64_ADDR16_HIGH:           r = R_PPC64_ADDR16_HIGH;
2174       break;
2175     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
2176       break;
2177     case BFD_RELOC_PPC64_ADDR16_HIGHA:          r = R_PPC64_ADDR16_HIGHA;
2178       break;
2179     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
2180       break;
2181     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
2182       break;
2183     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
2184       break;
2185     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
2186       break;
2187     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
2188       break;
2189     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
2190       break;
2191     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
2192       break;
2193     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
2194       break;
2195     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
2196       break;
2197     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
2198       break;
2199     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
2200       break;
2201     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
2202       break;
2203     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
2204       break;
2205     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
2206       break;
2207     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
2208       break;
2209     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
2210       break;
2211     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
2212       break;
2213     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
2214       break;
2215     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
2216       break;
2217     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
2218       break;
2219     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
2220       break;
2221     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
2222       break;
2223     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
2224       break;
2225     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
2226       break;
2227     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
2228       break;
2229     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
2230       break;
2231     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
2232       break;
2233     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
2234       break;
2235     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
2236       break;
2237     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
2238       break;
2239     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
2240       break;
2241     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
2242       break;
2243     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
2244       break;
2245     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
2246       break;
2247     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
2248       break;
2249     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
2250       break;
2251     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
2252       break;
2253     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
2254       break;
2255     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
2256       break;
2257     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
2258       break;
2259     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
2260       break;
2261     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
2262       break;
2263     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
2264       break;
2265     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
2266       break;
2267     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
2268       break;
2269     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
2270       break;
2271     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
2272       break;
2273     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2274       break;
2275     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2276       break;
2277     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2278       break;
2279     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2280       break;
2281     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2282       break;
2283     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2284       break;
2285     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
2286       break;
2287     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
2288       break;
2289     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2290       break;
2291     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2292       break;
2293     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2294       break;
2295     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2296       break;
2297     case BFD_RELOC_PPC64_TPREL16_HIGH:          r = R_PPC64_TPREL16_HIGH;
2298       break;
2299     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2300       break;
2301     case BFD_RELOC_PPC64_TPREL16_HIGHA:         r = R_PPC64_TPREL16_HIGHA;
2302       break;
2303     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2304       break;
2305     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2306       break;
2307     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2308       break;
2309     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2310       break;
2311     case BFD_RELOC_PPC64_DTPREL16_HIGH:         r = R_PPC64_DTPREL16_HIGH;
2312       break;
2313     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2314       break;
2315     case BFD_RELOC_PPC64_DTPREL16_HIGHA:        r = R_PPC64_DTPREL16_HIGHA;
2316       break;
2317     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2318       break;
2319     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2320       break;
2321     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2322       break;
2323     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2324       break;
2325     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2326       break;
2327     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2328       break;
2329     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2330       break;
2331     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2332       break;
2333     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2334       break;
2335     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2336       break;
2337     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2338       break;
2339     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2340       break;
2341     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2342       break;
2343     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2344       break;
2345     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2346       break;
2347     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2348       break;
2349     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2350       break;
2351     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2352       break;
2353     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2354       break;
2355     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2356       break;
2357     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2358       break;
2359     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2360       break;
2361     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2362       break;
2363     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2364       break;
2365     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2366       break;
2367     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2368       break;
2369     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2370       break;
2371     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2372       break;
2373     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2374       break;
2375     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
2376       break;
2377     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
2378       break;
2379     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
2380       break;
2381     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
2382       break;
2383     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2384       break;
2385     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2386       break;
2387     }
2388
2389   return ppc64_elf_howto_table[r];
2390 };
2391
2392 static reloc_howto_type *
2393 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2394                              const char *r_name)
2395 {
2396   unsigned int i;
2397
2398   for (i = 0;
2399        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2400        i++)
2401     if (ppc64_elf_howto_raw[i].name != NULL
2402         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2403       return &ppc64_elf_howto_raw[i];
2404
2405   return NULL;
2406 }
2407
2408 /* Set the howto pointer for a PowerPC ELF reloc.  */
2409
2410 static void
2411 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2412                          Elf_Internal_Rela *dst)
2413 {
2414   unsigned int type;
2415
2416   /* Initialize howto table if needed.  */
2417   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2418     ppc_howto_init ();
2419
2420   type = ELF64_R_TYPE (dst->r_info);
2421   if (type >= (sizeof (ppc64_elf_howto_table)
2422                / sizeof (ppc64_elf_howto_table[0])))
2423     {
2424       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2425                              abfd, (int) type);
2426       type = R_PPC64_NONE;
2427     }
2428   cache_ptr->howto = ppc64_elf_howto_table[type];
2429 }
2430
2431 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2432
2433 static bfd_reloc_status_type
2434 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2435                     void *data, asection *input_section,
2436                     bfd *output_bfd, char **error_message)
2437 {
2438   /* If this is a relocatable link (output_bfd test tells us), just
2439      call the generic function.  Any adjustment will be done at final
2440      link time.  */
2441   if (output_bfd != NULL)
2442     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2443                                   input_section, output_bfd, error_message);
2444
2445   /* Adjust the addend for sign extension of the low 16 bits.
2446      We won't actually be using the low 16 bits, so trashing them
2447      doesn't matter.  */
2448   reloc_entry->addend += 0x8000;
2449   return bfd_reloc_continue;
2450 }
2451
2452 static bfd_reloc_status_type
2453 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2454                         void *data, asection *input_section,
2455                         bfd *output_bfd, char **error_message)
2456 {
2457   if (output_bfd != NULL)
2458     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2459                                   input_section, output_bfd, error_message);
2460
2461   if (strcmp (symbol->section->name, ".opd") == 0
2462       && (symbol->section->owner->flags & DYNAMIC) == 0)
2463     {
2464       bfd_vma dest = opd_entry_value (symbol->section,
2465                                       symbol->value + reloc_entry->addend,
2466                                       NULL, NULL, FALSE);
2467       if (dest != (bfd_vma) -1)
2468         reloc_entry->addend = dest - (symbol->value
2469                                       + symbol->section->output_section->vma
2470                                       + symbol->section->output_offset);
2471     }
2472   return bfd_reloc_continue;
2473 }
2474
2475 static bfd_reloc_status_type
2476 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2477                          void *data, asection *input_section,
2478                          bfd *output_bfd, char **error_message)
2479 {
2480   long insn;
2481   enum elf_ppc64_reloc_type r_type;
2482   bfd_size_type octets;
2483   /* Assume 'at' branch hints.  */
2484   bfd_boolean is_isa_v2 = TRUE;
2485
2486   /* If this is a relocatable link (output_bfd test tells us), just
2487      call the generic function.  Any adjustment will be done at final
2488      link time.  */
2489   if (output_bfd != NULL)
2490     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2491                                   input_section, output_bfd, error_message);
2492
2493   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2494   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2495   insn &= ~(0x01 << 21);
2496   r_type = reloc_entry->howto->type;
2497   if (r_type == R_PPC64_ADDR14_BRTAKEN
2498       || r_type == R_PPC64_REL14_BRTAKEN)
2499     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2500
2501   if (is_isa_v2)
2502     {
2503       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2504          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2505          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2506       if ((insn & (0x14 << 21)) == (0x04 << 21))
2507         insn |= 0x02 << 21;
2508       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2509         insn |= 0x08 << 21;
2510       else
2511         goto out;
2512     }
2513   else
2514     {
2515       bfd_vma target = 0;
2516       bfd_vma from;
2517
2518       if (!bfd_is_com_section (symbol->section))
2519         target = symbol->value;
2520       target += symbol->section->output_section->vma;
2521       target += symbol->section->output_offset;
2522       target += reloc_entry->addend;
2523
2524       from = (reloc_entry->address
2525               + input_section->output_offset
2526               + input_section->output_section->vma);
2527
2528       /* Invert 'y' bit if not the default.  */
2529       if ((bfd_signed_vma) (target - from) < 0)
2530         insn ^= 0x01 << 21;
2531     }
2532   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2533  out:
2534   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2535                                  input_section, output_bfd, error_message);
2536 }
2537
2538 static bfd_reloc_status_type
2539 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2540                          void *data, asection *input_section,
2541                          bfd *output_bfd, char **error_message)
2542 {
2543   /* If this is a relocatable link (output_bfd test tells us), just
2544      call the generic function.  Any adjustment will be done at final
2545      link time.  */
2546   if (output_bfd != NULL)
2547     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2548                                   input_section, output_bfd, error_message);
2549
2550   /* Subtract the symbol section base address.  */
2551   reloc_entry->addend -= symbol->section->output_section->vma;
2552   return bfd_reloc_continue;
2553 }
2554
2555 static bfd_reloc_status_type
2556 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2557                             void *data, asection *input_section,
2558                             bfd *output_bfd, char **error_message)
2559 {
2560   /* If this is a relocatable link (output_bfd test tells us), just
2561      call the generic function.  Any adjustment will be done at final
2562      link time.  */
2563   if (output_bfd != NULL)
2564     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2565                                   input_section, output_bfd, error_message);
2566
2567   /* Subtract the symbol section base address.  */
2568   reloc_entry->addend -= symbol->section->output_section->vma;
2569
2570   /* Adjust the addend for sign extension of the low 16 bits.  */
2571   reloc_entry->addend += 0x8000;
2572   return bfd_reloc_continue;
2573 }
2574
2575 static bfd_reloc_status_type
2576 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2577                      void *data, asection *input_section,
2578                      bfd *output_bfd, char **error_message)
2579 {
2580   bfd_vma TOCstart;
2581
2582   /* If this is a relocatable link (output_bfd test tells us), just
2583      call the generic function.  Any adjustment will be done at final
2584      link time.  */
2585   if (output_bfd != NULL)
2586     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2587                                   input_section, output_bfd, error_message);
2588
2589   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2590   if (TOCstart == 0)
2591     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2592
2593   /* Subtract the TOC base address.  */
2594   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2595   return bfd_reloc_continue;
2596 }
2597
2598 static bfd_reloc_status_type
2599 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2600                         void *data, asection *input_section,
2601                         bfd *output_bfd, char **error_message)
2602 {
2603   bfd_vma TOCstart;
2604
2605   /* If this is a relocatable link (output_bfd test tells us), just
2606      call the generic function.  Any adjustment will be done at final
2607      link time.  */
2608   if (output_bfd != NULL)
2609     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2610                                   input_section, output_bfd, error_message);
2611
2612   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2613   if (TOCstart == 0)
2614     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2615
2616   /* Subtract the TOC base address.  */
2617   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2618
2619   /* Adjust the addend for sign extension of the low 16 bits.  */
2620   reloc_entry->addend += 0x8000;
2621   return bfd_reloc_continue;
2622 }
2623
2624 static bfd_reloc_status_type
2625 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2626                        void *data, asection *input_section,
2627                        bfd *output_bfd, char **error_message)
2628 {
2629   bfd_vma TOCstart;
2630   bfd_size_type octets;
2631
2632   /* If this is a relocatable link (output_bfd test tells us), just
2633      call the generic function.  Any adjustment will be done at final
2634      link time.  */
2635   if (output_bfd != NULL)
2636     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2637                                   input_section, output_bfd, error_message);
2638
2639   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2640   if (TOCstart == 0)
2641     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2642
2643   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2644   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2645   return bfd_reloc_ok;
2646 }
2647
2648 static bfd_reloc_status_type
2649 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2650                            void *data, asection *input_section,
2651                            bfd *output_bfd, char **error_message)
2652 {
2653   /* If this is a relocatable link (output_bfd test tells us), just
2654      call the generic function.  Any adjustment will be done at final
2655      link time.  */
2656   if (output_bfd != NULL)
2657     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2658                                   input_section, output_bfd, error_message);
2659
2660   if (error_message != NULL)
2661     {
2662       static char buf[60];
2663       sprintf (buf, "generic linker can't handle %s",
2664                reloc_entry->howto->name);
2665       *error_message = buf;
2666     }
2667   return bfd_reloc_dangerous;
2668 }
2669
2670 /* Track GOT entries needed for a given symbol.  We might need more
2671    than one got entry per symbol.  */
2672 struct got_entry
2673 {
2674   struct got_entry *next;
2675
2676   /* The symbol addend that we'll be placing in the GOT.  */
2677   bfd_vma addend;
2678
2679   /* Unlike other ELF targets, we use separate GOT entries for the same
2680      symbol referenced from different input files.  This is to support
2681      automatic multiple TOC/GOT sections, where the TOC base can vary
2682      from one input file to another.  After partitioning into TOC groups
2683      we merge entries within the group.
2684
2685      Point to the BFD owning this GOT entry.  */
2686   bfd *owner;
2687
2688   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2689      TLS_TPREL or TLS_DTPREL for tls entries.  */
2690   unsigned char tls_type;
2691
2692   /* Non-zero if got.ent points to real entry.  */
2693   unsigned char is_indirect;
2694
2695   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2696   union
2697     {
2698       bfd_signed_vma refcount;
2699       bfd_vma offset;
2700       struct got_entry *ent;
2701     } got;
2702 };
2703
2704 /* The same for PLT.  */
2705 struct plt_entry
2706 {
2707   struct plt_entry *next;
2708
2709   bfd_vma addend;
2710
2711   union
2712     {
2713       bfd_signed_vma refcount;
2714       bfd_vma offset;
2715     } plt;
2716 };
2717
2718 struct ppc64_elf_obj_tdata
2719 {
2720   struct elf_obj_tdata elf;
2721
2722   /* Shortcuts to dynamic linker sections.  */
2723   asection *got;
2724   asection *relgot;
2725
2726   /* Used during garbage collection.  We attach global symbols defined
2727      on removed .opd entries to this section so that the sym is removed.  */
2728   asection *deleted_section;
2729
2730   /* TLS local dynamic got entry handling.  Support for multiple GOT
2731      sections means we potentially need one of these for each input bfd.  */
2732   struct got_entry tlsld_got;
2733
2734   union {
2735     /* A copy of relocs before they are modified for --emit-relocs.  */
2736     Elf_Internal_Rela *relocs;
2737
2738     /* Section contents.  */
2739     bfd_byte *contents;
2740   } opd;
2741
2742   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2743      the reloc to be in the range -32768 to 32767.  */
2744   unsigned int has_small_toc_reloc : 1;
2745
2746   /* Set if toc/got ha relocs detected not using r2, or lo reloc
2747      instruction not one we handle.  */
2748   unsigned int unexpected_toc_insn : 1;
2749 };
2750
2751 #define ppc64_elf_tdata(bfd) \
2752   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2753
2754 #define ppc64_tlsld_got(bfd) \
2755   (&ppc64_elf_tdata (bfd)->tlsld_got)
2756
2757 #define is_ppc64_elf(bfd) \
2758   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2759    && elf_object_id (bfd) == PPC64_ELF_DATA)
2760
2761 /* Override the generic function because we store some extras.  */
2762
2763 static bfd_boolean
2764 ppc64_elf_mkobject (bfd *abfd)
2765 {
2766   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2767                                   PPC64_ELF_DATA);
2768 }
2769
2770 /* Fix bad default arch selected for a 64 bit input bfd when the
2771    default is 32 bit.  */
2772
2773 static bfd_boolean
2774 ppc64_elf_object_p (bfd *abfd)
2775 {
2776   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2777     {
2778       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2779
2780       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2781         {
2782           /* Relies on arch after 32 bit default being 64 bit default.  */
2783           abfd->arch_info = abfd->arch_info->next;
2784           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2785         }
2786     }
2787   return TRUE;
2788 }
2789
2790 /* Support for core dump NOTE sections.  */
2791
2792 static bfd_boolean
2793 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2794 {
2795   size_t offset, size;
2796
2797   if (note->descsz != 504)
2798     return FALSE;
2799
2800   /* pr_cursig */
2801   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2802
2803   /* pr_pid */
2804   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2805
2806   /* pr_reg */
2807   offset = 112;
2808   size = 384;
2809
2810   /* Make a ".reg/999" section.  */
2811   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2812                                           size, note->descpos + offset);
2813 }
2814
2815 static bfd_boolean
2816 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2817 {
2818   if (note->descsz != 136)
2819     return FALSE;
2820
2821   elf_tdata (abfd)->core->pid
2822     = bfd_get_32 (abfd, note->descdata + 24);
2823   elf_tdata (abfd)->core->program
2824     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2825   elf_tdata (abfd)->core->command
2826     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2827
2828   return TRUE;
2829 }
2830
2831 static char *
2832 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2833                            ...)
2834 {
2835   switch (note_type)
2836     {
2837     default:
2838       return NULL;
2839
2840     case NT_PRPSINFO:
2841       {
2842         char data[136];
2843         va_list ap;
2844
2845         va_start (ap, note_type);
2846         memset (data, 0, sizeof (data));
2847         strncpy (data + 40, va_arg (ap, const char *), 16);
2848         strncpy (data + 56, va_arg (ap, const char *), 80);
2849         va_end (ap);
2850         return elfcore_write_note (abfd, buf, bufsiz,
2851                                    "CORE", note_type, data, sizeof (data));
2852       }
2853
2854     case NT_PRSTATUS:
2855       {
2856         char data[504];
2857         va_list ap;
2858         long pid;
2859         int cursig;
2860         const void *greg;
2861
2862         va_start (ap, note_type);
2863         memset (data, 0, 112);
2864         pid = va_arg (ap, long);
2865         bfd_put_32 (abfd, pid, data + 32);
2866         cursig = va_arg (ap, int);
2867         bfd_put_16 (abfd, cursig, data + 12);
2868         greg = va_arg (ap, const void *);
2869         memcpy (data + 112, greg, 384);
2870         memset (data + 496, 0, 8);
2871         va_end (ap);
2872         return elfcore_write_note (abfd, buf, bufsiz,
2873                                    "CORE", note_type, data, sizeof (data));
2874       }
2875     }
2876 }
2877
2878 /* Add extra PPC sections.  */
2879
2880 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2881 {
2882   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
2883   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2884   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2885   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2886   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2887   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2888   { NULL,                     0,  0, 0,            0 }
2889 };
2890
2891 enum _ppc64_sec_type {
2892   sec_normal = 0,
2893   sec_opd = 1,
2894   sec_toc = 2
2895 };
2896
2897 struct _ppc64_elf_section_data
2898 {
2899   struct bfd_elf_section_data elf;
2900
2901   union
2902   {
2903     /* An array with one entry for each opd function descriptor.  */
2904     struct _opd_sec_data
2905     {
2906       /* Points to the function code section for local opd entries.  */
2907       asection **func_sec;
2908
2909       /* After editing .opd, adjust references to opd local syms.  */
2910       long *adjust;
2911     } opd;
2912
2913     /* An array for toc sections, indexed by offset/8.  */
2914     struct _toc_sec_data
2915     {
2916       /* Specifies the relocation symbol index used at a given toc offset.  */
2917       unsigned *symndx;
2918
2919       /* And the relocation addend.  */
2920       bfd_vma *add;
2921     } toc;
2922   } u;
2923
2924   enum _ppc64_sec_type sec_type:2;
2925
2926   /* Flag set when small branches are detected.  Used to
2927      select suitable defaults for the stub group size.  */
2928   unsigned int has_14bit_branch:1;
2929 };
2930
2931 #define ppc64_elf_section_data(sec) \
2932   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2933
2934 static bfd_boolean
2935 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2936 {
2937   if (!sec->used_by_bfd)
2938     {
2939       struct _ppc64_elf_section_data *sdata;
2940       bfd_size_type amt = sizeof (*sdata);
2941
2942       sdata = bfd_zalloc (abfd, amt);
2943       if (sdata == NULL)
2944         return FALSE;
2945       sec->used_by_bfd = sdata;
2946     }
2947
2948   return _bfd_elf_new_section_hook (abfd, sec);
2949 }
2950
2951 static struct _opd_sec_data *
2952 get_opd_info (asection * sec)
2953 {
2954   if (sec != NULL
2955       && ppc64_elf_section_data (sec) != NULL
2956       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2957     return &ppc64_elf_section_data (sec)->u.opd;
2958   return NULL;
2959 }
2960
2961 static inline int
2962 abiversion (bfd *abfd)
2963 {
2964   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
2965 }
2966
2967 static inline void
2968 set_abiversion (bfd *abfd, int ver)
2969 {
2970   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
2971   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
2972 }
2973 \f
2974 /* Parameters for the qsort hook.  */
2975 static bfd_boolean synthetic_relocatable;
2976
2977 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
2978
2979 static int
2980 compare_symbols (const void *ap, const void *bp)
2981 {
2982   const asymbol *a = * (const asymbol **) ap;
2983   const asymbol *b = * (const asymbol **) bp;
2984
2985   /* Section symbols first.  */
2986   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2987     return -1;
2988   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2989     return 1;
2990
2991   /* then .opd symbols.  */
2992   if (strcmp (a->section->name, ".opd") == 0
2993       && strcmp (b->section->name, ".opd") != 0)
2994     return -1;
2995   if (strcmp (a->section->name, ".opd") != 0
2996       && strcmp (b->section->name, ".opd") == 0)
2997     return 1;
2998
2999   /* then other code symbols.  */
3000   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3001       == (SEC_CODE | SEC_ALLOC)
3002       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3003          != (SEC_CODE | SEC_ALLOC))
3004     return -1;
3005
3006   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3007       != (SEC_CODE | SEC_ALLOC)
3008       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3009          == (SEC_CODE | SEC_ALLOC))
3010     return 1;
3011
3012   if (synthetic_relocatable)
3013     {
3014       if (a->section->id < b->section->id)
3015         return -1;
3016
3017       if (a->section->id > b->section->id)
3018         return 1;
3019     }
3020
3021   if (a->value + a->section->vma < b->value + b->section->vma)
3022     return -1;
3023
3024   if (a->value + a->section->vma > b->value + b->section->vma)
3025     return 1;
3026
3027   /* For syms with the same value, prefer strong dynamic global function
3028      syms over other syms.  */
3029   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3030     return -1;
3031
3032   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3033     return 1;
3034
3035   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3036     return -1;
3037
3038   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3039     return 1;
3040
3041   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3042     return -1;
3043
3044   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3045     return 1;
3046
3047   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3048     return -1;
3049
3050   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3051     return 1;
3052
3053   return 0;
3054 }
3055
3056 /* Search SYMS for a symbol of the given VALUE.  */
3057
3058 static asymbol *
3059 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
3060 {
3061   long mid;
3062
3063   if (id == -1)
3064     {
3065       while (lo < hi)
3066         {
3067           mid = (lo + hi) >> 1;
3068           if (syms[mid]->value + syms[mid]->section->vma < value)
3069             lo = mid + 1;
3070           else if (syms[mid]->value + syms[mid]->section->vma > value)
3071             hi = mid;
3072           else
3073             return syms[mid];
3074         }
3075     }
3076   else
3077     {
3078       while (lo < hi)
3079         {
3080           mid = (lo + hi) >> 1;
3081           if (syms[mid]->section->id < id)
3082             lo = mid + 1;
3083           else if (syms[mid]->section->id > id)
3084             hi = mid;
3085           else if (syms[mid]->value < value)
3086             lo = mid + 1;
3087           else if (syms[mid]->value > value)
3088             hi = mid;
3089           else
3090             return syms[mid];
3091         }
3092     }
3093   return NULL;
3094 }
3095
3096 static bfd_boolean
3097 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3098 {
3099   bfd_vma vma = *(bfd_vma *) ptr;
3100   return ((section->flags & SEC_ALLOC) != 0
3101           && section->vma <= vma
3102           && vma < section->vma + section->size);
3103 }
3104
3105 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3106    entry syms.  Also generate @plt symbols for the glink branch table.  */
3107
3108 static long
3109 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3110                                 long static_count, asymbol **static_syms,
3111                                 long dyn_count, asymbol **dyn_syms,
3112                                 asymbol **ret)
3113 {
3114   asymbol *s;
3115   long i;
3116   long count;
3117   char *names;
3118   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3119   asection *opd = NULL;
3120   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3121   asymbol **syms;
3122   int abi = abiversion (abfd);
3123
3124   *ret = NULL;
3125
3126   if (abi < 2)
3127     {
3128       opd = bfd_get_section_by_name (abfd, ".opd");
3129       if (opd == NULL && abi == 1)
3130         return 0;
3131     }
3132
3133   symcount = static_count;
3134   if (!relocatable)
3135     symcount += dyn_count;
3136   if (symcount == 0)
3137     return 0;
3138
3139   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3140   if (syms == NULL)
3141     return -1;
3142
3143   if (!relocatable && static_count != 0 && dyn_count != 0)
3144     {
3145       /* Use both symbol tables.  */
3146       memcpy (syms, static_syms, static_count * sizeof (*syms));
3147       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3148     }
3149   else if (!relocatable && static_count == 0)
3150     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3151   else
3152     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3153
3154   synthetic_relocatable = relocatable;
3155   qsort (syms, symcount, sizeof (*syms), compare_symbols);
3156
3157   if (!relocatable && symcount > 1)
3158     {
3159       long j;
3160       /* Trim duplicate syms, since we may have merged the normal and
3161          dynamic symbols.  Actually, we only care about syms that have
3162          different values, so trim any with the same value.  */
3163       for (i = 1, j = 1; i < symcount; ++i)
3164         if (syms[i - 1]->value + syms[i - 1]->section->vma
3165             != syms[i]->value + syms[i]->section->vma)
3166           syms[j++] = syms[i];
3167       symcount = j;
3168     }
3169
3170   i = 0;
3171   if (strcmp (syms[i]->section->name, ".opd") == 0)
3172     ++i;
3173   codesecsym = i;
3174
3175   for (; i < symcount; ++i)
3176     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3177          != (SEC_CODE | SEC_ALLOC))
3178         || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3179       break;
3180   codesecsymend = i;
3181
3182   for (; i < symcount; ++i)
3183     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3184       break;
3185   secsymend = i;
3186
3187   for (; i < symcount; ++i)
3188     if (strcmp (syms[i]->section->name, ".opd") != 0)
3189       break;
3190   opdsymend = i;
3191
3192   for (; i < symcount; ++i)
3193     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3194         != (SEC_CODE | SEC_ALLOC))
3195       break;
3196   symcount = i;
3197
3198   count = 0;
3199
3200   if (relocatable)
3201     {
3202       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3203       arelent *r;
3204       size_t size;
3205       long relcount;
3206
3207       if (opdsymend == secsymend)
3208         goto done;
3209
3210       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3211       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3212       if (relcount == 0)
3213         goto done;
3214
3215       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3216         {
3217           count = -1;
3218           goto done;
3219         }
3220
3221       size = 0;
3222       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3223         {
3224           asymbol *sym;
3225
3226           while (r < opd->relocation + relcount
3227                  && r->address < syms[i]->value + opd->vma)
3228             ++r;
3229
3230           if (r == opd->relocation + relcount)
3231             break;
3232
3233           if (r->address != syms[i]->value + opd->vma)
3234             continue;
3235
3236           if (r->howto->type != R_PPC64_ADDR64)
3237             continue;
3238
3239           sym = *r->sym_ptr_ptr;
3240           if (!sym_exists_at (syms, opdsymend, symcount,
3241                               sym->section->id, sym->value + r->addend))
3242             {
3243               ++count;
3244               size += sizeof (asymbol);
3245               size += strlen (syms[i]->name) + 2;
3246             }
3247         }
3248
3249       s = *ret = bfd_malloc (size);
3250       if (s == NULL)
3251         {
3252           count = -1;
3253           goto done;
3254         }
3255
3256       names = (char *) (s + count);
3257
3258       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3259         {
3260           asymbol *sym;
3261
3262           while (r < opd->relocation + relcount
3263                  && r->address < syms[i]->value + opd->vma)
3264             ++r;
3265
3266           if (r == opd->relocation + relcount)
3267             break;
3268
3269           if (r->address != syms[i]->value + opd->vma)
3270             continue;
3271
3272           if (r->howto->type != R_PPC64_ADDR64)
3273             continue;
3274
3275           sym = *r->sym_ptr_ptr;
3276           if (!sym_exists_at (syms, opdsymend, symcount,
3277                               sym->section->id, sym->value + r->addend))
3278             {
3279               size_t len;
3280
3281               *s = *syms[i];
3282               s->flags |= BSF_SYNTHETIC;
3283               s->section = sym->section;
3284               s->value = sym->value + r->addend;
3285               s->name = names;
3286               *names++ = '.';
3287               len = strlen (syms[i]->name);
3288               memcpy (names, syms[i]->name, len + 1);
3289               names += len + 1;
3290               /* Have udata.p point back to the original symbol this
3291                  synthetic symbol was derived from.  */
3292               s->udata.p = syms[i];
3293               s++;
3294             }
3295         }
3296     }
3297   else
3298     {
3299       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3300       bfd_byte *contents = NULL;
3301       size_t size;
3302       long plt_count = 0;
3303       bfd_vma glink_vma = 0, resolv_vma = 0;
3304       asection *dynamic, *glink = NULL, *relplt = NULL;
3305       arelent *p;
3306
3307       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3308         {
3309         free_contents_and_exit:
3310           if (contents)
3311             free (contents);
3312           count = -1;
3313           goto done;
3314         }
3315
3316       size = 0;
3317       for (i = secsymend; i < opdsymend; ++i)
3318         {
3319           bfd_vma ent;
3320
3321           /* Ignore bogus symbols.  */
3322           if (syms[i]->value > opd->size - 8)
3323             continue;
3324
3325           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3326           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3327             {
3328               ++count;
3329               size += sizeof (asymbol);
3330               size += strlen (syms[i]->name) + 2;
3331             }
3332         }
3333
3334       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
3335       if (dyn_count != 0
3336           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3337         {
3338           bfd_byte *dynbuf, *extdyn, *extdynend;
3339           size_t extdynsize;
3340           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3341
3342           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3343             goto free_contents_and_exit;
3344
3345           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3346           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3347
3348           extdyn = dynbuf;
3349           extdynend = extdyn + dynamic->size;
3350           for (; extdyn < extdynend; extdyn += extdynsize)
3351             {
3352               Elf_Internal_Dyn dyn;
3353               (*swap_dyn_in) (abfd, extdyn, &dyn);
3354
3355               if (dyn.d_tag == DT_NULL)
3356                 break;
3357
3358               if (dyn.d_tag == DT_PPC64_GLINK)
3359                 {
3360                   /* The first glink stub starts at offset 32; see
3361                      comment in ppc64_elf_finish_dynamic_sections. */
3362                   glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
3363                   /* The .glink section usually does not survive the final
3364                      link; search for the section (usually .text) where the
3365                      glink stubs now reside.  */
3366                   glink = bfd_sections_find_if (abfd, section_covers_vma,
3367                                                 &glink_vma);
3368                   break;
3369                 }
3370             }
3371
3372           free (dynbuf);
3373         }
3374
3375       if (glink != NULL)
3376         {
3377           /* Determine __glink trampoline by reading the relative branch
3378              from the first glink stub.  */
3379           bfd_byte buf[4];
3380           unsigned int off = 0;
3381
3382           while (bfd_get_section_contents (abfd, glink, buf,
3383                                            glink_vma + off - glink->vma, 4))
3384             {
3385               unsigned int insn = bfd_get_32 (abfd, buf);
3386               insn ^= B_DOT;
3387               if ((insn & ~0x3fffffc) == 0)
3388                 {
3389                   resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3390                   break;
3391                 }
3392               off += 4;
3393               if (off > 4)
3394                 break;
3395             }
3396
3397           if (resolv_vma)
3398             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3399
3400           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3401           if (relplt != NULL)
3402             {
3403               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3404               if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3405                 goto free_contents_and_exit;
3406
3407               plt_count = relplt->size / sizeof (Elf64_External_Rela);
3408               size += plt_count * sizeof (asymbol);
3409
3410               p = relplt->relocation;
3411               for (i = 0; i < plt_count; i++, p++)
3412                 {
3413                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3414                   if (p->addend != 0)
3415                     size += sizeof ("+0x") - 1 + 16;
3416                 }
3417             }
3418         }
3419
3420       s = *ret = bfd_malloc (size);
3421       if (s == NULL)
3422         goto free_contents_and_exit;
3423
3424       names = (char *) (s + count + plt_count + (resolv_vma != 0));
3425
3426       for (i = secsymend; i < opdsymend; ++i)
3427         {
3428           bfd_vma ent;
3429
3430           if (syms[i]->value > opd->size - 8)
3431             continue;
3432
3433           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3434           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3435             {
3436               long lo, hi;
3437               size_t len;
3438               asection *sec = abfd->sections;
3439
3440               *s = *syms[i];
3441               lo = codesecsym;
3442               hi = codesecsymend;
3443               while (lo < hi)
3444                 {
3445                   long mid = (lo + hi) >> 1;
3446                   if (syms[mid]->section->vma < ent)
3447                     lo = mid + 1;
3448                   else if (syms[mid]->section->vma > ent)
3449                     hi = mid;
3450                   else
3451                     {
3452                       sec = syms[mid]->section;
3453                       break;
3454                     }
3455                 }
3456
3457               if (lo >= hi && lo > codesecsym)
3458                 sec = syms[lo - 1]->section;
3459
3460               for (; sec != NULL; sec = sec->next)
3461                 {
3462                   if (sec->vma > ent)
3463                     break;
3464                   /* SEC_LOAD may not be set if SEC is from a separate debug
3465                      info file.  */
3466                   if ((sec->flags & SEC_ALLOC) == 0)
3467                     break;
3468                   if ((sec->flags & SEC_CODE) != 0)
3469                     s->section = sec;
3470                 }
3471               s->flags |= BSF_SYNTHETIC;
3472               s->value = ent - s->section->vma;
3473               s->name = names;
3474               *names++ = '.';
3475               len = strlen (syms[i]->name);
3476               memcpy (names, syms[i]->name, len + 1);
3477               names += len + 1;
3478               /* Have udata.p point back to the original symbol this
3479                  synthetic symbol was derived from.  */
3480               s->udata.p = syms[i];
3481               s++;
3482             }
3483         }
3484       free (contents);
3485
3486       if (glink != NULL && relplt != NULL)
3487         {
3488           if (resolv_vma)
3489             {
3490               /* Add a symbol for the main glink trampoline.  */
3491               memset (s, 0, sizeof *s);
3492               s->the_bfd = abfd;
3493               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3494               s->section = glink;
3495               s->value = resolv_vma - glink->vma;
3496               s->name = names;
3497               memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3498               names += sizeof ("__glink_PLTresolve");
3499               s++;
3500               count++;
3501             }
3502
3503           /* FIXME: It would be very much nicer to put sym@plt on the
3504              stub rather than on the glink branch table entry.  The
3505              objdump disassembler would then use a sensible symbol
3506              name on plt calls.  The difficulty in doing so is
3507              a) finding the stubs, and,
3508              b) matching stubs against plt entries, and,
3509              c) there can be multiple stubs for a given plt entry.
3510
3511              Solving (a) could be done by code scanning, but older
3512              ppc64 binaries used different stubs to current code.
3513              (b) is the tricky one since you need to known the toc
3514              pointer for at least one function that uses a pic stub to
3515              be able to calculate the plt address referenced.
3516              (c) means gdb would need to set multiple breakpoints (or
3517              find the glink branch itself) when setting breakpoints
3518              for pending shared library loads.  */
3519           p = relplt->relocation;
3520           for (i = 0; i < plt_count; i++, p++)
3521             {
3522               size_t len;
3523
3524               *s = **p->sym_ptr_ptr;
3525               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
3526                  we are defining a symbol, ensure one of them is set.  */
3527               if ((s->flags & BSF_LOCAL) == 0)
3528                 s->flags |= BSF_GLOBAL;
3529               s->flags |= BSF_SYNTHETIC;
3530               s->section = glink;
3531               s->value = glink_vma - glink->vma;
3532               s->name = names;
3533               s->udata.p = NULL;
3534               len = strlen ((*p->sym_ptr_ptr)->name);
3535               memcpy (names, (*p->sym_ptr_ptr)->name, len);
3536               names += len;
3537               if (p->addend != 0)
3538                 {
3539                   memcpy (names, "+0x", sizeof ("+0x") - 1);
3540                   names += sizeof ("+0x") - 1;
3541                   bfd_sprintf_vma (abfd, names, p->addend);
3542                   names += strlen (names);
3543                 }
3544               memcpy (names, "@plt", sizeof ("@plt"));
3545               names += sizeof ("@plt");
3546               s++;
3547               if (abi < 2)
3548                 {
3549                   glink_vma += 8;
3550                   if (i >= 0x8000)
3551                     glink_vma += 4;
3552                 }
3553               else
3554                 glink_vma += 4;
3555             }
3556           count += plt_count;
3557         }
3558     }
3559
3560  done:
3561   free (syms);
3562   return count;
3563 }
3564 \f
3565 /* The following functions are specific to the ELF linker, while
3566    functions above are used generally.  Those named ppc64_elf_* are
3567    called by the main ELF linker code.  They appear in this file more
3568    or less in the order in which they are called.  eg.
3569    ppc64_elf_check_relocs is called early in the link process,
3570    ppc64_elf_finish_dynamic_sections is one of the last functions
3571    called.
3572
3573    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3574    functions have both a function code symbol and a function descriptor
3575    symbol.  A call to foo in a relocatable object file looks like:
3576
3577    .            .text
3578    .    x:
3579    .            bl      .foo
3580    .            nop
3581
3582    The function definition in another object file might be:
3583
3584    .            .section .opd
3585    .    foo:    .quad   .foo
3586    .            .quad   .TOC.@tocbase
3587    .            .quad   0
3588    .
3589    .            .text
3590    .    .foo:   blr
3591
3592    When the linker resolves the call during a static link, the branch
3593    unsurprisingly just goes to .foo and the .opd information is unused.
3594    If the function definition is in a shared library, things are a little
3595    different:  The call goes via a plt call stub, the opd information gets
3596    copied to the plt, and the linker patches the nop.
3597
3598    .    x:
3599    .            bl      .foo_stub
3600    .            ld      2,40(1)
3601    .
3602    .
3603    .    .foo_stub:
3604    .            std     2,40(1)                 # in practice, the call stub
3605    .            addis   11,2,Lfoo@toc@ha        # is slightly optimized, but
3606    .            addi    11,11,Lfoo@toc@l        # this is the general idea
3607    .            ld      12,0(11)
3608    .            ld      2,8(11)
3609    .            mtctr   12
3610    .            ld      11,16(11)
3611    .            bctr
3612    .
3613    .            .section .plt
3614    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
3615
3616    The "reloc ()" notation is supposed to indicate that the linker emits
3617    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3618    copying.
3619
3620    What are the difficulties here?  Well, firstly, the relocations
3621    examined by the linker in check_relocs are against the function code
3622    sym .foo, while the dynamic relocation in the plt is emitted against
3623    the function descriptor symbol, foo.  Somewhere along the line, we need
3624    to carefully copy dynamic link information from one symbol to the other.
3625    Secondly, the generic part of the elf linker will make .foo a dynamic
3626    symbol as is normal for most other backends.  We need foo dynamic
3627    instead, at least for an application final link.  However, when
3628    creating a shared library containing foo, we need to have both symbols
3629    dynamic so that references to .foo are satisfied during the early
3630    stages of linking.  Otherwise the linker might decide to pull in a
3631    definition from some other object, eg. a static library.
3632
3633    Update: As of August 2004, we support a new convention.  Function
3634    calls may use the function descriptor symbol, ie. "bl foo".  This
3635    behaves exactly as "bl .foo".  */
3636
3637 /* Of those relocs that might be copied as dynamic relocs, this function
3638    selects those that must be copied when linking a shared library,
3639    even when the symbol is local.  */
3640
3641 static int
3642 must_be_dyn_reloc (struct bfd_link_info *info,
3643                    enum elf_ppc64_reloc_type r_type)
3644 {
3645   switch (r_type)
3646     {
3647     default:
3648       return 1;
3649
3650     case R_PPC64_REL32:
3651     case R_PPC64_REL64:
3652     case R_PPC64_REL30:
3653       return 0;
3654
3655     case R_PPC64_TPREL16:
3656     case R_PPC64_TPREL16_LO:
3657     case R_PPC64_TPREL16_HI:
3658     case R_PPC64_TPREL16_HA:
3659     case R_PPC64_TPREL16_DS:
3660     case R_PPC64_TPREL16_LO_DS:
3661     case R_PPC64_TPREL16_HIGH:
3662     case R_PPC64_TPREL16_HIGHA:
3663     case R_PPC64_TPREL16_HIGHER:
3664     case R_PPC64_TPREL16_HIGHERA:
3665     case R_PPC64_TPREL16_HIGHEST:
3666     case R_PPC64_TPREL16_HIGHESTA:
3667     case R_PPC64_TPREL64:
3668       return !info->executable;
3669     }
3670 }
3671
3672 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3673    copying dynamic variables from a shared lib into an app's dynbss
3674    section, and instead use a dynamic relocation to point into the
3675    shared lib.  With code that gcc generates, it's vital that this be
3676    enabled;  In the PowerPC64 ABI, the address of a function is actually
3677    the address of a function descriptor, which resides in the .opd
3678    section.  gcc uses the descriptor directly rather than going via the
3679    GOT as some other ABI's do, which means that initialized function
3680    pointers must reference the descriptor.  Thus, a function pointer
3681    initialized to the address of a function in a shared library will
3682    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3683    redefines the function descriptor symbol to point to the copy.  This
3684    presents a problem as a plt entry for that function is also
3685    initialized from the function descriptor symbol and the copy reloc
3686    may not be initialized first.  */
3687 #define ELIMINATE_COPY_RELOCS 1
3688
3689 /* Section name for stubs is the associated section name plus this
3690    string.  */
3691 #define STUB_SUFFIX ".stub"
3692
3693 /* Linker stubs.
3694    ppc_stub_long_branch:
3695    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3696    destination, but a 24 bit branch in a stub section will reach.
3697    .    b       dest
3698
3699    ppc_stub_plt_branch:
3700    Similar to the above, but a 24 bit branch in the stub section won't
3701    reach its destination.
3702    .    addis   %r11,%r2,xxx@toc@ha
3703    .    ld      %r12,xxx@toc@l(%r11)
3704    .    mtctr   %r12
3705    .    bctr
3706
3707    ppc_stub_plt_call:
3708    Used to call a function in a shared library.  If it so happens that
3709    the plt entry referenced crosses a 64k boundary, then an extra
3710    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3711    .    std     %r2,40(%r1)
3712    .    addis   %r11,%r2,xxx@toc@ha
3713    .    ld      %r12,xxx+0@toc@l(%r11)
3714    .    mtctr   %r12
3715    .    ld      %r2,xxx+8@toc@l(%r11)
3716    .    ld      %r11,xxx+16@toc@l(%r11)
3717    .    bctr
3718
3719    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3720    code to adjust the value and save r2 to support multiple toc sections.
3721    A ppc_stub_long_branch with an r2 offset looks like:
3722    .    std     %r2,40(%r1)
3723    .    addis   %r2,%r2,off@ha
3724    .    addi    %r2,%r2,off@l
3725    .    b       dest
3726
3727    A ppc_stub_plt_branch with an r2 offset looks like:
3728    .    std     %r2,40(%r1)
3729    .    addis   %r11,%r2,xxx@toc@ha
3730    .    ld      %r12,xxx@toc@l(%r11)
3731    .    addis   %r2,%r2,off@ha
3732    .    addi    %r2,%r2,off@l
3733    .    mtctr   %r12
3734    .    bctr
3735
3736    In cases where the "addis" instruction would add zero, the "addis" is
3737    omitted and following instructions modified slightly in some cases.
3738 */
3739
3740 enum ppc_stub_type {
3741   ppc_stub_none,
3742   ppc_stub_long_branch,
3743   ppc_stub_long_branch_r2off,
3744   ppc_stub_plt_branch,
3745   ppc_stub_plt_branch_r2off,
3746   ppc_stub_plt_call,
3747   ppc_stub_plt_call_r2save
3748 };
3749
3750 struct ppc_stub_hash_entry {
3751
3752   /* Base hash table entry structure.  */
3753   struct bfd_hash_entry root;
3754
3755   enum ppc_stub_type stub_type;
3756
3757   /* The stub section.  */
3758   asection *stub_sec;
3759
3760   /* Offset within stub_sec of the beginning of this stub.  */
3761   bfd_vma stub_offset;
3762
3763   /* Given the symbol's value and its section we can determine its final
3764      value when building the stubs (so the stub knows where to jump.  */
3765   bfd_vma target_value;
3766   asection *target_section;
3767
3768   /* The symbol table entry, if any, that this was derived from.  */
3769   struct ppc_link_hash_entry *h;
3770   struct plt_entry *plt_ent;
3771
3772   /* Where this stub is being called from, or, in the case of combined
3773      stub sections, the first input section in the group.  */
3774   asection *id_sec;
3775
3776   /* Symbol st_other.  */
3777   unsigned char other;
3778 };
3779
3780 struct ppc_branch_hash_entry {
3781
3782   /* Base hash table entry structure.  */
3783   struct bfd_hash_entry root;
3784
3785   /* Offset within branch lookup table.  */
3786   unsigned int offset;
3787
3788   /* Generation marker.  */
3789   unsigned int iter;
3790 };
3791
3792 /* Used to track dynamic relocations for local symbols.  */
3793 struct ppc_dyn_relocs
3794 {
3795   struct ppc_dyn_relocs *next;
3796
3797   /* The input section of the reloc.  */
3798   asection *sec;
3799
3800   /* Total number of relocs copied for the input section.  */
3801   unsigned int count : 31;
3802
3803   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
3804   unsigned int ifunc : 1;
3805 };
3806
3807 struct ppc_link_hash_entry
3808 {
3809   struct elf_link_hash_entry elf;
3810
3811   union {
3812     /* A pointer to the most recently used stub hash entry against this
3813        symbol.  */
3814     struct ppc_stub_hash_entry *stub_cache;
3815
3816     /* A pointer to the next symbol starting with a '.'  */
3817     struct ppc_link_hash_entry *next_dot_sym;
3818   } u;
3819
3820   /* Track dynamic relocs copied for this symbol.  */
3821   struct elf_dyn_relocs *dyn_relocs;
3822
3823   /* Link between function code and descriptor symbols.  */
3824   struct ppc_link_hash_entry *oh;
3825
3826   /* Flag function code and descriptor symbols.  */
3827   unsigned int is_func:1;
3828   unsigned int is_func_descriptor:1;
3829   unsigned int fake:1;
3830
3831   /* Whether global opd/toc sym has been adjusted or not.
3832      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3833      should be set for all globals defined in any opd/toc section.  */
3834   unsigned int adjust_done:1;
3835
3836   /* Set if we twiddled this symbol to weak at some stage.  */
3837   unsigned int was_undefined:1;
3838
3839   /* Contexts in which symbol is used in the GOT (or TOC).
3840      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3841      corresponding relocs are encountered during check_relocs.
3842      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3843      indicate the corresponding GOT entry type is not needed.
3844      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3845      a TPREL one.  We use a separate flag rather than setting TPREL
3846      just for convenience in distinguishing the two cases.  */
3847 #define TLS_GD           1      /* GD reloc. */
3848 #define TLS_LD           2      /* LD reloc. */
3849 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
3850 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
3851 #define TLS_TLS         16      /* Any TLS reloc.  */
3852 #define TLS_EXPLICIT    32      /* Marks TOC section TLS relocs. */
3853 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
3854 #define PLT_IFUNC      128      /* STT_GNU_IFUNC.  */
3855   unsigned char tls_mask;
3856 };
3857
3858 /* ppc64 ELF linker hash table.  */
3859
3860 struct ppc_link_hash_table
3861 {
3862   struct elf_link_hash_table elf;
3863
3864   /* The stub hash table.  */
3865   struct bfd_hash_table stub_hash_table;
3866
3867   /* Another hash table for plt_branch stubs.  */
3868   struct bfd_hash_table branch_hash_table;
3869
3870   /* Hash table for function prologue tocsave.  */
3871   htab_t tocsave_htab;
3872
3873   /* Linker stub bfd.  */
3874   bfd *stub_bfd;
3875
3876   /* Linker call-backs.  */
3877   asection * (*add_stub_section) (const char *, asection *);
3878   void (*layout_sections_again) (void);
3879
3880   /* Array to keep track of which stub sections have been created, and
3881      information on stub grouping.  */
3882   struct map_stub {
3883     /* This is the section to which stubs in the group will be attached.  */
3884     asection *link_sec;
3885     /* The stub section.  */
3886     asection *stub_sec;
3887     /* Along with elf_gp, specifies the TOC pointer used in this group.  */
3888     bfd_vma toc_off;
3889   } *stub_group;
3890
3891   /* Temp used when calculating TOC pointers.  */
3892   bfd_vma toc_curr;
3893   bfd *toc_bfd;
3894   asection *toc_first_sec;
3895
3896   /* Highest input section id.  */
3897   int top_id;
3898
3899   /* Highest output section index.  */
3900   int top_index;
3901
3902   /* Used when adding symbols.  */
3903   struct ppc_link_hash_entry *dot_syms;
3904
3905   /* List of input sections for each output section.  */
3906   asection **input_list;
3907
3908   /* Short-cuts to get to dynamic linker sections.  */
3909   asection *got;
3910   asection *plt;
3911   asection *relplt;
3912   asection *iplt;
3913   asection *reliplt;
3914   asection *dynbss;
3915   asection *relbss;
3916   asection *glink;
3917   asection *sfpr;
3918   asection *brlt;
3919   asection *relbrlt;
3920   asection *glink_eh_frame;
3921
3922   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
3923   struct ppc_link_hash_entry *tls_get_addr;
3924   struct ppc_link_hash_entry *tls_get_addr_fd;
3925
3926   /* The size of reliplt used by got entry relocs.  */
3927   bfd_size_type got_reli_size;
3928
3929   /* Statistics.  */
3930   unsigned long stub_count[ppc_stub_plt_call_r2save];
3931
3932   /* Number of stubs against global syms.  */
3933   unsigned long stub_globals;
3934
3935   /* Alignment of PLT call stubs.  */
3936   unsigned int plt_stub_align:4;
3937
3938   /* Set if we're linking code with function descriptors.  */
3939   unsigned int opd_abi:1;
3940
3941   /* Set if PLT call stubs should load r11.  */
3942   unsigned int plt_static_chain:1;
3943
3944   /* Set if PLT call stubs need a read-read barrier.  */
3945   unsigned int plt_thread_safe:1;
3946
3947   /* Set if we should emit symbols for stubs.  */
3948   unsigned int emit_stub_syms:1;
3949
3950   /* Set if __tls_get_addr optimization should not be done.  */
3951   unsigned int no_tls_get_addr_opt:1;
3952
3953   /* Support for multiple toc sections.  */
3954   unsigned int do_multi_toc:1;
3955   unsigned int multi_toc_needed:1;
3956   unsigned int second_toc_pass:1;
3957   unsigned int do_toc_opt:1;
3958
3959   /* Set on error.  */
3960   unsigned int stub_error:1;
3961
3962   /* Temp used by ppc64_elf_process_dot_syms.  */
3963   unsigned int twiddled_syms:1;
3964
3965   /* Incremented every time we size stubs.  */
3966   unsigned int stub_iteration;
3967
3968   /* Small local sym cache.  */
3969   struct sym_cache sym_cache;
3970 };
3971
3972 /* Rename some of the generic section flags to better document how they
3973    are used here.  */
3974
3975 /* Nonzero if this section has TLS related relocations.  */
3976 #define has_tls_reloc sec_flg0
3977
3978 /* Nonzero if this section has a call to __tls_get_addr.  */
3979 #define has_tls_get_addr_call sec_flg1
3980
3981 /* Nonzero if this section has any toc or got relocs.  */
3982 #define has_toc_reloc sec_flg2
3983
3984 /* Nonzero if this section has a call to another section that uses
3985    the toc or got.  */
3986 #define makes_toc_func_call sec_flg3
3987
3988 /* Recursion protection when determining above flag.  */
3989 #define call_check_in_progress sec_flg4
3990 #define call_check_done sec_flg5
3991
3992 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
3993
3994 #define ppc_hash_table(p) \
3995   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3996   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3997
3998 #define ppc_stub_hash_lookup(table, string, create, copy) \
3999   ((struct ppc_stub_hash_entry *) \
4000    bfd_hash_lookup ((table), (string), (create), (copy)))
4001
4002 #define ppc_branch_hash_lookup(table, string, create, copy) \
4003   ((struct ppc_branch_hash_entry *) \
4004    bfd_hash_lookup ((table), (string), (create), (copy)))
4005
4006 /* Create an entry in the stub hash table.  */
4007
4008 static struct bfd_hash_entry *
4009 stub_hash_newfunc (struct bfd_hash_entry *entry,
4010                    struct bfd_hash_table *table,
4011                    const char *string)
4012 {
4013   /* Allocate the structure if it has not already been allocated by a
4014      subclass.  */
4015   if (entry == NULL)
4016     {
4017       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4018       if (entry == NULL)
4019         return entry;
4020     }
4021
4022   /* Call the allocation method of the superclass.  */
4023   entry = bfd_hash_newfunc (entry, table, string);
4024   if (entry != NULL)
4025     {
4026       struct ppc_stub_hash_entry *eh;
4027
4028       /* Initialize the local fields.  */
4029       eh = (struct ppc_stub_hash_entry *) entry;
4030       eh->stub_type = ppc_stub_none;
4031       eh->stub_sec = NULL;
4032       eh->stub_offset = 0;
4033       eh->target_value = 0;
4034       eh->target_section = NULL;
4035       eh->h = NULL;
4036       eh->plt_ent = NULL;
4037       eh->id_sec = NULL;
4038       eh->other = 0;
4039     }
4040
4041   return entry;
4042 }
4043
4044 /* Create an entry in the branch hash table.  */
4045
4046 static struct bfd_hash_entry *
4047 branch_hash_newfunc (struct bfd_hash_entry *entry,
4048                      struct bfd_hash_table *table,
4049                      const char *string)
4050 {
4051   /* Allocate the structure if it has not already been allocated by a
4052      subclass.  */
4053   if (entry == NULL)
4054     {
4055       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4056       if (entry == NULL)
4057         return entry;
4058     }
4059
4060   /* Call the allocation method of the superclass.  */
4061   entry = bfd_hash_newfunc (entry, table, string);
4062   if (entry != NULL)
4063     {
4064       struct ppc_branch_hash_entry *eh;
4065
4066       /* Initialize the local fields.  */
4067       eh = (struct ppc_branch_hash_entry *) entry;
4068       eh->offset = 0;
4069       eh->iter = 0;
4070     }
4071
4072   return entry;
4073 }
4074
4075 /* Create an entry in a ppc64 ELF linker hash table.  */
4076
4077 static struct bfd_hash_entry *
4078 link_hash_newfunc (struct bfd_hash_entry *entry,
4079                    struct bfd_hash_table *table,
4080                    const char *string)
4081 {
4082   /* Allocate the structure if it has not already been allocated by a
4083      subclass.  */
4084   if (entry == NULL)
4085     {
4086       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4087       if (entry == NULL)
4088         return entry;
4089     }
4090
4091   /* Call the allocation method of the superclass.  */
4092   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4093   if (entry != NULL)
4094     {
4095       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4096
4097       memset (&eh->u.stub_cache, 0,
4098               (sizeof (struct ppc_link_hash_entry)
4099                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4100
4101       /* When making function calls, old ABI code references function entry
4102          points (dot symbols), while new ABI code references the function
4103          descriptor symbol.  We need to make any combination of reference and
4104          definition work together, without breaking archive linking.
4105
4106          For a defined function "foo" and an undefined call to "bar":
4107          An old object defines "foo" and ".foo", references ".bar" (possibly
4108          "bar" too).
4109          A new object defines "foo" and references "bar".
4110
4111          A new object thus has no problem with its undefined symbols being
4112          satisfied by definitions in an old object.  On the other hand, the
4113          old object won't have ".bar" satisfied by a new object.
4114
4115          Keep a list of newly added dot-symbols.  */
4116
4117       if (string[0] == '.')
4118         {
4119           struct ppc_link_hash_table *htab;
4120
4121           htab = (struct ppc_link_hash_table *) table;
4122           eh->u.next_dot_sym = htab->dot_syms;
4123           htab->dot_syms = eh;
4124         }
4125     }
4126
4127   return entry;
4128 }
4129
4130 struct tocsave_entry {
4131   asection *sec;
4132   bfd_vma offset;
4133 };
4134
4135 static hashval_t
4136 tocsave_htab_hash (const void *p)
4137 {
4138   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4139   return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4140 }
4141
4142 static int
4143 tocsave_htab_eq (const void *p1, const void *p2)
4144 {
4145   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4146   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4147   return e1->sec == e2->sec && e1->offset == e2->offset;
4148 }
4149
4150 /* Create a ppc64 ELF linker hash table.  */
4151
4152 static struct bfd_link_hash_table *
4153 ppc64_elf_link_hash_table_create (bfd *abfd)
4154 {
4155   struct ppc_link_hash_table *htab;
4156   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4157
4158   htab = bfd_zmalloc (amt);
4159   if (htab == NULL)
4160     return NULL;
4161
4162   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4163                                       sizeof (struct ppc_link_hash_entry),
4164                                       PPC64_ELF_DATA))
4165     {
4166       free (htab);
4167       return NULL;
4168     }
4169
4170   /* Init the stub hash table too.  */
4171   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4172                             sizeof (struct ppc_stub_hash_entry)))
4173     return NULL;
4174
4175   /* And the branch hash table.  */
4176   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4177                             sizeof (struct ppc_branch_hash_entry)))
4178     return NULL;
4179
4180   htab->tocsave_htab = htab_try_create (1024,
4181                                         tocsave_htab_hash,
4182                                         tocsave_htab_eq,
4183                                         NULL);
4184   if (htab->tocsave_htab == NULL)
4185     return NULL;
4186
4187   /* Initializing two fields of the union is just cosmetic.  We really
4188      only care about glist, but when compiled on a 32-bit host the
4189      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
4190      debugger inspection of these fields look nicer.  */
4191   htab->elf.init_got_refcount.refcount = 0;
4192   htab->elf.init_got_refcount.glist = NULL;
4193   htab->elf.init_plt_refcount.refcount = 0;
4194   htab->elf.init_plt_refcount.glist = NULL;
4195   htab->elf.init_got_offset.offset = 0;
4196   htab->elf.init_got_offset.glist = NULL;
4197   htab->elf.init_plt_offset.offset = 0;
4198   htab->elf.init_plt_offset.glist = NULL;
4199
4200   return &htab->elf.root;
4201 }
4202
4203 /* Free the derived linker hash table.  */
4204
4205 static void
4206 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
4207 {
4208   struct ppc_link_hash_table *htab = (struct ppc_link_hash_table *) hash;
4209
4210   bfd_hash_table_free (&htab->stub_hash_table);
4211   bfd_hash_table_free (&htab->branch_hash_table);
4212   if (htab->tocsave_htab)
4213     htab_delete (htab->tocsave_htab);
4214   _bfd_elf_link_hash_table_free (hash);
4215 }
4216
4217 /* Create sections for linker generated code.  */
4218
4219 static bfd_boolean
4220 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4221 {
4222   struct ppc_link_hash_table *htab;
4223   flagword flags;
4224
4225   htab = ppc_hash_table (info);
4226
4227   /* Create .sfpr for code to save and restore fp regs.  */
4228   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4229            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4230   htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4231                                                    flags);
4232   if (htab->sfpr == NULL
4233       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4234     return FALSE;
4235
4236   /* Create .glink for lazy dynamic linking support.  */
4237   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4238                                                     flags);
4239   if (htab->glink == NULL
4240       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4241     return FALSE;
4242
4243   if (!info->no_ld_generated_unwind_info)
4244     {
4245       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4246                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4247       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4248                                                                  ".eh_frame",
4249                                                                  flags);
4250       if (htab->glink_eh_frame == NULL
4251           || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4252         return FALSE;
4253     }
4254
4255   flags = SEC_ALLOC | SEC_LINKER_CREATED;
4256   htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4257   if (htab->iplt == NULL
4258       || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4259     return FALSE;
4260
4261   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4262            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4263   htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4264                                                       ".rela.iplt",
4265                                                       flags);
4266   if (htab->reliplt == NULL
4267       || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4268     return FALSE;
4269
4270   /* Create branch lookup table for plt_branch stubs.  */
4271   flags = (SEC_ALLOC | SEC_LOAD
4272            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4273   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4274                                                    flags);
4275   if (htab->brlt == NULL
4276       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4277     return FALSE;
4278
4279   if (!info->shared)
4280     return TRUE;
4281
4282   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4283            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4284   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4285                                                       ".rela.branch_lt",
4286                                                       flags);
4287   if (htab->relbrlt == NULL
4288       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4289     return FALSE;
4290
4291   return TRUE;
4292 }
4293
4294 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
4295
4296 bfd_boolean
4297 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4298 {
4299   struct ppc_link_hash_table *htab;
4300
4301   elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4302
4303 /* Always hook our dynamic sections into the first bfd, which is the
4304    linker created stub bfd.  This ensures that the GOT header is at
4305    the start of the output TOC section.  */
4306   htab = ppc_hash_table (info);
4307   if (htab == NULL)
4308     return FALSE;
4309   htab->stub_bfd = abfd;
4310   htab->elf.dynobj = abfd;
4311
4312   if (info->relocatable)
4313     return TRUE;
4314
4315   return create_linkage_sections (htab->elf.dynobj, info);
4316 }
4317
4318 /* Build a name for an entry in the stub hash table.  */
4319
4320 static char *
4321 ppc_stub_name (const asection *input_section,
4322                const asection *sym_sec,
4323                const struct ppc_link_hash_entry *h,
4324                const Elf_Internal_Rela *rel)
4325 {
4326   char *stub_name;
4327   ssize_t len;
4328
4329   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4330      offsets from a sym as a branch target?  In fact, we could
4331      probably assume the addend is always zero.  */
4332   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4333
4334   if (h)
4335     {
4336       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4337       stub_name = bfd_malloc (len);
4338       if (stub_name == NULL)
4339         return stub_name;
4340
4341       len = sprintf (stub_name, "%08x.%s+%x",
4342                      input_section->id & 0xffffffff,
4343                      h->elf.root.root.string,
4344                      (int) rel->r_addend & 0xffffffff);
4345     }
4346   else
4347     {
4348       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4349       stub_name = bfd_malloc (len);
4350       if (stub_name == NULL)
4351         return stub_name;
4352
4353       len = sprintf (stub_name, "%08x.%x:%x+%x",
4354                      input_section->id & 0xffffffff,
4355                      sym_sec->id & 0xffffffff,
4356                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4357                      (int) rel->r_addend & 0xffffffff);
4358     }
4359   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4360     stub_name[len - 2] = 0;
4361   return stub_name;
4362 }
4363
4364 /* Look up an entry in the stub hash.  Stub entries are cached because
4365    creating the stub name takes a bit of time.  */
4366
4367 static struct ppc_stub_hash_entry *
4368 ppc_get_stub_entry (const asection *input_section,
4369                     const asection *sym_sec,
4370                     struct ppc_link_hash_entry *h,
4371                     const Elf_Internal_Rela *rel,
4372                     struct ppc_link_hash_table *htab)
4373 {
4374   struct ppc_stub_hash_entry *stub_entry;
4375   const asection *id_sec;
4376
4377   /* If this input section is part of a group of sections sharing one
4378      stub section, then use the id of the first section in the group.
4379      Stub names need to include a section id, as there may well be
4380      more than one stub used to reach say, printf, and we need to
4381      distinguish between them.  */
4382   id_sec = htab->stub_group[input_section->id].link_sec;
4383
4384   if (h != NULL && h->u.stub_cache != NULL
4385       && h->u.stub_cache->h == h
4386       && h->u.stub_cache->id_sec == id_sec)
4387     {
4388       stub_entry = h->u.stub_cache;
4389     }
4390   else
4391     {
4392       char *stub_name;
4393
4394       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4395       if (stub_name == NULL)
4396         return NULL;
4397
4398       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4399                                          stub_name, FALSE, FALSE);
4400       if (h != NULL)
4401         h->u.stub_cache = stub_entry;
4402
4403       free (stub_name);
4404     }
4405
4406   return stub_entry;
4407 }
4408
4409 /* Add a new stub entry to the stub hash.  Not all fields of the new
4410    stub entry are initialised.  */
4411
4412 static struct ppc_stub_hash_entry *
4413 ppc_add_stub (const char *stub_name,
4414               asection *section,
4415               struct bfd_link_info *info)
4416 {
4417   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4418   asection *link_sec;
4419   asection *stub_sec;
4420   struct ppc_stub_hash_entry *stub_entry;
4421
4422   link_sec = htab->stub_group[section->id].link_sec;
4423   stub_sec = htab->stub_group[section->id].stub_sec;
4424   if (stub_sec == NULL)
4425     {
4426       stub_sec = htab->stub_group[link_sec->id].stub_sec;
4427       if (stub_sec == NULL)
4428         {
4429           size_t namelen;
4430           bfd_size_type len;
4431           char *s_name;
4432
4433           namelen = strlen (link_sec->name);
4434           len = namelen + sizeof (STUB_SUFFIX);
4435           s_name = bfd_alloc (htab->stub_bfd, len);
4436           if (s_name == NULL)
4437             return NULL;
4438
4439           memcpy (s_name, link_sec->name, namelen);
4440           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4441           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4442           if (stub_sec == NULL)
4443             return NULL;
4444           htab->stub_group[link_sec->id].stub_sec = stub_sec;
4445         }
4446       htab->stub_group[section->id].stub_sec = stub_sec;
4447     }
4448
4449   /* Enter this entry into the linker stub hash table.  */
4450   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4451                                      TRUE, FALSE);
4452   if (stub_entry == NULL)
4453     {
4454       info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4455                               section->owner, stub_name);
4456       return NULL;
4457     }
4458
4459   stub_entry->stub_sec = stub_sec;
4460   stub_entry->stub_offset = 0;
4461   stub_entry->id_sec = link_sec;
4462   return stub_entry;
4463 }
4464
4465 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4466    not already done.  */
4467
4468 static bfd_boolean
4469 create_got_section (bfd *abfd, struct bfd_link_info *info)
4470 {
4471   asection *got, *relgot;
4472   flagword flags;
4473   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4474
4475   if (!is_ppc64_elf (abfd))
4476     return FALSE;
4477   if (htab == NULL)
4478     return FALSE;
4479
4480   if (!htab->got)
4481     {
4482       if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4483         return FALSE;
4484
4485       htab->got = bfd_get_linker_section (htab->elf.dynobj, ".got");
4486       if (!htab->got)
4487         abort ();
4488     }
4489
4490   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4491            | SEC_LINKER_CREATED);
4492
4493   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4494   if (!got
4495       || !bfd_set_section_alignment (abfd, got, 3))
4496     return FALSE;
4497
4498   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4499                                                flags | SEC_READONLY);
4500   if (!relgot
4501       || ! bfd_set_section_alignment (abfd, relgot, 3))
4502     return FALSE;
4503
4504   ppc64_elf_tdata (abfd)->got = got;
4505   ppc64_elf_tdata (abfd)->relgot = relgot;
4506   return TRUE;
4507 }
4508
4509 /* Create the dynamic sections, and set up shortcuts.  */
4510
4511 static bfd_boolean
4512 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4513 {
4514   struct ppc_link_hash_table *htab;
4515
4516   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4517     return FALSE;
4518
4519   htab = ppc_hash_table (info);
4520   if (htab == NULL)
4521     return FALSE;
4522
4523   if (!htab->got)
4524     htab->got = bfd_get_linker_section (dynobj, ".got");
4525   htab->plt = bfd_get_linker_section (dynobj, ".plt");
4526   htab->relplt = bfd_get_linker_section (dynobj, ".rela.plt");
4527   htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
4528   if (!info->shared)
4529     htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
4530
4531   if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4532       || (!info->shared && !htab->relbss))
4533     abort ();
4534
4535   return TRUE;
4536 }
4537
4538 /* Follow indirect and warning symbol links.  */
4539
4540 static inline struct bfd_link_hash_entry *
4541 follow_link (struct bfd_link_hash_entry *h)
4542 {
4543   while (h->type == bfd_link_hash_indirect
4544          || h->type == bfd_link_hash_warning)
4545     h = h->u.i.link;
4546   return h;
4547 }
4548
4549 static inline struct elf_link_hash_entry *
4550 elf_follow_link (struct elf_link_hash_entry *h)
4551 {
4552   return (struct elf_link_hash_entry *) follow_link (&h->root);
4553 }
4554
4555 static inline struct ppc_link_hash_entry *
4556 ppc_follow_link (struct ppc_link_hash_entry *h)
4557 {
4558   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4559 }
4560
4561 /* Merge PLT info on FROM with that on TO.  */
4562
4563 static void
4564 move_plt_plist (struct ppc_link_hash_entry *from,
4565                 struct ppc_link_hash_entry *to)
4566 {
4567   if (from->elf.plt.plist != NULL)
4568     {
4569       if (to->elf.plt.plist != NULL)
4570         {
4571           struct plt_entry **entp;
4572           struct plt_entry *ent;
4573
4574           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4575             {
4576               struct plt_entry *dent;
4577
4578               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4579                 if (dent->addend == ent->addend)
4580                   {
4581                     dent->plt.refcount += ent->plt.refcount;
4582                     *entp = ent->next;
4583                     break;
4584                   }
4585               if (dent == NULL)
4586                 entp = &ent->next;
4587             }
4588           *entp = to->elf.plt.plist;
4589         }
4590
4591       to->elf.plt.plist = from->elf.plt.plist;
4592       from->elf.plt.plist = NULL;
4593     }
4594 }
4595
4596 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
4597
4598 static void
4599 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4600                                 struct elf_link_hash_entry *dir,
4601                                 struct elf_link_hash_entry *ind)
4602 {
4603   struct ppc_link_hash_entry *edir, *eind;
4604
4605   edir = (struct ppc_link_hash_entry *) dir;
4606   eind = (struct ppc_link_hash_entry *) ind;
4607
4608   edir->is_func |= eind->is_func;
4609   edir->is_func_descriptor |= eind->is_func_descriptor;
4610   edir->tls_mask |= eind->tls_mask;
4611   if (eind->oh != NULL)
4612     edir->oh = ppc_follow_link (eind->oh);
4613
4614   /* If called to transfer flags for a weakdef during processing
4615      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4616      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
4617   if (!(ELIMINATE_COPY_RELOCS
4618         && eind->elf.root.type != bfd_link_hash_indirect
4619         && edir->elf.dynamic_adjusted))
4620     edir->elf.non_got_ref |= eind->elf.non_got_ref;
4621
4622   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4623   edir->elf.ref_regular |= eind->elf.ref_regular;
4624   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4625   edir->elf.needs_plt |= eind->elf.needs_plt;
4626
4627   /* Copy over any dynamic relocs we may have on the indirect sym.  */
4628   if (eind->dyn_relocs != NULL)
4629     {
4630       if (edir->dyn_relocs != NULL)
4631         {
4632           struct elf_dyn_relocs **pp;
4633           struct elf_dyn_relocs *p;
4634
4635           /* Add reloc counts against the indirect sym to the direct sym
4636              list.  Merge any entries against the same section.  */
4637           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4638             {
4639               struct elf_dyn_relocs *q;
4640
4641               for (q = edir->dyn_relocs; q != NULL; q = q->next)
4642                 if (q->sec == p->sec)
4643                   {
4644                     q->pc_count += p->pc_count;
4645                     q->count += p->count;
4646                     *pp = p->next;
4647                     break;
4648                   }
4649               if (q == NULL)
4650                 pp = &p->next;
4651             }
4652           *pp = edir->dyn_relocs;
4653         }
4654
4655       edir->dyn_relocs = eind->dyn_relocs;
4656       eind->dyn_relocs = NULL;
4657     }
4658
4659   /* If we were called to copy over info for a weak sym, that's all.
4660      You might think dyn_relocs need not be copied over;  After all,
4661      both syms will be dynamic or both non-dynamic so we're just
4662      moving reloc accounting around.  However, ELIMINATE_COPY_RELOCS
4663      code in ppc64_elf_adjust_dynamic_symbol needs to check for
4664      dyn_relocs in read-only sections, and it does so on what is the
4665      DIR sym here.  */
4666   if (eind->elf.root.type != bfd_link_hash_indirect)
4667     return;
4668
4669   /* Copy over got entries that we may have already seen to the
4670      symbol which just became indirect.  */
4671   if (eind->elf.got.glist != NULL)
4672     {
4673       if (edir->elf.got.glist != NULL)
4674         {
4675           struct got_entry **entp;
4676           struct got_entry *ent;
4677
4678           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4679             {
4680               struct got_entry *dent;
4681
4682               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4683                 if (dent->addend == ent->addend
4684                     && dent->owner == ent->owner
4685                     && dent->tls_type == ent->tls_type)
4686                   {
4687                     dent->got.refcount += ent->got.refcount;
4688                     *entp = ent->next;
4689                     break;
4690                   }
4691               if (dent == NULL)
4692                 entp = &ent->next;
4693             }
4694           *entp = edir->elf.got.glist;
4695         }
4696
4697       edir->elf.got.glist = eind->elf.got.glist;
4698       eind->elf.got.glist = NULL;
4699     }
4700
4701   /* And plt entries.  */
4702   move_plt_plist (eind, edir);
4703
4704   if (eind->elf.dynindx != -1)
4705     {
4706       if (edir->elf.dynindx != -1)
4707         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4708                                 edir->elf.dynstr_index);
4709       edir->elf.dynindx = eind->elf.dynindx;
4710       edir->elf.dynstr_index = eind->elf.dynstr_index;
4711       eind->elf.dynindx = -1;
4712       eind->elf.dynstr_index = 0;
4713     }
4714 }
4715
4716 /* Find the function descriptor hash entry from the given function code
4717    hash entry FH.  Link the entries via their OH fields.  */
4718
4719 static struct ppc_link_hash_entry *
4720 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4721 {
4722   struct ppc_link_hash_entry *fdh = fh->oh;
4723
4724   if (fdh == NULL)
4725     {
4726       const char *fd_name = fh->elf.root.root.string + 1;
4727
4728       fdh = (struct ppc_link_hash_entry *)
4729         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4730       if (fdh == NULL)
4731         return fdh;
4732
4733       fdh->is_func_descriptor = 1;
4734       fdh->oh = fh;
4735       fh->is_func = 1;
4736       fh->oh = fdh;
4737     }
4738
4739   return ppc_follow_link (fdh);
4740 }
4741
4742 /* Make a fake function descriptor sym for the code sym FH.  */
4743
4744 static struct ppc_link_hash_entry *
4745 make_fdh (struct bfd_link_info *info,
4746           struct ppc_link_hash_entry *fh)
4747 {
4748   bfd *abfd;
4749   asymbol *newsym;
4750   struct bfd_link_hash_entry *bh;
4751   struct ppc_link_hash_entry *fdh;
4752
4753   abfd = fh->elf.root.u.undef.abfd;
4754   newsym = bfd_make_empty_symbol (abfd);
4755   newsym->name = fh->elf.root.root.string + 1;
4756   newsym->section = bfd_und_section_ptr;
4757   newsym->value = 0;
4758   newsym->flags = BSF_WEAK;
4759
4760   bh = NULL;
4761   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4762                                          newsym->flags, newsym->section,
4763                                          newsym->value, NULL, FALSE, FALSE,
4764                                          &bh))
4765     return NULL;
4766
4767   fdh = (struct ppc_link_hash_entry *) bh;
4768   fdh->elf.non_elf = 0;
4769   fdh->fake = 1;
4770   fdh->is_func_descriptor = 1;
4771   fdh->oh = fh;
4772   fh->is_func = 1;
4773   fh->oh = fdh;
4774   return fdh;
4775 }
4776
4777 /* Fix function descriptor symbols defined in .opd sections to be
4778    function type.  */
4779
4780 static bfd_boolean
4781 ppc64_elf_add_symbol_hook (bfd *ibfd,
4782                            struct bfd_link_info *info,
4783                            Elf_Internal_Sym *isym,
4784                            const char **name,
4785                            flagword *flags ATTRIBUTE_UNUSED,
4786                            asection **sec,
4787                            bfd_vma *value ATTRIBUTE_UNUSED)
4788 {
4789   if ((ibfd->flags & DYNAMIC) == 0
4790       && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4791     elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4792
4793   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4794     {
4795       if ((ibfd->flags & DYNAMIC) == 0)
4796         elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4797     }
4798   else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4799     ;
4800   else if (*sec != NULL
4801            && strcmp ((*sec)->name, ".opd") == 0)
4802     isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4803
4804   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4805     {
4806       if (abiversion (ibfd) == 0)
4807         set_abiversion (ibfd, 2);
4808       else if (abiversion (ibfd) == 1)
4809         {
4810           info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4811                                     " for ABI version 1\n"), name);
4812           bfd_set_error (bfd_error_bad_value);
4813           return FALSE;
4814         }
4815     }
4816
4817   return TRUE;
4818 }
4819
4820 /* Merge non-visibility st_other attributes: local entry point.  */
4821
4822 static void
4823 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4824                                   const Elf_Internal_Sym *isym,
4825                                   bfd_boolean definition,
4826                                   bfd_boolean dynamic)
4827 {
4828   if (definition && !dynamic)
4829     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4830                 | ELF_ST_VISIBILITY (h->other));
4831 }
4832
4833 /* This function makes an old ABI object reference to ".bar" cause the
4834    inclusion of a new ABI object archive that defines "bar".
4835    NAME is a symbol defined in an archive.  Return a symbol in the hash
4836    table that might be satisfied by the archive symbols.  */
4837
4838 static struct elf_link_hash_entry *
4839 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4840                                  struct bfd_link_info *info,
4841                                  const char *name)
4842 {
4843   struct elf_link_hash_entry *h;
4844   char *dot_name;
4845   size_t len;
4846
4847   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4848   if (h != NULL
4849       /* Don't return this sym if it is a fake function descriptor
4850          created by add_symbol_adjust.  */
4851       && !(h->root.type == bfd_link_hash_undefweak
4852            && ((struct ppc_link_hash_entry *) h)->fake))
4853     return h;
4854
4855   if (name[0] == '.')
4856     return h;
4857
4858   len = strlen (name);
4859   dot_name = bfd_alloc (abfd, len + 2);
4860   if (dot_name == NULL)
4861     return (struct elf_link_hash_entry *) 0 - 1;
4862   dot_name[0] = '.';
4863   memcpy (dot_name + 1, name, len + 1);
4864   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4865   bfd_release (abfd, dot_name);
4866   return h;
4867 }
4868
4869 /* This function satisfies all old ABI object references to ".bar" if a
4870    new ABI object defines "bar".  Well, at least, undefined dot symbols
4871    are made weak.  This stops later archive searches from including an
4872    object if we already have a function descriptor definition.  It also
4873    prevents the linker complaining about undefined symbols.
4874    We also check and correct mismatched symbol visibility here.  The
4875    most restrictive visibility of the function descriptor and the
4876    function entry symbol is used.  */
4877
4878 static bfd_boolean
4879 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4880 {
4881   struct ppc_link_hash_table *htab;
4882   struct ppc_link_hash_entry *fdh;
4883
4884   if (eh->elf.root.type == bfd_link_hash_indirect)
4885     return TRUE;
4886
4887   if (eh->elf.root.type == bfd_link_hash_warning)
4888     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4889
4890   if (eh->elf.root.root.string[0] != '.')
4891     abort ();
4892
4893   htab = ppc_hash_table (info);
4894   if (htab == NULL)
4895     return FALSE;
4896
4897   fdh = lookup_fdh (eh, htab);
4898   if (fdh == NULL)
4899     {
4900       if (!info->relocatable
4901           && (eh->elf.root.type == bfd_link_hash_undefined
4902               || eh->elf.root.type == bfd_link_hash_undefweak)
4903           && eh->elf.ref_regular)
4904         {
4905           /* Make an undefweak function descriptor sym, which is enough to
4906              pull in an --as-needed shared lib, but won't cause link
4907              errors.  Archives are handled elsewhere.  */
4908           fdh = make_fdh (info, eh);
4909           if (fdh == NULL)
4910             return FALSE;
4911           fdh->elf.ref_regular = 1;
4912         }
4913     }
4914   else
4915     {
4916       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4917       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4918       if (entry_vis < descr_vis)
4919         fdh->elf.other += entry_vis - descr_vis;
4920       else if (entry_vis > descr_vis)
4921         eh->elf.other += descr_vis - entry_vis;
4922
4923       if ((fdh->elf.root.type == bfd_link_hash_defined
4924            || fdh->elf.root.type == bfd_link_hash_defweak)
4925           && eh->elf.root.type == bfd_link_hash_undefined)
4926         {
4927           eh->elf.root.type = bfd_link_hash_undefweak;
4928           eh->was_undefined = 1;
4929           htab->twiddled_syms = 1;
4930         }
4931     }
4932
4933   return TRUE;
4934 }
4935
4936 /* Process list of dot-symbols we made in link_hash_newfunc.  */
4937
4938 static bfd_boolean
4939 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
4940 {
4941   struct ppc_link_hash_table *htab;
4942   struct ppc_link_hash_entry **p, *eh;
4943
4944   if (!is_ppc64_elf (info->output_bfd))
4945     return TRUE;
4946   htab = ppc_hash_table (info);
4947   if (htab == NULL)
4948     return FALSE;
4949
4950   if (is_ppc64_elf (ibfd))
4951     {
4952       p = &htab->dot_syms;
4953       while ((eh = *p) != NULL)
4954         {
4955           *p = NULL;
4956           if (&eh->elf == htab->elf.hgot)
4957             ;
4958           else if (htab->elf.hgot == NULL
4959                    && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4960             htab->elf.hgot = &eh->elf;
4961           else if (!add_symbol_adjust (eh, info))
4962             return FALSE;
4963           p = &eh->u.next_dot_sym;
4964         }
4965     }
4966
4967   /* Clear the list for non-ppc64 input files.  */
4968   p = &htab->dot_syms;
4969   while ((eh = *p) != NULL)
4970     {
4971       *p = NULL;
4972       p = &eh->u.next_dot_sym;
4973     }
4974
4975   /* We need to fix the undefs list for any syms we have twiddled to
4976      undef_weak.  */
4977   if (htab->twiddled_syms)
4978     {
4979       bfd_link_repair_undef_list (&htab->elf.root);
4980       htab->twiddled_syms = 0;
4981     }
4982   return TRUE;
4983 }
4984
4985 /* Undo hash table changes when an --as-needed input file is determined
4986    not to be needed.  */
4987
4988 static bfd_boolean
4989 ppc64_elf_notice_as_needed (bfd *ibfd,
4990                             struct bfd_link_info *info,
4991                             enum notice_asneeded_action act)
4992 {
4993   if (act == notice_not_needed)
4994     {
4995       struct ppc_link_hash_table *htab = ppc_hash_table (info);
4996
4997       if (htab == NULL)
4998         return FALSE;
4999
5000       htab->dot_syms = NULL;
5001     }
5002   return _bfd_elf_notice_as_needed (ibfd, info, act);
5003 }
5004
5005 /* If --just-symbols against a final linked binary, then assume we need
5006    toc adjusting stubs when calling functions defined there.  */
5007
5008 static void
5009 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5010 {
5011   if ((sec->flags & SEC_CODE) != 0
5012       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5013       && is_ppc64_elf (sec->owner))
5014     {
5015       asection *got = bfd_get_section_by_name (sec->owner, ".got");
5016       if (got != NULL
5017           && got->size >= elf_backend_got_header_size
5018           && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5019         sec->has_toc_reloc = 1;
5020     }
5021   _bfd_elf_link_just_syms (sec, info);
5022 }
5023
5024 static struct plt_entry **
5025 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5026                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5027 {
5028   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5029   struct plt_entry **local_plt;
5030   unsigned char *local_got_tls_masks;
5031
5032   if (local_got_ents == NULL)
5033     {
5034       bfd_size_type size = symtab_hdr->sh_info;
5035
5036       size *= (sizeof (*local_got_ents)
5037                + sizeof (*local_plt)
5038                + sizeof (*local_got_tls_masks));
5039       local_got_ents = bfd_zalloc (abfd, size);
5040       if (local_got_ents == NULL)
5041         return NULL;
5042       elf_local_got_ents (abfd) = local_got_ents;
5043     }
5044
5045   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5046     {
5047       struct got_entry *ent;
5048
5049       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5050         if (ent->addend == r_addend
5051             && ent->owner == abfd
5052             && ent->tls_type == tls_type)
5053           break;
5054       if (ent == NULL)
5055         {
5056           bfd_size_type amt = sizeof (*ent);
5057           ent = bfd_alloc (abfd, amt);
5058           if (ent == NULL)
5059             return FALSE;
5060           ent->next = local_got_ents[r_symndx];
5061           ent->addend = r_addend;
5062           ent->owner = abfd;
5063           ent->tls_type = tls_type;
5064           ent->is_indirect = FALSE;
5065           ent->got.refcount = 0;
5066           local_got_ents[r_symndx] = ent;
5067         }
5068       ent->got.refcount += 1;
5069     }
5070
5071   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5072   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5073   local_got_tls_masks[r_symndx] |= tls_type;
5074
5075   return local_plt + r_symndx;
5076 }
5077
5078 static bfd_boolean
5079 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5080 {
5081   struct plt_entry *ent;
5082
5083   for (ent = *plist; ent != NULL; ent = ent->next)
5084     if (ent->addend == addend)
5085       break;
5086   if (ent == NULL)
5087     {
5088       bfd_size_type amt = sizeof (*ent);
5089       ent = bfd_alloc (abfd, amt);
5090       if (ent == NULL)
5091         return FALSE;
5092       ent->next = *plist;
5093       ent->addend = addend;
5094       ent->plt.refcount = 0;
5095       *plist = ent;
5096     }
5097   ent->plt.refcount += 1;
5098   return TRUE;
5099 }
5100
5101 static bfd_boolean
5102 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5103 {
5104   return (r_type == R_PPC64_REL24
5105           || r_type == R_PPC64_REL14
5106           || r_type == R_PPC64_REL14_BRTAKEN
5107           || r_type == R_PPC64_REL14_BRNTAKEN
5108           || r_type == R_PPC64_ADDR24
5109           || r_type == R_PPC64_ADDR14
5110           || r_type == R_PPC64_ADDR14_BRTAKEN
5111           || r_type == R_PPC64_ADDR14_BRNTAKEN);
5112 }
5113
5114 /* Look through the relocs for a section during the first phase, and
5115    calculate needed space in the global offset table, procedure
5116    linkage table, and dynamic reloc sections.  */
5117
5118 static bfd_boolean
5119 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5120                         asection *sec, const Elf_Internal_Rela *relocs)
5121 {
5122   struct ppc_link_hash_table *htab;
5123   Elf_Internal_Shdr *symtab_hdr;
5124   struct elf_link_hash_entry **sym_hashes;
5125   const Elf_Internal_Rela *rel;
5126   const Elf_Internal_Rela *rel_end;
5127   asection *sreloc;
5128   asection **opd_sym_map;
5129   struct elf_link_hash_entry *tga, *dottga;
5130
5131   if (info->relocatable)
5132     return TRUE;
5133
5134   /* Don't do anything special with non-loaded, non-alloced sections.
5135      In particular, any relocs in such sections should not affect GOT
5136      and PLT reference counting (ie. we don't allow them to create GOT
5137      or PLT entries), there's no possibility or desire to optimize TLS
5138      relocs, and there's not much point in propagating relocs to shared
5139      libs that the dynamic linker won't relocate.  */
5140   if ((sec->flags & SEC_ALLOC) == 0)
5141     return TRUE;
5142
5143   BFD_ASSERT (is_ppc64_elf (abfd));
5144
5145   htab = ppc_hash_table (info);
5146   if (htab == NULL)
5147     return FALSE;
5148
5149   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5150                               FALSE, FALSE, TRUE);
5151   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5152                                  FALSE, FALSE, TRUE);
5153   symtab_hdr = &elf_symtab_hdr (abfd);
5154   sym_hashes = elf_sym_hashes (abfd);
5155   sreloc = NULL;
5156   opd_sym_map = NULL;
5157   if (strcmp (sec->name, ".opd") == 0)
5158     {
5159       /* Garbage collection needs some extra help with .opd sections.
5160          We don't want to necessarily keep everything referenced by
5161          relocs in .opd, as that would keep all functions.  Instead,
5162          if we reference an .opd symbol (a function descriptor), we
5163          want to keep the function code symbol's section.  This is
5164          easy for global symbols, but for local syms we need to keep
5165          information about the associated function section.  */
5166       bfd_size_type amt;
5167
5168       if (abiversion (abfd) == 0)
5169         set_abiversion (abfd, 1);
5170       else if (abiversion (abfd) == 2)
5171         {
5172           info->callbacks->einfo (_("%P: .opd not allowed in ABI version %d\n"),
5173                                   abiversion (abfd));
5174           bfd_set_error (bfd_error_bad_value);
5175           return FALSE;
5176         }
5177       amt = sec->size * sizeof (*opd_sym_map) / 8;
5178       opd_sym_map = bfd_zalloc (abfd, amt);
5179       if (opd_sym_map == NULL)
5180         return FALSE;
5181       ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
5182       BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
5183       ppc64_elf_section_data (sec)->sec_type = sec_opd;
5184     }
5185
5186   rel_end = relocs + sec->reloc_count;
5187   for (rel = relocs; rel < rel_end; rel++)
5188     {
5189       unsigned long r_symndx;
5190       struct elf_link_hash_entry *h;
5191       enum elf_ppc64_reloc_type r_type;
5192       int tls_type;
5193       struct _ppc64_elf_section_data *ppc64_sec;
5194       struct plt_entry **ifunc;
5195
5196       r_symndx = ELF64_R_SYM (rel->r_info);
5197       if (r_symndx < symtab_hdr->sh_info)
5198         h = NULL;
5199       else
5200         {
5201           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5202           h = elf_follow_link (h);
5203
5204           /* PR15323, ref flags aren't set for references in the same
5205              object.  */
5206           h->root.non_ir_ref = 1;
5207
5208           if (h == htab->elf.hgot)
5209             sec->has_toc_reloc = 1;
5210         }
5211
5212       tls_type = 0;
5213       ifunc = NULL;
5214       if (h != NULL)
5215         {
5216           if (h->type == STT_GNU_IFUNC)
5217             {
5218               h->needs_plt = 1;
5219               ifunc = &h->plt.plist;
5220             }
5221         }
5222       else
5223         {
5224           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5225                                                           abfd, r_symndx);
5226           if (isym == NULL)
5227             return FALSE;
5228
5229           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5230             {
5231               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5232                                              rel->r_addend, PLT_IFUNC);
5233               if (ifunc == NULL)
5234                 return FALSE;
5235             }
5236         }
5237       r_type = ELF64_R_TYPE (rel->r_info);
5238       if (is_branch_reloc (r_type))
5239         {
5240           if (h != NULL && (h == tga || h == dottga))
5241             {
5242               if (rel != relocs
5243                   && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5244                       || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5245                 /* We have a new-style __tls_get_addr call with a marker
5246                    reloc.  */
5247                 ;
5248               else
5249                 /* Mark this section as having an old-style call.  */
5250                 sec->has_tls_get_addr_call = 1;
5251             }
5252
5253           /* STT_GNU_IFUNC symbols must have a PLT entry.  */
5254           if (ifunc != NULL
5255               && !update_plt_info (abfd, ifunc, rel->r_addend))
5256             return FALSE;
5257         }
5258
5259       switch (r_type)
5260         {
5261         case R_PPC64_TLSGD:
5262         case R_PPC64_TLSLD:
5263           /* These special tls relocs tie a call to __tls_get_addr with
5264              its parameter symbol.  */
5265           break;
5266
5267         case R_PPC64_GOT_TLSLD16:
5268         case R_PPC64_GOT_TLSLD16_LO:
5269         case R_PPC64_GOT_TLSLD16_HI:
5270         case R_PPC64_GOT_TLSLD16_HA:
5271           tls_type = TLS_TLS | TLS_LD;
5272           goto dogottls;
5273
5274         case R_PPC64_GOT_TLSGD16:
5275         case R_PPC64_GOT_TLSGD16_LO:
5276         case R_PPC64_GOT_TLSGD16_HI:
5277         case R_PPC64_GOT_TLSGD16_HA:
5278           tls_type = TLS_TLS | TLS_GD;
5279           goto dogottls;
5280
5281         case R_PPC64_GOT_TPREL16_DS:
5282         case R_PPC64_GOT_TPREL16_LO_DS:
5283         case R_PPC64_GOT_TPREL16_HI:
5284         case R_PPC64_GOT_TPREL16_HA:
5285           if (!info->executable)
5286             info->flags |= DF_STATIC_TLS;
5287           tls_type = TLS_TLS | TLS_TPREL;
5288           goto dogottls;
5289
5290         case R_PPC64_GOT_DTPREL16_DS:
5291         case R_PPC64_GOT_DTPREL16_LO_DS:
5292         case R_PPC64_GOT_DTPREL16_HI:
5293         case R_PPC64_GOT_DTPREL16_HA:
5294           tls_type = TLS_TLS | TLS_DTPREL;
5295         dogottls:
5296           sec->has_tls_reloc = 1;
5297           /* Fall thru */
5298
5299         case R_PPC64_GOT16:
5300         case R_PPC64_GOT16_DS:
5301         case R_PPC64_GOT16_HA:
5302         case R_PPC64_GOT16_HI:
5303         case R_PPC64_GOT16_LO:
5304         case R_PPC64_GOT16_LO_DS:
5305           /* This symbol requires a global offset table entry.  */
5306           sec->has_toc_reloc = 1;
5307           if (r_type == R_PPC64_GOT_TLSLD16
5308               || r_type == R_PPC64_GOT_TLSGD16
5309               || r_type == R_PPC64_GOT_TPREL16_DS
5310               || r_type == R_PPC64_GOT_DTPREL16_DS
5311               || r_type == R_PPC64_GOT16
5312               || r_type == R_PPC64_GOT16_DS)
5313             {
5314               htab->do_multi_toc = 1;
5315               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5316             }
5317
5318           if (ppc64_elf_tdata (abfd)->got == NULL
5319               && !create_got_section (abfd, info))
5320             return FALSE;
5321
5322           if (h != NULL)
5323             {
5324               struct ppc_link_hash_entry *eh;
5325               struct got_entry *ent;
5326
5327               eh = (struct ppc_link_hash_entry *) h;
5328               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5329                 if (ent->addend == rel->r_addend
5330                     && ent->owner == abfd
5331                     && ent->tls_type == tls_type)
5332                   break;
5333               if (ent == NULL)
5334                 {
5335                   bfd_size_type amt = sizeof (*ent);
5336                   ent = bfd_alloc (abfd, amt);
5337                   if (ent == NULL)
5338                     return FALSE;
5339                   ent->next = eh->elf.got.glist;
5340                   ent->addend = rel->r_addend;
5341                   ent->owner = abfd;
5342                   ent->tls_type = tls_type;
5343                   ent->is_indirect = FALSE;
5344                   ent->got.refcount = 0;
5345                   eh->elf.got.glist = ent;
5346                 }
5347               ent->got.refcount += 1;
5348               eh->tls_mask |= tls_type;
5349             }
5350           else
5351             /* This is a global offset table entry for a local symbol.  */
5352             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5353                                         rel->r_addend, tls_type))
5354               return FALSE;
5355           break;
5356
5357         case R_PPC64_PLT16_HA:
5358         case R_PPC64_PLT16_HI:
5359         case R_PPC64_PLT16_LO:
5360         case R_PPC64_PLT32:
5361         case R_PPC64_PLT64:
5362           /* This symbol requires a procedure linkage table entry.  We
5363              actually build the entry in adjust_dynamic_symbol,
5364              because this might be a case of linking PIC code without
5365              linking in any dynamic objects, in which case we don't
5366              need to generate a procedure linkage table after all.  */
5367           if (h == NULL)
5368             {
5369               /* It does not make sense to have a procedure linkage
5370                  table entry for a local symbol.  */
5371               bfd_set_error (bfd_error_bad_value);
5372               return FALSE;
5373             }
5374           else
5375             {
5376               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5377                 return FALSE;
5378               h->needs_plt = 1;
5379               if (h->root.root.string[0] == '.'
5380                   && h->root.root.string[1] != '\0')
5381                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5382             }
5383           break;
5384
5385           /* The following relocations don't need to propagate the
5386              relocation if linking a shared object since they are
5387              section relative.  */
5388         case R_PPC64_SECTOFF:
5389         case R_PPC64_SECTOFF_LO:
5390         case R_PPC64_SECTOFF_HI:
5391         case R_PPC64_SECTOFF_HA:
5392         case R_PPC64_SECTOFF_DS:
5393         case R_PPC64_SECTOFF_LO_DS:
5394         case R_PPC64_DTPREL16:
5395         case R_PPC64_DTPREL16_LO:
5396         case R_PPC64_DTPREL16_HI:
5397         case R_PPC64_DTPREL16_HA:
5398         case R_PPC64_DTPREL16_DS:
5399         case R_PPC64_DTPREL16_LO_DS:
5400         case R_PPC64_DTPREL16_HIGH:
5401         case R_PPC64_DTPREL16_HIGHA:
5402         case R_PPC64_DTPREL16_HIGHER:
5403         case R_PPC64_DTPREL16_HIGHERA:
5404         case R_PPC64_DTPREL16_HIGHEST:
5405         case R_PPC64_DTPREL16_HIGHESTA:
5406           break;
5407
5408           /* Nor do these.  */
5409         case R_PPC64_REL16:
5410         case R_PPC64_REL16_LO:
5411         case R_PPC64_REL16_HI:
5412         case R_PPC64_REL16_HA:
5413           break;
5414
5415         case R_PPC64_TOC16:
5416         case R_PPC64_TOC16_DS:
5417           htab->do_multi_toc = 1;
5418           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5419         case R_PPC64_TOC16_LO:
5420         case R_PPC64_TOC16_HI:
5421         case R_PPC64_TOC16_HA:
5422         case R_PPC64_TOC16_LO_DS:
5423           sec->has_toc_reloc = 1;
5424           break;
5425
5426           /* This relocation describes the C++ object vtable hierarchy.
5427              Reconstruct it for later use during GC.  */
5428         case R_PPC64_GNU_VTINHERIT:
5429           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5430             return FALSE;
5431           break;
5432
5433           /* This relocation describes which C++ vtable entries are actually
5434              used.  Record for later use during GC.  */
5435         case R_PPC64_GNU_VTENTRY:
5436           BFD_ASSERT (h != NULL);
5437           if (h != NULL
5438               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5439             return FALSE;
5440           break;
5441
5442         case R_PPC64_REL14:
5443         case R_PPC64_REL14_BRTAKEN:
5444         case R_PPC64_REL14_BRNTAKEN:
5445           {
5446             asection *dest = NULL;
5447
5448             /* Heuristic: If jumping outside our section, chances are
5449                we are going to need a stub.  */
5450             if (h != NULL)
5451               {
5452                 /* If the sym is weak it may be overridden later, so
5453                    don't assume we know where a weak sym lives.  */
5454                 if (h->root.type == bfd_link_hash_defined)
5455                   dest = h->root.u.def.section;
5456               }
5457             else
5458               {
5459                 Elf_Internal_Sym *isym;
5460
5461                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5462                                               abfd, r_symndx);
5463                 if (isym == NULL)
5464                   return FALSE;
5465
5466                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5467               }
5468
5469             if (dest != sec)
5470               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5471           }
5472           /* Fall through.  */
5473
5474         case R_PPC64_REL24:
5475           if (h != NULL && ifunc == NULL)
5476             {
5477               /* We may need a .plt entry if the function this reloc
5478                  refers to is in a shared lib.  */
5479               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5480                 return FALSE;
5481               h->needs_plt = 1;
5482               if (h->root.root.string[0] == '.'
5483                   && h->root.root.string[1] != '\0')
5484                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5485               if (h == tga || h == dottga)
5486                 sec->has_tls_reloc = 1;
5487             }
5488           break;
5489
5490         case R_PPC64_TPREL64:
5491           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5492           if (!info->executable)
5493             info->flags |= DF_STATIC_TLS;
5494           goto dotlstoc;
5495
5496         case R_PPC64_DTPMOD64:
5497           if (rel + 1 < rel_end
5498               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5499               && rel[1].r_offset == rel->r_offset + 8)
5500             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5501           else
5502             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5503           goto dotlstoc;
5504
5505         case R_PPC64_DTPREL64:
5506           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5507           if (rel != relocs
5508               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5509               && rel[-1].r_offset == rel->r_offset - 8)
5510             /* This is the second reloc of a dtpmod, dtprel pair.
5511                Don't mark with TLS_DTPREL.  */
5512             goto dodyn;
5513
5514         dotlstoc:
5515           sec->has_tls_reloc = 1;
5516           if (h != NULL)
5517             {
5518               struct ppc_link_hash_entry *eh;
5519               eh = (struct ppc_link_hash_entry *) h;
5520               eh->tls_mask |= tls_type;
5521             }
5522           else
5523             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5524                                         rel->r_addend, tls_type))
5525               return FALSE;
5526
5527           ppc64_sec = ppc64_elf_section_data (sec);
5528           if (ppc64_sec->sec_type != sec_toc)
5529             {
5530               bfd_size_type amt;
5531
5532               /* One extra to simplify get_tls_mask.  */
5533               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5534               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5535               if (ppc64_sec->u.toc.symndx == NULL)
5536                 return FALSE;
5537               amt = sec->size * sizeof (bfd_vma) / 8;
5538               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5539               if (ppc64_sec->u.toc.add == NULL)
5540                 return FALSE;
5541               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5542               ppc64_sec->sec_type = sec_toc;
5543             }
5544           BFD_ASSERT (rel->r_offset % 8 == 0);
5545           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5546           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5547
5548           /* Mark the second slot of a GD or LD entry.
5549              -1 to indicate GD and -2 to indicate LD.  */
5550           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5551             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5552           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5553             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5554           goto dodyn;
5555
5556         case R_PPC64_TPREL16:
5557         case R_PPC64_TPREL16_LO:
5558         case R_PPC64_TPREL16_HI:
5559         case R_PPC64_TPREL16_HA:
5560         case R_PPC64_TPREL16_DS:
5561         case R_PPC64_TPREL16_LO_DS:
5562         case R_PPC64_TPREL16_HIGH:
5563         case R_PPC64_TPREL16_HIGHA:
5564         case R_PPC64_TPREL16_HIGHER:
5565         case R_PPC64_TPREL16_HIGHERA:
5566         case R_PPC64_TPREL16_HIGHEST:
5567         case R_PPC64_TPREL16_HIGHESTA:
5568           if (info->shared)
5569             {
5570               if (!info->executable)
5571                 info->flags |= DF_STATIC_TLS;
5572               goto dodyn;
5573             }
5574           break;
5575
5576         case R_PPC64_ADDR64:
5577           if (opd_sym_map != NULL
5578               && rel + 1 < rel_end
5579               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5580             {
5581               if (h != NULL)
5582                 {
5583                   if (h->root.root.string[0] == '.'
5584                       && h->root.root.string[1] != 0
5585                       && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5586                     ;
5587                   else
5588                     ((struct ppc_link_hash_entry *) h)->is_func = 1;
5589                 }
5590               else
5591                 {
5592                   asection *s;
5593                   Elf_Internal_Sym *isym;
5594
5595                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5596                                                 abfd, r_symndx);
5597                   if (isym == NULL)
5598                     return FALSE;
5599
5600                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5601                   if (s != NULL && s != sec)
5602                     opd_sym_map[rel->r_offset / 8] = s;
5603                 }
5604             }
5605           /* Fall through.  */
5606
5607         case R_PPC64_REL30:
5608         case R_PPC64_REL32:
5609         case R_PPC64_REL64:
5610         case R_PPC64_ADDR14:
5611         case R_PPC64_ADDR14_BRNTAKEN:
5612         case R_PPC64_ADDR14_BRTAKEN:
5613         case R_PPC64_ADDR16:
5614         case R_PPC64_ADDR16_DS:
5615         case R_PPC64_ADDR16_HA:
5616         case R_PPC64_ADDR16_HI:
5617         case R_PPC64_ADDR16_HIGH:
5618         case R_PPC64_ADDR16_HIGHA:
5619         case R_PPC64_ADDR16_HIGHER:
5620         case R_PPC64_ADDR16_HIGHERA:
5621         case R_PPC64_ADDR16_HIGHEST:
5622         case R_PPC64_ADDR16_HIGHESTA:
5623         case R_PPC64_ADDR16_LO:
5624         case R_PPC64_ADDR16_LO_DS:
5625         case R_PPC64_ADDR24:
5626         case R_PPC64_ADDR32:
5627         case R_PPC64_UADDR16:
5628         case R_PPC64_UADDR32:
5629         case R_PPC64_UADDR64:
5630         case R_PPC64_TOC:
5631           if (h != NULL && !info->shared)
5632             /* We may need a copy reloc.  */
5633             h->non_got_ref = 1;
5634
5635           /* Don't propagate .opd relocs.  */
5636           if (NO_OPD_RELOCS && opd_sym_map != NULL)
5637             break;
5638
5639           /* If we are creating a shared library, and this is a reloc
5640              against a global symbol, or a non PC relative reloc
5641              against a local symbol, then we need to copy the reloc
5642              into the shared library.  However, if we are linking with
5643              -Bsymbolic, we do not need to copy a reloc against a
5644              global symbol which is defined in an object we are
5645              including in the link (i.e., DEF_REGULAR is set).  At
5646              this point we have not seen all the input files, so it is
5647              possible that DEF_REGULAR is not set now but will be set
5648              later (it is never cleared).  In case of a weak definition,
5649              DEF_REGULAR may be cleared later by a strong definition in
5650              a shared library.  We account for that possibility below by
5651              storing information in the dyn_relocs field of the hash
5652              table entry.  A similar situation occurs when creating
5653              shared libraries and symbol visibility changes render the
5654              symbol local.
5655
5656              If on the other hand, we are creating an executable, we
5657              may need to keep relocations for symbols satisfied by a
5658              dynamic library if we manage to avoid copy relocs for the
5659              symbol.  */
5660         dodyn:
5661           if ((info->shared
5662                && (must_be_dyn_reloc (info, r_type)
5663                    || (h != NULL
5664                        && (!SYMBOLIC_BIND (info, h)
5665                            || h->root.type == bfd_link_hash_defweak
5666                            || !h->def_regular))))
5667               || (ELIMINATE_COPY_RELOCS
5668                   && !info->shared
5669                   && h != NULL
5670                   && (h->root.type == bfd_link_hash_defweak
5671                       || !h->def_regular))
5672               || (!info->shared
5673                   && ifunc != NULL))
5674             {
5675               /* We must copy these reloc types into the output file.
5676                  Create a reloc section in dynobj and make room for
5677                  this reloc.  */
5678               if (sreloc == NULL)
5679                 {
5680                   sreloc = _bfd_elf_make_dynamic_reloc_section
5681                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5682
5683                   if (sreloc == NULL)
5684                     return FALSE;
5685                 }
5686
5687               /* If this is a global symbol, we count the number of
5688                  relocations we need for this symbol.  */
5689               if (h != NULL)
5690                 {
5691                   struct elf_dyn_relocs *p;
5692                   struct elf_dyn_relocs **head;
5693
5694                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5695                   p = *head;
5696                   if (p == NULL || p->sec != sec)
5697                     {
5698                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5699                       if (p == NULL)
5700                         return FALSE;
5701                       p->next = *head;
5702                       *head = p;
5703                       p->sec = sec;
5704                       p->count = 0;
5705                       p->pc_count = 0;
5706                     }
5707                   p->count += 1;
5708                   if (!must_be_dyn_reloc (info, r_type))
5709                     p->pc_count += 1;
5710                 }
5711               else
5712                 {
5713                   /* Track dynamic relocs needed for local syms too.
5714                      We really need local syms available to do this
5715                      easily.  Oh well.  */
5716                   struct ppc_dyn_relocs *p;
5717                   struct ppc_dyn_relocs **head;
5718                   bfd_boolean is_ifunc;
5719                   asection *s;
5720                   void *vpp;
5721                   Elf_Internal_Sym *isym;
5722
5723                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5724                                                 abfd, r_symndx);
5725                   if (isym == NULL)
5726                     return FALSE;
5727
5728                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5729                   if (s == NULL)
5730                     s = sec;
5731
5732                   vpp = &elf_section_data (s)->local_dynrel;
5733                   head = (struct ppc_dyn_relocs **) vpp;
5734                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5735                   p = *head;
5736                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5737                     p = p->next;
5738                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5739                     {
5740                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5741                       if (p == NULL)
5742                         return FALSE;
5743                       p->next = *head;
5744                       *head = p;
5745                       p->sec = sec;
5746                       p->ifunc = is_ifunc;
5747                       p->count = 0;
5748                     }
5749                   p->count += 1;
5750                 }
5751             }
5752           break;
5753
5754         default:
5755           break;
5756         }
5757     }
5758
5759   return TRUE;
5760 }
5761
5762 /* Merge backend specific data from an object file to the output
5763    object file when linking.  */
5764
5765 static bfd_boolean
5766 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5767 {
5768   unsigned long iflags, oflags;
5769
5770   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5771     return TRUE;
5772
5773   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5774     return TRUE;
5775
5776   if (!_bfd_generic_verify_endian_match (ibfd, obfd))
5777     return FALSE;
5778
5779   iflags = elf_elfheader (ibfd)->e_flags;
5780   oflags = elf_elfheader (obfd)->e_flags;
5781
5782   if (!elf_flags_init (obfd) || oflags == 0)
5783     {
5784       elf_flags_init (obfd) = TRUE;
5785       elf_elfheader (obfd)->e_flags = iflags;
5786     }
5787   else if (iflags == oflags || iflags == 0)
5788     ;
5789   else if (iflags & ~EF_PPC64_ABI)
5790     {
5791       (*_bfd_error_handler)
5792         (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
5793       bfd_set_error (bfd_error_bad_value);
5794       return FALSE;
5795     }
5796   else
5797     {
5798       (*_bfd_error_handler)
5799         (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
5800          ibfd, iflags, oflags);
5801       bfd_set_error (bfd_error_bad_value);
5802       return FALSE;
5803     }
5804
5805   /* Merge Tag_compatibility attributes and any common GNU ones.  */
5806   _bfd_elf_merge_object_attributes (ibfd, obfd);
5807
5808   return TRUE;
5809 }
5810
5811 static bfd_boolean
5812 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5813 {
5814   /* Print normal ELF private data.  */
5815   _bfd_elf_print_private_bfd_data (abfd, ptr);
5816
5817   if (elf_elfheader (abfd)->e_flags != 0)
5818     {
5819       FILE *file = ptr;
5820
5821       /* xgettext:c-format */
5822       fprintf (file, _("private flags = 0x%lx:"),
5823                elf_elfheader (abfd)->e_flags);
5824
5825       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5826         fprintf (file, _(" [abiv%ld]"),
5827                  elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5828       fputc ('\n', file);
5829     }
5830
5831   return TRUE;
5832 }
5833
5834 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
5835    of the code entry point, and its section.  */
5836
5837 static bfd_vma
5838 opd_entry_value (asection *opd_sec,
5839                  bfd_vma offset,
5840                  asection **code_sec,
5841                  bfd_vma *code_off,
5842                  bfd_boolean in_code_sec)
5843 {
5844   bfd *opd_bfd = opd_sec->owner;
5845   Elf_Internal_Rela *relocs;
5846   Elf_Internal_Rela *lo, *hi, *look;
5847   bfd_vma val;
5848
5849   /* No relocs implies we are linking a --just-symbols object, or looking
5850      at a final linked executable with addr2line or somesuch.  */
5851   if (opd_sec->reloc_count == 0)
5852     {
5853       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5854
5855       if (contents == NULL)
5856         {
5857           if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5858             return (bfd_vma) -1;
5859           ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5860         }
5861
5862       val = bfd_get_64 (opd_bfd, contents + offset);
5863       if (code_sec != NULL)
5864         {
5865           asection *sec, *likely = NULL;
5866
5867           if (in_code_sec)
5868             {
5869               sec = *code_sec;
5870               if (sec->vma <= val
5871                   && val < sec->vma + sec->size)
5872                 likely = sec;
5873               else
5874                 val = -1;
5875             }
5876           else
5877             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5878               if (sec->vma <= val
5879                   && (sec->flags & SEC_LOAD) != 0
5880                   && (sec->flags & SEC_ALLOC) != 0)
5881                 likely = sec;
5882           if (likely != NULL)
5883             {
5884               *code_sec = likely;
5885               if (code_off != NULL)
5886                 *code_off = val - likely->vma;
5887             }
5888         }
5889       return val;
5890     }
5891
5892   BFD_ASSERT (is_ppc64_elf (opd_bfd));
5893
5894   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5895   if (relocs == NULL)
5896     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5897
5898   /* Go find the opd reloc at the sym address.  */
5899   lo = relocs;
5900   BFD_ASSERT (lo != NULL);
5901   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5902   val = (bfd_vma) -1;
5903   while (lo < hi)
5904     {
5905       look = lo + (hi - lo) / 2;
5906       if (look->r_offset < offset)
5907         lo = look + 1;
5908       else if (look->r_offset > offset)
5909         hi = look;
5910       else
5911         {
5912           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5913
5914           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5915               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5916             {
5917               unsigned long symndx = ELF64_R_SYM (look->r_info);
5918               asection *sec;
5919
5920               if (symndx < symtab_hdr->sh_info
5921                   || elf_sym_hashes (opd_bfd) == NULL)
5922                 {
5923                   Elf_Internal_Sym *sym;
5924
5925                   sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5926                   if (sym == NULL)
5927                     {
5928                       size_t symcnt = symtab_hdr->sh_info;
5929                       if (elf_sym_hashes (opd_bfd) == NULL)
5930                         symcnt = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
5931                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, symcnt,
5932                                                   0, NULL, NULL, NULL);
5933                       if (sym == NULL)
5934                         break;
5935                       symtab_hdr->contents = (bfd_byte *) sym;
5936                     }
5937
5938                   sym += symndx;
5939                   val = sym->st_value;
5940                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5941                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5942                 }
5943               else
5944                 {
5945                   struct elf_link_hash_entry **sym_hashes;
5946                   struct elf_link_hash_entry *rh;
5947
5948                   sym_hashes = elf_sym_hashes (opd_bfd);
5949                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
5950                   if (rh != NULL)
5951                     {
5952                       rh = elf_follow_link (rh);
5953                       BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5954                                   || rh->root.type == bfd_link_hash_defweak);
5955                       val = rh->root.u.def.value;
5956                       sec = rh->root.u.def.section;
5957                     }
5958                   else
5959                     {
5960                       /* Handle the odd case where we can be called
5961                          during bfd_elf_link_add_symbols before the
5962                          symbol hashes have been fully populated.  */
5963                       Elf_Internal_Sym *sym;
5964
5965                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, 1,
5966                                                   symndx, NULL, NULL, NULL);
5967                       if (sym == NULL)
5968                         break;
5969
5970                       val = sym->st_value;
5971                       sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5972                       free (sym);
5973                     }
5974                 }
5975               val += look->r_addend;
5976               if (code_off != NULL)
5977                 *code_off = val;
5978               if (code_sec != NULL)
5979                 {
5980                   if (in_code_sec && *code_sec != sec)
5981                     return -1;
5982                   else
5983                     *code_sec = sec;
5984                 }
5985               if (sec != NULL && sec->output_section != NULL)
5986                 val += sec->output_section->vma + sec->output_offset;
5987             }
5988           break;
5989         }
5990     }
5991
5992   return val;
5993 }
5994
5995 /* If the ELF symbol SYM might be a function in SEC, return the
5996    function size and set *CODE_OFF to the function's entry point,
5997    otherwise return zero.  */
5998
5999 static bfd_size_type
6000 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6001                               bfd_vma *code_off)
6002 {
6003   bfd_size_type size;
6004
6005   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6006                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6007     return 0;
6008
6009   size = 0;
6010   if (!(sym->flags & BSF_SYNTHETIC))
6011     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6012
6013   if (strcmp (sym->section->name, ".opd") == 0)
6014     {
6015       if (opd_entry_value (sym->section, sym->value,
6016                            &sec, code_off, TRUE) == (bfd_vma) -1)
6017         return 0;
6018       /* An old ABI binary with dot-syms has a size of 24 on the .opd
6019          symbol.  This size has nothing to do with the code size of the
6020          function, which is what we're supposed to return, but the
6021          code size isn't available without looking up the dot-sym.
6022          However, doing that would be a waste of time particularly
6023          since elf_find_function will look at the dot-sym anyway.
6024          Now, elf_find_function will keep the largest size of any
6025          function sym found at the code address of interest, so return
6026          1 here to avoid it incorrectly caching a larger function size
6027          for a small function.  This does mean we return the wrong
6028          size for a new-ABI function of size 24, but all that does is
6029          disable caching for such functions.  */
6030       if (size == 24)
6031         size = 1;
6032     }
6033   else
6034     {
6035       if (sym->section != sec)
6036         return 0;
6037       *code_off = sym->value;
6038     }
6039   if (size == 0)
6040     size = 1;
6041   return size;
6042 }
6043
6044 /* Return true if symbol is defined in a regular object file.  */
6045
6046 static bfd_boolean
6047 is_static_defined (struct elf_link_hash_entry *h)
6048 {
6049   return ((h->root.type == bfd_link_hash_defined
6050            || h->root.type == bfd_link_hash_defweak)
6051           && h->root.u.def.section != NULL
6052           && h->root.u.def.section->output_section != NULL);
6053 }
6054
6055 /* If FDH is a function descriptor symbol, return the associated code
6056    entry symbol if it is defined.  Return NULL otherwise.  */
6057
6058 static struct ppc_link_hash_entry *
6059 defined_code_entry (struct ppc_link_hash_entry *fdh)
6060 {
6061   if (fdh->is_func_descriptor)
6062     {
6063       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6064       if (fh->elf.root.type == bfd_link_hash_defined
6065           || fh->elf.root.type == bfd_link_hash_defweak)
6066         return fh;
6067     }
6068   return NULL;
6069 }
6070
6071 /* If FH is a function code entry symbol, return the associated
6072    function descriptor symbol if it is defined.  Return NULL otherwise.  */
6073
6074 static struct ppc_link_hash_entry *
6075 defined_func_desc (struct ppc_link_hash_entry *fh)
6076 {
6077   if (fh->oh != NULL
6078       && fh->oh->is_func_descriptor)
6079     {
6080       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6081       if (fdh->elf.root.type == bfd_link_hash_defined
6082           || fdh->elf.root.type == bfd_link_hash_defweak)
6083         return fdh;
6084     }
6085   return NULL;
6086 }
6087
6088 /* Mark all our entry sym sections, both opd and code section.  */
6089
6090 static void
6091 ppc64_elf_gc_keep (struct bfd_link_info *info)
6092 {
6093   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6094   struct bfd_sym_chain *sym;
6095
6096   if (htab == NULL)
6097     return;
6098
6099   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6100     {
6101       struct ppc_link_hash_entry *eh, *fh;
6102       asection *sec;
6103
6104       eh = (struct ppc_link_hash_entry *)
6105         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6106       if (eh == NULL)
6107         continue;
6108       if (eh->elf.root.type != bfd_link_hash_defined
6109           && eh->elf.root.type != bfd_link_hash_defweak)
6110         continue;
6111
6112       fh = defined_code_entry (eh);
6113       if (fh != NULL)
6114         {
6115           sec = fh->elf.root.u.def.section;
6116           sec->flags |= SEC_KEEP;
6117         }
6118       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6119                && opd_entry_value (eh->elf.root.u.def.section,
6120                                    eh->elf.root.u.def.value,
6121                                    &sec, NULL, FALSE) != (bfd_vma) -1)
6122         sec->flags |= SEC_KEEP;
6123
6124       sec = eh->elf.root.u.def.section;
6125       sec->flags |= SEC_KEEP;
6126     }
6127 }
6128
6129 /* Mark sections containing dynamically referenced symbols.  When
6130    building shared libraries, we must assume that any visible symbol is
6131    referenced.  */
6132
6133 static bfd_boolean
6134 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6135 {
6136   struct bfd_link_info *info = (struct bfd_link_info *) inf;
6137   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6138   struct ppc_link_hash_entry *fdh;
6139
6140   /* Dynamic linking info is on the func descriptor sym.  */
6141   fdh = defined_func_desc (eh);
6142   if (fdh != NULL)
6143     eh = fdh;
6144
6145   if ((eh->elf.root.type == bfd_link_hash_defined
6146        || eh->elf.root.type == bfd_link_hash_defweak)
6147       && (eh->elf.ref_dynamic
6148           || (!info->executable
6149               && eh->elf.def_regular
6150               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6151               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6152               && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6153                   || !bfd_hide_sym_by_version (info->version_info,
6154                                                eh->elf.root.root.string)))))
6155     {
6156       asection *code_sec;
6157       struct ppc_link_hash_entry *fh;
6158
6159       eh->elf.root.u.def.section->flags |= SEC_KEEP;
6160
6161       /* Function descriptor syms cause the associated
6162          function code sym section to be marked.  */
6163       fh = defined_code_entry (eh);
6164       if (fh != NULL)
6165         {
6166           code_sec = fh->elf.root.u.def.section;
6167           code_sec->flags |= SEC_KEEP;
6168         }
6169       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6170                && opd_entry_value (eh->elf.root.u.def.section,
6171                                    eh->elf.root.u.def.value,
6172                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
6173         code_sec->flags |= SEC_KEEP;
6174     }
6175
6176   return TRUE;
6177 }
6178
6179 /* Return the section that should be marked against GC for a given
6180    relocation.  */
6181
6182 static asection *
6183 ppc64_elf_gc_mark_hook (asection *sec,
6184                         struct bfd_link_info *info,
6185                         Elf_Internal_Rela *rel,
6186                         struct elf_link_hash_entry *h,
6187                         Elf_Internal_Sym *sym)
6188 {
6189   asection *rsec;
6190
6191   /* Syms return NULL if we're marking .opd, so we avoid marking all
6192      function sections, as all functions are referenced in .opd.  */
6193   rsec = NULL;
6194   if (get_opd_info (sec) != NULL)
6195     return rsec;
6196
6197   if (h != NULL)
6198     {
6199       enum elf_ppc64_reloc_type r_type;
6200       struct ppc_link_hash_entry *eh, *fh, *fdh;
6201
6202       r_type = ELF64_R_TYPE (rel->r_info);
6203       switch (r_type)
6204         {
6205         case R_PPC64_GNU_VTINHERIT:
6206         case R_PPC64_GNU_VTENTRY:
6207           break;
6208
6209         default:
6210           switch (h->root.type)
6211             {
6212             case bfd_link_hash_defined:
6213             case bfd_link_hash_defweak:
6214               eh = (struct ppc_link_hash_entry *) h;
6215               fdh = defined_func_desc (eh);
6216               if (fdh != NULL)
6217                 eh = fdh;
6218
6219               /* Function descriptor syms cause the associated
6220                  function code sym section to be marked.  */
6221               fh = defined_code_entry (eh);
6222               if (fh != NULL)
6223                 {
6224                   /* They also mark their opd section.  */
6225                   eh->elf.root.u.def.section->gc_mark = 1;
6226
6227                   rsec = fh->elf.root.u.def.section;
6228                 }
6229               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6230                        && opd_entry_value (eh->elf.root.u.def.section,
6231                                            eh->elf.root.u.def.value,
6232                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
6233                 eh->elf.root.u.def.section->gc_mark = 1;
6234               else
6235                 rsec = h->root.u.def.section;
6236               break;
6237
6238             case bfd_link_hash_common:
6239               rsec = h->root.u.c.p->section;
6240               break;
6241
6242             default:
6243               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6244             }
6245         }
6246     }
6247   else
6248     {
6249       struct _opd_sec_data *opd;
6250
6251       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6252       opd = get_opd_info (rsec);
6253       if (opd != NULL && opd->func_sec != NULL)
6254         {
6255           rsec->gc_mark = 1;
6256
6257           rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
6258         }
6259     }
6260
6261   return rsec;
6262 }
6263
6264 /* Update the .got, .plt. and dynamic reloc reference counts for the
6265    section being removed.  */
6266
6267 static bfd_boolean
6268 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6269                          asection *sec, const Elf_Internal_Rela *relocs)
6270 {
6271   struct ppc_link_hash_table *htab;
6272   Elf_Internal_Shdr *symtab_hdr;
6273   struct elf_link_hash_entry **sym_hashes;
6274   struct got_entry **local_got_ents;
6275   const Elf_Internal_Rela *rel, *relend;
6276
6277   if (info->relocatable)
6278     return TRUE;
6279
6280   if ((sec->flags & SEC_ALLOC) == 0)
6281     return TRUE;
6282
6283   elf_section_data (sec)->local_dynrel = NULL;
6284
6285   htab = ppc_hash_table (info);
6286   if (htab == NULL)
6287     return FALSE;
6288
6289   symtab_hdr = &elf_symtab_hdr (abfd);
6290   sym_hashes = elf_sym_hashes (abfd);
6291   local_got_ents = elf_local_got_ents (abfd);
6292
6293   relend = relocs + sec->reloc_count;
6294   for (rel = relocs; rel < relend; rel++)
6295     {
6296       unsigned long r_symndx;
6297       enum elf_ppc64_reloc_type r_type;
6298       struct elf_link_hash_entry *h = NULL;
6299       unsigned char tls_type = 0;
6300
6301       r_symndx = ELF64_R_SYM (rel->r_info);
6302       r_type = ELF64_R_TYPE (rel->r_info);
6303       if (r_symndx >= symtab_hdr->sh_info)
6304         {
6305           struct ppc_link_hash_entry *eh;
6306           struct elf_dyn_relocs **pp;
6307           struct elf_dyn_relocs *p;
6308
6309           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6310           h = elf_follow_link (h);
6311           eh = (struct ppc_link_hash_entry *) h;
6312
6313           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6314             if (p->sec == sec)
6315               {
6316                 /* Everything must go for SEC.  */
6317                 *pp = p->next;
6318                 break;
6319               }
6320         }
6321
6322       if (is_branch_reloc (r_type))
6323         {
6324           struct plt_entry **ifunc = NULL;
6325           if (h != NULL)
6326             {
6327               if (h->type == STT_GNU_IFUNC)
6328                 ifunc = &h->plt.plist;
6329             }
6330           else if (local_got_ents != NULL)
6331             {
6332               struct plt_entry **local_plt = (struct plt_entry **)
6333                 (local_got_ents + symtab_hdr->sh_info);
6334               unsigned char *local_got_tls_masks = (unsigned char *)
6335                 (local_plt + symtab_hdr->sh_info);
6336               if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6337                 ifunc = local_plt + r_symndx;
6338             }
6339           if (ifunc != NULL)
6340             {
6341               struct plt_entry *ent;
6342
6343               for (ent = *ifunc; ent != NULL; ent = ent->next)
6344                 if (ent->addend == rel->r_addend)
6345                   break;
6346               if (ent == NULL)
6347                 abort ();
6348               if (ent->plt.refcount > 0)
6349                 ent->plt.refcount -= 1;
6350               continue;
6351             }
6352         }
6353
6354       switch (r_type)
6355         {
6356         case R_PPC64_GOT_TLSLD16:
6357         case R_PPC64_GOT_TLSLD16_LO:
6358         case R_PPC64_GOT_TLSLD16_HI:
6359         case R_PPC64_GOT_TLSLD16_HA:
6360           tls_type = TLS_TLS | TLS_LD;
6361           goto dogot;
6362
6363         case R_PPC64_GOT_TLSGD16:
6364         case R_PPC64_GOT_TLSGD16_LO:
6365         case R_PPC64_GOT_TLSGD16_HI:
6366         case R_PPC64_GOT_TLSGD16_HA:
6367           tls_type = TLS_TLS | TLS_GD;
6368           goto dogot;
6369
6370         case R_PPC64_GOT_TPREL16_DS:
6371         case R_PPC64_GOT_TPREL16_LO_DS:
6372         case R_PPC64_GOT_TPREL16_HI:
6373         case R_PPC64_GOT_TPREL16_HA:
6374           tls_type = TLS_TLS | TLS_TPREL;
6375           goto dogot;
6376
6377         case R_PPC64_GOT_DTPREL16_DS:
6378         case R_PPC64_GOT_DTPREL16_LO_DS:
6379         case R_PPC64_GOT_DTPREL16_HI:
6380         case R_PPC64_GOT_DTPREL16_HA:
6381           tls_type = TLS_TLS | TLS_DTPREL;
6382           goto dogot;
6383
6384         case R_PPC64_GOT16:
6385         case R_PPC64_GOT16_DS:
6386         case R_PPC64_GOT16_HA:
6387         case R_PPC64_GOT16_HI:
6388         case R_PPC64_GOT16_LO:
6389         case R_PPC64_GOT16_LO_DS:
6390         dogot:
6391           {
6392             struct got_entry *ent;
6393
6394             if (h != NULL)
6395               ent = h->got.glist;
6396             else
6397               ent = local_got_ents[r_symndx];
6398
6399             for (; ent != NULL; ent = ent->next)
6400               if (ent->addend == rel->r_addend
6401                   && ent->owner == abfd
6402                   && ent->tls_type == tls_type)
6403                 break;
6404             if (ent == NULL)
6405               abort ();
6406             if (ent->got.refcount > 0)
6407               ent->got.refcount -= 1;
6408           }
6409           break;
6410
6411         case R_PPC64_PLT16_HA:
6412         case R_PPC64_PLT16_HI:
6413         case R_PPC64_PLT16_LO:
6414         case R_PPC64_PLT32:
6415         case R_PPC64_PLT64:
6416         case R_PPC64_REL14:
6417         case R_PPC64_REL14_BRNTAKEN:
6418         case R_PPC64_REL14_BRTAKEN:
6419         case R_PPC64_REL24:
6420           if (h != NULL)
6421             {
6422               struct plt_entry *ent;
6423
6424               for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6425                 if (ent->addend == rel->r_addend)
6426                   break;
6427               if (ent != NULL && ent->plt.refcount > 0)
6428                 ent->plt.refcount -= 1;
6429             }
6430           break;
6431
6432         default:
6433           break;
6434         }
6435     }
6436   return TRUE;
6437 }
6438
6439 /* The maximum size of .sfpr.  */
6440 #define SFPR_MAX (218*4)
6441
6442 struct sfpr_def_parms
6443 {
6444   const char name[12];
6445   unsigned char lo, hi;
6446   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6447   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6448 };
6449
6450 /* Auto-generate _save*, _rest* functions in .sfpr.  */
6451
6452 static bfd_boolean
6453 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6454 {
6455   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6456   unsigned int i;
6457   size_t len = strlen (parm->name);
6458   bfd_boolean writing = FALSE;
6459   char sym[16];
6460
6461   if (htab == NULL)
6462     return FALSE;
6463
6464   memcpy (sym, parm->name, len);
6465   sym[len + 2] = 0;
6466
6467   for (i = parm->lo; i <= parm->hi; i++)
6468     {
6469       struct elf_link_hash_entry *h;
6470
6471       sym[len + 0] = i / 10 + '0';
6472       sym[len + 1] = i % 10 + '0';
6473       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6474       if (h != NULL
6475           && !h->def_regular)
6476         {
6477           h->root.type = bfd_link_hash_defined;
6478           h->root.u.def.section = htab->sfpr;
6479           h->root.u.def.value = htab->sfpr->size;
6480           h->type = STT_FUNC;
6481           h->def_regular = 1;
6482           _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6483           writing = TRUE;
6484           if (htab->sfpr->contents == NULL)
6485             {
6486               htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6487               if (htab->sfpr->contents == NULL)
6488                 return FALSE;
6489             }
6490         }
6491       if (writing)
6492         {
6493           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6494           if (i != parm->hi)
6495             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6496           else
6497             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6498           htab->sfpr->size = p - htab->sfpr->contents;
6499         }
6500     }
6501
6502   return TRUE;
6503 }
6504
6505 static bfd_byte *
6506 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6507 {
6508   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6509   return p + 4;
6510 }
6511
6512 static bfd_byte *
6513 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6514 {
6515   p = savegpr0 (abfd, p, r);
6516   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6517   p = p + 4;
6518   bfd_put_32 (abfd, BLR, p);
6519   return p + 4;
6520 }
6521
6522 static bfd_byte *
6523 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6524 {
6525   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6526   return p + 4;
6527 }
6528
6529 static bfd_byte *
6530 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6531 {
6532   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6533   p = p + 4;
6534   p = restgpr0 (abfd, p, r);
6535   bfd_put_32 (abfd, MTLR_R0, p);
6536   p = p + 4;
6537   if (r == 29)
6538     {
6539       p = restgpr0 (abfd, p, 30);
6540       p = restgpr0 (abfd, p, 31);
6541     }
6542   bfd_put_32 (abfd, BLR, p);
6543   return p + 4;
6544 }
6545
6546 static bfd_byte *
6547 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6548 {
6549   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6550   return p + 4;
6551 }
6552
6553 static bfd_byte *
6554 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6555 {
6556   p = savegpr1 (abfd, p, r);
6557   bfd_put_32 (abfd, BLR, p);
6558   return p + 4;
6559 }
6560
6561 static bfd_byte *
6562 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6563 {
6564   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6565   return p + 4;
6566 }
6567
6568 static bfd_byte *
6569 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6570 {
6571   p = restgpr1 (abfd, p, r);
6572   bfd_put_32 (abfd, BLR, p);
6573   return p + 4;
6574 }
6575
6576 static bfd_byte *
6577 savefpr (bfd *abfd, bfd_byte *p, int r)
6578 {
6579   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6580   return p + 4;
6581 }
6582
6583 static bfd_byte *
6584 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6585 {
6586   p = savefpr (abfd, p, r);
6587   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6588   p = p + 4;
6589   bfd_put_32 (abfd, BLR, p);
6590   return p + 4;
6591 }
6592
6593 static bfd_byte *
6594 restfpr (bfd *abfd, bfd_byte *p, int r)
6595 {
6596   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6597   return p + 4;
6598 }
6599
6600 static bfd_byte *
6601 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6602 {
6603   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6604   p = p + 4;
6605   p = restfpr (abfd, p, r);
6606   bfd_put_32 (abfd, MTLR_R0, p);
6607   p = p + 4;
6608   if (r == 29)
6609     {
6610       p = restfpr (abfd, p, 30);
6611       p = restfpr (abfd, p, 31);
6612     }
6613   bfd_put_32 (abfd, BLR, p);
6614   return p + 4;
6615 }
6616
6617 static bfd_byte *
6618 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6619 {
6620   p = savefpr (abfd, p, r);
6621   bfd_put_32 (abfd, BLR, p);
6622   return p + 4;
6623 }
6624
6625 static bfd_byte *
6626 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6627 {
6628   p = restfpr (abfd, p, r);
6629   bfd_put_32 (abfd, BLR, p);
6630   return p + 4;
6631 }
6632
6633 static bfd_byte *
6634 savevr (bfd *abfd, bfd_byte *p, int r)
6635 {
6636   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6637   p = p + 4;
6638   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6639   return p + 4;
6640 }
6641
6642 static bfd_byte *
6643 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6644 {
6645   p = savevr (abfd, p, r);
6646   bfd_put_32 (abfd, BLR, p);
6647   return p + 4;
6648 }
6649
6650 static bfd_byte *
6651 restvr (bfd *abfd, bfd_byte *p, int r)
6652 {
6653   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6654   p = p + 4;
6655   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6656   return p + 4;
6657 }
6658
6659 static bfd_byte *
6660 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6661 {
6662   p = restvr (abfd, p, r);
6663   bfd_put_32 (abfd, BLR, p);
6664   return p + 4;
6665 }
6666
6667 /* Called via elf_link_hash_traverse to transfer dynamic linking
6668    information on function code symbol entries to their corresponding
6669    function descriptor symbol entries.  */
6670
6671 static bfd_boolean
6672 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6673 {
6674   struct bfd_link_info *info;
6675   struct ppc_link_hash_table *htab;
6676   struct plt_entry *ent;
6677   struct ppc_link_hash_entry *fh;
6678   struct ppc_link_hash_entry *fdh;
6679   bfd_boolean force_local;
6680
6681   fh = (struct ppc_link_hash_entry *) h;
6682   if (fh->elf.root.type == bfd_link_hash_indirect)
6683     return TRUE;
6684
6685   info = inf;
6686   htab = ppc_hash_table (info);
6687   if (htab == NULL)
6688     return FALSE;
6689
6690   /* Resolve undefined references to dot-symbols as the value
6691      in the function descriptor, if we have one in a regular object.
6692      This is to satisfy cases like ".quad .foo".  Calls to functions
6693      in dynamic objects are handled elsewhere.  */
6694   if (fh->elf.root.type == bfd_link_hash_undefweak
6695       && fh->was_undefined
6696       && (fdh = defined_func_desc (fh)) != NULL
6697       && get_opd_info (fdh->elf.root.u.def.section) != NULL
6698       && opd_entry_value (fdh->elf.root.u.def.section,
6699                           fdh->elf.root.u.def.value,
6700                           &fh->elf.root.u.def.section,
6701                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6702     {
6703       fh->elf.root.type = fdh->elf.root.type;
6704       fh->elf.forced_local = 1;
6705       fh->elf.def_regular = fdh->elf.def_regular;
6706       fh->elf.def_dynamic = fdh->elf.def_dynamic;
6707     }
6708
6709   /* If this is a function code symbol, transfer dynamic linking
6710      information to the function descriptor symbol.  */
6711   if (!fh->is_func)
6712     return TRUE;
6713
6714   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6715     if (ent->plt.refcount > 0)
6716       break;
6717   if (ent == NULL
6718       || fh->elf.root.root.string[0] != '.'
6719       || fh->elf.root.root.string[1] == '\0')
6720     return TRUE;
6721
6722   /* Find the corresponding function descriptor symbol.  Create it
6723      as undefined if necessary.  */
6724
6725   fdh = lookup_fdh (fh, htab);
6726   if (fdh == NULL
6727       && !info->executable
6728       && (fh->elf.root.type == bfd_link_hash_undefined
6729           || fh->elf.root.type == bfd_link_hash_undefweak))
6730     {
6731       fdh = make_fdh (info, fh);
6732       if (fdh == NULL)
6733         return FALSE;
6734     }
6735
6736   /* Fake function descriptors are made undefweak.  If the function
6737      code symbol is strong undefined, make the fake sym the same.
6738      If the function code symbol is defined, then force the fake
6739      descriptor local;  We can't support overriding of symbols in a
6740      shared library on a fake descriptor.  */
6741
6742   if (fdh != NULL
6743       && fdh->fake
6744       && fdh->elf.root.type == bfd_link_hash_undefweak)
6745     {
6746       if (fh->elf.root.type == bfd_link_hash_undefined)
6747         {
6748           fdh->elf.root.type = bfd_link_hash_undefined;
6749           bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6750         }
6751       else if (fh->elf.root.type == bfd_link_hash_defined
6752                || fh->elf.root.type == bfd_link_hash_defweak)
6753         {
6754           _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6755         }
6756     }
6757
6758   if (fdh != NULL
6759       && !fdh->elf.forced_local
6760       && (!info->executable
6761           || fdh->elf.def_dynamic
6762           || fdh->elf.ref_dynamic
6763           || (fdh->elf.root.type == bfd_link_hash_undefweak
6764               && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6765     {
6766       if (fdh->elf.dynindx == -1)
6767         if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6768           return FALSE;
6769       fdh->elf.ref_regular |= fh->elf.ref_regular;
6770       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6771       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6772       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6773       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6774         {
6775           move_plt_plist (fh, fdh);
6776           fdh->elf.needs_plt = 1;
6777         }
6778       fdh->is_func_descriptor = 1;
6779       fdh->oh = fh;
6780       fh->oh = fdh;
6781     }
6782
6783   /* Now that the info is on the function descriptor, clear the
6784      function code sym info.  Any function code syms for which we
6785      don't have a definition in a regular file, we force local.
6786      This prevents a shared library from exporting syms that have
6787      been imported from another library.  Function code syms that
6788      are really in the library we must leave global to prevent the
6789      linker dragging in a definition from a static library.  */
6790   force_local = (!fh->elf.def_regular
6791                  || fdh == NULL
6792                  || !fdh->elf.def_regular
6793                  || fdh->elf.forced_local);
6794   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6795
6796   return TRUE;
6797 }
6798
6799 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
6800    this hook to a) provide some gcc support functions, and b) transfer
6801    dynamic linking information gathered so far on function code symbol
6802    entries, to their corresponding function descriptor symbol entries.  */
6803
6804 static bfd_boolean
6805 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6806                             struct bfd_link_info *info)
6807 {
6808   struct ppc_link_hash_table *htab;
6809   unsigned int i;
6810   static const struct sfpr_def_parms funcs[] =
6811     {
6812       { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6813       { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6814       { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6815       { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6816       { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6817       { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6818       { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6819       { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6820       { "._savef", 14, 31, savefpr, savefpr1_tail },
6821       { "._restf", 14, 31, restfpr, restfpr1_tail },
6822       { "_savevr_", 20, 31, savevr, savevr_tail },
6823       { "_restvr_", 20, 31, restvr, restvr_tail }
6824     };
6825
6826   htab = ppc_hash_table (info);
6827   if (htab == NULL)
6828     return FALSE;
6829
6830   if (!info->relocatable
6831       && htab->elf.hgot != NULL)
6832     _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6833
6834   if (htab->sfpr == NULL)
6835     /* We don't have any relocs.  */
6836     return TRUE;
6837
6838   /* Provide any missing _save* and _rest* functions.  */
6839   htab->sfpr->size = 0;
6840   if (!info->relocatable)
6841     for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6842       if (!sfpr_define (info, &funcs[i]))
6843         return FALSE;
6844
6845   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6846
6847   if (htab->sfpr->size == 0)
6848     htab->sfpr->flags |= SEC_EXCLUDE;
6849
6850   return TRUE;
6851 }
6852
6853 /* Adjust a symbol defined by a dynamic object and referenced by a
6854    regular object.  The current definition is in some section of the
6855    dynamic object, but we're not including those sections.  We have to
6856    change the definition to something the rest of the link can
6857    understand.  */
6858
6859 static bfd_boolean
6860 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6861                                  struct elf_link_hash_entry *h)
6862 {
6863   struct ppc_link_hash_table *htab;
6864   asection *s;
6865
6866   htab = ppc_hash_table (info);
6867   if (htab == NULL)
6868     return FALSE;
6869
6870   /* Deal with function syms.  */
6871   if (h->type == STT_FUNC
6872       || h->type == STT_GNU_IFUNC
6873       || h->needs_plt)
6874     {
6875       /* Clear procedure linkage table information for any symbol that
6876          won't need a .plt entry.  */
6877       struct plt_entry *ent;
6878       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6879         if (ent->plt.refcount > 0)
6880           break;
6881       if (ent == NULL
6882           || (h->type != STT_GNU_IFUNC
6883               && (SYMBOL_CALLS_LOCAL (info, h)
6884                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6885                       && h->root.type == bfd_link_hash_undefweak))))
6886         {
6887           h->plt.plist = NULL;
6888           h->needs_plt = 0;
6889         }
6890     }
6891   else
6892     h->plt.plist = NULL;
6893
6894   /* If this is a weak symbol, and there is a real definition, the
6895      processor independent code will have arranged for us to see the
6896      real definition first, and we can just use the same value.  */
6897   if (h->u.weakdef != NULL)
6898     {
6899       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6900                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
6901       h->root.u.def.section = h->u.weakdef->root.u.def.section;
6902       h->root.u.def.value = h->u.weakdef->root.u.def.value;
6903       if (ELIMINATE_COPY_RELOCS)
6904         h->non_got_ref = h->u.weakdef->non_got_ref;
6905       return TRUE;
6906     }
6907
6908   /* If we are creating a shared library, we must presume that the
6909      only references to the symbol are via the global offset table.
6910      For such cases we need not do anything here; the relocations will
6911      be handled correctly by relocate_section.  */
6912   if (info->shared)
6913     return TRUE;
6914
6915   /* If there are no references to this symbol that do not use the
6916      GOT, we don't need to generate a copy reloc.  */
6917   if (!h->non_got_ref)
6918     return TRUE;
6919
6920   /* Don't generate a copy reloc for symbols defined in the executable.  */
6921   if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6922     return TRUE;
6923
6924   if (ELIMINATE_COPY_RELOCS)
6925     {
6926       struct ppc_link_hash_entry * eh;
6927       struct elf_dyn_relocs *p;
6928
6929       eh = (struct ppc_link_hash_entry *) h;
6930       for (p = eh->dyn_relocs; p != NULL; p = p->next)
6931         {
6932           s = p->sec->output_section;
6933           if (s != NULL && (s->flags & SEC_READONLY) != 0)
6934             break;
6935         }
6936
6937       /* If we didn't find any dynamic relocs in read-only sections, then
6938          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
6939       if (p == NULL)
6940         {
6941           h->non_got_ref = 0;
6942           return TRUE;
6943         }
6944     }
6945
6946   if (h->plt.plist != NULL)
6947     {
6948       /* We should never get here, but unfortunately there are versions
6949          of gcc out there that improperly (for this ABI) put initialized
6950          function pointers, vtable refs and suchlike in read-only
6951          sections.  Allow them to proceed, but warn that this might
6952          break at runtime.  */
6953       info->callbacks->einfo
6954         (_("%P: copy reloc against `%T' requires lazy plt linking; "
6955            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6956          h->root.root.string);
6957     }
6958
6959   /* This is a reference to a symbol defined by a dynamic object which
6960      is not a function.  */
6961
6962   /* We must allocate the symbol in our .dynbss section, which will
6963      become part of the .bss section of the executable.  There will be
6964      an entry for this symbol in the .dynsym section.  The dynamic
6965      object will contain position independent code, so all references
6966      from the dynamic object to this symbol will go through the global
6967      offset table.  The dynamic linker will use the .dynsym entry to
6968      determine the address it must put in the global offset table, so
6969      both the dynamic object and the regular object will refer to the
6970      same memory location for the variable.  */
6971
6972   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6973      to copy the initial value out of the dynamic object and into the
6974      runtime process image.  We need to remember the offset into the
6975      .rela.bss section we are going to use.  */
6976   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6977     {
6978       htab->relbss->size += sizeof (Elf64_External_Rela);
6979       h->needs_copy = 1;
6980     }
6981
6982   s = htab->dynbss;
6983
6984   return _bfd_elf_adjust_dynamic_copy (h, s);
6985 }
6986
6987 /* If given a function descriptor symbol, hide both the function code
6988    sym and the descriptor.  */
6989 static void
6990 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6991                        struct elf_link_hash_entry *h,
6992                        bfd_boolean force_local)
6993 {
6994   struct ppc_link_hash_entry *eh;
6995   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6996
6997   eh = (struct ppc_link_hash_entry *) h;
6998   if (eh->is_func_descriptor)
6999     {
7000       struct ppc_link_hash_entry *fh = eh->oh;
7001
7002       if (fh == NULL)
7003         {
7004           const char *p, *q;
7005           struct ppc_link_hash_table *htab;
7006           char save;
7007
7008           /* We aren't supposed to use alloca in BFD because on
7009              systems which do not have alloca the version in libiberty
7010              calls xmalloc, which might cause the program to crash
7011              when it runs out of memory.  This function doesn't have a
7012              return status, so there's no way to gracefully return an
7013              error.  So cheat.  We know that string[-1] can be safely
7014              accessed;  It's either a string in an ELF string table,
7015              or allocated in an objalloc structure.  */
7016
7017           p = eh->elf.root.root.string - 1;
7018           save = *p;
7019           *(char *) p = '.';
7020           htab = ppc_hash_table (info);
7021           if (htab == NULL)
7022             return;
7023
7024           fh = (struct ppc_link_hash_entry *)
7025             elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7026           *(char *) p = save;
7027
7028           /* Unfortunately, if it so happens that the string we were
7029              looking for was allocated immediately before this string,
7030              then we overwrote the string terminator.  That's the only
7031              reason the lookup should fail.  */
7032           if (fh == NULL)
7033             {
7034               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7035               while (q >= eh->elf.root.root.string && *q == *p)
7036                 --q, --p;
7037               if (q < eh->elf.root.root.string && *p == '.')
7038                 fh = (struct ppc_link_hash_entry *)
7039                   elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7040             }
7041           if (fh != NULL)
7042             {
7043               eh->oh = fh;
7044               fh->oh = eh;
7045             }
7046         }
7047       if (fh != NULL)
7048         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7049     }
7050 }
7051
7052 static bfd_boolean
7053 get_sym_h (struct elf_link_hash_entry **hp,
7054            Elf_Internal_Sym **symp,
7055            asection **symsecp,
7056            unsigned char **tls_maskp,
7057            Elf_Internal_Sym **locsymsp,
7058            unsigned long r_symndx,
7059            bfd *ibfd)
7060 {
7061   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7062
7063   if (r_symndx >= symtab_hdr->sh_info)
7064     {
7065       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7066       struct elf_link_hash_entry *h;
7067
7068       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7069       h = elf_follow_link (h);
7070
7071       if (hp != NULL)
7072         *hp = h;
7073
7074       if (symp != NULL)
7075         *symp = NULL;
7076
7077       if (symsecp != NULL)
7078         {
7079           asection *symsec = NULL;
7080           if (h->root.type == bfd_link_hash_defined
7081               || h->root.type == bfd_link_hash_defweak)
7082             symsec = h->root.u.def.section;
7083           *symsecp = symsec;
7084         }
7085
7086       if (tls_maskp != NULL)
7087         {
7088           struct ppc_link_hash_entry *eh;
7089
7090           eh = (struct ppc_link_hash_entry *) h;
7091           *tls_maskp = &eh->tls_mask;
7092         }
7093     }
7094   else
7095     {
7096       Elf_Internal_Sym *sym;
7097       Elf_Internal_Sym *locsyms = *locsymsp;
7098
7099       if (locsyms == NULL)
7100         {
7101           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7102           if (locsyms == NULL)
7103             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7104                                             symtab_hdr->sh_info,
7105                                             0, NULL, NULL, NULL);
7106           if (locsyms == NULL)
7107             return FALSE;
7108           *locsymsp = locsyms;
7109         }
7110       sym = locsyms + r_symndx;
7111
7112       if (hp != NULL)
7113         *hp = NULL;
7114
7115       if (symp != NULL)
7116         *symp = sym;
7117
7118       if (symsecp != NULL)
7119         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7120
7121       if (tls_maskp != NULL)
7122         {
7123           struct got_entry **lgot_ents;
7124           unsigned char *tls_mask;
7125
7126           tls_mask = NULL;
7127           lgot_ents = elf_local_got_ents (ibfd);
7128           if (lgot_ents != NULL)
7129             {
7130               struct plt_entry **local_plt = (struct plt_entry **)
7131                 (lgot_ents + symtab_hdr->sh_info);
7132               unsigned char *lgot_masks = (unsigned char *)
7133                 (local_plt + symtab_hdr->sh_info);
7134               tls_mask = &lgot_masks[r_symndx];
7135             }
7136           *tls_maskp = tls_mask;
7137         }
7138     }
7139   return TRUE;
7140 }
7141
7142 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
7143    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7144    type suitable for optimization, and 1 otherwise.  */
7145
7146 static int
7147 get_tls_mask (unsigned char **tls_maskp,
7148               unsigned long *toc_symndx,
7149               bfd_vma *toc_addend,
7150               Elf_Internal_Sym **locsymsp,
7151               const Elf_Internal_Rela *rel,
7152               bfd *ibfd)
7153 {
7154   unsigned long r_symndx;
7155   int next_r;
7156   struct elf_link_hash_entry *h;
7157   Elf_Internal_Sym *sym;
7158   asection *sec;
7159   bfd_vma off;
7160
7161   r_symndx = ELF64_R_SYM (rel->r_info);
7162   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7163     return 0;
7164
7165   if ((*tls_maskp != NULL && **tls_maskp != 0)
7166       || sec == NULL
7167       || ppc64_elf_section_data (sec) == NULL
7168       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7169     return 1;
7170
7171   /* Look inside a TOC section too.  */
7172   if (h != NULL)
7173     {
7174       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7175       off = h->root.u.def.value;
7176     }
7177   else
7178     off = sym->st_value;
7179   off += rel->r_addend;
7180   BFD_ASSERT (off % 8 == 0);
7181   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7182   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7183   if (toc_symndx != NULL)
7184     *toc_symndx = r_symndx;
7185   if (toc_addend != NULL)
7186     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7187   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7188     return 0;
7189   if ((h == NULL || is_static_defined (h))
7190       && (next_r == -1 || next_r == -2))
7191     return 1 - next_r;
7192   return 1;
7193 }
7194
7195 /* Find (or create) an entry in the tocsave hash table.  */
7196
7197 static struct tocsave_entry *
7198 tocsave_find (struct ppc_link_hash_table *htab,
7199               enum insert_option insert,
7200               Elf_Internal_Sym **local_syms,
7201               const Elf_Internal_Rela *irela,
7202               bfd *ibfd)
7203 {
7204   unsigned long r_indx;
7205   struct elf_link_hash_entry *h;
7206   Elf_Internal_Sym *sym;
7207   struct tocsave_entry ent, *p;
7208   hashval_t hash;
7209   struct tocsave_entry **slot;
7210
7211   r_indx = ELF64_R_SYM (irela->r_info);
7212   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7213     return NULL;
7214   if (ent.sec == NULL || ent.sec->output_section == NULL)
7215     {
7216       (*_bfd_error_handler)
7217         (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7218       return NULL;
7219     }
7220
7221   if (h != NULL)
7222     ent.offset = h->root.u.def.value;
7223   else
7224     ent.offset = sym->st_value;
7225   ent.offset += irela->r_addend;
7226
7227   hash = tocsave_htab_hash (&ent);
7228   slot = ((struct tocsave_entry **)
7229           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7230   if (slot == NULL)
7231     return NULL;
7232
7233   if (*slot == NULL)
7234     {
7235       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7236       if (p == NULL)
7237         return NULL;
7238       *p = ent;
7239       *slot = p;
7240     }
7241   return *slot;
7242 }
7243
7244 /* Adjust all global syms defined in opd sections.  In gcc generated
7245    code for the old ABI, these will already have been done.  */
7246
7247 static bfd_boolean
7248 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7249 {
7250   struct ppc_link_hash_entry *eh;
7251   asection *sym_sec;
7252   struct _opd_sec_data *opd;
7253
7254   if (h->root.type == bfd_link_hash_indirect)
7255     return TRUE;
7256
7257   if (h->root.type != bfd_link_hash_defined
7258       && h->root.type != bfd_link_hash_defweak)
7259     return TRUE;
7260
7261   eh = (struct ppc_link_hash_entry *) h;
7262   if (eh->adjust_done)
7263     return TRUE;
7264
7265   sym_sec = eh->elf.root.u.def.section;
7266   opd = get_opd_info (sym_sec);
7267   if (opd != NULL && opd->adjust != NULL)
7268     {
7269       long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
7270       if (adjust == -1)
7271         {
7272           /* This entry has been deleted.  */
7273           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7274           if (dsec == NULL)
7275             {
7276               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7277                 if (discarded_section (dsec))
7278                   {
7279                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7280                     break;
7281                   }
7282             }
7283           eh->elf.root.u.def.value = 0;
7284           eh->elf.root.u.def.section = dsec;
7285         }
7286       else
7287         eh->elf.root.u.def.value += adjust;
7288       eh->adjust_done = 1;
7289     }
7290   return TRUE;
7291 }
7292
7293 /* Handles decrementing dynamic reloc counts for the reloc specified by
7294    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
7295    have already been determined.  */
7296
7297 static bfd_boolean
7298 dec_dynrel_count (bfd_vma r_info,
7299                   asection *sec,
7300                   struct bfd_link_info *info,
7301                   Elf_Internal_Sym **local_syms,
7302                   struct elf_link_hash_entry *h,
7303                   Elf_Internal_Sym *sym)
7304 {
7305   enum elf_ppc64_reloc_type r_type;
7306   asection *sym_sec = NULL;
7307
7308   /* Can this reloc be dynamic?  This switch, and later tests here
7309      should be kept in sync with the code in check_relocs.  */
7310   r_type = ELF64_R_TYPE (r_info);
7311   switch (r_type)
7312     {
7313     default:
7314       return TRUE;
7315
7316     case R_PPC64_TPREL16:
7317     case R_PPC64_TPREL16_LO:
7318     case R_PPC64_TPREL16_HI:
7319     case R_PPC64_TPREL16_HA:
7320     case R_PPC64_TPREL16_DS:
7321     case R_PPC64_TPREL16_LO_DS:
7322     case R_PPC64_TPREL16_HIGH:
7323     case R_PPC64_TPREL16_HIGHA:
7324     case R_PPC64_TPREL16_HIGHER:
7325     case R_PPC64_TPREL16_HIGHERA:
7326     case R_PPC64_TPREL16_HIGHEST:
7327     case R_PPC64_TPREL16_HIGHESTA:
7328       if (!info->shared)
7329         return TRUE;
7330
7331     case R_PPC64_TPREL64:
7332     case R_PPC64_DTPMOD64:
7333     case R_PPC64_DTPREL64:
7334     case R_PPC64_ADDR64:
7335     case R_PPC64_REL30:
7336     case R_PPC64_REL32:
7337     case R_PPC64_REL64:
7338     case R_PPC64_ADDR14:
7339     case R_PPC64_ADDR14_BRNTAKEN:
7340     case R_PPC64_ADDR14_BRTAKEN:
7341     case R_PPC64_ADDR16:
7342     case R_PPC64_ADDR16_DS:
7343     case R_PPC64_ADDR16_HA:
7344     case R_PPC64_ADDR16_HI:
7345     case R_PPC64_ADDR16_HIGH:
7346     case R_PPC64_ADDR16_HIGHA:
7347     case R_PPC64_ADDR16_HIGHER:
7348     case R_PPC64_ADDR16_HIGHERA:
7349     case R_PPC64_ADDR16_HIGHEST:
7350     case R_PPC64_ADDR16_HIGHESTA:
7351     case R_PPC64_ADDR16_LO:
7352     case R_PPC64_ADDR16_LO_DS:
7353     case R_PPC64_ADDR24:
7354     case R_PPC64_ADDR32:
7355     case R_PPC64_UADDR16:
7356     case R_PPC64_UADDR32:
7357     case R_PPC64_UADDR64:
7358     case R_PPC64_TOC:
7359       break;
7360     }
7361
7362   if (local_syms != NULL)
7363     {
7364       unsigned long r_symndx;
7365       bfd *ibfd = sec->owner;
7366
7367       r_symndx = ELF64_R_SYM (r_info);
7368       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7369         return FALSE;
7370     }
7371
7372   if ((info->shared
7373        && (must_be_dyn_reloc (info, r_type)
7374            || (h != NULL
7375                && (!SYMBOLIC_BIND (info, h)
7376                    || h->root.type == bfd_link_hash_defweak
7377                    || !h->def_regular))))
7378       || (ELIMINATE_COPY_RELOCS
7379           && !info->shared
7380           && h != NULL
7381           && (h->root.type == bfd_link_hash_defweak
7382               || !h->def_regular)))
7383     ;
7384   else
7385     return TRUE;
7386
7387   if (h != NULL)
7388     {
7389       struct elf_dyn_relocs *p;
7390       struct elf_dyn_relocs **pp;
7391       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7392
7393       /* elf_gc_sweep may have already removed all dyn relocs associated
7394          with local syms for a given section.  Also, symbol flags are
7395          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
7396          report a dynreloc miscount.  */
7397       if (*pp == NULL && info->gc_sections)
7398         return TRUE;
7399
7400       while ((p = *pp) != NULL)
7401         {
7402           if (p->sec == sec)
7403             {
7404               if (!must_be_dyn_reloc (info, r_type))
7405                 p->pc_count -= 1;
7406               p->count -= 1;
7407               if (p->count == 0)
7408                 *pp = p->next;
7409               return TRUE;
7410             }
7411           pp = &p->next;
7412         }
7413     }
7414   else
7415     {
7416       struct ppc_dyn_relocs *p;
7417       struct ppc_dyn_relocs **pp;
7418       void *vpp;
7419       bfd_boolean is_ifunc;
7420
7421       if (local_syms == NULL)
7422         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7423       if (sym_sec == NULL)
7424         sym_sec = sec;
7425
7426       vpp = &elf_section_data (sym_sec)->local_dynrel;
7427       pp = (struct ppc_dyn_relocs **) vpp;
7428
7429       if (*pp == NULL && info->gc_sections)
7430         return TRUE;
7431
7432       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7433       while ((p = *pp) != NULL)
7434         {
7435           if (p->sec == sec && p->ifunc == is_ifunc)
7436             {
7437               p->count -= 1;
7438               if (p->count == 0)
7439                 *pp = p->next;
7440               return TRUE;
7441             }
7442           pp = &p->next;
7443         }
7444     }
7445
7446   info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7447                           sec->owner, sec);
7448   bfd_set_error (bfd_error_bad_value);
7449   return FALSE;
7450 }
7451
7452 /* Remove unused Official Procedure Descriptor entries.  Currently we
7453    only remove those associated with functions in discarded link-once
7454    sections, or weakly defined functions that have been overridden.  It
7455    would be possible to remove many more entries for statically linked
7456    applications.  */
7457
7458 bfd_boolean
7459 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
7460 {
7461   bfd *ibfd;
7462   bfd_boolean some_edited = FALSE;
7463   asection *need_pad = NULL;
7464
7465   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7466     {
7467       asection *sec;
7468       Elf_Internal_Rela *relstart, *rel, *relend;
7469       Elf_Internal_Shdr *symtab_hdr;
7470       Elf_Internal_Sym *local_syms;
7471       bfd_vma offset;
7472       struct _opd_sec_data *opd;
7473       bfd_boolean need_edit, add_aux_fields;
7474       bfd_size_type cnt_16b = 0;
7475
7476       if (!is_ppc64_elf (ibfd))
7477         continue;
7478
7479       sec = bfd_get_section_by_name (ibfd, ".opd");
7480       if (sec == NULL || sec->size == 0)
7481         continue;
7482
7483       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7484         continue;
7485
7486       if (sec->output_section == bfd_abs_section_ptr)
7487         continue;
7488
7489       /* Look through the section relocs.  */
7490       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7491         continue;
7492
7493       local_syms = NULL;
7494       symtab_hdr = &elf_symtab_hdr (ibfd);
7495
7496       /* Read the relocations.  */
7497       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7498                                             info->keep_memory);
7499       if (relstart == NULL)
7500         return FALSE;
7501
7502       /* First run through the relocs to check they are sane, and to
7503          determine whether we need to edit this opd section.  */
7504       need_edit = FALSE;
7505       need_pad = sec;
7506       offset = 0;
7507       relend = relstart + sec->reloc_count;
7508       for (rel = relstart; rel < relend; )
7509         {
7510           enum elf_ppc64_reloc_type r_type;
7511           unsigned long r_symndx;
7512           asection *sym_sec;
7513           struct elf_link_hash_entry *h;
7514           Elf_Internal_Sym *sym;
7515
7516           /* .opd contains a regular array of 16 or 24 byte entries.  We're
7517              only interested in the reloc pointing to a function entry
7518              point.  */
7519           if (rel->r_offset != offset
7520               || rel + 1 >= relend
7521               || (rel + 1)->r_offset != offset + 8)
7522             {
7523               /* If someone messes with .opd alignment then after a
7524                  "ld -r" we might have padding in the middle of .opd.
7525                  Also, there's nothing to prevent someone putting
7526                  something silly in .opd with the assembler.  No .opd
7527                  optimization for them!  */
7528             broken_opd:
7529               (*_bfd_error_handler)
7530                 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7531               need_edit = FALSE;
7532               break;
7533             }
7534
7535           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7536               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7537             {
7538               (*_bfd_error_handler)
7539                 (_("%B: unexpected reloc type %u in .opd section"),
7540                  ibfd, r_type);
7541               need_edit = FALSE;
7542               break;
7543             }
7544
7545           r_symndx = ELF64_R_SYM (rel->r_info);
7546           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7547                           r_symndx, ibfd))
7548             goto error_ret;
7549
7550           if (sym_sec == NULL || sym_sec->owner == NULL)
7551             {
7552               const char *sym_name;
7553               if (h != NULL)
7554                 sym_name = h->root.root.string;
7555               else
7556                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7557                                              sym_sec);
7558
7559               (*_bfd_error_handler)
7560                 (_("%B: undefined sym `%s' in .opd section"),
7561                  ibfd, sym_name);
7562               need_edit = FALSE;
7563               break;
7564             }
7565
7566           /* opd entries are always for functions defined in the
7567              current input bfd.  If the symbol isn't defined in the
7568              input bfd, then we won't be using the function in this
7569              bfd;  It must be defined in a linkonce section in another
7570              bfd, or is weak.  It's also possible that we are
7571              discarding the function due to a linker script /DISCARD/,
7572              which we test for via the output_section.  */
7573           if (sym_sec->owner != ibfd
7574               || sym_sec->output_section == bfd_abs_section_ptr)
7575             need_edit = TRUE;
7576
7577           rel += 2;
7578           if (rel == relend
7579               || (rel + 1 == relend && rel->r_offset == offset + 16))
7580             {
7581               if (sec->size == offset + 24)
7582                 {
7583                   need_pad = NULL;
7584                   break;
7585                 }
7586               if (rel == relend && sec->size == offset + 16)
7587                 {
7588                   cnt_16b++;
7589                   break;
7590                 }
7591               goto broken_opd;
7592             }
7593
7594           if (rel->r_offset == offset + 24)
7595             offset += 24;
7596           else if (rel->r_offset != offset + 16)
7597             goto broken_opd;
7598           else if (rel + 1 < relend
7599                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7600                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7601             {
7602               offset += 16;
7603               cnt_16b++;
7604             }
7605           else if (rel + 2 < relend
7606                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7607                    && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7608             {
7609               offset += 24;
7610               rel += 1;
7611             }
7612           else
7613             goto broken_opd;
7614         }
7615
7616       add_aux_fields = non_overlapping && cnt_16b > 0;
7617
7618       if (need_edit || add_aux_fields)
7619         {
7620           Elf_Internal_Rela *write_rel;
7621           Elf_Internal_Shdr *rel_hdr;
7622           bfd_byte *rptr, *wptr;
7623           bfd_byte *new_contents;
7624           bfd_boolean skip;
7625           long opd_ent_size;
7626           bfd_size_type amt;
7627
7628           new_contents = NULL;
7629           amt = sec->size * sizeof (long) / 8;
7630           opd = &ppc64_elf_section_data (sec)->u.opd;
7631           opd->adjust = bfd_zalloc (sec->owner, amt);
7632           if (opd->adjust == NULL)
7633             return FALSE;
7634           ppc64_elf_section_data (sec)->sec_type = sec_opd;
7635
7636           /* This seems a waste of time as input .opd sections are all
7637              zeros as generated by gcc, but I suppose there's no reason
7638              this will always be so.  We might start putting something in
7639              the third word of .opd entries.  */
7640           if ((sec->flags & SEC_IN_MEMORY) == 0)
7641             {
7642               bfd_byte *loc;
7643               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7644                 {
7645                   if (loc != NULL)
7646                     free (loc);
7647                 error_ret:
7648                   if (local_syms != NULL
7649                       && symtab_hdr->contents != (unsigned char *) local_syms)
7650                     free (local_syms);
7651                   if (elf_section_data (sec)->relocs != relstart)
7652                     free (relstart);
7653                   return FALSE;
7654                 }
7655               sec->contents = loc;
7656               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7657             }
7658
7659           elf_section_data (sec)->relocs = relstart;
7660
7661           new_contents = sec->contents;
7662           if (add_aux_fields)
7663             {
7664               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7665               if (new_contents == NULL)
7666                 return FALSE;
7667               need_pad = FALSE;
7668             }
7669           wptr = new_contents;
7670           rptr = sec->contents;
7671
7672           write_rel = relstart;
7673           skip = FALSE;
7674           offset = 0;
7675           opd_ent_size = 0;
7676           for (rel = relstart; rel < relend; rel++)
7677             {
7678               unsigned long r_symndx;
7679               asection *sym_sec;
7680               struct elf_link_hash_entry *h;
7681               Elf_Internal_Sym *sym;
7682
7683               r_symndx = ELF64_R_SYM (rel->r_info);
7684               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7685                               r_symndx, ibfd))
7686                 goto error_ret;
7687
7688               if (rel->r_offset == offset)
7689                 {
7690                   struct ppc_link_hash_entry *fdh = NULL;
7691
7692                   /* See if the .opd entry is full 24 byte or
7693                      16 byte (with fd_aux entry overlapped with next
7694                      fd_func).  */
7695                   opd_ent_size = 24;
7696                   if ((rel + 2 == relend && sec->size == offset + 16)
7697                       || (rel + 3 < relend
7698                           && rel[2].r_offset == offset + 16
7699                           && rel[3].r_offset == offset + 24
7700                           && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7701                           && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7702                     opd_ent_size = 16;
7703
7704                   if (h != NULL
7705                       && h->root.root.string[0] == '.')
7706                     {
7707                       struct ppc_link_hash_table *htab;
7708
7709                       htab = ppc_hash_table (info);
7710                       if (htab != NULL)
7711                         fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7712                                           htab);
7713                       if (fdh != NULL
7714                           && fdh->elf.root.type != bfd_link_hash_defined
7715                           && fdh->elf.root.type != bfd_link_hash_defweak)
7716                         fdh = NULL;
7717                     }
7718
7719                   skip = (sym_sec->owner != ibfd
7720                           || sym_sec->output_section == bfd_abs_section_ptr);
7721                   if (skip)
7722                     {
7723                       if (fdh != NULL && sym_sec->owner == ibfd)
7724                         {
7725                           /* Arrange for the function descriptor sym
7726                              to be dropped.  */
7727                           fdh->elf.root.u.def.value = 0;
7728                           fdh->elf.root.u.def.section = sym_sec;
7729                         }
7730                       opd->adjust[rel->r_offset / 8] = -1;
7731                     }
7732                   else
7733                     {
7734                       /* We'll be keeping this opd entry.  */
7735
7736                       if (fdh != NULL)
7737                         {
7738                           /* Redefine the function descriptor symbol to
7739                              this location in the opd section.  It is
7740                              necessary to update the value here rather
7741                              than using an array of adjustments as we do
7742                              for local symbols, because various places
7743                              in the generic ELF code use the value
7744                              stored in u.def.value.  */
7745                           fdh->elf.root.u.def.value = wptr - new_contents;
7746                           fdh->adjust_done = 1;
7747                         }
7748
7749                       /* Local syms are a bit tricky.  We could
7750                          tweak them as they can be cached, but
7751                          we'd need to look through the local syms
7752                          for the function descriptor sym which we
7753                          don't have at the moment.  So keep an
7754                          array of adjustments.  */
7755                       opd->adjust[rel->r_offset / 8]
7756                         = (wptr - new_contents) - (rptr - sec->contents);
7757
7758                       if (wptr != rptr)
7759                         memcpy (wptr, rptr, opd_ent_size);
7760                       wptr += opd_ent_size;
7761                       if (add_aux_fields && opd_ent_size == 16)
7762                         {
7763                           memset (wptr, '\0', 8);
7764                           wptr += 8;
7765                         }
7766                     }
7767                   rptr += opd_ent_size;
7768                   offset += opd_ent_size;
7769                 }
7770
7771               if (skip)
7772                 {
7773                   if (!NO_OPD_RELOCS
7774                       && !info->relocatable
7775                       && !dec_dynrel_count (rel->r_info, sec, info,
7776                                             NULL, h, sym))
7777                     goto error_ret;
7778                 }
7779               else
7780                 {
7781                   /* We need to adjust any reloc offsets to point to the
7782                      new opd entries.  While we're at it, we may as well
7783                      remove redundant relocs.  */
7784                   rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
7785                   if (write_rel != rel)
7786                     memcpy (write_rel, rel, sizeof (*rel));
7787                   ++write_rel;
7788                 }
7789             }
7790
7791           sec->size = wptr - new_contents;
7792           sec->reloc_count = write_rel - relstart;
7793           if (add_aux_fields)
7794             {
7795               free (sec->contents);
7796               sec->contents = new_contents;
7797             }
7798
7799           /* Fudge the header size too, as this is used later in
7800              elf_bfd_final_link if we are emitting relocs.  */
7801           rel_hdr = _bfd_elf_single_rel_hdr (sec);
7802           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7803           some_edited = TRUE;
7804         }
7805       else if (elf_section_data (sec)->relocs != relstart)
7806         free (relstart);
7807
7808       if (local_syms != NULL
7809           && symtab_hdr->contents != (unsigned char *) local_syms)
7810         {
7811           if (!info->keep_memory)
7812             free (local_syms);
7813           else
7814             symtab_hdr->contents = (unsigned char *) local_syms;
7815         }
7816     }
7817
7818   if (some_edited)
7819     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7820
7821   /* If we are doing a final link and the last .opd entry is just 16 byte
7822      long, add a 8 byte padding after it.  */
7823   if (need_pad != NULL && !info->relocatable)
7824     {
7825       bfd_byte *p;
7826
7827       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7828         {
7829           BFD_ASSERT (need_pad->size > 0);
7830
7831           p = bfd_malloc (need_pad->size + 8);
7832           if (p == NULL)
7833             return FALSE;
7834
7835           if (! bfd_get_section_contents (need_pad->owner, need_pad,
7836                                           p, 0, need_pad->size))
7837             return FALSE;
7838
7839           need_pad->contents = p;
7840           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7841         }
7842       else
7843         {
7844           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7845           if (p == NULL)
7846             return FALSE;
7847
7848           need_pad->contents = p;
7849         }
7850
7851       memset (need_pad->contents + need_pad->size, 0, 8);
7852       need_pad->size += 8;
7853     }
7854
7855   return TRUE;
7856 }
7857
7858 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
7859
7860 asection *
7861 ppc64_elf_tls_setup (struct bfd_link_info *info,
7862                      int no_tls_get_addr_opt,
7863                      int *no_multi_toc)
7864 {
7865   struct ppc_link_hash_table *htab;
7866
7867   htab = ppc_hash_table (info);
7868   if (htab == NULL)
7869     return NULL;
7870
7871   if (abiversion (info->output_bfd) == 1)
7872     htab->opd_abi = 1;
7873
7874   if (*no_multi_toc)
7875     htab->do_multi_toc = 0;
7876   else if (!htab->do_multi_toc)
7877     *no_multi_toc = 1;
7878
7879   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7880                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7881                                               FALSE, FALSE, TRUE));
7882   /* Move dynamic linking info to the function descriptor sym.  */
7883   if (htab->tls_get_addr != NULL)
7884     func_desc_adjust (&htab->tls_get_addr->elf, info);
7885   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7886                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7887                                                  FALSE, FALSE, TRUE));
7888   if (!no_tls_get_addr_opt)
7889     {
7890       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7891
7892       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7893                                   FALSE, FALSE, TRUE);
7894       if (opt != NULL)
7895         func_desc_adjust (opt, info);
7896       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7897                                      FALSE, FALSE, TRUE);
7898       if (opt_fd != NULL
7899           && (opt_fd->root.type == bfd_link_hash_defined
7900               || opt_fd->root.type == bfd_link_hash_defweak))
7901         {
7902           /* If glibc supports an optimized __tls_get_addr call stub,
7903              signalled by the presence of __tls_get_addr_opt, and we'll
7904              be calling __tls_get_addr via a plt call stub, then
7905              make __tls_get_addr point to __tls_get_addr_opt.  */
7906           tga_fd = &htab->tls_get_addr_fd->elf;
7907           if (htab->elf.dynamic_sections_created
7908               && tga_fd != NULL
7909               && (tga_fd->type == STT_FUNC
7910                   || tga_fd->needs_plt)
7911               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7912                    || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7913                        && tga_fd->root.type == bfd_link_hash_undefweak)))
7914             {
7915               struct plt_entry *ent;
7916
7917               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7918                 if (ent->plt.refcount > 0)
7919                   break;
7920               if (ent != NULL)
7921                 {
7922                   tga_fd->root.type = bfd_link_hash_indirect;
7923                   tga_fd->root.u.i.link = &opt_fd->root;
7924                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7925                   if (opt_fd->dynindx != -1)
7926                     {
7927                       /* Use __tls_get_addr_opt in dynamic relocations.  */
7928                       opt_fd->dynindx = -1;
7929                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7930                                               opt_fd->dynstr_index);
7931                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7932                         return NULL;
7933                     }
7934                   htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7935                   tga = &htab->tls_get_addr->elf;
7936                   if (opt != NULL && tga != NULL)
7937                     {
7938                       tga->root.type = bfd_link_hash_indirect;
7939                       tga->root.u.i.link = &opt->root;
7940                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
7941                       _bfd_elf_link_hash_hide_symbol (info, opt,
7942                                                       tga->forced_local);
7943                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7944                     }
7945                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7946                   htab->tls_get_addr_fd->is_func_descriptor = 1;
7947                   if (htab->tls_get_addr != NULL)
7948                     {
7949                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7950                       htab->tls_get_addr->is_func = 1;
7951                     }
7952                 }
7953             }
7954         }
7955       else
7956         no_tls_get_addr_opt = TRUE;
7957     }
7958   htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
7959   return _bfd_elf_tls_setup (info->output_bfd, info);
7960 }
7961
7962 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7963    HASH1 or HASH2.  */
7964
7965 static bfd_boolean
7966 branch_reloc_hash_match (const bfd *ibfd,
7967                          const Elf_Internal_Rela *rel,
7968                          const struct ppc_link_hash_entry *hash1,
7969                          const struct ppc_link_hash_entry *hash2)
7970 {
7971   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7972   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7973   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7974
7975   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7976     {
7977       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7978       struct elf_link_hash_entry *h;
7979
7980       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7981       h = elf_follow_link (h);
7982       if (h == &hash1->elf || h == &hash2->elf)
7983         return TRUE;
7984     }
7985   return FALSE;
7986 }
7987
7988 /* Run through all the TLS relocs looking for optimization
7989    opportunities.  The linker has been hacked (see ppc64elf.em) to do
7990    a preliminary section layout so that we know the TLS segment
7991    offsets.  We can't optimize earlier because some optimizations need
7992    to know the tp offset, and we need to optimize before allocating
7993    dynamic relocations.  */
7994
7995 bfd_boolean
7996 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7997 {
7998   bfd *ibfd;
7999   asection *sec;
8000   struct ppc_link_hash_table *htab;
8001   unsigned char *toc_ref;
8002   int pass;
8003
8004   if (info->relocatable || !info->executable)
8005     return TRUE;
8006
8007   htab = ppc_hash_table (info);
8008   if (htab == NULL)
8009     return FALSE;
8010
8011   /* Make two passes over the relocs.  On the first pass, mark toc
8012      entries involved with tls relocs, and check that tls relocs
8013      involved in setting up a tls_get_addr call are indeed followed by
8014      such a call.  If they are not, we can't do any tls optimization.
8015      On the second pass twiddle tls_mask flags to notify
8016      relocate_section that optimization can be done, and adjust got
8017      and plt refcounts.  */
8018   toc_ref = NULL;
8019   for (pass = 0; pass < 2; ++pass)
8020     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8021       {
8022         Elf_Internal_Sym *locsyms = NULL;
8023         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8024
8025         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8026           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8027             {
8028               Elf_Internal_Rela *relstart, *rel, *relend;
8029               bfd_boolean found_tls_get_addr_arg = 0;
8030
8031               /* Read the relocations.  */
8032               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8033                                                     info->keep_memory);
8034               if (relstart == NULL)
8035                 return FALSE;
8036
8037               relend = relstart + sec->reloc_count;
8038               for (rel = relstart; rel < relend; rel++)
8039                 {
8040                   enum elf_ppc64_reloc_type r_type;
8041                   unsigned long r_symndx;
8042                   struct elf_link_hash_entry *h;
8043                   Elf_Internal_Sym *sym;
8044                   asection *sym_sec;
8045                   unsigned char *tls_mask;
8046                   unsigned char tls_set, tls_clear, tls_type = 0;
8047                   bfd_vma value;
8048                   bfd_boolean ok_tprel, is_local;
8049                   long toc_ref_index = 0;
8050                   int expecting_tls_get_addr = 0;
8051                   bfd_boolean ret = FALSE;
8052
8053                   r_symndx = ELF64_R_SYM (rel->r_info);
8054                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8055                                   r_symndx, ibfd))
8056                     {
8057                     err_free_rel:
8058                       if (elf_section_data (sec)->relocs != relstart)
8059                         free (relstart);
8060                       if (toc_ref != NULL)
8061                         free (toc_ref);
8062                       if (locsyms != NULL
8063                           && (elf_symtab_hdr (ibfd).contents
8064                               != (unsigned char *) locsyms))
8065                         free (locsyms);
8066                       return ret;
8067                     }
8068
8069                   if (h != NULL)
8070                     {
8071                       if (h->root.type == bfd_link_hash_defined
8072                           || h->root.type == bfd_link_hash_defweak)
8073                         value = h->root.u.def.value;
8074                       else if (h->root.type == bfd_link_hash_undefweak)
8075                         value = 0;
8076                       else
8077                         {
8078                           found_tls_get_addr_arg = 0;
8079                           continue;
8080                         }
8081                     }
8082                   else
8083                     /* Symbols referenced by TLS relocs must be of type
8084                        STT_TLS.  So no need for .opd local sym adjust.  */
8085                     value = sym->st_value;
8086
8087                   ok_tprel = FALSE;
8088                   is_local = FALSE;
8089                   if (h == NULL
8090                       || !h->def_dynamic)
8091                     {
8092                       is_local = TRUE;
8093                       if (h != NULL
8094                           && h->root.type == bfd_link_hash_undefweak)
8095                         ok_tprel = TRUE;
8096                       else
8097                         {
8098                           value += sym_sec->output_offset;
8099                           value += sym_sec->output_section->vma;
8100                           value -= htab->elf.tls_sec->vma;
8101                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8102                                       < (bfd_vma) 1 << 32);
8103                         }
8104                     }
8105
8106                   r_type = ELF64_R_TYPE (rel->r_info);
8107                   /* If this section has old-style __tls_get_addr calls
8108                      without marker relocs, then check that each
8109                      __tls_get_addr call reloc is preceded by a reloc
8110                      that conceivably belongs to the __tls_get_addr arg
8111                      setup insn.  If we don't find matching arg setup
8112                      relocs, don't do any tls optimization.  */
8113                   if (pass == 0
8114                       && sec->has_tls_get_addr_call
8115                       && h != NULL
8116                       && (h == &htab->tls_get_addr->elf
8117                           || h == &htab->tls_get_addr_fd->elf)
8118                       && !found_tls_get_addr_arg
8119                       && is_branch_reloc (r_type))
8120                     {
8121                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8122                                                 "TLS optimization disabled\n"),
8123                                               ibfd, sec, rel->r_offset);
8124                       ret = TRUE;
8125                       goto err_free_rel;
8126                     }
8127
8128                   found_tls_get_addr_arg = 0;
8129                   switch (r_type)
8130                     {
8131                     case R_PPC64_GOT_TLSLD16:
8132                     case R_PPC64_GOT_TLSLD16_LO:
8133                       expecting_tls_get_addr = 1;
8134                       found_tls_get_addr_arg = 1;
8135                       /* Fall thru */
8136
8137                     case R_PPC64_GOT_TLSLD16_HI:
8138                     case R_PPC64_GOT_TLSLD16_HA:
8139                       /* These relocs should never be against a symbol
8140                          defined in a shared lib.  Leave them alone if
8141                          that turns out to be the case.  */
8142                       if (!is_local)
8143                         continue;
8144
8145                       /* LD -> LE */
8146                       tls_set = 0;
8147                       tls_clear = TLS_LD;
8148                       tls_type = TLS_TLS | TLS_LD;
8149                       break;
8150
8151                     case R_PPC64_GOT_TLSGD16:
8152                     case R_PPC64_GOT_TLSGD16_LO:
8153                       expecting_tls_get_addr = 1;
8154                       found_tls_get_addr_arg = 1;
8155                       /* Fall thru */
8156
8157                     case R_PPC64_GOT_TLSGD16_HI:
8158                     case R_PPC64_GOT_TLSGD16_HA:
8159                       if (ok_tprel)
8160                         /* GD -> LE */
8161                         tls_set = 0;
8162                       else
8163                         /* GD -> IE */
8164                         tls_set = TLS_TLS | TLS_TPRELGD;
8165                       tls_clear = TLS_GD;
8166                       tls_type = TLS_TLS | TLS_GD;
8167                       break;
8168
8169                     case R_PPC64_GOT_TPREL16_DS:
8170                     case R_PPC64_GOT_TPREL16_LO_DS:
8171                     case R_PPC64_GOT_TPREL16_HI:
8172                     case R_PPC64_GOT_TPREL16_HA:
8173                       if (ok_tprel)
8174                         {
8175                           /* IE -> LE */
8176                           tls_set = 0;
8177                           tls_clear = TLS_TPREL;
8178                           tls_type = TLS_TLS | TLS_TPREL;
8179                           break;
8180                         }
8181                       continue;
8182
8183                     case R_PPC64_TLSGD:
8184                     case R_PPC64_TLSLD:
8185                       found_tls_get_addr_arg = 1;
8186                       /* Fall thru */
8187
8188                     case R_PPC64_TLS:
8189                     case R_PPC64_TOC16:
8190                     case R_PPC64_TOC16_LO:
8191                       if (sym_sec == NULL || sym_sec != toc)
8192                         continue;
8193
8194                       /* Mark this toc entry as referenced by a TLS
8195                          code sequence.  We can do that now in the
8196                          case of R_PPC64_TLS, and after checking for
8197                          tls_get_addr for the TOC16 relocs.  */
8198                       if (toc_ref == NULL)
8199                         toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8200                       if (toc_ref == NULL)
8201                         goto err_free_rel;
8202
8203                       if (h != NULL)
8204                         value = h->root.u.def.value;
8205                       else
8206                         value = sym->st_value;
8207                       value += rel->r_addend;
8208                       BFD_ASSERT (value < toc->size && value % 8 == 0);
8209                       toc_ref_index = (value + toc->output_offset) / 8;
8210                       if (r_type == R_PPC64_TLS
8211                           || r_type == R_PPC64_TLSGD
8212                           || r_type == R_PPC64_TLSLD)
8213                         {
8214                           toc_ref[toc_ref_index] = 1;
8215                           continue;
8216                         }
8217
8218                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
8219                         continue;
8220
8221                       tls_set = 0;
8222                       tls_clear = 0;
8223                       expecting_tls_get_addr = 2;
8224                       break;
8225
8226                     case R_PPC64_TPREL64:
8227                       if (pass == 0
8228                           || sec != toc
8229                           || toc_ref == NULL
8230                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8231                         continue;
8232                       if (ok_tprel)
8233                         {
8234                           /* IE -> LE */
8235                           tls_set = TLS_EXPLICIT;
8236                           tls_clear = TLS_TPREL;
8237                           break;
8238                         }
8239                       continue;
8240
8241                     case R_PPC64_DTPMOD64:
8242                       if (pass == 0
8243                           || sec != toc
8244                           || toc_ref == NULL
8245                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8246                         continue;
8247                       if (rel + 1 < relend
8248                           && (rel[1].r_info
8249                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8250                           && rel[1].r_offset == rel->r_offset + 8)
8251                         {
8252                           if (ok_tprel)
8253                             /* GD -> LE */
8254                             tls_set = TLS_EXPLICIT | TLS_GD;
8255                           else
8256                             /* GD -> IE */
8257                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8258                           tls_clear = TLS_GD;
8259                         }
8260                       else
8261                         {
8262                           if (!is_local)
8263                             continue;
8264
8265                           /* LD -> LE */
8266                           tls_set = TLS_EXPLICIT;
8267                           tls_clear = TLS_LD;
8268                         }
8269                       break;
8270
8271                     default:
8272                       continue;
8273                     }
8274
8275                   if (pass == 0)
8276                     {
8277                       if (!expecting_tls_get_addr
8278                           || !sec->has_tls_get_addr_call)
8279                         continue;
8280
8281                       if (rel + 1 < relend
8282                           && branch_reloc_hash_match (ibfd, rel + 1,
8283                                                       htab->tls_get_addr,
8284                                                       htab->tls_get_addr_fd))
8285                         {
8286                           if (expecting_tls_get_addr == 2)
8287                             {
8288                               /* Check for toc tls entries.  */
8289                               unsigned char *toc_tls;
8290                               int retval;
8291
8292                               retval = get_tls_mask (&toc_tls, NULL, NULL,
8293                                                      &locsyms,
8294                                                      rel, ibfd);
8295                               if (retval == 0)
8296                                 goto err_free_rel;
8297                               if (toc_tls != NULL)
8298                                 {
8299                                   if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8300                                     found_tls_get_addr_arg = 1;
8301                                   if (retval > 1)
8302                                     toc_ref[toc_ref_index] = 1;
8303                                 }
8304                             }
8305                           continue;
8306                         }
8307
8308                       if (expecting_tls_get_addr != 1)
8309                         continue;
8310
8311                       /* Uh oh, we didn't find the expected call.  We
8312                          could just mark this symbol to exclude it
8313                          from tls optimization but it's safer to skip
8314                          the entire optimization.  */
8315                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8316                                                 "TLS optimization disabled\n"),
8317                                               ibfd, sec, rel->r_offset);
8318                       ret = TRUE;
8319                       goto err_free_rel;
8320                     }
8321
8322                   if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8323                     {
8324                       struct plt_entry *ent;
8325                       for (ent = htab->tls_get_addr->elf.plt.plist;
8326                            ent != NULL;
8327                            ent = ent->next)
8328                         if (ent->addend == 0)
8329                           {
8330                             if (ent->plt.refcount > 0)
8331                               {
8332                                 ent->plt.refcount -= 1;
8333                                 expecting_tls_get_addr = 0;
8334                               }
8335                             break;
8336                           }
8337                     }
8338
8339                   if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8340                     {
8341                       struct plt_entry *ent;
8342                       for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8343                            ent != NULL;
8344                            ent = ent->next)
8345                         if (ent->addend == 0)
8346                           {
8347                             if (ent->plt.refcount > 0)
8348                               ent->plt.refcount -= 1;
8349                             break;
8350                           }
8351                     }
8352
8353                   if (tls_clear == 0)
8354                     continue;
8355
8356                   if ((tls_set & TLS_EXPLICIT) == 0)
8357                     {
8358                       struct got_entry *ent;
8359
8360                       /* Adjust got entry for this reloc.  */
8361                       if (h != NULL)
8362                         ent = h->got.glist;
8363                       else
8364                         ent = elf_local_got_ents (ibfd)[r_symndx];
8365
8366                       for (; ent != NULL; ent = ent->next)
8367                         if (ent->addend == rel->r_addend
8368                             && ent->owner == ibfd
8369                             && ent->tls_type == tls_type)
8370                           break;
8371                       if (ent == NULL)
8372                         abort ();
8373
8374                       if (tls_set == 0)
8375                         {
8376                           /* We managed to get rid of a got entry.  */
8377                           if (ent->got.refcount > 0)
8378                             ent->got.refcount -= 1;
8379                         }
8380                     }
8381                   else
8382                     {
8383                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
8384                          we'll lose one or two dyn relocs.  */
8385                       if (!dec_dynrel_count (rel->r_info, sec, info,
8386                                              NULL, h, sym))
8387                         return FALSE;
8388
8389                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
8390                         {
8391                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8392                                                  NULL, h, sym))
8393                             return FALSE;
8394                         }
8395                     }
8396
8397                   *tls_mask |= tls_set;
8398                   *tls_mask &= ~tls_clear;
8399                 }
8400
8401               if (elf_section_data (sec)->relocs != relstart)
8402                 free (relstart);
8403             }
8404
8405         if (locsyms != NULL
8406             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8407           {
8408             if (!info->keep_memory)
8409               free (locsyms);
8410             else
8411               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8412           }
8413       }
8414
8415   if (toc_ref != NULL)
8416     free (toc_ref);
8417   return TRUE;
8418 }
8419
8420 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8421    the values of any global symbols in a toc section that has been
8422    edited.  Globals in toc sections should be a rarity, so this function
8423    sets a flag if any are found in toc sections other than the one just
8424    edited, so that futher hash table traversals can be avoided.  */
8425
8426 struct adjust_toc_info
8427 {
8428   asection *toc;
8429   unsigned long *skip;
8430   bfd_boolean global_toc_syms;
8431 };
8432
8433 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8434
8435 static bfd_boolean
8436 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8437 {
8438   struct ppc_link_hash_entry *eh;
8439   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8440   unsigned long i;
8441
8442   if (h->root.type != bfd_link_hash_defined
8443       && h->root.type != bfd_link_hash_defweak)
8444     return TRUE;
8445
8446   eh = (struct ppc_link_hash_entry *) h;
8447   if (eh->adjust_done)
8448     return TRUE;
8449
8450   if (eh->elf.root.u.def.section == toc_inf->toc)
8451     {
8452       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8453         i = toc_inf->toc->rawsize >> 3;
8454       else
8455         i = eh->elf.root.u.def.value >> 3;
8456
8457       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8458         {
8459           (*_bfd_error_handler)
8460             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8461           do
8462             ++i;
8463           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8464           eh->elf.root.u.def.value = (bfd_vma) i << 3;
8465         }
8466
8467       eh->elf.root.u.def.value -= toc_inf->skip[i];
8468       eh->adjust_done = 1;
8469     }
8470   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8471     toc_inf->global_toc_syms = TRUE;
8472
8473   return TRUE;
8474 }
8475
8476 /* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc.  */
8477
8478 static bfd_boolean
8479 ok_lo_toc_insn (unsigned int insn)
8480 {
8481   return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8482           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8483           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8484           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8485           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8486           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8487           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8488           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8489           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8490           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8491           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8492           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8493           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8494           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8495           || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8496               && (insn & 3) != 1)
8497           || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8498               && ((insn & 3) == 0 || (insn & 3) == 3))
8499           || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8500 }
8501
8502 /* Examine all relocs referencing .toc sections in order to remove
8503    unused .toc entries.  */
8504
8505 bfd_boolean
8506 ppc64_elf_edit_toc (struct bfd_link_info *info)
8507 {
8508   bfd *ibfd;
8509   struct adjust_toc_info toc_inf;
8510   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8511
8512   htab->do_toc_opt = 1;
8513   toc_inf.global_toc_syms = TRUE;
8514   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8515     {
8516       asection *toc, *sec;
8517       Elf_Internal_Shdr *symtab_hdr;
8518       Elf_Internal_Sym *local_syms;
8519       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8520       unsigned long *skip, *drop;
8521       unsigned char *used;
8522       unsigned char *keep, last, some_unused;
8523
8524       if (!is_ppc64_elf (ibfd))
8525         continue;
8526
8527       toc = bfd_get_section_by_name (ibfd, ".toc");
8528       if (toc == NULL
8529           || toc->size == 0
8530           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8531           || discarded_section (toc))
8532         continue;
8533
8534       toc_relocs = NULL;
8535       local_syms = NULL;
8536       symtab_hdr = &elf_symtab_hdr (ibfd);
8537
8538       /* Look at sections dropped from the final link.  */
8539       skip = NULL;
8540       relstart = NULL;
8541       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8542         {
8543           if (sec->reloc_count == 0
8544               || !discarded_section (sec)
8545               || get_opd_info (sec)
8546               || (sec->flags & SEC_ALLOC) == 0
8547               || (sec->flags & SEC_DEBUGGING) != 0)
8548             continue;
8549
8550           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8551           if (relstart == NULL)
8552             goto error_ret;
8553
8554           /* Run through the relocs to see which toc entries might be
8555              unused.  */
8556           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8557             {
8558               enum elf_ppc64_reloc_type r_type;
8559               unsigned long r_symndx;
8560               asection *sym_sec;
8561               struct elf_link_hash_entry *h;
8562               Elf_Internal_Sym *sym;
8563               bfd_vma val;
8564
8565               r_type = ELF64_R_TYPE (rel->r_info);
8566               switch (r_type)
8567                 {
8568                 default:
8569                   continue;
8570
8571                 case R_PPC64_TOC16:
8572                 case R_PPC64_TOC16_LO:
8573                 case R_PPC64_TOC16_HI:
8574                 case R_PPC64_TOC16_HA:
8575                 case R_PPC64_TOC16_DS:
8576                 case R_PPC64_TOC16_LO_DS:
8577                   break;
8578                 }
8579
8580               r_symndx = ELF64_R_SYM (rel->r_info);
8581               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8582                               r_symndx, ibfd))
8583                 goto error_ret;
8584
8585               if (sym_sec != toc)
8586                 continue;
8587
8588               if (h != NULL)
8589                 val = h->root.u.def.value;
8590               else
8591                 val = sym->st_value;
8592               val += rel->r_addend;
8593
8594               if (val >= toc->size)
8595                 continue;
8596
8597               /* Anything in the toc ought to be aligned to 8 bytes.
8598                  If not, don't mark as unused.  */
8599               if (val & 7)
8600                 continue;
8601
8602               if (skip == NULL)
8603                 {
8604                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8605                   if (skip == NULL)
8606                     goto error_ret;
8607                 }
8608
8609               skip[val >> 3] = ref_from_discarded;
8610             }
8611
8612           if (elf_section_data (sec)->relocs != relstart)
8613             free (relstart);
8614         }
8615
8616       /* For largetoc loads of address constants, we can convert
8617          .  addis rx,2,addr@got@ha
8618          .  ld ry,addr@got@l(rx)
8619          to
8620          .  addis rx,2,addr@toc@ha
8621          .  addi ry,rx,addr@toc@l
8622          when addr is within 2G of the toc pointer.  This then means
8623          that the word storing "addr" in the toc is no longer needed.  */
8624
8625       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8626           && toc->output_section->rawsize < (bfd_vma) 1 << 31
8627           && toc->reloc_count != 0)
8628         {
8629           /* Read toc relocs.  */
8630           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8631                                                   info->keep_memory);
8632           if (toc_relocs == NULL)
8633             goto error_ret;
8634
8635           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8636             {
8637               enum elf_ppc64_reloc_type r_type;
8638               unsigned long r_symndx;
8639               asection *sym_sec;
8640               struct elf_link_hash_entry *h;
8641               Elf_Internal_Sym *sym;
8642               bfd_vma val, addr;
8643
8644               r_type = ELF64_R_TYPE (rel->r_info);
8645               if (r_type != R_PPC64_ADDR64)
8646                 continue;
8647
8648               r_symndx = ELF64_R_SYM (rel->r_info);
8649               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8650                               r_symndx, ibfd))
8651                 goto error_ret;
8652
8653               if (sym_sec == NULL
8654                   || discarded_section (sym_sec))
8655                 continue;
8656
8657               if (!SYMBOL_CALLS_LOCAL (info, h))
8658                 continue;
8659
8660               if (h != NULL)
8661                 {
8662                   if (h->type == STT_GNU_IFUNC)
8663                     continue;
8664                   val = h->root.u.def.value;
8665                 }
8666               else
8667                 {
8668                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8669                     continue;
8670                   val = sym->st_value;
8671                 }
8672               val += rel->r_addend;
8673               val += sym_sec->output_section->vma + sym_sec->output_offset;
8674
8675               /* We don't yet know the exact toc pointer value, but we
8676                  know it will be somewhere in the toc section.  Don't
8677                  optimize if the difference from any possible toc
8678                  pointer is outside [ff..f80008000, 7fff7fff].  */
8679               addr = toc->output_section->vma + TOC_BASE_OFF;
8680               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8681                 continue;
8682
8683               addr = toc->output_section->vma + toc->output_section->rawsize;
8684               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8685                 continue;
8686
8687               if (skip == NULL)
8688                 {
8689                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8690                   if (skip == NULL)
8691                     goto error_ret;
8692                 }
8693
8694               skip[rel->r_offset >> 3]
8695                 |= can_optimize | ((rel - toc_relocs) << 2);
8696             }
8697         }
8698
8699       if (skip == NULL)
8700         continue;
8701
8702       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8703       if (used == NULL)
8704         {
8705         error_ret:
8706           if (local_syms != NULL
8707               && symtab_hdr->contents != (unsigned char *) local_syms)
8708             free (local_syms);
8709           if (sec != NULL
8710               && relstart != NULL
8711               && elf_section_data (sec)->relocs != relstart)
8712             free (relstart);
8713           if (toc_relocs != NULL
8714               && elf_section_data (toc)->relocs != toc_relocs)
8715             free (toc_relocs);
8716           if (skip != NULL)
8717             free (skip);
8718           return FALSE;
8719         }
8720
8721       /* Now check all kept sections that might reference the toc.
8722          Check the toc itself last.  */
8723       for (sec = (ibfd->sections == toc && toc->next ? toc->next
8724                   : ibfd->sections);
8725            sec != NULL;
8726            sec = (sec == toc ? NULL
8727                   : sec->next == NULL ? toc
8728                   : sec->next == toc && toc->next ? toc->next
8729                   : sec->next))
8730         {
8731           int repeat;
8732
8733           if (sec->reloc_count == 0
8734               || discarded_section (sec)
8735               || get_opd_info (sec)
8736               || (sec->flags & SEC_ALLOC) == 0
8737               || (sec->flags & SEC_DEBUGGING) != 0)
8738             continue;
8739
8740           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8741                                                 info->keep_memory);
8742           if (relstart == NULL)
8743             goto error_ret;
8744
8745           /* Mark toc entries referenced as used.  */
8746           do
8747             {
8748               repeat = 0;
8749               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8750                 {
8751                   enum elf_ppc64_reloc_type r_type;
8752                   unsigned long r_symndx;
8753                   asection *sym_sec;
8754                   struct elf_link_hash_entry *h;
8755                   Elf_Internal_Sym *sym;
8756                   bfd_vma val;
8757                   enum {no_check, check_lo, check_ha} insn_check;
8758
8759                   r_type = ELF64_R_TYPE (rel->r_info);
8760                   switch (r_type)
8761                     {
8762                     default:
8763                       insn_check = no_check;
8764                       break;
8765
8766                     case R_PPC64_GOT_TLSLD16_HA:
8767                     case R_PPC64_GOT_TLSGD16_HA:
8768                     case R_PPC64_GOT_TPREL16_HA:
8769                     case R_PPC64_GOT_DTPREL16_HA:
8770                     case R_PPC64_GOT16_HA:
8771                     case R_PPC64_TOC16_HA:
8772                       insn_check = check_ha;
8773                       break;
8774
8775                     case R_PPC64_GOT_TLSLD16_LO:
8776                     case R_PPC64_GOT_TLSGD16_LO:
8777                     case R_PPC64_GOT_TPREL16_LO_DS:
8778                     case R_PPC64_GOT_DTPREL16_LO_DS:
8779                     case R_PPC64_GOT16_LO:
8780                     case R_PPC64_GOT16_LO_DS:
8781                     case R_PPC64_TOC16_LO:
8782                     case R_PPC64_TOC16_LO_DS:
8783                       insn_check = check_lo;
8784                       break;
8785                     }
8786
8787                   if (insn_check != no_check)
8788                     {
8789                       bfd_vma off = rel->r_offset & ~3;
8790                       unsigned char buf[4];
8791                       unsigned int insn;
8792
8793                       if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8794                         {
8795                           free (used);
8796                           goto error_ret;
8797                         }
8798                       insn = bfd_get_32 (ibfd, buf);
8799                       if (insn_check == check_lo
8800                           ? !ok_lo_toc_insn (insn)
8801                           : ((insn & ((0x3f << 26) | 0x1f << 16))
8802                              != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8803                         {
8804                           char str[12];
8805
8806                           ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8807                           sprintf (str, "%#08x", insn);
8808                           info->callbacks->einfo
8809                             (_("%P: %H: toc optimization is not supported for"
8810                                " %s instruction.\n"),
8811                              ibfd, sec, rel->r_offset & ~3, str);
8812                         }
8813                     }
8814
8815                   switch (r_type)
8816                     {
8817                     case R_PPC64_TOC16:
8818                     case R_PPC64_TOC16_LO:
8819                     case R_PPC64_TOC16_HI:
8820                     case R_PPC64_TOC16_HA:
8821                     case R_PPC64_TOC16_DS:
8822                     case R_PPC64_TOC16_LO_DS:
8823                       /* In case we're taking addresses of toc entries.  */
8824                     case R_PPC64_ADDR64:
8825                       break;
8826
8827                     default:
8828                       continue;
8829                     }
8830
8831                   r_symndx = ELF64_R_SYM (rel->r_info);
8832                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8833                                   r_symndx, ibfd))
8834                     {
8835                       free (used);
8836                       goto error_ret;
8837                     }
8838
8839                   if (sym_sec != toc)
8840                     continue;
8841
8842                   if (h != NULL)
8843                     val = h->root.u.def.value;
8844                   else
8845                     val = sym->st_value;
8846                   val += rel->r_addend;
8847
8848                   if (val >= toc->size)
8849                     continue;
8850
8851                   if ((skip[val >> 3] & can_optimize) != 0)
8852                     {
8853                       bfd_vma off;
8854                       unsigned char opc;
8855
8856                       switch (r_type)
8857                         {
8858                         case R_PPC64_TOC16_HA:
8859                           break;
8860
8861                         case R_PPC64_TOC16_LO_DS:
8862                           off = rel->r_offset;
8863                           off += (bfd_big_endian (ibfd) ? -2 : 3);
8864                           if (!bfd_get_section_contents (ibfd, sec, &opc,
8865                                                          off, 1))
8866                             {
8867                               free (used);
8868                               goto error_ret;
8869                             }
8870                           if ((opc & (0x3f << 2)) == (58u << 2))
8871                             break;
8872                           /* Fall thru */
8873
8874                         default:
8875                           /* Wrong sort of reloc, or not a ld.  We may
8876                              as well clear ref_from_discarded too.  */
8877                           skip[val >> 3] = 0;
8878                         }
8879                     }
8880
8881                   if (sec != toc)
8882                     used[val >> 3] = 1;
8883                   /* For the toc section, we only mark as used if this
8884                      entry itself isn't unused.  */
8885                   else if ((used[rel->r_offset >> 3]
8886                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8887                            && !used[val >> 3])
8888                     {
8889                       /* Do all the relocs again, to catch reference
8890                          chains.  */
8891                       repeat = 1;
8892                       used[val >> 3] = 1;
8893                     }
8894                 }
8895             }
8896           while (repeat);
8897
8898           if (elf_section_data (sec)->relocs != relstart)
8899             free (relstart);
8900         }
8901
8902       /* Merge the used and skip arrays.  Assume that TOC
8903          doublewords not appearing as either used or unused belong
8904          to to an entry more than one doubleword in size.  */
8905       for (drop = skip, keep = used, last = 0, some_unused = 0;
8906            drop < skip + (toc->size + 7) / 8;
8907            ++drop, ++keep)
8908         {
8909           if (*keep)
8910             {
8911               *drop &= ~ref_from_discarded;
8912               if ((*drop & can_optimize) != 0)
8913                 some_unused = 1;
8914               last = 0;
8915             }
8916           else if ((*drop & ref_from_discarded) != 0)
8917             {
8918               some_unused = 1;
8919               last = ref_from_discarded;
8920             }
8921           else
8922             *drop = last;
8923         }
8924
8925       free (used);
8926
8927       if (some_unused)
8928         {
8929           bfd_byte *contents, *src;
8930           unsigned long off;
8931           Elf_Internal_Sym *sym;
8932           bfd_boolean local_toc_syms = FALSE;
8933
8934           /* Shuffle the toc contents, and at the same time convert the
8935              skip array from booleans into offsets.  */
8936           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8937             goto error_ret;
8938
8939           elf_section_data (toc)->this_hdr.contents = contents;
8940
8941           for (src = contents, off = 0, drop = skip;
8942                src < contents + toc->size;
8943                src += 8, ++drop)
8944             {
8945               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8946                 off += 8;
8947               else if (off != 0)
8948                 {
8949                   *drop = off;
8950                   memcpy (src - off, src, 8);
8951                 }
8952             }
8953           *drop = off;
8954           toc->rawsize = toc->size;
8955           toc->size = src - contents - off;
8956
8957           /* Adjust addends for relocs against the toc section sym,
8958              and optimize any accesses we can.  */
8959           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8960             {
8961               if (sec->reloc_count == 0
8962                   || discarded_section (sec))
8963                 continue;
8964
8965               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8966                                                     info->keep_memory);
8967               if (relstart == NULL)
8968                 goto error_ret;
8969
8970               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8971                 {
8972                   enum elf_ppc64_reloc_type r_type;
8973                   unsigned long r_symndx;
8974                   asection *sym_sec;
8975                   struct elf_link_hash_entry *h;
8976                   bfd_vma val;
8977
8978                   r_type = ELF64_R_TYPE (rel->r_info);
8979                   switch (r_type)
8980                     {
8981                     default:
8982                       continue;
8983
8984                     case R_PPC64_TOC16:
8985                     case R_PPC64_TOC16_LO:
8986                     case R_PPC64_TOC16_HI:
8987                     case R_PPC64_TOC16_HA:
8988                     case R_PPC64_TOC16_DS:
8989                     case R_PPC64_TOC16_LO_DS:
8990                     case R_PPC64_ADDR64:
8991                       break;
8992                     }
8993
8994                   r_symndx = ELF64_R_SYM (rel->r_info);
8995                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8996                                   r_symndx, ibfd))
8997                     goto error_ret;
8998
8999                   if (sym_sec != toc)
9000                     continue;
9001
9002                   if (h != NULL)
9003                     val = h->root.u.def.value;
9004                   else
9005                     {
9006                       val = sym->st_value;
9007                       if (val != 0)
9008                         local_toc_syms = TRUE;
9009                     }
9010
9011                   val += rel->r_addend;
9012
9013                   if (val > toc->rawsize)
9014                     val = toc->rawsize;
9015                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
9016                     continue;
9017                   else if ((skip[val >> 3] & can_optimize) != 0)
9018                     {
9019                       Elf_Internal_Rela *tocrel
9020                         = toc_relocs + (skip[val >> 3] >> 2);
9021                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9022
9023                       switch (r_type)
9024                         {
9025                         case R_PPC64_TOC16_HA:
9026                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9027                           break;
9028
9029                         case R_PPC64_TOC16_LO_DS:
9030                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9031                           break;
9032
9033                         default:
9034                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9035                             ppc_howto_init ();
9036                           info->callbacks->einfo
9037                             (_("%P: %H: %s references "
9038                                "optimized away TOC entry\n"),
9039                              ibfd, sec, rel->r_offset,
9040                              ppc64_elf_howto_table[r_type]->name);
9041                           bfd_set_error (bfd_error_bad_value);
9042                           goto error_ret;
9043                         }
9044                       rel->r_addend = tocrel->r_addend;
9045                       elf_section_data (sec)->relocs = relstart;
9046                       continue;
9047                     }
9048
9049                   if (h != NULL || sym->st_value != 0)
9050                     continue;
9051
9052                   rel->r_addend -= skip[val >> 3];
9053                   elf_section_data (sec)->relocs = relstart;
9054                 }
9055
9056               if (elf_section_data (sec)->relocs != relstart)
9057                 free (relstart);
9058             }
9059
9060           /* We shouldn't have local or global symbols defined in the TOC,
9061              but handle them anyway.  */
9062           if (local_syms != NULL)
9063             for (sym = local_syms;
9064                  sym < local_syms + symtab_hdr->sh_info;
9065                  ++sym)
9066               if (sym->st_value != 0
9067                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9068                 {
9069                   unsigned long i;
9070
9071                   if (sym->st_value > toc->rawsize)
9072                     i = toc->rawsize >> 3;
9073                   else
9074                     i = sym->st_value >> 3;
9075
9076                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9077                     {
9078                       if (local_toc_syms)
9079                         (*_bfd_error_handler)
9080                           (_("%s defined on removed toc entry"),
9081                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9082                       do
9083                         ++i;
9084                       while ((skip[i] & (ref_from_discarded | can_optimize)));
9085                       sym->st_value = (bfd_vma) i << 3;
9086                     }
9087
9088                   sym->st_value -= skip[i];
9089                   symtab_hdr->contents = (unsigned char *) local_syms;
9090                 }
9091
9092           /* Adjust any global syms defined in this toc input section.  */
9093           if (toc_inf.global_toc_syms)
9094             {
9095               toc_inf.toc = toc;
9096               toc_inf.skip = skip;
9097               toc_inf.global_toc_syms = FALSE;
9098               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9099                                       &toc_inf);
9100             }
9101
9102           if (toc->reloc_count != 0)
9103             {
9104               Elf_Internal_Shdr *rel_hdr;
9105               Elf_Internal_Rela *wrel;
9106               bfd_size_type sz;
9107
9108               /* Remove unused toc relocs, and adjust those we keep.  */
9109               if (toc_relocs == NULL)
9110                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9111                                                         info->keep_memory);
9112               if (toc_relocs == NULL)
9113                 goto error_ret;
9114
9115               wrel = toc_relocs;
9116               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9117                 if ((skip[rel->r_offset >> 3]
9118                      & (ref_from_discarded | can_optimize)) == 0)
9119                   {
9120                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9121                     wrel->r_info = rel->r_info;
9122                     wrel->r_addend = rel->r_addend;
9123                     ++wrel;
9124                   }
9125                 else if (!dec_dynrel_count (rel->r_info, toc, info,
9126                                             &local_syms, NULL, NULL))
9127                   goto error_ret;
9128
9129               elf_section_data (toc)->relocs = toc_relocs;
9130               toc->reloc_count = wrel - toc_relocs;
9131               rel_hdr = _bfd_elf_single_rel_hdr (toc);
9132               sz = rel_hdr->sh_entsize;
9133               rel_hdr->sh_size = toc->reloc_count * sz;
9134             }
9135         }
9136       else if (toc_relocs != NULL
9137                && elf_section_data (toc)->relocs != toc_relocs)
9138         free (toc_relocs);
9139
9140       if (local_syms != NULL
9141           && symtab_hdr->contents != (unsigned char *) local_syms)
9142         {
9143           if (!info->keep_memory)
9144             free (local_syms);
9145           else
9146             symtab_hdr->contents = (unsigned char *) local_syms;
9147         }
9148       free (skip);
9149     }
9150
9151   return TRUE;
9152 }
9153
9154 /* Return true iff input section I references the TOC using
9155    instructions limited to +/-32k offsets.  */
9156
9157 bfd_boolean
9158 ppc64_elf_has_small_toc_reloc (asection *i)
9159 {
9160   return (is_ppc64_elf (i->owner)
9161           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9162 }
9163
9164 /* Allocate space for one GOT entry.  */
9165
9166 static void
9167 allocate_got (struct elf_link_hash_entry *h,
9168               struct bfd_link_info *info,
9169               struct got_entry *gent)
9170 {
9171   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9172   bfd_boolean dyn;
9173   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9174   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9175                  ? 16 : 8);
9176   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9177                   ? 2 : 1) * sizeof (Elf64_External_Rela);
9178   asection *got = ppc64_elf_tdata (gent->owner)->got;
9179
9180   gent->got.offset = got->size;
9181   got->size += entsize;
9182
9183   dyn = htab->elf.dynamic_sections_created;
9184   if (h->type == STT_GNU_IFUNC)
9185     {
9186       htab->reliplt->size += rentsize;
9187       htab->got_reli_size += rentsize;
9188     }
9189   else if ((info->shared
9190             || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9191            && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9192                || h->root.type != bfd_link_hash_undefweak))
9193     {
9194       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9195       relgot->size += rentsize;
9196     }
9197 }
9198
9199 /* This function merges got entries in the same toc group.  */
9200
9201 static void
9202 merge_got_entries (struct got_entry **pent)
9203 {
9204   struct got_entry *ent, *ent2;
9205
9206   for (ent = *pent; ent != NULL; ent = ent->next)
9207     if (!ent->is_indirect)
9208       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9209         if (!ent2->is_indirect
9210             && ent2->addend == ent->addend
9211             && ent2->tls_type == ent->tls_type
9212             && elf_gp (ent2->owner) == elf_gp (ent->owner))
9213           {
9214             ent2->is_indirect = TRUE;
9215             ent2->got.ent = ent;
9216           }
9217 }
9218
9219 /* Allocate space in .plt, .got and associated reloc sections for
9220    dynamic relocs.  */
9221
9222 static bfd_boolean
9223 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9224 {
9225   struct bfd_link_info *info;
9226   struct ppc_link_hash_table *htab;
9227   asection *s;
9228   struct ppc_link_hash_entry *eh;
9229   struct elf_dyn_relocs *p;
9230   struct got_entry **pgent, *gent;
9231
9232   if (h->root.type == bfd_link_hash_indirect)
9233     return TRUE;
9234
9235   info = (struct bfd_link_info *) inf;
9236   htab = ppc_hash_table (info);
9237   if (htab == NULL)
9238     return FALSE;
9239
9240   if ((htab->elf.dynamic_sections_created
9241        && h->dynindx != -1
9242        && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
9243       || h->type == STT_GNU_IFUNC)
9244     {
9245       struct plt_entry *pent;
9246       bfd_boolean doneone = FALSE;
9247       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9248         if (pent->plt.refcount > 0)
9249           {
9250             if (!htab->elf.dynamic_sections_created
9251                 || h->dynindx == -1)
9252               {
9253                 s = htab->iplt;
9254                 pent->plt.offset = s->size;
9255                 s->size += PLT_ENTRY_SIZE (htab);
9256                 s = htab->reliplt;
9257               }
9258             else
9259               {
9260                 /* If this is the first .plt entry, make room for the special
9261                    first entry.  */
9262                 s = htab->plt;
9263                 if (s->size == 0)
9264                   s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9265
9266                 pent->plt.offset = s->size;
9267
9268                 /* Make room for this entry.  */
9269                 s->size += PLT_ENTRY_SIZE (htab);
9270
9271                 /* Make room for the .glink code.  */
9272                 s = htab->glink;
9273                 if (s->size == 0)
9274                   s->size += GLINK_CALL_STUB_SIZE;
9275                 if (htab->opd_abi)
9276                   {
9277                     /* We need bigger stubs past index 32767.  */
9278                     if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9279                       s->size += 4;
9280                     s->size += 2*4;
9281                   }
9282                 else
9283                   s->size += 4;
9284
9285                 /* We also need to make an entry in the .rela.plt section.  */
9286                 s = htab->relplt;
9287               }
9288             s->size += sizeof (Elf64_External_Rela);
9289             doneone = TRUE;
9290           }
9291         else
9292           pent->plt.offset = (bfd_vma) -1;
9293       if (!doneone)
9294         {
9295           h->plt.plist = NULL;
9296           h->needs_plt = 0;
9297         }
9298     }
9299   else
9300     {
9301       h->plt.plist = NULL;
9302       h->needs_plt = 0;
9303     }
9304
9305   eh = (struct ppc_link_hash_entry *) h;
9306   /* Run through the TLS GD got entries first if we're changing them
9307      to TPREL.  */
9308   if ((eh->tls_mask & TLS_TPRELGD) != 0)
9309     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9310       if (gent->got.refcount > 0
9311           && (gent->tls_type & TLS_GD) != 0)
9312         {
9313           /* This was a GD entry that has been converted to TPREL.  If
9314              there happens to be a TPREL entry we can use that one.  */
9315           struct got_entry *ent;
9316           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9317             if (ent->got.refcount > 0
9318                 && (ent->tls_type & TLS_TPREL) != 0
9319                 && ent->addend == gent->addend
9320                 && ent->owner == gent->owner)
9321               {
9322                 gent->got.refcount = 0;
9323                 break;
9324               }
9325
9326           /* If not, then we'll be using our own TPREL entry.  */
9327           if (gent->got.refcount != 0)
9328             gent->tls_type = TLS_TLS | TLS_TPREL;
9329         }
9330
9331   /* Remove any list entry that won't generate a word in the GOT before
9332      we call merge_got_entries.  Otherwise we risk merging to empty
9333      entries.  */
9334   pgent = &h->got.glist;
9335   while ((gent = *pgent) != NULL)
9336     if (gent->got.refcount > 0)
9337       {
9338         if ((gent->tls_type & TLS_LD) != 0
9339             && !h->def_dynamic)
9340           {
9341             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9342             *pgent = gent->next;
9343           }
9344         else
9345           pgent = &gent->next;
9346       }
9347     else
9348       *pgent = gent->next;
9349
9350   if (!htab->do_multi_toc)
9351     merge_got_entries (&h->got.glist);
9352
9353   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9354     if (!gent->is_indirect)
9355       {
9356         /* Make sure this symbol is output as a dynamic symbol.
9357            Undefined weak syms won't yet be marked as dynamic,
9358            nor will all TLS symbols.  */
9359         if (h->dynindx == -1
9360             && !h->forced_local
9361             && h->type != STT_GNU_IFUNC
9362             && htab->elf.dynamic_sections_created)
9363           {
9364             if (! bfd_elf_link_record_dynamic_symbol (info, h))
9365               return FALSE;
9366           }
9367
9368         if (!is_ppc64_elf (gent->owner))
9369           abort ();
9370
9371         allocate_got (h, info, gent);
9372       }
9373
9374   if (eh->dyn_relocs == NULL
9375       || (!htab->elf.dynamic_sections_created
9376           && h->type != STT_GNU_IFUNC))
9377     return TRUE;
9378
9379   /* In the shared -Bsymbolic case, discard space allocated for
9380      dynamic pc-relative relocs against symbols which turn out to be
9381      defined in regular objects.  For the normal shared case, discard
9382      space for relocs that have become local due to symbol visibility
9383      changes.  */
9384
9385   if (info->shared)
9386     {
9387       /* Relocs that use pc_count are those that appear on a call insn,
9388          or certain REL relocs (see must_be_dyn_reloc) that can be
9389          generated via assembly.  We want calls to protected symbols to
9390          resolve directly to the function rather than going via the plt.
9391          If people want function pointer comparisons to work as expected
9392          then they should avoid writing weird assembly.  */
9393       if (SYMBOL_CALLS_LOCAL (info, h))
9394         {
9395           struct elf_dyn_relocs **pp;
9396
9397           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9398             {
9399               p->count -= p->pc_count;
9400               p->pc_count = 0;
9401               if (p->count == 0)
9402                 *pp = p->next;
9403               else
9404                 pp = &p->next;
9405             }
9406         }
9407
9408       /* Also discard relocs on undefined weak syms with non-default
9409          visibility.  */
9410       if (eh->dyn_relocs != NULL
9411           && h->root.type == bfd_link_hash_undefweak)
9412         {
9413           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9414             eh->dyn_relocs = NULL;
9415
9416           /* Make sure this symbol is output as a dynamic symbol.
9417              Undefined weak syms won't yet be marked as dynamic.  */
9418           else if (h->dynindx == -1
9419                    && !h->forced_local)
9420             {
9421               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9422                 return FALSE;
9423             }
9424         }
9425     }
9426   else if (h->type == STT_GNU_IFUNC)
9427     {
9428       if (!h->non_got_ref)
9429         eh->dyn_relocs = NULL;
9430     }
9431   else if (ELIMINATE_COPY_RELOCS)
9432     {
9433       /* For the non-shared case, discard space for relocs against
9434          symbols which turn out to need copy relocs or are not
9435          dynamic.  */
9436
9437       if (!h->non_got_ref
9438           && !h->def_regular)
9439         {
9440           /* Make sure this symbol is output as a dynamic symbol.
9441              Undefined weak syms won't yet be marked as dynamic.  */
9442           if (h->dynindx == -1
9443               && !h->forced_local)
9444             {
9445               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9446                 return FALSE;
9447             }
9448
9449           /* If that succeeded, we know we'll be keeping all the
9450              relocs.  */
9451           if (h->dynindx != -1)
9452             goto keep;
9453         }
9454
9455       eh->dyn_relocs = NULL;
9456
9457     keep: ;
9458     }
9459
9460   /* Finally, allocate space.  */
9461   for (p = eh->dyn_relocs; p != NULL; p = p->next)
9462     {
9463       asection *sreloc = elf_section_data (p->sec)->sreloc;
9464       if (eh->elf.type == STT_GNU_IFUNC)
9465         sreloc = htab->reliplt;
9466       sreloc->size += p->count * sizeof (Elf64_External_Rela);
9467     }
9468
9469   return TRUE;
9470 }
9471
9472 /* Find any dynamic relocs that apply to read-only sections.  */
9473
9474 static bfd_boolean
9475 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9476 {
9477   struct ppc_link_hash_entry *eh;
9478   struct elf_dyn_relocs *p;
9479
9480   eh = (struct ppc_link_hash_entry *) h;
9481   for (p = eh->dyn_relocs; p != NULL; p = p->next)
9482     {
9483       asection *s = p->sec->output_section;
9484
9485       if (s != NULL && (s->flags & SEC_READONLY) != 0)
9486         {
9487           struct bfd_link_info *info = inf;
9488
9489           info->flags |= DF_TEXTREL;
9490
9491           /* Not an error, just cut short the traversal.  */
9492           return FALSE;
9493         }
9494     }
9495   return TRUE;
9496 }
9497
9498 /* Set the sizes of the dynamic sections.  */
9499
9500 static bfd_boolean
9501 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9502                                  struct bfd_link_info *info)
9503 {
9504   struct ppc_link_hash_table *htab;
9505   bfd *dynobj;
9506   asection *s;
9507   bfd_boolean relocs;
9508   bfd *ibfd;
9509   struct got_entry *first_tlsld;
9510
9511   htab = ppc_hash_table (info);
9512   if (htab == NULL)
9513     return FALSE;
9514
9515   dynobj = htab->elf.dynobj;
9516   if (dynobj == NULL)
9517     abort ();
9518
9519   if (htab->elf.dynamic_sections_created)
9520     {
9521       /* Set the contents of the .interp section to the interpreter.  */
9522       if (info->executable)
9523         {
9524           s = bfd_get_linker_section (dynobj, ".interp");
9525           if (s == NULL)
9526             abort ();
9527           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9528           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9529         }
9530     }
9531
9532   /* Set up .got offsets for local syms, and space for local dynamic
9533      relocs.  */
9534   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9535     {
9536       struct got_entry **lgot_ents;
9537       struct got_entry **end_lgot_ents;
9538       struct plt_entry **local_plt;
9539       struct plt_entry **end_local_plt;
9540       unsigned char *lgot_masks;
9541       bfd_size_type locsymcount;
9542       Elf_Internal_Shdr *symtab_hdr;
9543
9544       if (!is_ppc64_elf (ibfd))
9545         continue;
9546
9547       for (s = ibfd->sections; s != NULL; s = s->next)
9548         {
9549           struct ppc_dyn_relocs *p;
9550
9551           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9552             {
9553               if (!bfd_is_abs_section (p->sec)
9554                   && bfd_is_abs_section (p->sec->output_section))
9555                 {
9556                   /* Input section has been discarded, either because
9557                      it is a copy of a linkonce section or due to
9558                      linker script /DISCARD/, so we'll be discarding
9559                      the relocs too.  */
9560                 }
9561               else if (p->count != 0)
9562                 {
9563                   asection *srel = elf_section_data (p->sec)->sreloc;
9564                   if (p->ifunc)
9565                     srel = htab->reliplt;
9566                   srel->size += p->count * sizeof (Elf64_External_Rela);
9567                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9568                     info->flags |= DF_TEXTREL;
9569                 }
9570             }
9571         }
9572
9573       lgot_ents = elf_local_got_ents (ibfd);
9574       if (!lgot_ents)
9575         continue;
9576
9577       symtab_hdr = &elf_symtab_hdr (ibfd);
9578       locsymcount = symtab_hdr->sh_info;
9579       end_lgot_ents = lgot_ents + locsymcount;
9580       local_plt = (struct plt_entry **) end_lgot_ents;
9581       end_local_plt = local_plt + locsymcount;
9582       lgot_masks = (unsigned char *) end_local_plt;
9583       s = ppc64_elf_tdata (ibfd)->got;
9584       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9585         {
9586           struct got_entry **pent, *ent;
9587
9588           pent = lgot_ents;
9589           while ((ent = *pent) != NULL)
9590             if (ent->got.refcount > 0)
9591               {
9592                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9593                   {
9594                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
9595                     *pent = ent->next;
9596                   }
9597                 else
9598                   {
9599                     unsigned int ent_size = 8;
9600                     unsigned int rel_size = sizeof (Elf64_External_Rela);
9601
9602                     ent->got.offset = s->size;
9603                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9604                       {
9605                         ent_size *= 2;
9606                         rel_size *= 2;
9607                       }
9608                     s->size += ent_size;
9609                     if ((*lgot_masks & PLT_IFUNC) != 0)
9610                       {
9611                         htab->reliplt->size += rel_size;
9612                         htab->got_reli_size += rel_size;
9613                       }
9614                     else if (info->shared)
9615                       {
9616                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9617                         srel->size += rel_size;
9618                       }
9619                     pent = &ent->next;
9620                   }
9621               }
9622             else
9623               *pent = ent->next;
9624         }
9625
9626       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
9627       for (; local_plt < end_local_plt; ++local_plt)
9628         {
9629           struct plt_entry *ent;
9630
9631           for (ent = *local_plt; ent != NULL; ent = ent->next)
9632             if (ent->plt.refcount > 0)
9633               {
9634                 s = htab->iplt;
9635                 ent->plt.offset = s->size;
9636                 s->size += PLT_ENTRY_SIZE (htab);
9637
9638                 htab->reliplt->size += sizeof (Elf64_External_Rela);
9639               }
9640             else
9641               ent->plt.offset = (bfd_vma) -1;
9642         }
9643     }
9644
9645   /* Allocate global sym .plt and .got entries, and space for global
9646      sym dynamic relocs.  */
9647   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9648
9649   first_tlsld = NULL;
9650   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9651     {
9652       struct got_entry *ent;
9653
9654       if (!is_ppc64_elf (ibfd))
9655         continue;
9656
9657       ent = ppc64_tlsld_got (ibfd);
9658       if (ent->got.refcount > 0)
9659         {
9660           if (!htab->do_multi_toc && first_tlsld != NULL)
9661             {
9662               ent->is_indirect = TRUE;
9663               ent->got.ent = first_tlsld;
9664             }
9665           else
9666             {
9667               if (first_tlsld == NULL)
9668                 first_tlsld = ent;
9669               s = ppc64_elf_tdata (ibfd)->got;
9670               ent->got.offset = s->size;
9671               ent->owner = ibfd;
9672               s->size += 16;
9673               if (info->shared)
9674                 {
9675                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9676                   srel->size += sizeof (Elf64_External_Rela);
9677                 }
9678             }
9679         }
9680       else
9681         ent->got.offset = (bfd_vma) -1;
9682     }
9683
9684   /* We now have determined the sizes of the various dynamic sections.
9685      Allocate memory for them.  */
9686   relocs = FALSE;
9687   for (s = dynobj->sections; s != NULL; s = s->next)
9688     {
9689       if ((s->flags & SEC_LINKER_CREATED) == 0)
9690         continue;
9691
9692       if (s == htab->brlt || s == htab->relbrlt)
9693         /* These haven't been allocated yet;  don't strip.  */
9694         continue;
9695       else if (s == htab->got
9696                || s == htab->plt
9697                || s == htab->iplt
9698                || s == htab->glink
9699                || s == htab->dynbss)
9700         {
9701           /* Strip this section if we don't need it; see the
9702              comment below.  */
9703         }
9704       else if (s == htab->glink_eh_frame)
9705         {
9706           if (!bfd_is_abs_section (s->output_section))
9707             /* Not sized yet.  */
9708             continue;
9709         }
9710       else if (CONST_STRNEQ (s->name, ".rela"))
9711         {
9712           if (s->size != 0)
9713             {
9714               if (s != htab->relplt)
9715                 relocs = TRUE;
9716
9717               /* We use the reloc_count field as a counter if we need
9718                  to copy relocs into the output file.  */
9719               s->reloc_count = 0;
9720             }
9721         }
9722       else
9723         {
9724           /* It's not one of our sections, so don't allocate space.  */
9725           continue;
9726         }
9727
9728       if (s->size == 0)
9729         {
9730           /* If we don't need this section, strip it from the
9731              output file.  This is mostly to handle .rela.bss and
9732              .rela.plt.  We must create both sections in
9733              create_dynamic_sections, because they must be created
9734              before the linker maps input sections to output
9735              sections.  The linker does that before
9736              adjust_dynamic_symbol is called, and it is that
9737              function which decides whether anything needs to go
9738              into these sections.  */
9739           s->flags |= SEC_EXCLUDE;
9740           continue;
9741         }
9742
9743       if ((s->flags & SEC_HAS_CONTENTS) == 0)
9744         continue;
9745
9746       /* Allocate memory for the section contents.  We use bfd_zalloc
9747          here in case unused entries are not reclaimed before the
9748          section's contents are written out.  This should not happen,
9749          but this way if it does we get a R_PPC64_NONE reloc in .rela
9750          sections instead of garbage.
9751          We also rely on the section contents being zero when writing
9752          the GOT.  */
9753       s->contents = bfd_zalloc (dynobj, s->size);
9754       if (s->contents == NULL)
9755         return FALSE;
9756     }
9757
9758   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9759     {
9760       if (!is_ppc64_elf (ibfd))
9761         continue;
9762
9763       s = ppc64_elf_tdata (ibfd)->got;
9764       if (s != NULL && s != htab->got)
9765         {
9766           if (s->size == 0)
9767             s->flags |= SEC_EXCLUDE;
9768           else
9769             {
9770               s->contents = bfd_zalloc (ibfd, s->size);
9771               if (s->contents == NULL)
9772                 return FALSE;
9773             }
9774         }
9775       s = ppc64_elf_tdata (ibfd)->relgot;
9776       if (s != NULL)
9777         {
9778           if (s->size == 0)
9779             s->flags |= SEC_EXCLUDE;
9780           else
9781             {
9782               s->contents = bfd_zalloc (ibfd, s->size);
9783               if (s->contents == NULL)
9784                 return FALSE;
9785               relocs = TRUE;
9786               s->reloc_count = 0;
9787             }
9788         }
9789     }
9790
9791   if (htab->elf.dynamic_sections_created)
9792     {
9793       /* Add some entries to the .dynamic section.  We fill in the
9794          values later, in ppc64_elf_finish_dynamic_sections, but we
9795          must add the entries now so that we get the correct size for
9796          the .dynamic section.  The DT_DEBUG entry is filled in by the
9797          dynamic linker and used by the debugger.  */
9798 #define add_dynamic_entry(TAG, VAL) \
9799   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9800
9801       if (info->executable)
9802         {
9803           if (!add_dynamic_entry (DT_DEBUG, 0))
9804             return FALSE;
9805         }
9806
9807       if (htab->plt != NULL && htab->plt->size != 0)
9808         {
9809           if (!add_dynamic_entry (DT_PLTGOT, 0)
9810               || !add_dynamic_entry (DT_PLTRELSZ, 0)
9811               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9812               || !add_dynamic_entry (DT_JMPREL, 0)
9813               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9814             return FALSE;
9815         }
9816
9817       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
9818         {
9819           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9820               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9821             return FALSE;
9822         }
9823
9824       if (!htab->no_tls_get_addr_opt
9825           && htab->tls_get_addr_fd != NULL
9826           && htab->tls_get_addr_fd->elf.plt.plist != NULL
9827           && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9828         return FALSE;
9829
9830       if (relocs)
9831         {
9832           if (!add_dynamic_entry (DT_RELA, 0)
9833               || !add_dynamic_entry (DT_RELASZ, 0)
9834               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9835             return FALSE;
9836
9837           /* If any dynamic relocs apply to a read-only section,
9838              then we need a DT_TEXTREL entry.  */
9839           if ((info->flags & DF_TEXTREL) == 0)
9840             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
9841
9842           if ((info->flags & DF_TEXTREL) != 0)
9843             {
9844               if (!add_dynamic_entry (DT_TEXTREL, 0))
9845                 return FALSE;
9846             }
9847         }
9848     }
9849 #undef add_dynamic_entry
9850
9851   return TRUE;
9852 }
9853
9854 /* Determine the type of stub needed, if any, for a call.  */
9855
9856 static inline enum ppc_stub_type
9857 ppc_type_of_stub (asection *input_sec,
9858                   const Elf_Internal_Rela *rel,
9859                   struct ppc_link_hash_entry **hash,
9860                   struct plt_entry **plt_ent,
9861                   bfd_vma destination,
9862                   unsigned long local_off)
9863 {
9864   struct ppc_link_hash_entry *h = *hash;
9865   bfd_vma location;
9866   bfd_vma branch_offset;
9867   bfd_vma max_branch_offset;
9868   enum elf_ppc64_reloc_type r_type;
9869
9870   if (h != NULL)
9871     {
9872       struct plt_entry *ent;
9873       struct ppc_link_hash_entry *fdh = h;
9874       if (h->oh != NULL
9875           && h->oh->is_func_descriptor)
9876         {
9877           fdh = ppc_follow_link (h->oh);
9878           *hash = fdh;
9879         }
9880
9881       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9882         if (ent->addend == rel->r_addend
9883             && ent->plt.offset != (bfd_vma) -1)
9884           {
9885             *plt_ent = ent;
9886             return ppc_stub_plt_call;
9887           }
9888
9889       /* Here, we know we don't have a plt entry.  If we don't have a
9890          either a defined function descriptor or a defined entry symbol
9891          in a regular object file, then it is pointless trying to make
9892          any other type of stub.  */
9893       if (!is_static_defined (&fdh->elf)
9894           && !is_static_defined (&h->elf))
9895         return ppc_stub_none;
9896     }
9897   else if (elf_local_got_ents (input_sec->owner) != NULL)
9898     {
9899       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9900       struct plt_entry **local_plt = (struct plt_entry **)
9901         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9902       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9903
9904       if (local_plt[r_symndx] != NULL)
9905         {
9906           struct plt_entry *ent;
9907
9908           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9909             if (ent->addend == rel->r_addend
9910                 && ent->plt.offset != (bfd_vma) -1)
9911               {
9912                 *plt_ent = ent;
9913                 return ppc_stub_plt_call;
9914               }
9915         }
9916     }
9917
9918   /* Determine where the call point is.  */
9919   location = (input_sec->output_offset
9920               + input_sec->output_section->vma
9921               + rel->r_offset);
9922
9923   branch_offset = destination - location;
9924   r_type = ELF64_R_TYPE (rel->r_info);
9925
9926   /* Determine if a long branch stub is needed.  */
9927   max_branch_offset = 1 << 25;
9928   if (r_type != R_PPC64_REL24)
9929     max_branch_offset = 1 << 15;
9930
9931   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
9932     /* We need a stub.  Figure out whether a long_branch or plt_branch
9933        is needed later.  */
9934     return ppc_stub_long_branch;
9935
9936   return ppc_stub_none;
9937 }
9938
9939 /* With power7 weakly ordered memory model, it is possible for ld.so
9940    to update a plt entry in one thread and have another thread see a
9941    stale zero toc entry.  To avoid this we need some sort of acquire
9942    barrier in the call stub.  One solution is to make the load of the
9943    toc word seem to appear to depend on the load of the function entry
9944    word.  Another solution is to test for r2 being zero, and branch to
9945    the appropriate glink entry if so.
9946
9947    .    fake dep barrier        compare
9948    .    ld 12,xxx(2)            ld 12,xxx(2)
9949    .    mtctr 12                mtctr 12
9950    .    xor 11,12,12            ld 2,xxx+8(2)
9951    .    add 2,2,11              cmpldi 2,0
9952    .    ld 2,xxx+8(2)           bnectr+
9953    .    bctr                    b <glink_entry>
9954
9955    The solution involving the compare turns out to be faster, so
9956    that's what we use unless the branch won't reach.  */
9957
9958 #define ALWAYS_USE_FAKE_DEP 0
9959 #define ALWAYS_EMIT_R2SAVE 0
9960
9961 #define PPC_LO(v) ((v) & 0xffff)
9962 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9963 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9964
9965 static inline unsigned int
9966 plt_stub_size (struct ppc_link_hash_table *htab,
9967                struct ppc_stub_hash_entry *stub_entry,
9968                bfd_vma off)
9969 {
9970   unsigned size = 12;
9971
9972   if (ALWAYS_EMIT_R2SAVE
9973       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9974     size += 4;
9975   if (PPC_HA (off) != 0)
9976     size += 4;
9977   if (htab->opd_abi)
9978     {
9979       size += 4;
9980       if (htab->plt_static_chain)
9981         size += 4;
9982       if (htab->plt_thread_safe)
9983         size += 8;
9984       if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
9985         size += 4;
9986     }
9987   if (stub_entry->h != NULL
9988       && (stub_entry->h == htab->tls_get_addr_fd
9989           || stub_entry->h == htab->tls_get_addr)
9990       && !htab->no_tls_get_addr_opt)
9991     size += 13 * 4;
9992   return size;
9993 }
9994
9995 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
9996    then return the padding needed to do so.  */
9997 static inline unsigned int
9998 plt_stub_pad (struct ppc_link_hash_table *htab,
9999               struct ppc_stub_hash_entry *stub_entry,
10000               bfd_vma plt_off)
10001 {
10002   int stub_align = 1 << htab->plt_stub_align;
10003   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10004   bfd_vma stub_off = stub_entry->stub_sec->size;
10005
10006   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10007       > (stub_size & -stub_align))
10008     return stub_align - (stub_off & (stub_align - 1));
10009   return 0;
10010 }
10011
10012 /* Build a .plt call stub.  */
10013
10014 static inline bfd_byte *
10015 build_plt_stub (struct ppc_link_hash_table *htab,
10016                 struct ppc_stub_hash_entry *stub_entry,
10017                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10018 {
10019   bfd *obfd = htab->stub_bfd;
10020   bfd_boolean plt_load_toc = htab->opd_abi;
10021   bfd_boolean plt_static_chain = htab->plt_static_chain;
10022   bfd_boolean plt_thread_safe = htab->plt_thread_safe;
10023   bfd_boolean use_fake_dep = plt_thread_safe;
10024   bfd_vma cmp_branch_off = 0;
10025
10026   if (!ALWAYS_USE_FAKE_DEP
10027       && plt_load_toc
10028       && plt_thread_safe
10029       && !(stub_entry->h != NULL
10030            && (stub_entry->h == htab->tls_get_addr_fd
10031                || stub_entry->h == htab->tls_get_addr)
10032            && !htab->no_tls_get_addr_opt))
10033     {
10034       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10035       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10036                           / PLT_ENTRY_SIZE (htab));
10037       bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10038       bfd_vma to, from;
10039
10040       if (pltindex > 32768)
10041         glinkoff += (pltindex - 32768) * 4;
10042       to = (glinkoff
10043             + htab->glink->output_offset
10044             + htab->glink->output_section->vma);
10045       from = (p - stub_entry->stub_sec->contents
10046               + 4 * (ALWAYS_EMIT_R2SAVE
10047                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10048               + 4 * (PPC_HA (offset) != 0)
10049               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10050                      != PPC_HA (offset))
10051               + 4 * (plt_static_chain != 0)
10052               + 20
10053               + stub_entry->stub_sec->output_offset
10054               + stub_entry->stub_sec->output_section->vma);
10055       cmp_branch_off = to - from;
10056       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10057     }
10058
10059   if (PPC_HA (offset) != 0)
10060     {
10061       if (r != NULL)
10062         {
10063           if (ALWAYS_EMIT_R2SAVE
10064               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10065             r[0].r_offset += 4;
10066           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10067           r[1].r_offset = r[0].r_offset + 4;
10068           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10069           r[1].r_addend = r[0].r_addend;
10070           if (plt_load_toc)
10071             {
10072               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10073                 {
10074                   r[2].r_offset = r[1].r_offset + 4;
10075                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10076                   r[2].r_addend = r[0].r_addend;
10077                 }
10078               else
10079                 {
10080                   r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10081                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10082                   r[2].r_addend = r[0].r_addend + 8;
10083                   if (plt_static_chain)
10084                     {
10085                       r[3].r_offset = r[2].r_offset + 4;
10086                       r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10087                       r[3].r_addend = r[0].r_addend + 16;
10088                     }
10089                 }
10090             }
10091         }
10092       if (ALWAYS_EMIT_R2SAVE
10093           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10094         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10095       bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p),     p += 4;
10096       bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),      p += 4;
10097       if (plt_load_toc
10098           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10099         {
10100           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10101           offset = 0;
10102         }
10103       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10104       if (plt_load_toc)
10105         {
10106           if (use_fake_dep)
10107             {
10108               bfd_put_32 (obfd, XOR_R2_R12_R12, p),             p += 4;
10109               bfd_put_32 (obfd, ADD_R11_R11_R2, p),             p += 4;
10110             }
10111           bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10112           if (plt_static_chain)
10113             bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10114         }
10115     }
10116   else
10117     {
10118       if (r != NULL)
10119         {
10120           if (ALWAYS_EMIT_R2SAVE
10121               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10122             r[0].r_offset += 4;
10123           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10124           if (plt_load_toc)
10125             {
10126               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10127                 {
10128                   r[1].r_offset = r[0].r_offset + 4;
10129                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10130                   r[1].r_addend = r[0].r_addend;
10131                 }
10132               else
10133                 {
10134                   r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10135                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10136                   r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10137                   if (plt_static_chain)
10138                     {
10139                       r[2].r_offset = r[1].r_offset + 4;
10140                       r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10141                       r[2].r_addend = r[0].r_addend + 8;
10142                     }
10143                 }
10144             }
10145         }
10146       if (ALWAYS_EMIT_R2SAVE
10147           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10148         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10149       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
10150       if (plt_load_toc
10151           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10152         {
10153           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
10154           offset = 0;
10155         }
10156       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10157       if (plt_load_toc)
10158         {
10159           if (use_fake_dep)
10160             {
10161               bfd_put_32 (obfd, XOR_R11_R12_R12, p),            p += 4;
10162               bfd_put_32 (obfd, ADD_R2_R2_R11, p),              p += 4;
10163             }
10164           if (plt_static_chain)
10165             bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10166           bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10167         }
10168     }
10169   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10170     {
10171       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
10172       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
10173       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10174     }
10175   else
10176     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
10177   return p;
10178 }
10179
10180 /* Build a special .plt call stub for __tls_get_addr.  */
10181
10182 #define LD_R11_0R3      0xe9630000
10183 #define LD_R12_0R3      0xe9830000
10184 #define MR_R0_R3        0x7c601b78
10185 #define CMPDI_R11_0     0x2c2b0000
10186 #define ADD_R3_R12_R13  0x7c6c6a14
10187 #define BEQLR           0x4d820020
10188 #define MR_R3_R0        0x7c030378
10189 #define STD_R11_0R1     0xf9610000
10190 #define BCTRL           0x4e800421
10191 #define LD_R11_0R1      0xe9610000
10192 #define MTLR_R11        0x7d6803a6
10193
10194 static inline bfd_byte *
10195 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10196                          struct ppc_stub_hash_entry *stub_entry,
10197                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10198 {
10199   bfd *obfd = htab->stub_bfd;
10200
10201   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
10202   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
10203   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
10204   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
10205   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
10206   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
10207   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
10208   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
10209   bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10210
10211   if (r != NULL)
10212     r[0].r_offset += 9 * 4;
10213   p = build_plt_stub (htab, stub_entry, p, offset, r);
10214   bfd_put_32 (obfd, BCTRL, p - 4);
10215
10216   bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10217   bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p),     p += 4;
10218   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
10219   bfd_put_32 (obfd, BLR, p),                    p += 4;
10220
10221   return p;
10222 }
10223
10224 static Elf_Internal_Rela *
10225 get_relocs (asection *sec, int count)
10226 {
10227   Elf_Internal_Rela *relocs;
10228   struct bfd_elf_section_data *elfsec_data;
10229
10230   elfsec_data = elf_section_data (sec);
10231   relocs = elfsec_data->relocs;
10232   if (relocs == NULL)
10233     {
10234       bfd_size_type relsize;
10235       relsize = sec->reloc_count * sizeof (*relocs);
10236       relocs = bfd_alloc (sec->owner, relsize);
10237       if (relocs == NULL)
10238         return NULL;
10239       elfsec_data->relocs = relocs;
10240       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10241                                           sizeof (Elf_Internal_Shdr));
10242       if (elfsec_data->rela.hdr == NULL)
10243         return NULL;
10244       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10245                                         * sizeof (Elf64_External_Rela));
10246       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10247       sec->reloc_count = 0;
10248     }
10249   relocs += sec->reloc_count;
10250   sec->reloc_count += count;
10251   return relocs;
10252 }
10253
10254 static bfd_vma
10255 get_r2off (struct bfd_link_info *info,
10256            struct ppc_stub_hash_entry *stub_entry)
10257 {
10258   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10259   bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
10260
10261   if (r2off == 0)
10262     {
10263       /* Support linking -R objects.  Get the toc pointer from the
10264          opd entry.  */
10265       char buf[8];
10266       if (!htab->opd_abi)
10267         return r2off;
10268       asection *opd = stub_entry->h->elf.root.u.def.section;
10269       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10270
10271       if (strcmp (opd->name, ".opd") != 0
10272           || opd->reloc_count != 0)
10273         {
10274           info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10275                                   stub_entry->h->elf.root.root.string);
10276           bfd_set_error (bfd_error_bad_value);
10277           return 0;
10278         }
10279       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10280         return 0;
10281       r2off = bfd_get_64 (opd->owner, buf);
10282       r2off -= elf_gp (info->output_bfd);
10283     }
10284   r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
10285   return r2off;
10286 }
10287
10288 static bfd_boolean
10289 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10290 {
10291   struct ppc_stub_hash_entry *stub_entry;
10292   struct ppc_branch_hash_entry *br_entry;
10293   struct bfd_link_info *info;
10294   struct ppc_link_hash_table *htab;
10295   bfd_byte *loc;
10296   bfd_byte *p;
10297   bfd_vma dest, off;
10298   int size;
10299   Elf_Internal_Rela *r;
10300   asection *plt;
10301
10302   /* Massage our args to the form they really have.  */
10303   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10304   info = in_arg;
10305
10306   htab = ppc_hash_table (info);
10307   if (htab == NULL)
10308     return FALSE;
10309
10310   /* Make a note of the offset within the stubs for this entry.  */
10311   stub_entry->stub_offset = stub_entry->stub_sec->size;
10312   loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
10313
10314   htab->stub_count[stub_entry->stub_type - 1] += 1;
10315   switch (stub_entry->stub_type)
10316     {
10317     case ppc_stub_long_branch:
10318     case ppc_stub_long_branch_r2off:
10319       /* Branches are relative.  This is where we are going to.  */
10320       dest = (stub_entry->target_value
10321               + stub_entry->target_section->output_offset
10322               + stub_entry->target_section->output_section->vma);
10323       dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10324       off = dest;
10325
10326       /* And this is where we are coming from.  */
10327       off -= (stub_entry->stub_offset
10328               + stub_entry->stub_sec->output_offset
10329               + stub_entry->stub_sec->output_section->vma);
10330
10331       size = 4;
10332       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10333         {
10334           bfd_vma r2off = get_r2off (info, stub_entry);
10335
10336           if (r2off == 0)
10337             {
10338               htab->stub_error = TRUE;
10339               return FALSE;
10340             }
10341           bfd_put_32 (htab->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10342           loc += 4;
10343           size = 12;
10344           if (PPC_HA (r2off) != 0)
10345             {
10346               size = 16;
10347               bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10348               loc += 4;
10349             }
10350           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
10351           loc += 4;
10352           off -= size - 4;
10353         }
10354       bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
10355
10356       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10357         {
10358           info->callbacks->einfo
10359             (_("%P: long branch stub `%s' offset overflow\n"),
10360              stub_entry->root.string);
10361           htab->stub_error = TRUE;
10362           return FALSE;
10363         }
10364
10365       if (info->emitrelocations)
10366         {
10367           r = get_relocs (stub_entry->stub_sec, 1);
10368           if (r == NULL)
10369             return FALSE;
10370           r->r_offset = loc - stub_entry->stub_sec->contents;
10371           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10372           r->r_addend = dest;
10373           if (stub_entry->h != NULL)
10374             {
10375               struct elf_link_hash_entry **hashes;
10376               unsigned long symndx;
10377               struct ppc_link_hash_entry *h;
10378
10379               hashes = elf_sym_hashes (htab->stub_bfd);
10380               if (hashes == NULL)
10381                 {
10382                   bfd_size_type hsize;
10383
10384                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10385                   hashes = bfd_zalloc (htab->stub_bfd, hsize);
10386                   if (hashes == NULL)
10387                     return FALSE;
10388                   elf_sym_hashes (htab->stub_bfd) = hashes;
10389                   htab->stub_globals = 1;
10390                 }
10391               symndx = htab->stub_globals++;
10392               h = stub_entry->h;
10393               hashes[symndx] = &h->elf;
10394               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10395               if (h->oh != NULL && h->oh->is_func)
10396                 h = ppc_follow_link (h->oh);
10397               if (h->elf.root.u.def.section != stub_entry->target_section)
10398                 /* H is an opd symbol.  The addend must be zero.  */
10399                 r->r_addend = 0;
10400               else
10401                 {
10402                   off = (h->elf.root.u.def.value
10403                          + h->elf.root.u.def.section->output_offset
10404                          + h->elf.root.u.def.section->output_section->vma);
10405                   r->r_addend -= off;
10406                 }
10407             }
10408         }
10409       break;
10410
10411     case ppc_stub_plt_branch:
10412     case ppc_stub_plt_branch_r2off:
10413       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10414                                          stub_entry->root.string + 9,
10415                                          FALSE, FALSE);
10416       if (br_entry == NULL)
10417         {
10418           info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10419                                   stub_entry->root.string);
10420           htab->stub_error = TRUE;
10421           return FALSE;
10422         }
10423
10424       dest = (stub_entry->target_value
10425               + stub_entry->target_section->output_offset
10426               + stub_entry->target_section->output_section->vma);
10427       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10428         dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10429
10430       bfd_put_64 (htab->brlt->owner, dest,
10431                   htab->brlt->contents + br_entry->offset);
10432
10433       if (br_entry->iter == htab->stub_iteration)
10434         {
10435           br_entry->iter = 0;
10436
10437           if (htab->relbrlt != NULL)
10438             {
10439               /* Create a reloc for the branch lookup table entry.  */
10440               Elf_Internal_Rela rela;
10441               bfd_byte *rl;
10442
10443               rela.r_offset = (br_entry->offset
10444                                + htab->brlt->output_offset
10445                                + htab->brlt->output_section->vma);
10446               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10447               rela.r_addend = dest;
10448
10449               rl = htab->relbrlt->contents;
10450               rl += (htab->relbrlt->reloc_count++
10451                      * sizeof (Elf64_External_Rela));
10452               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10453             }
10454           else if (info->emitrelocations)
10455             {
10456               r = get_relocs (htab->brlt, 1);
10457               if (r == NULL)
10458                 return FALSE;
10459               /* brlt, being SEC_LINKER_CREATED does not go through the
10460                  normal reloc processing.  Symbols and offsets are not
10461                  translated from input file to output file form, so
10462                  set up the offset per the output file.  */
10463               r->r_offset = (br_entry->offset
10464                              + htab->brlt->output_offset
10465                              + htab->brlt->output_section->vma);
10466               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10467               r->r_addend = dest;
10468             }
10469         }
10470
10471       dest = (br_entry->offset
10472               + htab->brlt->output_offset
10473               + htab->brlt->output_section->vma);
10474
10475       off = (dest
10476              - elf_gp (htab->brlt->output_section->owner)
10477              - htab->stub_group[stub_entry->id_sec->id].toc_off);
10478
10479       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10480         {
10481           info->callbacks->einfo
10482             (_("%P: linkage table error against `%T'\n"),
10483              stub_entry->root.string);
10484           bfd_set_error (bfd_error_bad_value);
10485           htab->stub_error = TRUE;
10486           return FALSE;
10487         }
10488
10489       if (info->emitrelocations)
10490         {
10491           r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
10492           if (r == NULL)
10493             return FALSE;
10494           r[0].r_offset = loc - stub_entry->stub_sec->contents;
10495           if (bfd_big_endian (info->output_bfd))
10496             r[0].r_offset += 2;
10497           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off
10498               && htab->opd_abi)
10499             r[0].r_offset += 4;
10500           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10501           r[0].r_addend = dest;
10502           if (PPC_HA (off) != 0)
10503             {
10504               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10505               r[1].r_offset = r[0].r_offset + 4;
10506               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10507               r[1].r_addend = r[0].r_addend;
10508             }
10509         }
10510
10511       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off
10512           || !htab->opd_abi)
10513         {
10514           if (PPC_HA (off) != 0)
10515             {
10516               size = 16;
10517               bfd_put_32 (htab->stub_bfd, ADDIS_R11_R2 | PPC_HA (off), loc);
10518               loc += 4;
10519               bfd_put_32 (htab->stub_bfd, LD_R12_0R11 | PPC_LO (off), loc);
10520             }
10521           else
10522             {
10523               size = 12;
10524               bfd_put_32 (htab->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
10525             }
10526         }
10527       else
10528         {
10529           bfd_vma r2off = get_r2off (info, stub_entry);
10530
10531           if (r2off == 0)
10532             {
10533               htab->stub_error = TRUE;
10534               return FALSE;
10535             }
10536
10537           bfd_put_32 (htab->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10538           loc += 4;
10539           size = 20;
10540           if (PPC_HA (off) != 0)
10541             {
10542               size += 4;
10543               bfd_put_32 (htab->stub_bfd, ADDIS_R11_R2 | PPC_HA (off), loc);
10544               loc += 4;
10545               bfd_put_32 (htab->stub_bfd, LD_R12_0R11 | PPC_LO (off), loc);
10546               loc += 4;
10547             }
10548           else
10549             {
10550               bfd_put_32 (htab->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
10551               loc += 4;
10552             }
10553
10554           if (PPC_HA (r2off) != 0)
10555             {
10556               size += 4;
10557               bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10558               loc += 4;
10559             }
10560           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
10561         }
10562       loc += 4;
10563       bfd_put_32 (htab->stub_bfd, MTCTR_R12, loc);
10564       loc += 4;
10565       bfd_put_32 (htab->stub_bfd, BCTR, loc);
10566       break;
10567
10568     case ppc_stub_plt_call:
10569     case ppc_stub_plt_call_r2save:
10570       if (stub_entry->h != NULL
10571           && stub_entry->h->is_func_descriptor
10572           && stub_entry->h->oh != NULL)
10573         {
10574           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10575
10576           /* If the old-ABI "dot-symbol" is undefined make it weak so
10577              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
10578              FIXME: We used to define the symbol on one of the call
10579              stubs instead, which is why we test symbol section id
10580              against htab->top_id in various places.  Likely all
10581              these checks could now disappear.  */
10582           if (fh->elf.root.type == bfd_link_hash_undefined)
10583             fh->elf.root.type = bfd_link_hash_undefweak;
10584           /* Stop undo_symbol_twiddle changing it back to undefined.  */
10585           fh->was_undefined = 0;
10586         }
10587
10588       /* Now build the stub.  */
10589       dest = stub_entry->plt_ent->plt.offset & ~1;
10590       if (dest >= (bfd_vma) -2)
10591         abort ();
10592
10593       plt = htab->plt;
10594       if (!htab->elf.dynamic_sections_created
10595           || stub_entry->h == NULL
10596           || stub_entry->h->elf.dynindx == -1)
10597         plt = htab->iplt;
10598
10599       dest += plt->output_offset + plt->output_section->vma;
10600
10601       if (stub_entry->h == NULL
10602           && (stub_entry->plt_ent->plt.offset & 1) == 0)
10603         {
10604           Elf_Internal_Rela rela;
10605           bfd_byte *rl;
10606
10607           rela.r_offset = dest;
10608           if (htab->opd_abi)
10609             rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
10610           else
10611             rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
10612           rela.r_addend = (stub_entry->target_value
10613                            + stub_entry->target_section->output_offset
10614                            + stub_entry->target_section->output_section->vma);
10615
10616           rl = (htab->reliplt->contents
10617                 + (htab->reliplt->reloc_count++
10618                    * sizeof (Elf64_External_Rela)));
10619           bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
10620           stub_entry->plt_ent->plt.offset |= 1;
10621         }
10622
10623       off = (dest
10624              - elf_gp (plt->output_section->owner)
10625              - htab->stub_group[stub_entry->id_sec->id].toc_off);
10626
10627       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10628         {
10629           info->callbacks->einfo
10630             (_("%P: linkage table error against `%T'\n"),
10631              stub_entry->h != NULL
10632              ? stub_entry->h->elf.root.root.string
10633              : "<local sym>");
10634           bfd_set_error (bfd_error_bad_value);
10635           htab->stub_error = TRUE;
10636           return FALSE;
10637         }
10638
10639       if (htab->plt_stub_align != 0)
10640         {
10641           unsigned pad = plt_stub_pad (htab, stub_entry, off);
10642
10643           stub_entry->stub_sec->size += pad;
10644           stub_entry->stub_offset = stub_entry->stub_sec->size;
10645           loc += pad;
10646         }
10647
10648       r = NULL;
10649       if (info->emitrelocations)
10650         {
10651           r = get_relocs (stub_entry->stub_sec,
10652                           (2
10653                            + (PPC_HA (off) != 0)
10654                            + (htab->plt_static_chain
10655                               && PPC_HA (off + 16) == PPC_HA (off))));
10656           if (r == NULL)
10657             return FALSE;
10658           r[0].r_offset = loc - stub_entry->stub_sec->contents;
10659           if (bfd_big_endian (info->output_bfd))
10660             r[0].r_offset += 2;
10661           r[0].r_addend = dest;
10662         }
10663       if (stub_entry->h != NULL
10664           && (stub_entry->h == htab->tls_get_addr_fd
10665               || stub_entry->h == htab->tls_get_addr)
10666           && !htab->no_tls_get_addr_opt)
10667         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
10668       else
10669         p = build_plt_stub (htab, stub_entry, loc, off, r);
10670       size = p - loc;
10671       break;
10672
10673     default:
10674       BFD_FAIL ();
10675       return FALSE;
10676     }
10677
10678   stub_entry->stub_sec->size += size;
10679
10680   if (htab->emit_stub_syms)
10681     {
10682       struct elf_link_hash_entry *h;
10683       size_t len1, len2;
10684       char *name;
10685       const char *const stub_str[] = { "long_branch",
10686                                        "long_branch_r2off",
10687                                        "plt_branch",
10688                                        "plt_branch_r2off",
10689                                        "plt_call",
10690                                        "plt_call" };
10691
10692       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10693       len2 = strlen (stub_entry->root.string);
10694       name = bfd_malloc (len1 + len2 + 2);
10695       if (name == NULL)
10696         return FALSE;
10697       memcpy (name, stub_entry->root.string, 9);
10698       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10699       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10700       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
10701       if (h == NULL)
10702         return FALSE;
10703       if (h->root.type == bfd_link_hash_new)
10704         {
10705           h->root.type = bfd_link_hash_defined;
10706           h->root.u.def.section = stub_entry->stub_sec;
10707           h->root.u.def.value = stub_entry->stub_offset;
10708           h->ref_regular = 1;
10709           h->def_regular = 1;
10710           h->ref_regular_nonweak = 1;
10711           h->forced_local = 1;
10712           h->non_elf = 0;
10713         }
10714     }
10715
10716   return TRUE;
10717 }
10718
10719 /* As above, but don't actually build the stub.  Just bump offset so
10720    we know stub section sizes, and select plt_branch stubs where
10721    long_branch stubs won't do.  */
10722
10723 static bfd_boolean
10724 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10725 {
10726   struct ppc_stub_hash_entry *stub_entry;
10727   struct bfd_link_info *info;
10728   struct ppc_link_hash_table *htab;
10729   bfd_vma off;
10730   int size;
10731
10732   /* Massage our args to the form they really have.  */
10733   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10734   info = in_arg;
10735
10736   htab = ppc_hash_table (info);
10737   if (htab == NULL)
10738     return FALSE;
10739
10740   if (stub_entry->stub_type == ppc_stub_plt_call
10741       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10742     {
10743       asection *plt;
10744       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
10745       if (off >= (bfd_vma) -2)
10746         abort ();
10747       plt = htab->plt;
10748       if (!htab->elf.dynamic_sections_created
10749           || stub_entry->h == NULL
10750           || stub_entry->h->elf.dynindx == -1)
10751         plt = htab->iplt;
10752       off += (plt->output_offset
10753               + plt->output_section->vma
10754               - elf_gp (plt->output_section->owner)
10755               - htab->stub_group[stub_entry->id_sec->id].toc_off);
10756
10757       size = plt_stub_size (htab, stub_entry, off);
10758       if (htab->plt_stub_align)
10759         size += plt_stub_pad (htab, stub_entry, off);
10760       if (info->emitrelocations)
10761         {
10762           stub_entry->stub_sec->reloc_count
10763             += ((PPC_HA (off) != 0)
10764                 + (htab->opd_abi
10765                    ? 2 + (htab->plt_static_chain
10766                           && PPC_HA (off + 16) == PPC_HA (off))
10767                    : 1));
10768           stub_entry->stub_sec->flags |= SEC_RELOC;
10769         }
10770     }
10771   else
10772     {
10773       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10774          variants.  */
10775       bfd_vma r2off = 0;
10776       bfd_vma local_off = 0;
10777
10778       off = (stub_entry->target_value
10779              + stub_entry->target_section->output_offset
10780              + stub_entry->target_section->output_section->vma);
10781       off -= (stub_entry->stub_sec->size
10782               + stub_entry->stub_sec->output_offset
10783               + stub_entry->stub_sec->output_section->vma);
10784
10785       /* Reset the stub type from the plt variant in case we now
10786          can reach with a shorter stub.  */
10787       if (stub_entry->stub_type >= ppc_stub_plt_branch)
10788         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10789
10790       size = 4;
10791       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10792         {
10793           r2off = get_r2off (info, stub_entry);
10794           if (r2off == 0 && htab->opd_abi)
10795             {
10796               htab->stub_error = TRUE;
10797               return FALSE;
10798             }
10799           size = 12;
10800           if (PPC_HA (r2off) != 0)
10801             size = 16;
10802           off -= size - 4;
10803         }
10804
10805       local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10806
10807       /* If the branch offset if too big, use a ppc_stub_plt_branch.
10808          Do the same for -R objects without function descriptors.  */
10809       if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
10810           || (stub_entry->stub_type == ppc_stub_long_branch_r2off
10811               && r2off == 0))
10812         {
10813           struct ppc_branch_hash_entry *br_entry;
10814
10815           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10816                                              stub_entry->root.string + 9,
10817                                              TRUE, FALSE);
10818           if (br_entry == NULL)
10819             {
10820               info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
10821                                       stub_entry->root.string);
10822               htab->stub_error = TRUE;
10823               return FALSE;
10824             }
10825
10826           if (br_entry->iter != htab->stub_iteration)
10827             {
10828               br_entry->iter = htab->stub_iteration;
10829               br_entry->offset = htab->brlt->size;
10830               htab->brlt->size += 8;
10831
10832               if (htab->relbrlt != NULL)
10833                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10834               else if (info->emitrelocations)
10835                 {
10836                   htab->brlt->reloc_count += 1;
10837                   htab->brlt->flags |= SEC_RELOC;
10838                 }
10839             }
10840
10841           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
10842           off = (br_entry->offset
10843                  + htab->brlt->output_offset
10844                  + htab->brlt->output_section->vma
10845                  - elf_gp (htab->brlt->output_section->owner)
10846                  - htab->stub_group[stub_entry->id_sec->id].toc_off);
10847
10848           if (info->emitrelocations)
10849             {
10850               stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10851               stub_entry->stub_sec->flags |= SEC_RELOC;
10852             }
10853
10854           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off
10855               || !htab->opd_abi)
10856             {
10857               size = 12;
10858               if (PPC_HA (off) != 0)
10859                 size = 16;
10860             }
10861           else
10862             {
10863               size = 20;
10864               if (PPC_HA (off) != 0)
10865                 size += 4;
10866
10867               if (PPC_HA (r2off) != 0)
10868                 size += 4;
10869             }
10870         }
10871       else if (info->emitrelocations)
10872         {
10873           stub_entry->stub_sec->reloc_count += 1;
10874           stub_entry->stub_sec->flags |= SEC_RELOC;
10875         }
10876     }
10877
10878   stub_entry->stub_sec->size += size;
10879   return TRUE;
10880 }
10881
10882 /* Set up various things so that we can make a list of input sections
10883    for each output section included in the link.  Returns -1 on error,
10884    0 when no stubs will be needed, and 1 on success.  */
10885
10886 int
10887 ppc64_elf_setup_section_lists
10888   (struct bfd_link_info *info,
10889    asection *(*add_stub_section) (const char *, asection *),
10890    void (*layout_sections_again) (void))
10891 {
10892   bfd *input_bfd;
10893   int top_id, top_index, id;
10894   asection *section;
10895   asection **input_list;
10896   bfd_size_type amt;
10897   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10898
10899   if (htab == NULL)
10900     return -1;
10901   /* Stash our params away.  */
10902   htab->add_stub_section = add_stub_section;
10903   htab->layout_sections_again = layout_sections_again;
10904
10905   /* Find the top input section id.  */
10906   for (input_bfd = info->input_bfds, top_id = 3;
10907        input_bfd != NULL;
10908        input_bfd = input_bfd->link_next)
10909     {
10910       for (section = input_bfd->sections;
10911            section != NULL;
10912            section = section->next)
10913         {
10914           if (top_id < section->id)
10915             top_id = section->id;
10916         }
10917     }
10918
10919   htab->top_id = top_id;
10920   amt = sizeof (struct map_stub) * (top_id + 1);
10921   htab->stub_group = bfd_zmalloc (amt);
10922   if (htab->stub_group == NULL)
10923     return -1;
10924
10925   /* Set toc_off for com, und, abs and ind sections.  */
10926   for (id = 0; id < 3; id++)
10927     htab->stub_group[id].toc_off = TOC_BASE_OFF;
10928
10929   /* We can't use output_bfd->section_count here to find the top output
10930      section index as some sections may have been removed, and
10931      strip_excluded_output_sections doesn't renumber the indices.  */
10932   for (section = info->output_bfd->sections, top_index = 0;
10933        section != NULL;
10934        section = section->next)
10935     {
10936       if (top_index < section->index)
10937         top_index = section->index;
10938     }
10939
10940   htab->top_index = top_index;
10941   amt = sizeof (asection *) * (top_index + 1);
10942   input_list = bfd_zmalloc (amt);
10943   htab->input_list = input_list;
10944   if (input_list == NULL)
10945     return -1;
10946
10947   return 1;
10948 }
10949
10950 /* Set up for first pass at multitoc partitioning.  */
10951
10952 void
10953 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10954 {
10955   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10956
10957   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
10958   htab->toc_bfd = NULL;
10959   htab->toc_first_sec = NULL;
10960 }
10961
10962 /* The linker repeatedly calls this function for each TOC input section
10963    and linker generated GOT section.  Group input bfds such that the toc
10964    within a group is less than 64k in size.  */
10965
10966 bfd_boolean
10967 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10968 {
10969   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10970   bfd_vma addr, off, limit;
10971
10972   if (htab == NULL)
10973     return FALSE;
10974
10975   if (!htab->second_toc_pass)
10976     {
10977       /* Keep track of the first .toc or .got section for this input bfd.  */
10978       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
10979
10980       if (new_bfd)
10981         {
10982           htab->toc_bfd = isec->owner;
10983           htab->toc_first_sec = isec;
10984         }
10985
10986       addr = isec->output_offset + isec->output_section->vma;
10987       off = addr - htab->toc_curr;
10988       limit = 0x80008000;
10989       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10990         limit = 0x10000;
10991       if (off + isec->size > limit)
10992         {
10993           addr = (htab->toc_first_sec->output_offset
10994                   + htab->toc_first_sec->output_section->vma);
10995           htab->toc_curr = addr;
10996         }
10997
10998       /* toc_curr is the base address of this toc group.  Set elf_gp
10999          for the input section to be the offset relative to the
11000          output toc base plus 0x8000.  Making the input elf_gp an
11001          offset allows us to move the toc as a whole without
11002          recalculating input elf_gp.  */
11003       off = htab->toc_curr - elf_gp (isec->output_section->owner);
11004       off += TOC_BASE_OFF;
11005
11006       /* Die if someone uses a linker script that doesn't keep input
11007          file .toc and .got together.  */
11008       if (new_bfd
11009           && elf_gp (isec->owner) != 0
11010           && elf_gp (isec->owner) != off)
11011         return FALSE;
11012
11013       elf_gp (isec->owner) = off;
11014       return TRUE;
11015     }
11016
11017   /* During the second pass toc_first_sec points to the start of
11018      a toc group, and toc_curr is used to track the old elf_gp.
11019      We use toc_bfd to ensure we only look at each bfd once.  */
11020   if (htab->toc_bfd == isec->owner)
11021     return TRUE;
11022   htab->toc_bfd = isec->owner;
11023
11024   if (htab->toc_first_sec == NULL
11025       || htab->toc_curr != elf_gp (isec->owner))
11026     {
11027       htab->toc_curr = elf_gp (isec->owner);
11028       htab->toc_first_sec = isec;
11029     }
11030   addr = (htab->toc_first_sec->output_offset
11031           + htab->toc_first_sec->output_section->vma);
11032   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11033   elf_gp (isec->owner) = off;
11034
11035   return TRUE;
11036 }
11037
11038 /* Called via elf_link_hash_traverse to merge GOT entries for global
11039    symbol H.  */
11040
11041 static bfd_boolean
11042 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11043 {
11044   if (h->root.type == bfd_link_hash_indirect)
11045     return TRUE;
11046
11047   merge_got_entries (&h->got.glist);
11048
11049   return TRUE;
11050 }
11051
11052 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11053    symbol H.  */
11054
11055 static bfd_boolean
11056 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11057 {
11058   struct got_entry *gent;
11059
11060   if (h->root.type == bfd_link_hash_indirect)
11061     return TRUE;
11062
11063   for (gent = h->got.glist; gent != NULL; gent = gent->next)
11064     if (!gent->is_indirect)
11065       allocate_got (h, (struct bfd_link_info *) inf, gent);
11066   return TRUE;
11067 }
11068
11069 /* Called on the first multitoc pass after the last call to
11070    ppc64_elf_next_toc_section.  This function removes duplicate GOT
11071    entries.  */
11072
11073 bfd_boolean
11074 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11075 {
11076   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11077   struct bfd *ibfd, *ibfd2;
11078   bfd_boolean done_something;
11079
11080   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11081
11082   if (!htab->do_multi_toc)
11083     return FALSE;
11084
11085   /* Merge global sym got entries within a toc group.  */
11086   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11087
11088   /* And tlsld_got.  */
11089   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11090     {
11091       struct got_entry *ent, *ent2;
11092
11093       if (!is_ppc64_elf (ibfd))
11094         continue;
11095
11096       ent = ppc64_tlsld_got (ibfd);
11097       if (!ent->is_indirect
11098           && ent->got.offset != (bfd_vma) -1)
11099         {
11100           for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
11101             {
11102               if (!is_ppc64_elf (ibfd2))
11103                 continue;
11104
11105               ent2 = ppc64_tlsld_got (ibfd2);
11106               if (!ent2->is_indirect
11107                   && ent2->got.offset != (bfd_vma) -1
11108                   && elf_gp (ibfd2) == elf_gp (ibfd))
11109                 {
11110                   ent2->is_indirect = TRUE;
11111                   ent2->got.ent = ent;
11112                 }
11113             }
11114         }
11115     }
11116
11117   /* Zap sizes of got sections.  */
11118   htab->reliplt->rawsize = htab->reliplt->size;
11119   htab->reliplt->size -= htab->got_reli_size;
11120   htab->got_reli_size = 0;
11121
11122   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11123     {
11124       asection *got, *relgot;
11125
11126       if (!is_ppc64_elf (ibfd))
11127         continue;
11128
11129       got = ppc64_elf_tdata (ibfd)->got;
11130       if (got != NULL)
11131         {
11132           got->rawsize = got->size;
11133           got->size = 0;
11134           relgot = ppc64_elf_tdata (ibfd)->relgot;
11135           relgot->rawsize = relgot->size;
11136           relgot->size = 0;
11137         }
11138     }
11139
11140   /* Now reallocate the got, local syms first.  We don't need to
11141      allocate section contents again since we never increase size.  */
11142   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11143     {
11144       struct got_entry **lgot_ents;
11145       struct got_entry **end_lgot_ents;
11146       struct plt_entry **local_plt;
11147       struct plt_entry **end_local_plt;
11148       unsigned char *lgot_masks;
11149       bfd_size_type locsymcount;
11150       Elf_Internal_Shdr *symtab_hdr;
11151       asection *s;
11152
11153       if (!is_ppc64_elf (ibfd))
11154         continue;
11155
11156       lgot_ents = elf_local_got_ents (ibfd);
11157       if (!lgot_ents)
11158         continue;
11159
11160       symtab_hdr = &elf_symtab_hdr (ibfd);
11161       locsymcount = symtab_hdr->sh_info;
11162       end_lgot_ents = lgot_ents + locsymcount;
11163       local_plt = (struct plt_entry **) end_lgot_ents;
11164       end_local_plt = local_plt + locsymcount;
11165       lgot_masks = (unsigned char *) end_local_plt;
11166       s = ppc64_elf_tdata (ibfd)->got;
11167       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11168         {
11169           struct got_entry *ent;
11170
11171           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11172             {
11173               unsigned int ent_size = 8;
11174               unsigned int rel_size = sizeof (Elf64_External_Rela);
11175
11176               ent->got.offset = s->size;
11177               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11178                 {
11179                   ent_size *= 2;
11180                   rel_size *= 2;
11181                 }
11182               s->size += ent_size;
11183               if ((*lgot_masks & PLT_IFUNC) != 0)
11184                 {
11185                   htab->reliplt->size += rel_size;
11186                   htab->got_reli_size += rel_size;
11187                 }
11188               else if (info->shared)
11189                 {
11190                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11191                   srel->size += rel_size;
11192                 }
11193             }
11194         }
11195     }
11196
11197   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11198
11199   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11200     {
11201       struct got_entry *ent;
11202
11203       if (!is_ppc64_elf (ibfd))
11204         continue;
11205
11206       ent = ppc64_tlsld_got (ibfd);
11207       if (!ent->is_indirect
11208           && ent->got.offset != (bfd_vma) -1)
11209         {
11210           asection *s = ppc64_elf_tdata (ibfd)->got;
11211           ent->got.offset = s->size;
11212           s->size += 16;
11213           if (info->shared)
11214             {
11215               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11216               srel->size += sizeof (Elf64_External_Rela);
11217             }
11218         }
11219     }
11220
11221   done_something = htab->reliplt->rawsize != htab->reliplt->size;
11222   if (!done_something)
11223     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11224       {
11225         asection *got;
11226
11227         if (!is_ppc64_elf (ibfd))
11228           continue;
11229
11230         got = ppc64_elf_tdata (ibfd)->got;
11231         if (got != NULL)
11232           {
11233             done_something = got->rawsize != got->size;
11234             if (done_something)
11235               break;
11236           }
11237       }
11238
11239   if (done_something)
11240     (*htab->layout_sections_again) ();
11241
11242   /* Set up for second pass over toc sections to recalculate elf_gp
11243      on input sections.  */
11244   htab->toc_bfd = NULL;
11245   htab->toc_first_sec = NULL;
11246   htab->second_toc_pass = TRUE;
11247   return done_something;
11248 }
11249
11250 /* Called after second pass of multitoc partitioning.  */
11251
11252 void
11253 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11254 {
11255   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11256
11257   /* After the second pass, toc_curr tracks the TOC offset used
11258      for code sections below in ppc64_elf_next_input_section.  */
11259   htab->toc_curr = TOC_BASE_OFF;
11260 }
11261
11262 /* No toc references were found in ISEC.  If the code in ISEC makes no
11263    calls, then there's no need to use toc adjusting stubs when branching
11264    into ISEC.  Actually, indirect calls from ISEC are OK as they will
11265    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
11266    needed, and 2 if a cyclical call-graph was found but no other reason
11267    for a stub was detected.  If called from the top level, a return of
11268    2 means the same as a return of 0.  */
11269
11270 static int
11271 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11272 {
11273   int ret;
11274
11275   /* Mark this section as checked.  */
11276   isec->call_check_done = 1;
11277
11278   /* We know none of our code bearing sections will need toc stubs.  */
11279   if ((isec->flags & SEC_LINKER_CREATED) != 0)
11280     return 0;
11281
11282   if (isec->size == 0)
11283     return 0;
11284
11285   if (isec->output_section == NULL)
11286     return 0;
11287
11288   ret = 0;
11289   if (isec->reloc_count != 0)
11290     {
11291       Elf_Internal_Rela *relstart, *rel;
11292       Elf_Internal_Sym *local_syms;
11293       struct ppc_link_hash_table *htab;
11294
11295       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11296                                             info->keep_memory);
11297       if (relstart == NULL)
11298         return -1;
11299
11300       /* Look for branches to outside of this section.  */
11301       local_syms = NULL;
11302       htab = ppc_hash_table (info);
11303       if (htab == NULL)
11304         return -1;
11305
11306       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11307         {
11308           enum elf_ppc64_reloc_type r_type;
11309           unsigned long r_symndx;
11310           struct elf_link_hash_entry *h;
11311           struct ppc_link_hash_entry *eh;
11312           Elf_Internal_Sym *sym;
11313           asection *sym_sec;
11314           struct _opd_sec_data *opd;
11315           bfd_vma sym_value;
11316           bfd_vma dest;
11317
11318           r_type = ELF64_R_TYPE (rel->r_info);
11319           if (r_type != R_PPC64_REL24
11320               && r_type != R_PPC64_REL14
11321               && r_type != R_PPC64_REL14_BRTAKEN
11322               && r_type != R_PPC64_REL14_BRNTAKEN)
11323             continue;
11324
11325           r_symndx = ELF64_R_SYM (rel->r_info);
11326           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11327                           isec->owner))
11328             {
11329               ret = -1;
11330               break;
11331             }
11332
11333           /* Calls to dynamic lib functions go through a plt call stub
11334              that uses r2.  */
11335           eh = (struct ppc_link_hash_entry *) h;
11336           if (eh != NULL
11337               && (eh->elf.plt.plist != NULL
11338                   || (eh->oh != NULL
11339                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11340             {
11341               ret = 1;
11342               break;
11343             }
11344
11345           if (sym_sec == NULL)
11346             /* Ignore other undefined symbols.  */
11347             continue;
11348
11349           /* Assume branches to other sections not included in the
11350              link need stubs too, to cover -R and absolute syms.  */
11351           if (sym_sec->output_section == NULL)
11352             {
11353               ret = 1;
11354               break;
11355             }
11356
11357           if (h == NULL)
11358             sym_value = sym->st_value;
11359           else
11360             {
11361               if (h->root.type != bfd_link_hash_defined
11362                   && h->root.type != bfd_link_hash_defweak)
11363                 abort ();
11364               sym_value = h->root.u.def.value;
11365             }
11366           sym_value += rel->r_addend;
11367
11368           /* If this branch reloc uses an opd sym, find the code section.  */
11369           opd = get_opd_info (sym_sec);
11370           if (opd != NULL)
11371             {
11372               if (h == NULL && opd->adjust != NULL)
11373                 {
11374                   long adjust;
11375
11376                   adjust = opd->adjust[sym->st_value / 8];
11377                   if (adjust == -1)
11378                     /* Assume deleted functions won't ever be called.  */
11379                     continue;
11380                   sym_value += adjust;
11381                 }
11382
11383               dest = opd_entry_value (sym_sec, sym_value,
11384                                       &sym_sec, NULL, FALSE);
11385               if (dest == (bfd_vma) -1)
11386                 continue;
11387             }
11388           else
11389             dest = (sym_value
11390                     + sym_sec->output_offset
11391                     + sym_sec->output_section->vma);
11392
11393           /* Ignore branch to self.  */
11394           if (sym_sec == isec)
11395             continue;
11396
11397           /* If the called function uses the toc, we need a stub.  */
11398           if (sym_sec->has_toc_reloc
11399               || sym_sec->makes_toc_func_call)
11400             {
11401               ret = 1;
11402               break;
11403             }
11404
11405           /* Assume any branch that needs a long branch stub might in fact
11406              need a plt_branch stub.  A plt_branch stub uses r2.  */
11407           else if (dest - (isec->output_offset
11408                            + isec->output_section->vma
11409                            + rel->r_offset) + (1 << 25)
11410                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11411                                                              ? h->other
11412                                                              : sym->st_other))
11413             {
11414               ret = 1;
11415               break;
11416             }
11417
11418           /* If calling back to a section in the process of being
11419              tested, we can't say for sure that no toc adjusting stubs
11420              are needed, so don't return zero.  */
11421           else if (sym_sec->call_check_in_progress)
11422             ret = 2;
11423
11424           /* Branches to another section that itself doesn't have any TOC
11425              references are OK.  Recursively call ourselves to check.  */
11426           else if (!sym_sec->call_check_done)
11427             {
11428               int recur;
11429
11430               /* Mark current section as indeterminate, so that other
11431                  sections that call back to current won't be marked as
11432                  known.  */
11433               isec->call_check_in_progress = 1;
11434               recur = toc_adjusting_stub_needed (info, sym_sec);
11435               isec->call_check_in_progress = 0;
11436
11437               if (recur != 0)
11438                 {
11439                   ret = recur;
11440                   if (recur != 2)
11441                     break;
11442                 }
11443             }
11444         }
11445
11446       if (local_syms != NULL
11447           && (elf_symtab_hdr (isec->owner).contents
11448               != (unsigned char *) local_syms))
11449         free (local_syms);
11450       if (elf_section_data (isec)->relocs != relstart)
11451         free (relstart);
11452     }
11453
11454   if ((ret & 1) == 0
11455       && isec->map_head.s != NULL
11456       && (strcmp (isec->output_section->name, ".init") == 0
11457           || strcmp (isec->output_section->name, ".fini") == 0))
11458     {
11459       if (isec->map_head.s->has_toc_reloc
11460           || isec->map_head.s->makes_toc_func_call)
11461         ret = 1;
11462       else if (!isec->map_head.s->call_check_done)
11463         {
11464           int recur;
11465           isec->call_check_in_progress = 1;
11466           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11467           isec->call_check_in_progress = 0;
11468           if (recur != 0)
11469             ret = recur;
11470         }
11471     }
11472
11473   if (ret == 1)
11474     isec->makes_toc_func_call = 1;
11475
11476   return ret;
11477 }
11478
11479 /* The linker repeatedly calls this function for each input section,
11480    in the order that input sections are linked into output sections.
11481    Build lists of input sections to determine groupings between which
11482    we may insert linker stubs.  */
11483
11484 bfd_boolean
11485 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11486 {
11487   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11488
11489   if (htab == NULL)
11490     return FALSE;
11491
11492   if ((isec->output_section->flags & SEC_CODE) != 0
11493       && isec->output_section->index <= htab->top_index)
11494     {
11495       asection **list = htab->input_list + isec->output_section->index;
11496       /* Steal the link_sec pointer for our list.  */
11497 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
11498       /* This happens to make the list in reverse order,
11499          which is what we want.  */
11500       PREV_SEC (isec) = *list;
11501       *list = isec;
11502     }
11503
11504   if (htab->multi_toc_needed)
11505     {
11506       /* If a code section has a function that uses the TOC then we need
11507          to use the right TOC (obviously).  Also, make sure that .opd gets
11508          the correct TOC value for R_PPC64_TOC relocs that don't have or
11509          can't find their function symbol (shouldn't ever happen now).
11510          Also specially treat .fixup for the linux kernel.  .fixup
11511          contains branches, but only back to the function that hit an
11512          exception.  */
11513       if (isec->has_toc_reloc
11514           || (isec->flags & SEC_CODE) == 0
11515           || strcmp (isec->name, ".fixup") == 0)
11516         {
11517           if (elf_gp (isec->owner) != 0)
11518             htab->toc_curr = elf_gp (isec->owner);
11519         }
11520       else
11521         {
11522           if (!isec->call_check_done
11523               && toc_adjusting_stub_needed (info, isec) < 0)
11524             return FALSE;
11525           /* If we make a local call from this section, ie. a branch
11526              without a following nop, then we have no place to put a
11527              toc restoring insn.  We must use the same toc group as
11528              the callee.
11529              Testing makes_toc_func_call actually tests for *any*
11530              calls to functions that need a good toc pointer.  A more
11531              precise test would be better, as this one will set
11532              incorrect values for pasted .init/.fini fragments.
11533              (Fixed later in check_pasted_section.)  */
11534           if (isec->makes_toc_func_call
11535               && elf_gp (isec->owner) != 0)
11536             htab->toc_curr = elf_gp (isec->owner);
11537         }
11538     }
11539
11540   /* Functions that don't use the TOC can belong in any TOC group.
11541      Use the last TOC base.  */
11542   htab->stub_group[isec->id].toc_off = htab->toc_curr;
11543   return TRUE;
11544 }
11545
11546 /* Check that all .init and .fini sections use the same toc, if they
11547    have toc relocs.  */
11548
11549 static bfd_boolean
11550 check_pasted_section (struct bfd_link_info *info, const char *name)
11551 {
11552   asection *o = bfd_get_section_by_name (info->output_bfd, name);
11553
11554   if (o != NULL)
11555     {
11556       struct ppc_link_hash_table *htab = ppc_hash_table (info);
11557       bfd_vma toc_off = 0;
11558       asection *i;
11559
11560       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11561         if (i->has_toc_reloc)
11562           {
11563             if (toc_off == 0)
11564               toc_off = htab->stub_group[i->id].toc_off;
11565             else if (toc_off != htab->stub_group[i->id].toc_off)
11566               return FALSE;
11567           }
11568
11569       if (toc_off == 0)
11570         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11571           if (i->makes_toc_func_call)
11572             {
11573               toc_off = htab->stub_group[i->id].toc_off;
11574               break;
11575             }
11576
11577       /* Make sure the whole pasted function uses the same toc offset.  */
11578       if (toc_off != 0)
11579         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11580           htab->stub_group[i->id].toc_off = toc_off;
11581     }
11582   return TRUE;
11583 }
11584
11585 bfd_boolean
11586 ppc64_elf_check_init_fini (struct bfd_link_info *info)
11587 {
11588   return (check_pasted_section (info, ".init")
11589           & check_pasted_section (info, ".fini"));
11590 }
11591
11592 /* See whether we can group stub sections together.  Grouping stub
11593    sections may result in fewer stubs.  More importantly, we need to
11594    put all .init* and .fini* stubs at the beginning of the .init or
11595    .fini output sections respectively, because glibc splits the
11596    _init and _fini functions into multiple parts.  Putting a stub in
11597    the middle of a function is not a good idea.  */
11598
11599 static void
11600 group_sections (struct ppc_link_hash_table *htab,
11601                 bfd_size_type stub_group_size,
11602                 bfd_boolean stubs_always_before_branch)
11603 {
11604   asection **list;
11605   bfd_size_type stub14_group_size;
11606   bfd_boolean suppress_size_errors;
11607
11608   suppress_size_errors = FALSE;
11609   stub14_group_size = stub_group_size;
11610   if (stub_group_size == 1)
11611     {
11612       /* Default values.  */
11613       if (stubs_always_before_branch)
11614         {
11615           stub_group_size = 0x1e00000;
11616           stub14_group_size = 0x7800;
11617         }
11618       else
11619         {
11620           stub_group_size = 0x1c00000;
11621           stub14_group_size = 0x7000;
11622         }
11623       suppress_size_errors = TRUE;
11624     }
11625
11626   list = htab->input_list + htab->top_index;
11627   do
11628     {
11629       asection *tail = *list;
11630       while (tail != NULL)
11631         {
11632           asection *curr;
11633           asection *prev;
11634           bfd_size_type total;
11635           bfd_boolean big_sec;
11636           bfd_vma curr_toc;
11637
11638           curr = tail;
11639           total = tail->size;
11640           big_sec = total > (ppc64_elf_section_data (tail) != NULL
11641                              && ppc64_elf_section_data (tail)->has_14bit_branch
11642                              ? stub14_group_size : stub_group_size);
11643           if (big_sec && !suppress_size_errors)
11644             (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
11645                                      tail->owner, tail);
11646           curr_toc = htab->stub_group[tail->id].toc_off;
11647
11648           while ((prev = PREV_SEC (curr)) != NULL
11649                  && ((total += curr->output_offset - prev->output_offset)
11650                      < (ppc64_elf_section_data (prev) != NULL
11651                         && ppc64_elf_section_data (prev)->has_14bit_branch
11652                         ? stub14_group_size : stub_group_size))
11653                  && htab->stub_group[prev->id].toc_off == curr_toc)
11654             curr = prev;
11655
11656           /* OK, the size from the start of CURR to the end is less
11657              than stub_group_size and thus can be handled by one stub
11658              section.  (or the tail section is itself larger than
11659              stub_group_size, in which case we may be toast.)  We
11660              should really be keeping track of the total size of stubs
11661              added here, as stubs contribute to the final output
11662              section size.  That's a little tricky, and this way will
11663              only break if stubs added make the total size more than
11664              2^25, ie. for the default stub_group_size, if stubs total
11665              more than 2097152 bytes, or nearly 75000 plt call stubs.  */
11666           do
11667             {
11668               prev = PREV_SEC (tail);
11669               /* Set up this stub group.  */
11670               htab->stub_group[tail->id].link_sec = curr;
11671             }
11672           while (tail != curr && (tail = prev) != NULL);
11673
11674           /* But wait, there's more!  Input sections up to stub_group_size
11675              bytes before the stub section can be handled by it too.
11676              Don't do this if we have a really large section after the
11677              stubs, as adding more stubs increases the chance that
11678              branches may not reach into the stub section.  */
11679           if (!stubs_always_before_branch && !big_sec)
11680             {
11681               total = 0;
11682               while (prev != NULL
11683                      && ((total += tail->output_offset - prev->output_offset)
11684                          < (ppc64_elf_section_data (prev) != NULL
11685                             && ppc64_elf_section_data (prev)->has_14bit_branch
11686                             ? stub14_group_size : stub_group_size))
11687                      && htab->stub_group[prev->id].toc_off == curr_toc)
11688                 {
11689                   tail = prev;
11690                   prev = PREV_SEC (tail);
11691                   htab->stub_group[tail->id].link_sec = curr;
11692                 }
11693             }
11694           tail = prev;
11695         }
11696     }
11697   while (list-- != htab->input_list);
11698   free (htab->input_list);
11699 #undef PREV_SEC
11700 }
11701
11702 static const unsigned char glink_eh_frame_cie[] =
11703 {
11704   0, 0, 0, 16,                          /* length.  */
11705   0, 0, 0, 0,                           /* id.  */
11706   1,                                    /* CIE version.  */
11707   'z', 'R', 0,                          /* Augmentation string.  */
11708   4,                                    /* Code alignment.  */
11709   0x78,                                 /* Data alignment.  */
11710   65,                                   /* RA reg.  */
11711   1,                                    /* Augmentation size.  */
11712   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
11713   DW_CFA_def_cfa, 1, 0                  /* def_cfa: r1 offset 0.  */
11714 };
11715
11716 /* Stripping output sections is normally done before dynamic section
11717    symbols have been allocated.  This function is called later, and
11718    handles cases like htab->brlt which is mapped to its own output
11719    section.  */
11720
11721 static void
11722 maybe_strip_output (struct bfd_link_info *info, asection *isec)
11723 {
11724   if (isec->size == 0
11725       && isec->output_section->size == 0
11726       && !(isec->output_section->flags & SEC_KEEP)
11727       && !bfd_section_removed_from_list (info->output_bfd,
11728                                          isec->output_section)
11729       && elf_section_data (isec->output_section)->dynindx == 0)
11730     {
11731       isec->output_section->flags |= SEC_EXCLUDE;
11732       bfd_section_list_remove (info->output_bfd, isec->output_section);
11733       info->output_bfd->section_count--;
11734     }
11735 }
11736
11737 /* Determine and set the size of the stub section for a final link.
11738
11739    The basic idea here is to examine all the relocations looking for
11740    PC-relative calls to a target that is unreachable with a "bl"
11741    instruction.  */
11742
11743 bfd_boolean
11744 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
11745                       bfd_boolean plt_static_chain, int plt_thread_safe,
11746                       int plt_stub_align)
11747 {
11748   bfd_size_type stub_group_size;
11749   bfd_boolean stubs_always_before_branch;
11750   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11751
11752   if (htab == NULL)
11753     return FALSE;
11754
11755   htab->plt_static_chain = plt_static_chain;
11756   htab->plt_stub_align = plt_stub_align;
11757   if (plt_thread_safe == -1 && !info->executable)
11758     plt_thread_safe = 1;
11759   if (!htab->opd_abi)
11760     plt_thread_safe = 0;
11761   else if (plt_thread_safe == -1)
11762     {
11763       static const char *const thread_starter[] =
11764         {
11765           "pthread_create",
11766           /* libstdc++ */
11767           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11768           /* librt */
11769           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11770           "mq_notify", "create_timer",
11771           /* libanl */
11772           "getaddrinfo_a",
11773           /* libgomp */
11774           "GOMP_parallel_start",
11775           "GOMP_parallel_loop_static_start",
11776           "GOMP_parallel_loop_dynamic_start",
11777           "GOMP_parallel_loop_guided_start",
11778           "GOMP_parallel_loop_runtime_start",
11779           "GOMP_parallel_sections_start",
11780         };
11781       unsigned i;
11782
11783       for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
11784         {
11785           struct elf_link_hash_entry *h;
11786           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11787                                     FALSE, FALSE, TRUE);
11788           plt_thread_safe = h != NULL && h->ref_regular;
11789           if (plt_thread_safe)
11790             break;
11791         }
11792     }
11793   htab->plt_thread_safe = plt_thread_safe;
11794   stubs_always_before_branch = group_size < 0;
11795   if (group_size < 0)
11796     stub_group_size = -group_size;
11797   else
11798     stub_group_size = group_size;
11799
11800   group_sections (htab, stub_group_size, stubs_always_before_branch);
11801
11802   while (1)
11803     {
11804       bfd *input_bfd;
11805       unsigned int bfd_indx;
11806       asection *stub_sec;
11807
11808       htab->stub_iteration += 1;
11809
11810       for (input_bfd = info->input_bfds, bfd_indx = 0;
11811            input_bfd != NULL;
11812            input_bfd = input_bfd->link_next, bfd_indx++)
11813         {
11814           Elf_Internal_Shdr *symtab_hdr;
11815           asection *section;
11816           Elf_Internal_Sym *local_syms = NULL;
11817
11818           if (!is_ppc64_elf (input_bfd))
11819             continue;
11820
11821           /* We'll need the symbol table in a second.  */
11822           symtab_hdr = &elf_symtab_hdr (input_bfd);
11823           if (symtab_hdr->sh_info == 0)
11824             continue;
11825
11826           /* Walk over each section attached to the input bfd.  */
11827           for (section = input_bfd->sections;
11828                section != NULL;
11829                section = section->next)
11830             {
11831               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
11832
11833               /* If there aren't any relocs, then there's nothing more
11834                  to do.  */
11835               if ((section->flags & SEC_RELOC) == 0
11836                   || (section->flags & SEC_ALLOC) == 0
11837                   || (section->flags & SEC_LOAD) == 0
11838                   || (section->flags & SEC_CODE) == 0
11839                   || section->reloc_count == 0)
11840                 continue;
11841
11842               /* If this section is a link-once section that will be
11843                  discarded, then don't create any stubs.  */
11844               if (section->output_section == NULL
11845                   || section->output_section->owner != info->output_bfd)
11846                 continue;
11847
11848               /* Get the relocs.  */
11849               internal_relocs
11850                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
11851                                              info->keep_memory);
11852               if (internal_relocs == NULL)
11853                 goto error_ret_free_local;
11854
11855               /* Now examine each relocation.  */
11856               irela = internal_relocs;
11857               irelaend = irela + section->reloc_count;
11858               for (; irela < irelaend; irela++)
11859                 {
11860                   enum elf_ppc64_reloc_type r_type;
11861                   unsigned int r_indx;
11862                   enum ppc_stub_type stub_type;
11863                   struct ppc_stub_hash_entry *stub_entry;
11864                   asection *sym_sec, *code_sec;
11865                   bfd_vma sym_value, code_value;
11866                   bfd_vma destination;
11867                   unsigned long local_off;
11868                   bfd_boolean ok_dest;
11869                   struct ppc_link_hash_entry *hash;
11870                   struct ppc_link_hash_entry *fdh;
11871                   struct elf_link_hash_entry *h;
11872                   Elf_Internal_Sym *sym;
11873                   char *stub_name;
11874                   const asection *id_sec;
11875                   struct _opd_sec_data *opd;
11876                   struct plt_entry *plt_ent;
11877
11878                   r_type = ELF64_R_TYPE (irela->r_info);
11879                   r_indx = ELF64_R_SYM (irela->r_info);
11880
11881                   if (r_type >= R_PPC64_max)
11882                     {
11883                       bfd_set_error (bfd_error_bad_value);
11884                       goto error_ret_free_internal;
11885                     }
11886
11887                   /* Only look for stubs on branch instructions.  */
11888                   if (r_type != R_PPC64_REL24
11889                       && r_type != R_PPC64_REL14
11890                       && r_type != R_PPC64_REL14_BRTAKEN
11891                       && r_type != R_PPC64_REL14_BRNTAKEN)
11892                     continue;
11893
11894                   /* Now determine the call target, its name, value,
11895                      section.  */
11896                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11897                                   r_indx, input_bfd))
11898                     goto error_ret_free_internal;
11899                   hash = (struct ppc_link_hash_entry *) h;
11900
11901                   ok_dest = FALSE;
11902                   fdh = NULL;
11903                   sym_value = 0;
11904                   if (hash == NULL)
11905                     {
11906                       sym_value = sym->st_value;
11907                       ok_dest = TRUE;
11908                     }
11909                   else if (hash->elf.root.type == bfd_link_hash_defined
11910                            || hash->elf.root.type == bfd_link_hash_defweak)
11911                     {
11912                       sym_value = hash->elf.root.u.def.value;
11913                       if (sym_sec->output_section != NULL)
11914                         ok_dest = TRUE;
11915                     }
11916                   else if (hash->elf.root.type == bfd_link_hash_undefweak
11917                            || hash->elf.root.type == bfd_link_hash_undefined)
11918                     {
11919                       /* Recognise an old ABI func code entry sym, and
11920                          use the func descriptor sym instead if it is
11921                          defined.  */
11922                       if (hash->elf.root.root.string[0] == '.'
11923                           && (fdh = lookup_fdh (hash, htab)) != NULL)
11924                         {
11925                           if (fdh->elf.root.type == bfd_link_hash_defined
11926                               || fdh->elf.root.type == bfd_link_hash_defweak)
11927                             {
11928                               sym_sec = fdh->elf.root.u.def.section;
11929                               sym_value = fdh->elf.root.u.def.value;
11930                               if (sym_sec->output_section != NULL)
11931                                 ok_dest = TRUE;
11932                             }
11933                           else
11934                             fdh = NULL;
11935                         }
11936                     }
11937                   else
11938                     {
11939                       bfd_set_error (bfd_error_bad_value);
11940                       goto error_ret_free_internal;
11941                     }
11942
11943                   destination = 0;
11944                   local_off = 0;
11945                   if (ok_dest)
11946                     {
11947                       sym_value += irela->r_addend;
11948                       destination = (sym_value
11949                                      + sym_sec->output_offset
11950                                      + sym_sec->output_section->vma);
11951                       local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
11952                                                             ? hash->elf.other
11953                                                             : sym->st_other);
11954                     }
11955
11956                   code_sec = sym_sec;
11957                   code_value = sym_value;
11958                   opd = get_opd_info (sym_sec);
11959                   if (opd != NULL)
11960                     {
11961                       bfd_vma dest;
11962
11963                       if (hash == NULL && opd->adjust != NULL)
11964                         {
11965                           long adjust = opd->adjust[sym_value / 8];
11966                           if (adjust == -1)
11967                             continue;
11968                           code_value += adjust;
11969                           sym_value += adjust;
11970                         }
11971                       dest = opd_entry_value (sym_sec, sym_value,
11972                                               &code_sec, &code_value, FALSE);
11973                       if (dest != (bfd_vma) -1)
11974                         {
11975                           destination = dest;
11976                           if (fdh != NULL)
11977                             {
11978                               /* Fixup old ABI sym to point at code
11979                                  entry.  */
11980                               hash->elf.root.type = bfd_link_hash_defweak;
11981                               hash->elf.root.u.def.section = code_sec;
11982                               hash->elf.root.u.def.value = code_value;
11983                             }
11984                         }
11985                     }
11986
11987                   /* Determine what (if any) linker stub is needed.  */
11988                   plt_ent = NULL;
11989                   stub_type = ppc_type_of_stub (section, irela, &hash,
11990                                                 &plt_ent, destination,
11991                                                 local_off);
11992
11993                   if (stub_type != ppc_stub_plt_call)
11994                     {
11995                       /* Check whether we need a TOC adjusting stub.
11996                          Since the linker pastes together pieces from
11997                          different object files when creating the
11998                          _init and _fini functions, it may be that a
11999                          call to what looks like a local sym is in
12000                          fact a call needing a TOC adjustment.  */
12001                       if (code_sec != NULL
12002                           && code_sec->output_section != NULL
12003                           && (htab->stub_group[code_sec->id].toc_off
12004                               != htab->stub_group[section->id].toc_off)
12005                           && (code_sec->has_toc_reloc
12006                               || code_sec->makes_toc_func_call))
12007                         stub_type = ppc_stub_long_branch_r2off;
12008                     }
12009
12010                   if (stub_type == ppc_stub_none)
12011                     continue;
12012
12013                   /* __tls_get_addr calls might be eliminated.  */
12014                   if (stub_type != ppc_stub_plt_call
12015                       && hash != NULL
12016                       && (hash == htab->tls_get_addr
12017                           || hash == htab->tls_get_addr_fd)
12018                       && section->has_tls_reloc
12019                       && irela != internal_relocs)
12020                     {
12021                       /* Get tls info.  */
12022                       unsigned char *tls_mask;
12023
12024                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12025                                          irela - 1, input_bfd))
12026                         goto error_ret_free_internal;
12027                       if (*tls_mask != 0)
12028                         continue;
12029                     }
12030
12031                   if (stub_type == ppc_stub_plt_call
12032                       && irela + 1 < irelaend
12033                       && irela[1].r_offset == irela->r_offset + 4
12034                       && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12035                     {
12036                       if (!tocsave_find (htab, INSERT,
12037                                          &local_syms, irela + 1, input_bfd))
12038                         goto error_ret_free_internal;
12039                     }
12040                   else if (stub_type == ppc_stub_plt_call)
12041                     stub_type = ppc_stub_plt_call_r2save;
12042
12043                   /* Support for grouping stub sections.  */
12044                   id_sec = htab->stub_group[section->id].link_sec;
12045
12046                   /* Get the name of this stub.  */
12047                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12048                   if (!stub_name)
12049                     goto error_ret_free_internal;
12050
12051                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12052                                                      stub_name, FALSE, FALSE);
12053                   if (stub_entry != NULL)
12054                     {
12055                       /* The proper stub has already been created.  */
12056                       free (stub_name);
12057                       if (stub_type == ppc_stub_plt_call_r2save)
12058                         stub_entry->stub_type = stub_type;
12059                       continue;
12060                     }
12061
12062                   stub_entry = ppc_add_stub (stub_name, section, info);
12063                   if (stub_entry == NULL)
12064                     {
12065                       free (stub_name);
12066                     error_ret_free_internal:
12067                       if (elf_section_data (section)->relocs == NULL)
12068                         free (internal_relocs);
12069                     error_ret_free_local:
12070                       if (local_syms != NULL
12071                           && (symtab_hdr->contents
12072                               != (unsigned char *) local_syms))
12073                         free (local_syms);
12074                       return FALSE;
12075                     }
12076
12077                   stub_entry->stub_type = stub_type;
12078                   if (stub_type != ppc_stub_plt_call
12079                       && stub_type != ppc_stub_plt_call_r2save)
12080                     {
12081                       stub_entry->target_value = code_value;
12082                       stub_entry->target_section = code_sec;
12083                     }
12084                   else
12085                     {
12086                       stub_entry->target_value = sym_value;
12087                       stub_entry->target_section = sym_sec;
12088                     }
12089                   stub_entry->h = hash;
12090                   stub_entry->plt_ent = plt_ent;
12091                   stub_entry->other = hash ? hash->elf.other : sym->st_other;
12092
12093                   if (stub_entry->h != NULL)
12094                     htab->stub_globals += 1;
12095                 }
12096
12097               /* We're done with the internal relocs, free them.  */
12098               if (elf_section_data (section)->relocs != internal_relocs)
12099                 free (internal_relocs);
12100             }
12101
12102           if (local_syms != NULL
12103               && symtab_hdr->contents != (unsigned char *) local_syms)
12104             {
12105               if (!info->keep_memory)
12106                 free (local_syms);
12107               else
12108                 symtab_hdr->contents = (unsigned char *) local_syms;
12109             }
12110         }
12111
12112       /* We may have added some stubs.  Find out the new size of the
12113          stub sections.  */
12114       for (stub_sec = htab->stub_bfd->sections;
12115            stub_sec != NULL;
12116            stub_sec = stub_sec->next)
12117         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12118           {
12119             stub_sec->rawsize = stub_sec->size;
12120             stub_sec->size = 0;
12121             stub_sec->reloc_count = 0;
12122             stub_sec->flags &= ~SEC_RELOC;
12123           }
12124
12125       htab->brlt->size = 0;
12126       htab->brlt->reloc_count = 0;
12127       htab->brlt->flags &= ~SEC_RELOC;
12128       if (htab->relbrlt != NULL)
12129         htab->relbrlt->size = 0;
12130
12131       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12132
12133       if (info->emitrelocations
12134           && htab->glink != NULL && htab->glink->size != 0)
12135         {
12136           htab->glink->reloc_count = 1;
12137           htab->glink->flags |= SEC_RELOC;
12138         }
12139
12140       if (htab->glink_eh_frame != NULL
12141           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12142           && htab->glink_eh_frame->output_section->size != 0)
12143         {
12144           size_t size = 0, align;
12145
12146           for (stub_sec = htab->stub_bfd->sections;
12147                stub_sec != NULL;
12148                stub_sec = stub_sec->next)
12149             if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12150               size += 20;
12151           if (htab->glink != NULL && htab->glink->size != 0)
12152             size += 24;
12153           if (size != 0)
12154             size += sizeof (glink_eh_frame_cie);
12155           align = 1;
12156           align <<= htab->glink_eh_frame->output_section->alignment_power;
12157           align -= 1;
12158           size = (size + align) & ~align;
12159           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12160           htab->glink_eh_frame->size = size;
12161         }
12162
12163       if (htab->plt_stub_align != 0)
12164         for (stub_sec = htab->stub_bfd->sections;
12165              stub_sec != NULL;
12166              stub_sec = stub_sec->next)
12167           if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12168             stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
12169                               & (-1 << htab->plt_stub_align));
12170
12171       for (stub_sec = htab->stub_bfd->sections;
12172            stub_sec != NULL;
12173            stub_sec = stub_sec->next)
12174         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12175             && stub_sec->rawsize != stub_sec->size)
12176           break;
12177
12178       /* Exit from this loop when no stubs have been added, and no stubs
12179          have changed size.  */
12180       if (stub_sec == NULL
12181           && (htab->glink_eh_frame == NULL
12182               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12183         break;
12184
12185       /* Ask the linker to do its stuff.  */
12186       (*htab->layout_sections_again) ();
12187     }
12188
12189   maybe_strip_output (info, htab->brlt);
12190   if (htab->glink_eh_frame != NULL)
12191     maybe_strip_output (info, htab->glink_eh_frame);
12192
12193   return TRUE;
12194 }
12195
12196 /* Called after we have determined section placement.  If sections
12197    move, we'll be called again.  Provide a value for TOCstart.  */
12198
12199 bfd_vma
12200 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12201 {
12202   asection *s;
12203   bfd_vma TOCstart;
12204
12205   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12206      order.  The TOC starts where the first of these sections starts.  */
12207   s = bfd_get_section_by_name (obfd, ".got");
12208   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12209     s = bfd_get_section_by_name (obfd, ".toc");
12210   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12211     s = bfd_get_section_by_name (obfd, ".tocbss");
12212   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12213     s = bfd_get_section_by_name (obfd, ".plt");
12214   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12215     {
12216       /* This may happen for
12217          o  references to TOC base (SYM@toc / TOC[tc0]) without a
12218          .toc directive
12219          o  bad linker script
12220          o --gc-sections and empty TOC sections
12221
12222          FIXME: Warn user?  */
12223
12224       /* Look for a likely section.  We probably won't even be
12225          using TOCstart.  */
12226       for (s = obfd->sections; s != NULL; s = s->next)
12227         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12228                          | SEC_EXCLUDE))
12229             == (SEC_ALLOC | SEC_SMALL_DATA))
12230           break;
12231       if (s == NULL)
12232         for (s = obfd->sections; s != NULL; s = s->next)
12233           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12234               == (SEC_ALLOC | SEC_SMALL_DATA))
12235             break;
12236       if (s == NULL)
12237         for (s = obfd->sections; s != NULL; s = s->next)
12238           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12239               == SEC_ALLOC)
12240             break;
12241       if (s == NULL)
12242         for (s = obfd->sections; s != NULL; s = s->next)
12243           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12244             break;
12245     }
12246
12247   TOCstart = 0;
12248   if (s != NULL)
12249     TOCstart = s->output_section->vma + s->output_offset;
12250
12251   _bfd_set_gp_value (obfd, TOCstart);
12252
12253   if (info != NULL && s != NULL && is_ppc64_elf (obfd))
12254     {
12255       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12256
12257       if (htab != NULL
12258           && htab->elf.hgot != NULL)
12259         {
12260           htab->elf.hgot->type = STT_OBJECT;
12261           htab->elf.hgot->root.type = bfd_link_hash_defined;
12262           htab->elf.hgot->root.u.def.value = TOC_BASE_OFF;
12263           htab->elf.hgot->root.u.def.section = s;
12264         }
12265     }
12266   return TOCstart;
12267 }
12268
12269 /* Build all the stubs associated with the current output file.
12270    The stubs are kept in a hash table attached to the main linker
12271    hash table.  This function is called via gldelf64ppc_finish.  */
12272
12273 bfd_boolean
12274 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
12275                        struct bfd_link_info *info,
12276                        char **stats)
12277 {
12278   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12279   asection *stub_sec;
12280   bfd_byte *p;
12281   int stub_sec_count = 0;
12282
12283   if (htab == NULL)
12284     return FALSE;
12285
12286   htab->emit_stub_syms = emit_stub_syms;
12287
12288   /* Allocate memory to hold the linker stubs.  */
12289   for (stub_sec = htab->stub_bfd->sections;
12290        stub_sec != NULL;
12291        stub_sec = stub_sec->next)
12292     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12293         && stub_sec->size != 0)
12294       {
12295         stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
12296         if (stub_sec->contents == NULL)
12297           return FALSE;
12298         /* We want to check that built size is the same as calculated
12299            size.  rawsize is a convenient location to use.  */
12300         stub_sec->rawsize = stub_sec->size;
12301         stub_sec->size = 0;
12302       }
12303
12304   if (htab->glink != NULL && htab->glink->size != 0)
12305     {
12306       unsigned int indx;
12307       bfd_vma plt0;
12308
12309       /* Build the .glink plt call stub.  */
12310       if (htab->emit_stub_syms)
12311         {
12312           struct elf_link_hash_entry *h;
12313           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12314                                     TRUE, FALSE, FALSE);
12315           if (h == NULL)
12316             return FALSE;
12317           if (h->root.type == bfd_link_hash_new)
12318             {
12319               h->root.type = bfd_link_hash_defined;
12320               h->root.u.def.section = htab->glink;
12321               h->root.u.def.value = 8;
12322               h->ref_regular = 1;
12323               h->def_regular = 1;
12324               h->ref_regular_nonweak = 1;
12325               h->forced_local = 1;
12326               h->non_elf = 0;
12327             }
12328         }
12329       plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
12330       if (info->emitrelocations)
12331         {
12332           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12333           if (r == NULL)
12334             return FALSE;
12335           r->r_offset = (htab->glink->output_offset
12336                          + htab->glink->output_section->vma);
12337           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12338           r->r_addend = plt0;
12339         }
12340       p = htab->glink->contents;
12341       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
12342       bfd_put_64 (htab->glink->owner, plt0, p);
12343       p += 8;
12344       if (htab->opd_abi)
12345         {
12346           bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12347           p += 4;
12348           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12349           p += 4;
12350           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12351           p += 4;
12352           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12353           p += 4;
12354           bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12355           p += 4;
12356           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12357           p += 4;
12358           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12359           p += 4;
12360           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12361           p += 4;
12362           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12363           p += 4;
12364           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12365           p += 4;
12366         }
12367       else
12368         {
12369           bfd_put_32 (htab->glink->owner, MFLR_R0, p);
12370           p += 4;
12371           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12372           p += 4;
12373           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12374           p += 4;
12375           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12376           p += 4;
12377           bfd_put_32 (htab->glink->owner, MTLR_R0, p);
12378           p += 4;
12379           bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
12380           p += 4;
12381           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12382           p += 4;
12383           bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
12384           p += 4;
12385           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12386           p += 4;
12387           bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
12388           p += 4;
12389           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12390           p += 4;
12391           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
12392           p += 4;
12393         }
12394       bfd_put_32 (htab->glink->owner, BCTR, p);
12395       p += 4;
12396       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
12397         {
12398           bfd_put_32 (htab->glink->owner, NOP, p);
12399           p += 4;
12400         }
12401
12402       /* Build the .glink lazy link call stubs.  */
12403       indx = 0;
12404       while (p < htab->glink->contents + htab->glink->size)
12405         {
12406           if (htab->opd_abi)
12407             {
12408               if (indx < 0x8000)
12409                 {
12410                   bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
12411                   p += 4;
12412                 }
12413               else
12414                 {
12415                   bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
12416                   p += 4;
12417                   bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
12418                               p);
12419                   p += 4;
12420                 }
12421             }
12422           bfd_put_32 (htab->glink->owner,
12423                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
12424           indx++;
12425           p += 4;
12426         }
12427       htab->glink->rawsize = p - htab->glink->contents;
12428     }
12429
12430   if (htab->brlt->size != 0)
12431     {
12432       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
12433                                          htab->brlt->size);
12434       if (htab->brlt->contents == NULL)
12435         return FALSE;
12436     }
12437   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
12438     {
12439       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
12440                                             htab->relbrlt->size);
12441       if (htab->relbrlt->contents == NULL)
12442         return FALSE;
12443     }
12444
12445   if (htab->glink_eh_frame != NULL
12446       && htab->glink_eh_frame->size != 0)
12447     {
12448       bfd_vma val;
12449       bfd_byte *last_fde;
12450       size_t last_fde_len, size, align, pad;
12451
12452       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12453       if (p == NULL)
12454         return FALSE;
12455       htab->glink_eh_frame->contents = p;
12456       last_fde = p;
12457
12458       htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12459
12460       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12461       /* CIE length (rewrite in case little-endian).  */
12462       last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12463       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12464       p += sizeof (glink_eh_frame_cie);
12465
12466       for (stub_sec = htab->stub_bfd->sections;
12467            stub_sec != NULL;
12468            stub_sec = stub_sec->next)
12469         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12470           {
12471             last_fde = p;
12472             last_fde_len = 16;
12473             /* FDE length.  */
12474             bfd_put_32 (htab->elf.dynobj, 16, p);
12475             p += 4;
12476             /* CIE pointer.  */
12477             val = p - htab->glink_eh_frame->contents;
12478             bfd_put_32 (htab->elf.dynobj, val, p);
12479             p += 4;
12480             /* Offset to stub section.  */
12481             val = (stub_sec->output_section->vma
12482                    + stub_sec->output_offset);
12483             val -= (htab->glink_eh_frame->output_section->vma
12484                     + htab->glink_eh_frame->output_offset);
12485             val -= p - htab->glink_eh_frame->contents;
12486             if (val + 0x80000000 > 0xffffffff)
12487               {
12488                 info->callbacks->einfo
12489                   (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
12490                    stub_sec->name);
12491                 return FALSE;
12492               }
12493             bfd_put_32 (htab->elf.dynobj, val, p);
12494             p += 4;
12495             /* stub section size.  */
12496             bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
12497             p += 4;
12498             /* Augmentation.  */
12499             p += 1;
12500             /* Pad.  */
12501             p += 3;
12502           }
12503       if (htab->glink != NULL && htab->glink->size != 0)
12504         {
12505           last_fde = p;
12506           last_fde_len = 20;
12507           /* FDE length.  */
12508           bfd_put_32 (htab->elf.dynobj, 20, p);
12509           p += 4;
12510           /* CIE pointer.  */
12511           val = p - htab->glink_eh_frame->contents;
12512           bfd_put_32 (htab->elf.dynobj, val, p);
12513           p += 4;
12514           /* Offset to .glink.  */
12515           val = (htab->glink->output_section->vma
12516                  + htab->glink->output_offset
12517                  + 8);
12518           val -= (htab->glink_eh_frame->output_section->vma
12519                   + htab->glink_eh_frame->output_offset);
12520           val -= p - htab->glink_eh_frame->contents;
12521           if (val + 0x80000000 > 0xffffffff)
12522             {
12523               info->callbacks->einfo
12524                 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
12525                  htab->glink->name);
12526               return FALSE;
12527             }
12528           bfd_put_32 (htab->elf.dynobj, val, p);
12529           p += 4;
12530           /* .glink size.  */
12531           bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
12532           p += 4;
12533           /* Augmentation.  */
12534           p += 1;
12535
12536           *p++ = DW_CFA_advance_loc + 1;
12537           *p++ = DW_CFA_register;
12538           *p++ = 65;
12539           *p++ = 12;
12540           *p++ = DW_CFA_advance_loc + 4;
12541           *p++ = DW_CFA_restore_extended;
12542           *p++ = 65;
12543         }
12544       /* Subsume any padding into the last FDE if user .eh_frame
12545          sections are aligned more than glink_eh_frame.  Otherwise any
12546          zero padding will be seen as a terminator.  */
12547       size = p - htab->glink_eh_frame->contents;
12548       align = 1;
12549       align <<= htab->glink_eh_frame->output_section->alignment_power;
12550       align -= 1;
12551       pad = ((size + align) & ~align) - size;
12552       htab->glink_eh_frame->size = size + pad;
12553       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12554     }
12555
12556   /* Build the stubs as directed by the stub hash table.  */
12557   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
12558
12559   if (htab->relbrlt != NULL)
12560     htab->relbrlt->reloc_count = 0;
12561
12562   if (htab->plt_stub_align != 0)
12563     for (stub_sec = htab->stub_bfd->sections;
12564          stub_sec != NULL;
12565          stub_sec = stub_sec->next)
12566       if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12567         stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
12568                           & (-1 << htab->plt_stub_align));
12569
12570   for (stub_sec = htab->stub_bfd->sections;
12571        stub_sec != NULL;
12572        stub_sec = stub_sec->next)
12573     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12574       {
12575         stub_sec_count += 1;
12576         if (stub_sec->rawsize != stub_sec->size)
12577           break;
12578       }
12579
12580   if (stub_sec != NULL
12581       || htab->glink->rawsize != htab->glink->size
12582       || (htab->glink_eh_frame != NULL
12583           && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
12584     {
12585       htab->stub_error = TRUE;
12586       info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
12587     }
12588
12589   if (htab->stub_error)
12590     return FALSE;
12591
12592   if (stats != NULL)
12593     {
12594       *stats = bfd_malloc (500);
12595       if (*stats == NULL)
12596         return FALSE;
12597
12598       sprintf (*stats, _("linker stubs in %u group%s\n"
12599                          "  branch       %lu\n"
12600                          "  toc adjust   %lu\n"
12601                          "  long branch  %lu\n"
12602                          "  long toc adj %lu\n"
12603                          "  plt call     %lu\n"
12604                          "  plt call toc %lu"),
12605                stub_sec_count,
12606                stub_sec_count == 1 ? "" : "s",
12607                htab->stub_count[ppc_stub_long_branch - 1],
12608                htab->stub_count[ppc_stub_long_branch_r2off - 1],
12609                htab->stub_count[ppc_stub_plt_branch - 1],
12610                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
12611                htab->stub_count[ppc_stub_plt_call - 1],
12612                htab->stub_count[ppc_stub_plt_call_r2save - 1]);
12613     }
12614   return TRUE;
12615 }
12616
12617 /* This function undoes the changes made by add_symbol_adjust.  */
12618
12619 static bfd_boolean
12620 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12621 {
12622   struct ppc_link_hash_entry *eh;
12623
12624   if (h->root.type == bfd_link_hash_indirect)
12625     return TRUE;
12626
12627   eh = (struct ppc_link_hash_entry *) h;
12628   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
12629     return TRUE;
12630
12631   eh->elf.root.type = bfd_link_hash_undefined;
12632   return TRUE;
12633 }
12634
12635 void
12636 ppc64_elf_restore_symbols (struct bfd_link_info *info)
12637 {
12638   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12639
12640   if (htab != NULL)
12641     elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
12642 }
12643
12644 /* What to do when ld finds relocations against symbols defined in
12645    discarded sections.  */
12646
12647 static unsigned int
12648 ppc64_elf_action_discarded (asection *sec)
12649 {
12650   if (strcmp (".opd", sec->name) == 0)
12651     return 0;
12652
12653   if (strcmp (".toc", sec->name) == 0)
12654     return 0;
12655
12656   if (strcmp (".toc1", sec->name) == 0)
12657     return 0;
12658
12659   return _bfd_elf_default_action_discarded (sec);
12660 }
12661
12662 /* The RELOCATE_SECTION function is called by the ELF backend linker
12663    to handle the relocations for a section.
12664
12665    The relocs are always passed as Rela structures; if the section
12666    actually uses Rel structures, the r_addend field will always be
12667    zero.
12668
12669    This function is responsible for adjust the section contents as
12670    necessary, and (if using Rela relocs and generating a
12671    relocatable output file) adjusting the reloc addend as
12672    necessary.
12673
12674    This function does not have to worry about setting the reloc
12675    address or the reloc symbol index.
12676
12677    LOCAL_SYMS is a pointer to the swapped in local symbols.
12678
12679    LOCAL_SECTIONS is an array giving the section in the input file
12680    corresponding to the st_shndx field of each local symbol.
12681
12682    The global hash table entry for the global symbols can be found
12683    via elf_sym_hashes (input_bfd).
12684
12685    When generating relocatable output, this function must handle
12686    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
12687    going to be the section symbol corresponding to the output
12688    section, which means that the addend must be adjusted
12689    accordingly.  */
12690
12691 static bfd_boolean
12692 ppc64_elf_relocate_section (bfd *output_bfd,
12693                             struct bfd_link_info *info,
12694                             bfd *input_bfd,
12695                             asection *input_section,
12696                             bfd_byte *contents,
12697                             Elf_Internal_Rela *relocs,
12698                             Elf_Internal_Sym *local_syms,
12699                             asection **local_sections)
12700 {
12701   struct ppc_link_hash_table *htab;
12702   Elf_Internal_Shdr *symtab_hdr;
12703   struct elf_link_hash_entry **sym_hashes;
12704   Elf_Internal_Rela *rel;
12705   Elf_Internal_Rela *relend;
12706   Elf_Internal_Rela outrel;
12707   bfd_byte *loc;
12708   struct got_entry **local_got_ents;
12709   bfd_vma TOCstart;
12710   bfd_boolean ret = TRUE;
12711   bfd_boolean is_opd;
12712   /* Assume 'at' branch hints.  */
12713   bfd_boolean is_isa_v2 = TRUE;
12714   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
12715
12716   /* Initialize howto table if needed.  */
12717   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
12718     ppc_howto_init ();
12719
12720   htab = ppc_hash_table (info);
12721   if (htab == NULL)
12722     return FALSE;
12723
12724   /* Don't relocate stub sections.  */
12725   if (input_section->owner == htab->stub_bfd)
12726     return TRUE;
12727
12728   BFD_ASSERT (is_ppc64_elf (input_bfd));
12729
12730   local_got_ents = elf_local_got_ents (input_bfd);
12731   TOCstart = elf_gp (output_bfd);
12732   symtab_hdr = &elf_symtab_hdr (input_bfd);
12733   sym_hashes = elf_sym_hashes (input_bfd);
12734   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
12735
12736   rel = relocs;
12737   relend = relocs + input_section->reloc_count;
12738   for (; rel < relend; rel++)
12739     {
12740       enum elf_ppc64_reloc_type r_type;
12741       bfd_vma addend;
12742       bfd_reloc_status_type r;
12743       Elf_Internal_Sym *sym;
12744       asection *sec;
12745       struct elf_link_hash_entry *h_elf;
12746       struct ppc_link_hash_entry *h;
12747       struct ppc_link_hash_entry *fdh;
12748       const char *sym_name;
12749       unsigned long r_symndx, toc_symndx;
12750       bfd_vma toc_addend;
12751       unsigned char tls_mask, tls_gd, tls_type;
12752       unsigned char sym_type;
12753       bfd_vma relocation;
12754       bfd_boolean unresolved_reloc;
12755       bfd_boolean warned;
12756       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
12757       unsigned int insn;
12758       unsigned int mask;
12759       struct ppc_stub_hash_entry *stub_entry;
12760       bfd_vma max_br_offset;
12761       bfd_vma from;
12762       const Elf_Internal_Rela orig_rel = *rel;
12763
12764       r_type = ELF64_R_TYPE (rel->r_info);
12765       r_symndx = ELF64_R_SYM (rel->r_info);
12766
12767       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
12768          symbol of the previous ADDR64 reloc.  The symbol gives us the
12769          proper TOC base to use.  */
12770       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
12771           && rel != relocs
12772           && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
12773           && is_opd)
12774         r_symndx = ELF64_R_SYM (rel[-1].r_info);
12775
12776       sym = NULL;
12777       sec = NULL;
12778       h_elf = NULL;
12779       sym_name = NULL;
12780       unresolved_reloc = FALSE;
12781       warned = FALSE;
12782
12783       if (r_symndx < symtab_hdr->sh_info)
12784         {
12785           /* It's a local symbol.  */
12786           struct _opd_sec_data *opd;
12787
12788           sym = local_syms + r_symndx;
12789           sec = local_sections[r_symndx];
12790           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
12791           sym_type = ELF64_ST_TYPE (sym->st_info);
12792           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
12793           opd = get_opd_info (sec);
12794           if (opd != NULL && opd->adjust != NULL)
12795             {
12796               long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
12797               if (adjust == -1)
12798                 relocation = 0;
12799               else
12800                 {
12801                   /* If this is a relocation against the opd section sym
12802                      and we have edited .opd, adjust the reloc addend so
12803                      that ld -r and ld --emit-relocs output is correct.
12804                      If it is a reloc against some other .opd symbol,
12805                      then the symbol value will be adjusted later.  */
12806                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
12807                     rel->r_addend += adjust;
12808                   else
12809                     relocation += adjust;
12810                 }
12811             }
12812         }
12813       else
12814         {
12815           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
12816                                    r_symndx, symtab_hdr, sym_hashes,
12817                                    h_elf, sec, relocation,
12818                                    unresolved_reloc, warned);
12819           sym_name = h_elf->root.root.string;
12820           sym_type = h_elf->type;
12821           if (sec != NULL
12822               && sec->owner == output_bfd
12823               && strcmp (sec->name, ".opd") == 0)
12824             {
12825               /* This is a symbol defined in a linker script.  All
12826                  such are defined in output sections, even those
12827                  defined by simple assignment from a symbol defined in
12828                  an input section.  Transfer the symbol to an
12829                  appropriate input .opd section, so that a branch to
12830                  this symbol will be mapped to the location specified
12831                  by the opd entry.  */
12832               struct bfd_link_order *lo;
12833               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
12834                 if (lo->type == bfd_indirect_link_order)
12835                   {
12836                     asection *isec = lo->u.indirect.section;
12837                     if (h_elf->root.u.def.value >= isec->output_offset
12838                         && h_elf->root.u.def.value < (isec->output_offset
12839                                                       + isec->size))
12840                       {
12841                         h_elf->root.u.def.value -= isec->output_offset;
12842                         h_elf->root.u.def.section = isec;
12843                         sec = isec;
12844                         break;
12845                       }
12846                   }
12847             }
12848         }
12849       h = (struct ppc_link_hash_entry *) h_elf;
12850
12851       if (sec != NULL && discarded_section (sec))
12852         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
12853                                          rel, 1, relend,
12854                                          ppc64_elf_howto_table[r_type], 0,
12855                                          contents);
12856
12857       if (info->relocatable)
12858         continue;
12859
12860       if (h != NULL && &h->elf == htab->elf.hgot)
12861         {
12862           relocation = (TOCstart
12863                         + htab->stub_group[input_section->id].toc_off);
12864           sec = bfd_abs_section_ptr;
12865           unresolved_reloc = FALSE;
12866         }
12867
12868       /* TLS optimizations.  Replace instruction sequences and relocs
12869          based on information we collected in tls_optimize.  We edit
12870          RELOCS so that --emit-relocs will output something sensible
12871          for the final instruction stream.  */
12872       tls_mask = 0;
12873       tls_gd = 0;
12874       toc_symndx = 0;
12875       if (h != NULL)
12876         tls_mask = h->tls_mask;
12877       else if (local_got_ents != NULL)
12878         {
12879           struct plt_entry **local_plt = (struct plt_entry **)
12880             (local_got_ents + symtab_hdr->sh_info);
12881           unsigned char *lgot_masks = (unsigned char *)
12882             (local_plt + symtab_hdr->sh_info);
12883           tls_mask = lgot_masks[r_symndx];
12884         }
12885       if (tls_mask == 0
12886           && (r_type == R_PPC64_TLS
12887               || r_type == R_PPC64_TLSGD
12888               || r_type == R_PPC64_TLSLD))
12889         {
12890           /* Check for toc tls entries.  */
12891           unsigned char *toc_tls;
12892
12893           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12894                              &local_syms, rel, input_bfd))
12895             return FALSE;
12896
12897           if (toc_tls)
12898             tls_mask = *toc_tls;
12899         }
12900
12901       /* Check that tls relocs are used with tls syms, and non-tls
12902          relocs are used with non-tls syms.  */
12903       if (r_symndx != STN_UNDEF
12904           && r_type != R_PPC64_NONE
12905           && (h == NULL
12906               || h->elf.root.type == bfd_link_hash_defined
12907               || h->elf.root.type == bfd_link_hash_defweak)
12908           && (IS_PPC64_TLS_RELOC (r_type)
12909               != (sym_type == STT_TLS
12910                   || (sym_type == STT_SECTION
12911                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
12912         {
12913           if (tls_mask != 0
12914               && (r_type == R_PPC64_TLS
12915                   || r_type == R_PPC64_TLSGD
12916                   || r_type == R_PPC64_TLSLD))
12917             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
12918             ;
12919           else
12920             info->callbacks->einfo
12921               (!IS_PPC64_TLS_RELOC (r_type)
12922                ? _("%P: %H: %s used with TLS symbol `%T'\n")
12923                : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
12924                input_bfd, input_section, rel->r_offset,
12925                ppc64_elf_howto_table[r_type]->name,
12926                sym_name);
12927         }
12928
12929       /* Ensure reloc mapping code below stays sane.  */
12930       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
12931           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
12932           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
12933           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
12934           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
12935           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
12936           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
12937           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
12938           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
12939           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
12940         abort ();
12941
12942       switch (r_type)
12943         {
12944         default:
12945           break;
12946
12947         case R_PPC64_LO_DS_OPT:
12948           insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12949           if ((insn & (0x3f << 26)) != 58u << 26)
12950             abort ();
12951           insn += (14u << 26) - (58u << 26);
12952           bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12953           r_type = R_PPC64_TOC16_LO;
12954           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12955           break;
12956
12957         case R_PPC64_TOC16:
12958         case R_PPC64_TOC16_LO:
12959         case R_PPC64_TOC16_DS:
12960         case R_PPC64_TOC16_LO_DS:
12961           {
12962             /* Check for toc tls entries.  */
12963             unsigned char *toc_tls;
12964             int retval;
12965
12966             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12967                                    &local_syms, rel, input_bfd);
12968             if (retval == 0)
12969               return FALSE;
12970
12971             if (toc_tls)
12972               {
12973                 tls_mask = *toc_tls;
12974                 if (r_type == R_PPC64_TOC16_DS
12975                     || r_type == R_PPC64_TOC16_LO_DS)
12976                   {
12977                     if (tls_mask != 0
12978                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
12979                       goto toctprel;
12980                   }
12981                 else
12982                   {
12983                     /* If we found a GD reloc pair, then we might be
12984                        doing a GD->IE transition.  */
12985                     if (retval == 2)
12986                       {
12987                         tls_gd = TLS_TPRELGD;
12988                         if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12989                           goto tls_ldgd_opt;
12990                       }
12991                     else if (retval == 3)
12992                       {
12993                         if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12994                           goto tls_ldgd_opt;
12995                       }
12996                   }
12997               }
12998           }
12999           break;
13000
13001         case R_PPC64_GOT_TPREL16_HI:
13002         case R_PPC64_GOT_TPREL16_HA:
13003           if (tls_mask != 0
13004               && (tls_mask & TLS_TPREL) == 0)
13005             {
13006               rel->r_offset -= d_offset;
13007               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13008               r_type = R_PPC64_NONE;
13009               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13010             }
13011           break;
13012
13013         case R_PPC64_GOT_TPREL16_DS:
13014         case R_PPC64_GOT_TPREL16_LO_DS:
13015           if (tls_mask != 0
13016               && (tls_mask & TLS_TPREL) == 0)
13017             {
13018             toctprel:
13019               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
13020               insn &= 31 << 21;
13021               insn |= 0x3c0d0000;       /* addis 0,13,0 */
13022               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
13023               r_type = R_PPC64_TPREL16_HA;
13024               if (toc_symndx != 0)
13025                 {
13026                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13027                   rel->r_addend = toc_addend;
13028                   /* We changed the symbol.  Start over in order to
13029                      get h, sym, sec etc. right.  */
13030                   rel--;
13031                   continue;
13032                 }
13033               else
13034                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13035             }
13036           break;
13037
13038         case R_PPC64_TLS:
13039           if (tls_mask != 0
13040               && (tls_mask & TLS_TPREL) == 0)
13041             {
13042               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
13043               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13044               if (insn == 0)
13045                 abort ();
13046               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
13047               /* Was PPC64_TLS which sits on insn boundary, now
13048                  PPC64_TPREL16_LO which is at low-order half-word.  */
13049               rel->r_offset += d_offset;
13050               r_type = R_PPC64_TPREL16_LO;
13051               if (toc_symndx != 0)
13052                 {
13053                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13054                   rel->r_addend = toc_addend;
13055                   /* We changed the symbol.  Start over in order to
13056                      get h, sym, sec etc. right.  */
13057                   rel--;
13058                   continue;
13059                 }
13060               else
13061                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13062             }
13063           break;
13064
13065         case R_PPC64_GOT_TLSGD16_HI:
13066         case R_PPC64_GOT_TLSGD16_HA:
13067           tls_gd = TLS_TPRELGD;
13068           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13069             goto tls_gdld_hi;
13070           break;
13071
13072         case R_PPC64_GOT_TLSLD16_HI:
13073         case R_PPC64_GOT_TLSLD16_HA:
13074           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13075             {
13076             tls_gdld_hi:
13077               if ((tls_mask & tls_gd) != 0)
13078                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13079                           + R_PPC64_GOT_TPREL16_DS);
13080               else
13081                 {
13082                   rel->r_offset -= d_offset;
13083                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13084                   r_type = R_PPC64_NONE;
13085                 }
13086               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13087             }
13088           break;
13089
13090         case R_PPC64_GOT_TLSGD16:
13091         case R_PPC64_GOT_TLSGD16_LO:
13092           tls_gd = TLS_TPRELGD;
13093           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13094             goto tls_ldgd_opt;
13095           break;
13096
13097         case R_PPC64_GOT_TLSLD16:
13098         case R_PPC64_GOT_TLSLD16_LO:
13099           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13100             {
13101               unsigned int insn1, insn2, insn3;
13102               bfd_vma offset;
13103
13104             tls_ldgd_opt:
13105               offset = (bfd_vma) -1;
13106               /* If not using the newer R_PPC64_TLSGD/LD to mark
13107                  __tls_get_addr calls, we must trust that the call
13108                  stays with its arg setup insns, ie. that the next
13109                  reloc is the __tls_get_addr call associated with
13110                  the current reloc.  Edit both insns.  */
13111               if (input_section->has_tls_get_addr_call
13112                   && rel + 1 < relend
13113                   && branch_reloc_hash_match (input_bfd, rel + 1,
13114                                               htab->tls_get_addr,
13115                                               htab->tls_get_addr_fd))
13116                 offset = rel[1].r_offset;
13117               if ((tls_mask & tls_gd) != 0)
13118                 {
13119                   /* IE */
13120                   insn1 = bfd_get_32 (output_bfd,
13121                                       contents + rel->r_offset - d_offset);
13122                   insn1 &= (1 << 26) - (1 << 2);
13123                   insn1 |= 58 << 26;    /* ld */
13124                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13125                   if (offset != (bfd_vma) -1)
13126                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13127                   if ((tls_mask & TLS_EXPLICIT) == 0)
13128                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13129                               + R_PPC64_GOT_TPREL16_DS);
13130                   else
13131                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13132                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13133                 }
13134               else
13135                 {
13136                   /* LE */
13137                   insn1 = 0x3c6d0000;   /* addis 3,13,0 */
13138                   insn2 = 0x38630000;   /* addi 3,3,0 */
13139                   if (tls_gd == 0)
13140                     {
13141                       /* Was an LD reloc.  */
13142                       if (toc_symndx)
13143                         sec = local_sections[toc_symndx];
13144                       for (r_symndx = 0;
13145                            r_symndx < symtab_hdr->sh_info;
13146                            r_symndx++)
13147                         if (local_sections[r_symndx] == sec)
13148                           break;
13149                       if (r_symndx >= symtab_hdr->sh_info)
13150                         r_symndx = STN_UNDEF;
13151                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13152                       if (r_symndx != STN_UNDEF)
13153                         rel->r_addend -= (local_syms[r_symndx].st_value
13154                                           + sec->output_offset
13155                                           + sec->output_section->vma);
13156                     }
13157                   else if (toc_symndx != 0)
13158                     {
13159                       r_symndx = toc_symndx;
13160                       rel->r_addend = toc_addend;
13161                     }
13162                   r_type = R_PPC64_TPREL16_HA;
13163                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13164                   if (offset != (bfd_vma) -1)
13165                     {
13166                       rel[1].r_info = ELF64_R_INFO (r_symndx,
13167                                                     R_PPC64_TPREL16_LO);
13168                       rel[1].r_offset = offset + d_offset;
13169                       rel[1].r_addend = rel->r_addend;
13170                     }
13171                 }
13172               bfd_put_32 (output_bfd, insn1,
13173                           contents + rel->r_offset - d_offset);
13174               if (offset != (bfd_vma) -1)
13175                 {
13176                   insn3 = bfd_get_32 (output_bfd,
13177                                       contents + offset + 4);
13178                   if (insn3 == NOP
13179                       || insn3 == CROR_151515 || insn3 == CROR_313131)
13180                     {
13181                       rel[1].r_offset += 4;
13182                       bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13183                       insn2 = NOP;
13184                     }
13185                   bfd_put_32 (output_bfd, insn2, contents + offset);
13186                 }
13187               if ((tls_mask & tls_gd) == 0
13188                   && (tls_gd == 0 || toc_symndx != 0))
13189                 {
13190                   /* We changed the symbol.  Start over in order
13191                      to get h, sym, sec etc. right.  */
13192                   rel--;
13193                   continue;
13194                 }
13195             }
13196           break;
13197
13198         case R_PPC64_TLSGD:
13199           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13200             {
13201               unsigned int insn2, insn3;
13202               bfd_vma offset = rel->r_offset;
13203
13204               if ((tls_mask & TLS_TPRELGD) != 0)
13205                 {
13206                   /* IE */
13207                   r_type = R_PPC64_NONE;
13208                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13209                 }
13210               else
13211                 {
13212                   /* LE */
13213                   if (toc_symndx != 0)
13214                     {
13215                       r_symndx = toc_symndx;
13216                       rel->r_addend = toc_addend;
13217                     }
13218                   r_type = R_PPC64_TPREL16_LO;
13219                   rel->r_offset = offset + d_offset;
13220                   insn2 = 0x38630000;   /* addi 3,3,0 */
13221                 }
13222               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13223               /* Zap the reloc on the _tls_get_addr call too.  */
13224               BFD_ASSERT (offset == rel[1].r_offset);
13225               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13226               insn3 = bfd_get_32 (output_bfd,
13227                                   contents + offset + 4);
13228               if (insn3 == NOP
13229                   || insn3 == CROR_151515 || insn3 == CROR_313131)
13230                 {
13231                   rel->r_offset += 4;
13232                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13233                   insn2 = NOP;
13234                 }
13235               bfd_put_32 (output_bfd, insn2, contents + offset);
13236               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13237                 {
13238                   rel--;
13239                   continue;
13240                 }
13241             }
13242           break;
13243
13244         case R_PPC64_TLSLD:
13245           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13246             {
13247               unsigned int insn2, insn3;
13248               bfd_vma offset = rel->r_offset;
13249
13250               if (toc_symndx)
13251                 sec = local_sections[toc_symndx];
13252               for (r_symndx = 0;
13253                    r_symndx < symtab_hdr->sh_info;
13254                    r_symndx++)
13255                 if (local_sections[r_symndx] == sec)
13256                   break;
13257               if (r_symndx >= symtab_hdr->sh_info)
13258                 r_symndx = STN_UNDEF;
13259               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13260               if (r_symndx != STN_UNDEF)
13261                 rel->r_addend -= (local_syms[r_symndx].st_value
13262                                   + sec->output_offset
13263                                   + sec->output_section->vma);
13264
13265               r_type = R_PPC64_TPREL16_LO;
13266               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13267               rel->r_offset = offset + d_offset;
13268               /* Zap the reloc on the _tls_get_addr call too.  */
13269               BFD_ASSERT (offset == rel[1].r_offset);
13270               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13271               insn2 = 0x38630000;       /* addi 3,3,0 */
13272               insn3 = bfd_get_32 (output_bfd,
13273                                   contents + offset + 4);
13274               if (insn3 == NOP
13275                   || insn3 == CROR_151515 || insn3 == CROR_313131)
13276                 {
13277                   rel->r_offset += 4;
13278                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13279                   insn2 = NOP;
13280                 }
13281               bfd_put_32 (output_bfd, insn2, contents + offset);
13282               rel--;
13283               continue;
13284             }
13285           break;
13286
13287         case R_PPC64_DTPMOD64:
13288           if (rel + 1 < relend
13289               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13290               && rel[1].r_offset == rel->r_offset + 8)
13291             {
13292               if ((tls_mask & TLS_GD) == 0)
13293                 {
13294                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13295                   if ((tls_mask & TLS_TPRELGD) != 0)
13296                     r_type = R_PPC64_TPREL64;
13297                   else
13298                     {
13299                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13300                       r_type = R_PPC64_NONE;
13301                     }
13302                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13303                 }
13304             }
13305           else
13306             {
13307               if ((tls_mask & TLS_LD) == 0)
13308                 {
13309                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13310                   r_type = R_PPC64_NONE;
13311                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13312                 }
13313             }
13314           break;
13315
13316         case R_PPC64_TPREL64:
13317           if ((tls_mask & TLS_TPREL) == 0)
13318             {
13319               r_type = R_PPC64_NONE;
13320               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13321             }
13322           break;
13323         }
13324
13325       /* Handle other relocations that tweak non-addend part of insn.  */
13326       insn = 0;
13327       max_br_offset = 1 << 25;
13328       addend = rel->r_addend;
13329       reloc_dest = DEST_NORMAL;
13330       switch (r_type)
13331         {
13332         default:
13333           break;
13334
13335         case R_PPC64_TOCSAVE:
13336           if (relocation + addend == (rel->r_offset
13337                                       + input_section->output_offset
13338                                       + input_section->output_section->vma)
13339               && tocsave_find (htab, NO_INSERT,
13340                                &local_syms, rel, input_bfd))
13341             {
13342               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13343               if (insn == NOP
13344                   || insn == CROR_151515 || insn == CROR_313131)
13345                 bfd_put_32 (input_bfd,
13346                             STD_R2_0R1 + STK_TOC (htab),
13347                             contents + rel->r_offset);
13348             }
13349           break;
13350
13351           /* Branch taken prediction relocations.  */
13352         case R_PPC64_ADDR14_BRTAKEN:
13353         case R_PPC64_REL14_BRTAKEN:
13354           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
13355           /* Fall thru.  */
13356
13357           /* Branch not taken prediction relocations.  */
13358         case R_PPC64_ADDR14_BRNTAKEN:
13359         case R_PPC64_REL14_BRNTAKEN:
13360           insn |= bfd_get_32 (output_bfd,
13361                               contents + rel->r_offset) & ~(0x01 << 21);
13362           /* Fall thru.  */
13363
13364         case R_PPC64_REL14:
13365           max_br_offset = 1 << 15;
13366           /* Fall thru.  */
13367
13368         case R_PPC64_REL24:
13369           /* Calls to functions with a different TOC, such as calls to
13370              shared objects, need to alter the TOC pointer.  This is
13371              done using a linkage stub.  A REL24 branching to these
13372              linkage stubs needs to be followed by a nop, as the nop
13373              will be replaced with an instruction to restore the TOC
13374              base pointer.  */
13375           fdh = h;
13376           if (h != NULL
13377               && h->oh != NULL
13378               && h->oh->is_func_descriptor)
13379             fdh = ppc_follow_link (h->oh);
13380           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13381                                            htab);
13382           if (stub_entry != NULL
13383               && (stub_entry->stub_type == ppc_stub_plt_call
13384                   || stub_entry->stub_type == ppc_stub_plt_call_r2save
13385                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13386                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
13387             {
13388               bfd_boolean can_plt_call = FALSE;
13389
13390               /* All of these stubs will modify r2, so there must be a
13391                  branch and link followed by a nop.  The nop is
13392                  replaced by an insn to restore r2.  */
13393               if (rel->r_offset + 8 <= input_section->size)
13394                 {
13395                   unsigned long br;
13396
13397                   br = bfd_get_32 (input_bfd,
13398                                    contents + rel->r_offset);
13399                   if ((br & 1) != 0)
13400                     {
13401                       unsigned long nop;
13402
13403                       nop = bfd_get_32 (input_bfd,
13404                                         contents + rel->r_offset + 4);
13405                       if (nop == NOP
13406                           || nop == CROR_151515 || nop == CROR_313131)
13407                         {
13408                           if (h != NULL
13409                               && (h == htab->tls_get_addr_fd
13410                                   || h == htab->tls_get_addr)
13411                               && !htab->no_tls_get_addr_opt)
13412                             {
13413                               /* Special stub used, leave nop alone.  */
13414                             }
13415                           else
13416                             bfd_put_32 (input_bfd,
13417                                         LD_R2_0R1 + STK_TOC (htab),
13418                                         contents + rel->r_offset + 4);
13419                           can_plt_call = TRUE;
13420                         }
13421                     }
13422                 }
13423
13424               if (!can_plt_call && h != NULL)
13425                 {
13426                   const char *name = h->elf.root.root.string;
13427
13428                   if (*name == '.')
13429                     ++name;
13430
13431                   if (strncmp (name, "__libc_start_main", 17) == 0
13432                       && (name[17] == 0 || name[17] == '@'))
13433                     {
13434                       /* Allow crt1 branch to go via a toc adjusting
13435                          stub.  Other calls that never return could do
13436                          the same, if we could detect such.  */
13437                       can_plt_call = TRUE;
13438                     }
13439                 }
13440
13441               if (!can_plt_call)
13442                 {
13443                   /* g++ as of 20130507 emits self-calls without a
13444                      following nop.  This is arguably wrong since we
13445                      have conflicting information.  On the one hand a
13446                      global symbol and on the other a local call
13447                      sequence, but don't error for this special case.
13448                      It isn't possible to cheaply verify we have
13449                      exactly such a call.  Allow all calls to the same
13450                      section.  */
13451                   asection *code_sec = sec;
13452
13453                   if (get_opd_info (sec) != NULL)
13454                     {
13455                       bfd_vma off = (relocation + addend
13456                                      - sec->output_section->vma
13457                                      - sec->output_offset);
13458
13459                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
13460                     }
13461                   if (code_sec == input_section)
13462                     can_plt_call = TRUE;
13463                 }
13464
13465               if (!can_plt_call)
13466                 {
13467                   info->callbacks->einfo
13468                     (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13469                        "recompile with -fPIC"),
13470                      input_bfd, input_section, rel->r_offset, sym_name);
13471
13472                   bfd_set_error (bfd_error_bad_value);
13473                   ret = FALSE;
13474                 }
13475
13476               if (can_plt_call
13477                   && (stub_entry->stub_type == ppc_stub_plt_call
13478                       || stub_entry->stub_type == ppc_stub_plt_call_r2save))
13479                 unresolved_reloc = FALSE;
13480             }
13481
13482           if ((stub_entry == NULL
13483                || stub_entry->stub_type == ppc_stub_long_branch
13484                || stub_entry->stub_type == ppc_stub_plt_branch)
13485               && get_opd_info (sec) != NULL)
13486             {
13487               /* The branch destination is the value of the opd entry. */
13488               bfd_vma off = (relocation + addend
13489                              - sec->output_section->vma
13490                              - sec->output_offset);
13491               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
13492               if (dest != (bfd_vma) -1)
13493                 {
13494                   relocation = dest;
13495                   addend = 0;
13496                   reloc_dest = DEST_OPD;
13497                 }
13498             }
13499
13500           /* If the branch is out of reach we ought to have a long
13501              branch stub.  */
13502           from = (rel->r_offset
13503                   + input_section->output_offset
13504                   + input_section->output_section->vma);
13505
13506           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
13507                                                   ? fdh->elf.other
13508                                                   : sym->st_other);
13509
13510           if (stub_entry != NULL
13511               && (stub_entry->stub_type == ppc_stub_long_branch
13512                   || stub_entry->stub_type == ppc_stub_plt_branch)
13513               && (r_type == R_PPC64_ADDR14_BRTAKEN
13514                   || r_type == R_PPC64_ADDR14_BRNTAKEN
13515                   || (relocation + addend - from + max_br_offset
13516                       < 2 * max_br_offset)))
13517             /* Don't use the stub if this branch is in range.  */
13518             stub_entry = NULL;
13519
13520           if (stub_entry != NULL)
13521             {
13522               /* Munge up the value and addend so that we call the stub
13523                  rather than the procedure directly.  */
13524               relocation = (stub_entry->stub_offset
13525                             + stub_entry->stub_sec->output_offset
13526                             + stub_entry->stub_sec->output_section->vma);
13527               addend = 0;
13528               reloc_dest = DEST_STUB;
13529
13530               if ((stub_entry->stub_type == ppc_stub_plt_call
13531                    || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13532                   && (ALWAYS_EMIT_R2SAVE
13533                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13534                   && rel + 1 < relend
13535                   && rel[1].r_offset == rel->r_offset + 4
13536                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
13537                 relocation += 4;
13538             }
13539
13540           if (insn != 0)
13541             {
13542               if (is_isa_v2)
13543                 {
13544                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
13545                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
13546                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
13547                   if ((insn & (0x14 << 21)) == (0x04 << 21))
13548                     insn |= 0x02 << 21;
13549                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
13550                     insn |= 0x08 << 21;
13551                   else
13552                     break;
13553                 }
13554               else
13555                 {
13556                   /* Invert 'y' bit if not the default.  */
13557                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
13558                     insn ^= 0x01 << 21;
13559                 }
13560
13561               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
13562             }
13563
13564           /* NOP out calls to undefined weak functions.
13565              We can thus call a weak function without first
13566              checking whether the function is defined.  */
13567           else if (h != NULL
13568                    && h->elf.root.type == bfd_link_hash_undefweak
13569                    && h->elf.dynindx == -1
13570                    && r_type == R_PPC64_REL24
13571                    && relocation == 0
13572                    && addend == 0)
13573             {
13574               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13575               continue;
13576             }
13577           break;
13578         }
13579
13580       /* Set `addend'.  */
13581       tls_type = 0;
13582       switch (r_type)
13583         {
13584         default:
13585           info->callbacks->einfo
13586             (_("%P: %B: unknown relocation type %d for `%T'\n"),
13587              input_bfd, (int) r_type, sym_name);
13588
13589           bfd_set_error (bfd_error_bad_value);
13590           ret = FALSE;
13591           continue;
13592
13593         case R_PPC64_NONE:
13594         case R_PPC64_TLS:
13595         case R_PPC64_TLSGD:
13596         case R_PPC64_TLSLD:
13597         case R_PPC64_TOCSAVE:
13598         case R_PPC64_GNU_VTINHERIT:
13599         case R_PPC64_GNU_VTENTRY:
13600           continue;
13601
13602           /* GOT16 relocations.  Like an ADDR16 using the symbol's
13603              address in the GOT as relocation value instead of the
13604              symbol's value itself.  Also, create a GOT entry for the
13605              symbol and put the symbol value there.  */
13606         case R_PPC64_GOT_TLSGD16:
13607         case R_PPC64_GOT_TLSGD16_LO:
13608         case R_PPC64_GOT_TLSGD16_HI:
13609         case R_PPC64_GOT_TLSGD16_HA:
13610           tls_type = TLS_TLS | TLS_GD;
13611           goto dogot;
13612
13613         case R_PPC64_GOT_TLSLD16:
13614         case R_PPC64_GOT_TLSLD16_LO:
13615         case R_PPC64_GOT_TLSLD16_HI:
13616         case R_PPC64_GOT_TLSLD16_HA:
13617           tls_type = TLS_TLS | TLS_LD;
13618           goto dogot;
13619
13620         case R_PPC64_GOT_TPREL16_DS:
13621         case R_PPC64_GOT_TPREL16_LO_DS:
13622         case R_PPC64_GOT_TPREL16_HI:
13623         case R_PPC64_GOT_TPREL16_HA:
13624           tls_type = TLS_TLS | TLS_TPREL;
13625           goto dogot;
13626
13627         case R_PPC64_GOT_DTPREL16_DS:
13628         case R_PPC64_GOT_DTPREL16_LO_DS:
13629         case R_PPC64_GOT_DTPREL16_HI:
13630         case R_PPC64_GOT_DTPREL16_HA:
13631           tls_type = TLS_TLS | TLS_DTPREL;
13632           goto dogot;
13633
13634         case R_PPC64_GOT16:
13635         case R_PPC64_GOT16_LO:
13636         case R_PPC64_GOT16_HI:
13637         case R_PPC64_GOT16_HA:
13638         case R_PPC64_GOT16_DS:
13639         case R_PPC64_GOT16_LO_DS:
13640         dogot:
13641           {
13642             /* Relocation is to the entry for this symbol in the global
13643                offset table.  */
13644             asection *got;
13645             bfd_vma *offp;
13646             bfd_vma off;
13647             unsigned long indx = 0;
13648             struct got_entry *ent;
13649
13650             if (tls_type == (TLS_TLS | TLS_LD)
13651                 && (h == NULL
13652                     || !h->elf.def_dynamic))
13653               ent = ppc64_tlsld_got (input_bfd);
13654             else
13655               {
13656
13657                 if (h != NULL)
13658                   {
13659                     bfd_boolean dyn = htab->elf.dynamic_sections_created;
13660                     if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
13661                                                           &h->elf)
13662                         || (info->shared
13663                             && SYMBOL_CALLS_LOCAL (info, &h->elf)))
13664                       /* This is actually a static link, or it is a
13665                          -Bsymbolic link and the symbol is defined
13666                          locally, or the symbol was forced to be local
13667                          because of a version file.  */
13668                       ;
13669                     else
13670                       {
13671                         BFD_ASSERT (h->elf.dynindx != -1);
13672                         indx = h->elf.dynindx;
13673                         unresolved_reloc = FALSE;
13674                       }
13675                     ent = h->elf.got.glist;
13676                   }
13677                 else
13678                   {
13679                     if (local_got_ents == NULL)
13680                       abort ();
13681                     ent = local_got_ents[r_symndx];
13682                   }
13683
13684                 for (; ent != NULL; ent = ent->next)
13685                   if (ent->addend == orig_rel.r_addend
13686                       && ent->owner == input_bfd
13687                       && ent->tls_type == tls_type)
13688                     break;
13689               }
13690
13691             if (ent == NULL)
13692               abort ();
13693             if (ent->is_indirect)
13694               ent = ent->got.ent;
13695             offp = &ent->got.offset;
13696             got = ppc64_elf_tdata (ent->owner)->got;
13697             if (got == NULL)
13698               abort ();
13699
13700             /* The offset must always be a multiple of 8.  We use the
13701                least significant bit to record whether we have already
13702                processed this entry.  */
13703             off = *offp;
13704             if ((off & 1) != 0)
13705               off &= ~1;
13706             else
13707               {
13708                 /* Generate relocs for the dynamic linker, except in
13709                    the case of TLSLD where we'll use one entry per
13710                    module.  */
13711                 asection *relgot;
13712                 bfd_boolean ifunc;
13713
13714                 *offp = off | 1;
13715                 relgot = NULL;
13716                 ifunc = (h != NULL
13717                          ? h->elf.type == STT_GNU_IFUNC
13718                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
13719                 if (ifunc)
13720                   relgot = htab->reliplt;
13721                 else if ((info->shared || indx != 0)
13722                          && (h == NULL
13723                              || (tls_type == (TLS_TLS | TLS_LD)
13724                                  && !h->elf.def_dynamic)
13725                              || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13726                              || h->elf.root.type != bfd_link_hash_undefweak))
13727                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
13728                 if (relgot != NULL)
13729                   {
13730                     outrel.r_offset = (got->output_section->vma
13731                                        + got->output_offset
13732                                        + off);
13733                     outrel.r_addend = addend;
13734                     if (tls_type & (TLS_LD | TLS_GD))
13735                       {
13736                         outrel.r_addend = 0;
13737                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
13738                         if (tls_type == (TLS_TLS | TLS_GD))
13739                           {
13740                             loc = relgot->contents;
13741                             loc += (relgot->reloc_count++
13742                                     * sizeof (Elf64_External_Rela));
13743                             bfd_elf64_swap_reloca_out (output_bfd,
13744                                                        &outrel, loc);
13745                             outrel.r_offset += 8;
13746                             outrel.r_addend = addend;
13747                             outrel.r_info
13748                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
13749                           }
13750                       }
13751                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
13752                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
13753                     else if (tls_type == (TLS_TLS | TLS_TPREL))
13754                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
13755                     else if (indx != 0)
13756                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
13757                     else
13758                       {
13759                         if (ifunc)
13760                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13761                         else
13762                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13763
13764                         /* Write the .got section contents for the sake
13765                            of prelink.  */
13766                         loc = got->contents + off;
13767                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
13768                                     loc);
13769                       }
13770
13771                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
13772                       {
13773                         outrel.r_addend += relocation;
13774                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
13775                           outrel.r_addend -= htab->elf.tls_sec->vma;
13776                       }
13777                     loc = relgot->contents;
13778                     loc += (relgot->reloc_count++
13779                             * sizeof (Elf64_External_Rela));
13780                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13781                   }
13782
13783                 /* Init the .got section contents here if we're not
13784                    emitting a reloc.  */
13785                 else
13786                   {
13787                     relocation += addend;
13788                     if (tls_type == (TLS_TLS | TLS_LD))
13789                       relocation = 1;
13790                     else if (tls_type != 0)
13791                       {
13792                         relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
13793                         if (tls_type == (TLS_TLS | TLS_TPREL))
13794                           relocation += DTP_OFFSET - TP_OFFSET;
13795
13796                         if (tls_type == (TLS_TLS | TLS_GD))
13797                           {
13798                             bfd_put_64 (output_bfd, relocation,
13799                                         got->contents + off + 8);
13800                             relocation = 1;
13801                           }
13802                       }
13803
13804                     bfd_put_64 (output_bfd, relocation,
13805                                 got->contents + off);
13806                   }
13807               }
13808
13809             if (off >= (bfd_vma) -2)
13810               abort ();
13811
13812             relocation = got->output_section->vma + got->output_offset + off;
13813             addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
13814           }
13815           break;
13816
13817         case R_PPC64_PLT16_HA:
13818         case R_PPC64_PLT16_HI:
13819         case R_PPC64_PLT16_LO:
13820         case R_PPC64_PLT32:
13821         case R_PPC64_PLT64:
13822           /* Relocation is to the entry for this symbol in the
13823              procedure linkage table.  */
13824
13825           /* Resolve a PLT reloc against a local symbol directly,
13826              without using the procedure linkage table.  */
13827           if (h == NULL)
13828             break;
13829
13830           /* It's possible that we didn't make a PLT entry for this
13831              symbol.  This happens when statically linking PIC code,
13832              or when using -Bsymbolic.  Go find a match if there is a
13833              PLT entry.  */
13834           if (htab->plt != NULL)
13835             {
13836               struct plt_entry *ent;
13837               for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
13838                 if (ent->addend == orig_rel.r_addend
13839                     && ent->plt.offset != (bfd_vma) -1)
13840                   {
13841                     relocation = (htab->plt->output_section->vma
13842                                   + htab->plt->output_offset
13843                                   + ent->plt.offset);
13844                     unresolved_reloc = FALSE;
13845                   }
13846             }
13847           break;
13848
13849         case R_PPC64_TOC:
13850           /* Relocation value is TOC base.  */
13851           relocation = TOCstart;
13852           if (r_symndx == STN_UNDEF)
13853             relocation += htab->stub_group[input_section->id].toc_off;
13854           else if (unresolved_reloc)
13855             ;
13856           else if (sec != NULL && sec->id <= htab->top_id)
13857             relocation += htab->stub_group[sec->id].toc_off;
13858           else
13859             unresolved_reloc = TRUE;
13860           goto dodyn;
13861
13862           /* TOC16 relocs.  We want the offset relative to the TOC base,
13863              which is the address of the start of the TOC plus 0x8000.
13864              The TOC consists of sections .got, .toc, .tocbss, and .plt,
13865              in this order.  */
13866         case R_PPC64_TOC16:
13867         case R_PPC64_TOC16_LO:
13868         case R_PPC64_TOC16_HI:
13869         case R_PPC64_TOC16_DS:
13870         case R_PPC64_TOC16_LO_DS:
13871         case R_PPC64_TOC16_HA:
13872           addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
13873           break;
13874
13875           /* Relocate against the beginning of the section.  */
13876         case R_PPC64_SECTOFF:
13877         case R_PPC64_SECTOFF_LO:
13878         case R_PPC64_SECTOFF_HI:
13879         case R_PPC64_SECTOFF_DS:
13880         case R_PPC64_SECTOFF_LO_DS:
13881         case R_PPC64_SECTOFF_HA:
13882           if (sec != NULL)
13883             addend -= sec->output_section->vma;
13884           break;
13885
13886         case R_PPC64_REL16:
13887         case R_PPC64_REL16_LO:
13888         case R_PPC64_REL16_HI:
13889         case R_PPC64_REL16_HA:
13890           break;
13891
13892         case R_PPC64_REL14:
13893         case R_PPC64_REL14_BRNTAKEN:
13894         case R_PPC64_REL14_BRTAKEN:
13895         case R_PPC64_REL24:
13896           break;
13897
13898         case R_PPC64_TPREL16:
13899         case R_PPC64_TPREL16_LO:
13900         case R_PPC64_TPREL16_HI:
13901         case R_PPC64_TPREL16_HA:
13902         case R_PPC64_TPREL16_DS:
13903         case R_PPC64_TPREL16_LO_DS:
13904         case R_PPC64_TPREL16_HIGH:
13905         case R_PPC64_TPREL16_HIGHA:
13906         case R_PPC64_TPREL16_HIGHER:
13907         case R_PPC64_TPREL16_HIGHERA:
13908         case R_PPC64_TPREL16_HIGHEST:
13909         case R_PPC64_TPREL16_HIGHESTA:
13910           if (h != NULL
13911               && h->elf.root.type == bfd_link_hash_undefweak
13912               && h->elf.dynindx == -1)
13913             {
13914               /* Make this relocation against an undefined weak symbol
13915                  resolve to zero.  This is really just a tweak, since
13916                  code using weak externs ought to check that they are
13917                  defined before using them.  */
13918               bfd_byte *p = contents + rel->r_offset - d_offset;
13919
13920               insn = bfd_get_32 (output_bfd, p);
13921               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
13922               if (insn != 0)
13923                 bfd_put_32 (output_bfd, insn, p);
13924               break;
13925             }
13926           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13927           if (info->shared)
13928             /* The TPREL16 relocs shouldn't really be used in shared
13929                libs as they will result in DT_TEXTREL being set, but
13930                support them anyway.  */
13931             goto dodyn;
13932           break;
13933
13934         case R_PPC64_DTPREL16:
13935         case R_PPC64_DTPREL16_LO:
13936         case R_PPC64_DTPREL16_HI:
13937         case R_PPC64_DTPREL16_HA:
13938         case R_PPC64_DTPREL16_DS:
13939         case R_PPC64_DTPREL16_LO_DS:
13940         case R_PPC64_DTPREL16_HIGH:
13941         case R_PPC64_DTPREL16_HIGHA:
13942         case R_PPC64_DTPREL16_HIGHER:
13943         case R_PPC64_DTPREL16_HIGHERA:
13944         case R_PPC64_DTPREL16_HIGHEST:
13945         case R_PPC64_DTPREL16_HIGHESTA:
13946           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13947           break;
13948
13949         case R_PPC64_DTPMOD64:
13950           relocation = 1;
13951           addend = 0;
13952           goto dodyn;
13953
13954         case R_PPC64_TPREL64:
13955           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13956           goto dodyn;
13957
13958         case R_PPC64_DTPREL64:
13959           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13960           /* Fall thru */
13961
13962           /* Relocations that may need to be propagated if this is a
13963              dynamic object.  */
13964         case R_PPC64_REL30:
13965         case R_PPC64_REL32:
13966         case R_PPC64_REL64:
13967         case R_PPC64_ADDR14:
13968         case R_PPC64_ADDR14_BRNTAKEN:
13969         case R_PPC64_ADDR14_BRTAKEN:
13970         case R_PPC64_ADDR16:
13971         case R_PPC64_ADDR16_DS:
13972         case R_PPC64_ADDR16_HA:
13973         case R_PPC64_ADDR16_HI:
13974         case R_PPC64_ADDR16_HIGH:
13975         case R_PPC64_ADDR16_HIGHA:
13976         case R_PPC64_ADDR16_HIGHER:
13977         case R_PPC64_ADDR16_HIGHERA:
13978         case R_PPC64_ADDR16_HIGHEST:
13979         case R_PPC64_ADDR16_HIGHESTA:
13980         case R_PPC64_ADDR16_LO:
13981         case R_PPC64_ADDR16_LO_DS:
13982         case R_PPC64_ADDR24:
13983         case R_PPC64_ADDR32:
13984         case R_PPC64_ADDR64:
13985         case R_PPC64_UADDR16:
13986         case R_PPC64_UADDR32:
13987         case R_PPC64_UADDR64:
13988         dodyn:
13989           if ((input_section->flags & SEC_ALLOC) == 0)
13990             break;
13991
13992           if (NO_OPD_RELOCS && is_opd)
13993             break;
13994
13995           if ((info->shared
13996                && (h == NULL
13997                    || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13998                    || h->elf.root.type != bfd_link_hash_undefweak)
13999                && (must_be_dyn_reloc (info, r_type)
14000                    || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
14001               || (ELIMINATE_COPY_RELOCS
14002                   && !info->shared
14003                   && h != NULL
14004                   && h->elf.dynindx != -1
14005                   && !h->elf.non_got_ref
14006                   && !h->elf.def_regular)
14007               || (!info->shared
14008                   && (h != NULL
14009                       ? h->elf.type == STT_GNU_IFUNC
14010                       : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
14011             {
14012               bfd_boolean skip, relocate;
14013               asection *sreloc;
14014               bfd_vma out_off;
14015
14016               /* When generating a dynamic object, these relocations
14017                  are copied into the output file to be resolved at run
14018                  time.  */
14019
14020               skip = FALSE;
14021               relocate = FALSE;
14022
14023               out_off = _bfd_elf_section_offset (output_bfd, info,
14024                                                  input_section, rel->r_offset);
14025               if (out_off == (bfd_vma) -1)
14026                 skip = TRUE;
14027               else if (out_off == (bfd_vma) -2)
14028                 skip = TRUE, relocate = TRUE;
14029               out_off += (input_section->output_section->vma
14030                           + input_section->output_offset);
14031               outrel.r_offset = out_off;
14032               outrel.r_addend = rel->r_addend;
14033
14034               /* Optimize unaligned reloc use.  */
14035               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14036                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14037                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14038               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14039                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14040                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14041               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14042                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14043                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14044
14045               if (skip)
14046                 memset (&outrel, 0, sizeof outrel);
14047               else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
14048                        && !is_opd
14049                        && r_type != R_PPC64_TOC)
14050                 {
14051                   BFD_ASSERT (h->elf.dynindx != -1);
14052                   outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14053                 }
14054               else
14055                 {
14056                   /* This symbol is local, or marked to become local,
14057                      or this is an opd section reloc which must point
14058                      at a local function.  */
14059                   outrel.r_addend += relocation;
14060                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14061                     {
14062                       if (is_opd && h != NULL)
14063                         {
14064                           /* Lie about opd entries.  This case occurs
14065                              when building shared libraries and we
14066                              reference a function in another shared
14067                              lib.  The same thing happens for a weak
14068                              definition in an application that's
14069                              overridden by a strong definition in a
14070                              shared lib.  (I believe this is a generic
14071                              bug in binutils handling of weak syms.)
14072                              In these cases we won't use the opd
14073                              entry in this lib.  */
14074                           unresolved_reloc = FALSE;
14075                         }
14076                       if (!is_opd
14077                           && r_type == R_PPC64_ADDR64
14078                           && (h != NULL
14079                               ? h->elf.type == STT_GNU_IFUNC
14080                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14081                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14082                       else
14083                         {
14084                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14085
14086                           /* We need to relocate .opd contents for ld.so.
14087                              Prelink also wants simple and consistent rules
14088                              for relocs.  This make all RELATIVE relocs have
14089                              *r_offset equal to r_addend.  */
14090                           relocate = TRUE;
14091                         }
14092                     }
14093                   else
14094                     {
14095                       long indx = 0;
14096
14097                       if (h != NULL
14098                           ? h->elf.type == STT_GNU_IFUNC
14099                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14100                         {
14101                           info->callbacks->einfo
14102                             (_("%P: %H: %s for indirect "
14103                                "function `%T' unsupported\n"),
14104                              input_bfd, input_section, rel->r_offset,
14105                              ppc64_elf_howto_table[r_type]->name,
14106                              sym_name);
14107                           ret = FALSE;
14108                         }
14109                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14110                         ;
14111                       else if (sec == NULL || sec->owner == NULL)
14112                         {
14113                           bfd_set_error (bfd_error_bad_value);
14114                           return FALSE;
14115                         }
14116                       else
14117                         {
14118                           asection *osec;
14119
14120                           osec = sec->output_section;
14121                           indx = elf_section_data (osec)->dynindx;
14122
14123                           if (indx == 0)
14124                             {
14125                               if ((osec->flags & SEC_READONLY) == 0
14126                                   && htab->elf.data_index_section != NULL)
14127                                 osec = htab->elf.data_index_section;
14128                               else
14129                                 osec = htab->elf.text_index_section;
14130                               indx = elf_section_data (osec)->dynindx;
14131                             }
14132                           BFD_ASSERT (indx != 0);
14133
14134                           /* We are turning this relocation into one
14135                              against a section symbol, so subtract out
14136                              the output section's address but not the
14137                              offset of the input section in the output
14138                              section.  */
14139                           outrel.r_addend -= osec->vma;
14140                         }
14141
14142                       outrel.r_info = ELF64_R_INFO (indx, r_type);
14143                     }
14144                 }
14145
14146               sreloc = elf_section_data (input_section)->sreloc;
14147               if (h != NULL
14148                   ? h->elf.type == STT_GNU_IFUNC
14149                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14150                 sreloc = htab->reliplt;
14151               if (sreloc == NULL)
14152                 abort ();
14153
14154               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14155                   >= sreloc->size)
14156                 abort ();
14157               loc = sreloc->contents;
14158               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14159               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14160
14161               /* If this reloc is against an external symbol, it will
14162                  be computed at runtime, so there's no need to do
14163                  anything now.  However, for the sake of prelink ensure
14164                  that the section contents are a known value.  */
14165               if (! relocate)
14166                 {
14167                   unresolved_reloc = FALSE;
14168                   /* The value chosen here is quite arbitrary as ld.so
14169                      ignores section contents except for the special
14170                      case of .opd where the contents might be accessed
14171                      before relocation.  Choose zero, as that won't
14172                      cause reloc overflow.  */
14173                   relocation = 0;
14174                   addend = 0;
14175                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14176                      to improve backward compatibility with older
14177                      versions of ld.  */
14178                   if (r_type == R_PPC64_ADDR64)
14179                     addend = outrel.r_addend;
14180                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
14181                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
14182                     addend = (input_section->output_section->vma
14183                               + input_section->output_offset
14184                               + rel->r_offset);
14185                 }
14186             }
14187           break;
14188
14189         case R_PPC64_COPY:
14190         case R_PPC64_GLOB_DAT:
14191         case R_PPC64_JMP_SLOT:
14192         case R_PPC64_JMP_IREL:
14193         case R_PPC64_RELATIVE:
14194           /* We shouldn't ever see these dynamic relocs in relocatable
14195              files.  */
14196           /* Fall through.  */
14197
14198         case R_PPC64_PLTGOT16:
14199         case R_PPC64_PLTGOT16_DS:
14200         case R_PPC64_PLTGOT16_HA:
14201         case R_PPC64_PLTGOT16_HI:
14202         case R_PPC64_PLTGOT16_LO:
14203         case R_PPC64_PLTGOT16_LO_DS:
14204         case R_PPC64_PLTREL32:
14205         case R_PPC64_PLTREL64:
14206           /* These ones haven't been implemented yet.  */
14207
14208           info->callbacks->einfo
14209             (_("%P: %B: %s is not supported for `%T'\n"),
14210              input_bfd,
14211              ppc64_elf_howto_table[r_type]->name, sym_name);
14212
14213           bfd_set_error (bfd_error_invalid_operation);
14214           ret = FALSE;
14215           continue;
14216         }
14217
14218       /* Multi-instruction sequences that access the TOC can be
14219          optimized, eg. addis ra,r2,0; addi rb,ra,x;
14220          to             nop;           addi rb,r2,x;  */
14221       switch (r_type)
14222         {
14223         default:
14224           break;
14225
14226         case R_PPC64_GOT_TLSLD16_HI:
14227         case R_PPC64_GOT_TLSGD16_HI:
14228         case R_PPC64_GOT_TPREL16_HI:
14229         case R_PPC64_GOT_DTPREL16_HI:
14230         case R_PPC64_GOT16_HI:
14231         case R_PPC64_TOC16_HI:
14232           /* These relocs would only be useful if building up an
14233              offset to later add to r2, perhaps in an indexed
14234              addressing mode instruction.  Don't try to optimize.
14235              Unfortunately, the possibility of someone building up an
14236              offset like this or even with the HA relocs, means that
14237              we need to check the high insn when optimizing the low
14238              insn.  */
14239           break;
14240
14241         case R_PPC64_GOT_TLSLD16_HA:
14242         case R_PPC64_GOT_TLSGD16_HA:
14243         case R_PPC64_GOT_TPREL16_HA:
14244         case R_PPC64_GOT_DTPREL16_HA:
14245         case R_PPC64_GOT16_HA:
14246         case R_PPC64_TOC16_HA:
14247           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14248               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14249             {
14250               bfd_byte *p = contents + (rel->r_offset & ~3);
14251               bfd_put_32 (input_bfd, NOP, p);
14252             }
14253           break;
14254
14255         case R_PPC64_GOT_TLSLD16_LO:
14256         case R_PPC64_GOT_TLSGD16_LO:
14257         case R_PPC64_GOT_TPREL16_LO_DS:
14258         case R_PPC64_GOT_DTPREL16_LO_DS:
14259         case R_PPC64_GOT16_LO:
14260         case R_PPC64_GOT16_LO_DS:
14261         case R_PPC64_TOC16_LO:
14262         case R_PPC64_TOC16_LO_DS:
14263           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14264               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14265             {
14266               bfd_byte *p = contents + (rel->r_offset & ~3);
14267               insn = bfd_get_32 (input_bfd, p);
14268               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14269                 {
14270                   /* Transform addic to addi when we change reg.  */
14271                   insn &= ~((0x3f << 26) | (0x1f << 16));
14272                   insn |= (14u << 26) | (2 << 16);
14273                 }
14274               else
14275                 {
14276                   insn &= ~(0x1f << 16);
14277                   insn |= 2 << 16;
14278                 }
14279               bfd_put_32 (input_bfd, insn, p);
14280             }
14281           break;
14282         }
14283
14284       /* Do any further special processing.  */
14285       switch (r_type)
14286         {
14287         default:
14288           break;
14289
14290         case R_PPC64_REL16_HA:
14291         case R_PPC64_ADDR16_HA:
14292         case R_PPC64_ADDR16_HIGHA:
14293         case R_PPC64_ADDR16_HIGHERA:
14294         case R_PPC64_ADDR16_HIGHESTA:
14295         case R_PPC64_TOC16_HA:
14296         case R_PPC64_SECTOFF_HA:
14297         case R_PPC64_TPREL16_HA:
14298         case R_PPC64_TPREL16_HIGHA:
14299         case R_PPC64_TPREL16_HIGHERA:
14300         case R_PPC64_TPREL16_HIGHESTA:
14301         case R_PPC64_DTPREL16_HA:
14302         case R_PPC64_DTPREL16_HIGHA:
14303         case R_PPC64_DTPREL16_HIGHERA:
14304         case R_PPC64_DTPREL16_HIGHESTA:
14305           /* It's just possible that this symbol is a weak symbol
14306              that's not actually defined anywhere. In that case,
14307              'sec' would be NULL, and we should leave the symbol
14308              alone (it will be set to zero elsewhere in the link).  */
14309           if (sec == NULL)
14310             break;
14311           /* Fall thru */
14312
14313         case R_PPC64_GOT16_HA:
14314         case R_PPC64_PLTGOT16_HA:
14315         case R_PPC64_PLT16_HA:
14316         case R_PPC64_GOT_TLSGD16_HA:
14317         case R_PPC64_GOT_TLSLD16_HA:
14318         case R_PPC64_GOT_TPREL16_HA:
14319         case R_PPC64_GOT_DTPREL16_HA:
14320           /* Add 0x10000 if sign bit in 0:15 is set.
14321              Bits 0:15 are not used.  */
14322           addend += 0x8000;
14323           break;
14324
14325         case R_PPC64_ADDR16_DS:
14326         case R_PPC64_ADDR16_LO_DS:
14327         case R_PPC64_GOT16_DS:
14328         case R_PPC64_GOT16_LO_DS:
14329         case R_PPC64_PLT16_LO_DS:
14330         case R_PPC64_SECTOFF_DS:
14331         case R_PPC64_SECTOFF_LO_DS:
14332         case R_PPC64_TOC16_DS:
14333         case R_PPC64_TOC16_LO_DS:
14334         case R_PPC64_PLTGOT16_DS:
14335         case R_PPC64_PLTGOT16_LO_DS:
14336         case R_PPC64_GOT_TPREL16_DS:
14337         case R_PPC64_GOT_TPREL16_LO_DS:
14338         case R_PPC64_GOT_DTPREL16_DS:
14339         case R_PPC64_GOT_DTPREL16_LO_DS:
14340         case R_PPC64_TPREL16_DS:
14341         case R_PPC64_TPREL16_LO_DS:
14342         case R_PPC64_DTPREL16_DS:
14343         case R_PPC64_DTPREL16_LO_DS:
14344           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14345           mask = 3;
14346           /* If this reloc is against an lq insn, then the value must be
14347              a multiple of 16.  This is somewhat of a hack, but the
14348              "correct" way to do this by defining _DQ forms of all the
14349              _DS relocs bloats all reloc switches in this file.  It
14350              doesn't seem to make much sense to use any of these relocs
14351              in data, so testing the insn should be safe.  */
14352           if ((insn & (0x3f << 26)) == (56u << 26))
14353             mask = 15;
14354           if (((relocation + addend) & mask) != 0)
14355             {
14356               info->callbacks->einfo
14357                 (_("%P: %H: error: %s not a multiple of %u\n"),
14358                  input_bfd, input_section, rel->r_offset,
14359                  ppc64_elf_howto_table[r_type]->name,
14360                  mask + 1);
14361               bfd_set_error (bfd_error_bad_value);
14362               ret = FALSE;
14363               continue;
14364             }
14365           break;
14366         }
14367
14368       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
14369          because such sections are not SEC_ALLOC and thus ld.so will
14370          not process them.  */
14371       if (unresolved_reloc
14372           && !((input_section->flags & SEC_DEBUGGING) != 0
14373                && h->elf.def_dynamic)
14374           && _bfd_elf_section_offset (output_bfd, info, input_section,
14375                                       rel->r_offset) != (bfd_vma) -1)
14376         {
14377           info->callbacks->einfo
14378             (_("%P: %H: unresolvable %s against `%T'\n"),
14379              input_bfd, input_section, rel->r_offset,
14380              ppc64_elf_howto_table[(int) r_type]->name,
14381              h->elf.root.root.string);
14382           ret = FALSE;
14383         }
14384
14385       r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
14386                                     input_bfd,
14387                                     input_section,
14388                                     contents,
14389                                     rel->r_offset,
14390                                     relocation,
14391                                     addend);
14392
14393       if (r != bfd_reloc_ok)
14394         {
14395           char *more_info = NULL;
14396           const char *reloc_name = ppc64_elf_howto_table[r_type]->name;
14397
14398           if (reloc_dest != DEST_NORMAL)
14399             {
14400               more_info = bfd_malloc (strlen (reloc_name) + 8);
14401               if (more_info != NULL)
14402                 {
14403                   strcpy (more_info, reloc_name);
14404                   strcat (more_info, (reloc_dest == DEST_OPD
14405                                       ? " (OPD)" : " (stub)"));
14406                   reloc_name = more_info;
14407                 }
14408             }
14409
14410           if (r == bfd_reloc_overflow)
14411             {
14412               if (warned)
14413                 continue;
14414               if (h != NULL
14415                   && h->elf.root.type == bfd_link_hash_undefweak
14416                   && ppc64_elf_howto_table[r_type]->pc_relative)
14417                 {
14418                   /* Assume this is a call protected by other code that
14419                      detects the symbol is undefined.  If this is the case,
14420                      we can safely ignore the overflow.  If not, the
14421                      program is hosed anyway, and a little warning isn't
14422                      going to help.  */
14423
14424                   continue;
14425                 }
14426
14427               if (!((*info->callbacks->reloc_overflow)
14428                     (info, &h->elf.root, sym_name,
14429                      reloc_name, orig_rel.r_addend,
14430                      input_bfd, input_section, rel->r_offset)))
14431                 return FALSE;
14432             }
14433           else
14434             {
14435               info->callbacks->einfo
14436                 (_("%P: %H: %s against `%T': error %d\n"),
14437                  input_bfd, input_section, rel->r_offset,
14438                  reloc_name, sym_name, (int) r);
14439               ret = FALSE;
14440             }
14441           if (more_info != NULL)
14442             free (more_info);
14443         }
14444     }
14445
14446   /* If we're emitting relocations, then shortly after this function
14447      returns, reloc offsets and addends for this section will be
14448      adjusted.  Worse, reloc symbol indices will be for the output
14449      file rather than the input.  Save a copy of the relocs for
14450      opd_entry_value.  */
14451   if (is_opd && (info->emitrelocations || info->relocatable))
14452     {
14453       bfd_size_type amt;
14454       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
14455       rel = bfd_alloc (input_bfd, amt);
14456       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
14457       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
14458       if (rel == NULL)
14459         return FALSE;
14460       memcpy (rel, relocs, amt);
14461     }
14462   return ret;
14463 }
14464
14465 /* Adjust the value of any local symbols in opd sections.  */
14466
14467 static int
14468 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
14469                               const char *name ATTRIBUTE_UNUSED,
14470                               Elf_Internal_Sym *elfsym,
14471                               asection *input_sec,
14472                               struct elf_link_hash_entry *h)
14473 {
14474   struct _opd_sec_data *opd;
14475   long adjust;
14476   bfd_vma value;
14477
14478   if (h != NULL)
14479     return 1;
14480
14481   opd = get_opd_info (input_sec);
14482   if (opd == NULL || opd->adjust == NULL)
14483     return 1;
14484
14485   value = elfsym->st_value - input_sec->output_offset;
14486   if (!info->relocatable)
14487     value -= input_sec->output_section->vma;
14488
14489   adjust = opd->adjust[value / 8];
14490   if (adjust == -1)
14491     return 2;
14492
14493   elfsym->st_value += adjust;
14494   return 1;
14495 }
14496
14497 /* Finish up dynamic symbol handling.  We set the contents of various
14498    dynamic sections here.  */
14499
14500 static bfd_boolean
14501 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
14502                                  struct bfd_link_info *info,
14503                                  struct elf_link_hash_entry *h,
14504                                  Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
14505 {
14506   struct ppc_link_hash_table *htab;
14507   struct plt_entry *ent;
14508   Elf_Internal_Rela rela;
14509   bfd_byte *loc;
14510
14511   htab = ppc_hash_table (info);
14512   if (htab == NULL)
14513     return FALSE;
14514
14515   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14516     if (ent->plt.offset != (bfd_vma) -1)
14517       {
14518         /* This symbol has an entry in the procedure linkage
14519            table.  Set it up.  */
14520         if (!htab->elf.dynamic_sections_created
14521             || h->dynindx == -1)
14522           {
14523             BFD_ASSERT (h->type == STT_GNU_IFUNC
14524                         && h->def_regular
14525                         && (h->root.type == bfd_link_hash_defined
14526                             || h->root.type == bfd_link_hash_defweak));
14527             rela.r_offset = (htab->iplt->output_section->vma
14528                              + htab->iplt->output_offset
14529                              + ent->plt.offset);
14530             if (htab->opd_abi)
14531               rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14532             else
14533               rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14534             rela.r_addend = (h->root.u.def.value
14535                              + h->root.u.def.section->output_offset
14536                              + h->root.u.def.section->output_section->vma
14537                              + ent->addend);
14538             loc = (htab->reliplt->contents
14539                    + (htab->reliplt->reloc_count++
14540                       * sizeof (Elf64_External_Rela)));
14541           }
14542         else
14543           {
14544             rela.r_offset = (htab->plt->output_section->vma
14545                              + htab->plt->output_offset
14546                              + ent->plt.offset);
14547             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14548             rela.r_addend = ent->addend;
14549             loc = (htab->relplt->contents
14550                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14551                       / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14552           }
14553         bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
14554       }
14555
14556   if (h->needs_copy)
14557     {
14558       /* This symbol needs a copy reloc.  Set it up.  */
14559
14560       if (h->dynindx == -1
14561           || (h->root.type != bfd_link_hash_defined
14562               && h->root.type != bfd_link_hash_defweak)
14563           || htab->relbss == NULL)
14564         abort ();
14565
14566       rela.r_offset = (h->root.u.def.value
14567                        + h->root.u.def.section->output_section->vma
14568                        + h->root.u.def.section->output_offset);
14569       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
14570       rela.r_addend = 0;
14571       loc = htab->relbss->contents;
14572       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
14573       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
14574     }
14575
14576   return TRUE;
14577 }
14578
14579 /* Used to decide how to sort relocs in an optimal manner for the
14580    dynamic linker, before writing them out.  */
14581
14582 static enum elf_reloc_type_class
14583 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
14584                             const asection *rel_sec,
14585                             const Elf_Internal_Rela *rela)
14586 {
14587   enum elf_ppc64_reloc_type r_type;
14588   struct ppc_link_hash_table *htab = ppc_hash_table (info);
14589
14590   if (rel_sec == htab->reliplt)
14591     return reloc_class_ifunc;
14592
14593   r_type = ELF64_R_TYPE (rela->r_info);
14594   switch (r_type)
14595     {
14596     case R_PPC64_RELATIVE:
14597       return reloc_class_relative;
14598     case R_PPC64_JMP_SLOT:
14599       return reloc_class_plt;
14600     case R_PPC64_COPY:
14601       return reloc_class_copy;
14602     default:
14603       return reloc_class_normal;
14604     }
14605 }
14606
14607 /* Finish up the dynamic sections.  */
14608
14609 static bfd_boolean
14610 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
14611                                    struct bfd_link_info *info)
14612 {
14613   struct ppc_link_hash_table *htab;
14614   bfd *dynobj;
14615   asection *sdyn;
14616
14617   htab = ppc_hash_table (info);
14618   if (htab == NULL)
14619     return FALSE;
14620
14621   dynobj = htab->elf.dynobj;
14622   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
14623
14624   if (htab->elf.dynamic_sections_created)
14625     {
14626       Elf64_External_Dyn *dyncon, *dynconend;
14627
14628       if (sdyn == NULL || htab->got == NULL)
14629         abort ();
14630
14631       dyncon = (Elf64_External_Dyn *) sdyn->contents;
14632       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
14633       for (; dyncon < dynconend; dyncon++)
14634         {
14635           Elf_Internal_Dyn dyn;
14636           asection *s;
14637
14638           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
14639
14640           switch (dyn.d_tag)
14641             {
14642             default:
14643               continue;
14644
14645             case DT_PPC64_GLINK:
14646               s = htab->glink;
14647               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14648               /* We stupidly defined DT_PPC64_GLINK to be the start
14649                  of glink rather than the first entry point, which is
14650                  what ld.so needs, and now have a bigger stub to
14651                  support automatic multiple TOCs.  */
14652               dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
14653               break;
14654
14655             case DT_PPC64_OPD:
14656               s = bfd_get_section_by_name (output_bfd, ".opd");
14657               if (s == NULL)
14658                 continue;
14659               dyn.d_un.d_ptr = s->vma;
14660               break;
14661
14662             case DT_PPC64_OPDSZ:
14663               s = bfd_get_section_by_name (output_bfd, ".opd");
14664               if (s == NULL)
14665                 continue;
14666               dyn.d_un.d_val = s->size;
14667               break;
14668
14669             case DT_PLTGOT:
14670               s = htab->plt;
14671               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14672               break;
14673
14674             case DT_JMPREL:
14675               s = htab->relplt;
14676               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14677               break;
14678
14679             case DT_PLTRELSZ:
14680               dyn.d_un.d_val = htab->relplt->size;
14681               break;
14682
14683             case DT_RELASZ:
14684               /* Don't count procedure linkage table relocs in the
14685                  overall reloc count.  */
14686               s = htab->relplt;
14687               if (s == NULL)
14688                 continue;
14689               dyn.d_un.d_val -= s->size;
14690               break;
14691
14692             case DT_RELA:
14693               /* We may not be using the standard ELF linker script.
14694                  If .rela.plt is the first .rela section, we adjust
14695                  DT_RELA to not include it.  */
14696               s = htab->relplt;
14697               if (s == NULL)
14698                 continue;
14699               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
14700                 continue;
14701               dyn.d_un.d_ptr += s->size;
14702               break;
14703             }
14704
14705           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
14706         }
14707     }
14708
14709   if (htab->got != NULL && htab->got->size != 0)
14710     {
14711       /* Fill in the first entry in the global offset table.
14712          We use it to hold the link-time TOCbase.  */
14713       bfd_put_64 (output_bfd,
14714                   elf_gp (output_bfd) + TOC_BASE_OFF,
14715                   htab->got->contents);
14716
14717       /* Set .got entry size.  */
14718       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
14719     }
14720
14721   if (htab->plt != NULL && htab->plt->size != 0)
14722     {
14723       /* Set .plt entry size.  */
14724       elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
14725         = PLT_ENTRY_SIZE (htab);
14726     }
14727
14728   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
14729      brlt ourselves if emitrelocations.  */
14730   if (htab->brlt != NULL
14731       && htab->brlt->reloc_count != 0
14732       && !_bfd_elf_link_output_relocs (output_bfd,
14733                                        htab->brlt,
14734                                        elf_section_data (htab->brlt)->rela.hdr,
14735                                        elf_section_data (htab->brlt)->relocs,
14736                                        NULL))
14737     return FALSE;
14738
14739   if (htab->glink != NULL
14740       && htab->glink->reloc_count != 0
14741       && !_bfd_elf_link_output_relocs (output_bfd,
14742                                        htab->glink,
14743                                        elf_section_data (htab->glink)->rela.hdr,
14744                                        elf_section_data (htab->glink)->relocs,
14745                                        NULL))
14746     return FALSE;
14747
14748
14749   if (htab->glink_eh_frame != NULL
14750       && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
14751       && !_bfd_elf_write_section_eh_frame (output_bfd, info,
14752                                            htab->glink_eh_frame,
14753                                            htab->glink_eh_frame->contents))
14754     return FALSE;
14755
14756   /* We need to handle writing out multiple GOT sections ourselves,
14757      since we didn't add them to DYNOBJ.  We know dynobj is the first
14758      bfd.  */
14759   while ((dynobj = dynobj->link_next) != NULL)
14760     {
14761       asection *s;
14762
14763       if (!is_ppc64_elf (dynobj))
14764         continue;
14765
14766       s = ppc64_elf_tdata (dynobj)->got;
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       s = ppc64_elf_tdata (dynobj)->relgot;
14775       if (s != NULL
14776           && s->size != 0
14777           && s->output_section != bfd_abs_section_ptr
14778           && !bfd_set_section_contents (output_bfd, s->output_section,
14779                                         s->contents, s->output_offset,
14780                                         s->size))
14781         return FALSE;
14782     }
14783
14784   return TRUE;
14785 }
14786
14787 #include "elf64-target.h"
14788
14789 /* FreeBSD support */
14790
14791 #undef  TARGET_LITTLE_SYM
14792 #undef  TARGET_LITTLE_NAME
14793
14794 #undef  TARGET_BIG_SYM
14795 #define TARGET_BIG_SYM  bfd_elf64_powerpc_freebsd_vec
14796 #undef  TARGET_BIG_NAME
14797 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
14798
14799 #undef  ELF_OSABI
14800 #define ELF_OSABI       ELFOSABI_FREEBSD
14801
14802 #undef  elf64_bed
14803 #define elf64_bed       elf64_powerpc_fbsd_bed
14804
14805 #include "elf64-target.h"
14806