* elf64-ppc.c (ppc64_elf_func_desc_adjust): Don't hide .TOC.
[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  _bfd_generic_verify_endian_match
85 #define bfd_elf64_new_section_hook            ppc64_elf_new_section_hook
86 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
87 #define bfd_elf64_bfd_link_hash_table_free    ppc64_elf_link_hash_table_free
88 #define bfd_elf64_get_synthetic_symtab        ppc64_elf_get_synthetic_symtab
89 #define bfd_elf64_bfd_link_just_syms          ppc64_elf_link_just_syms
90
91 #define elf_backend_object_p                  ppc64_elf_object_p
92 #define elf_backend_grok_prstatus             ppc64_elf_grok_prstatus
93 #define elf_backend_grok_psinfo               ppc64_elf_grok_psinfo
94 #define elf_backend_write_core_note           ppc64_elf_write_core_note
95 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
96 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
97 #define elf_backend_add_symbol_hook           ppc64_elf_add_symbol_hook
98 #define elf_backend_check_directives          ppc64_elf_process_dot_syms
99 #define elf_backend_as_needed_cleanup         ppc64_elf_as_needed_cleanup
100 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
101 #define elf_backend_check_relocs              ppc64_elf_check_relocs
102 #define elf_backend_gc_keep                   ppc64_elf_gc_keep
103 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
104 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
105 #define elf_backend_gc_sweep_hook             ppc64_elf_gc_sweep_hook
106 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
107 #define elf_backend_hide_symbol               ppc64_elf_hide_symbol
108 #define elf_backend_maybe_function_sym        ppc64_elf_maybe_function_sym
109 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
110 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
111 #define elf_backend_init_index_section        _bfd_elf_init_2_index_sections
112 #define elf_backend_action_discarded          ppc64_elf_action_discarded
113 #define elf_backend_relocate_section          ppc64_elf_relocate_section
114 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
115 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
116 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
117 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
118 #define elf_backend_special_sections          ppc64_elf_special_sections
119 #define elf_backend_post_process_headers      _bfd_elf_set_osabi
120
121 /* The name of the dynamic interpreter.  This is put in the .interp
122    section.  */
123 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
124
125 /* The size in bytes of an entry in the procedure linkage table.  */
126 #define PLT_ENTRY_SIZE 24
127
128 /* The initial size of the plt reserved for the dynamic linker.  */
129 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
130
131 /* TOC base pointers offset from start of TOC.  */
132 #define TOC_BASE_OFF    0x8000
133
134 /* Offset of tp and dtp pointers from start of TLS block.  */
135 #define TP_OFFSET       0x7000
136 #define DTP_OFFSET      0x8000
137
138 /* .plt call stub instructions.  The normal stub is like this, but
139    sometimes the .plt entry crosses a 64k boundary and we need to
140    insert an addi to adjust r12.  */
141 #define PLT_CALL_STUB_SIZE (7*4)
142 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha     */
143 #define STD_R2_40R1     0xf8410028      /* std   %r2,40(%r1)         */
144 #define LD_R11_0R12     0xe96c0000      /* ld    %r11,xxx+0@l(%r12)  */
145 #define MTCTR_R11       0x7d6903a6      /* mtctr %r11                */
146 #define LD_R2_0R12      0xe84c0000      /* ld    %r2,xxx+8@l(%r12)   */
147                                         /* ld    %r11,xxx+16@l(%r12) */
148 #define BCTR            0x4e800420      /* bctr                      */
149
150
151 #define ADDIS_R12_R12   0x3d8c0000      /* addis %r12,%r12,off@ha  */
152 #define ADDI_R12_R12    0x398c0000      /* addi %r12,%r12,off@l  */
153 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
154 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
155
156 #define XOR_R11_R11_R11 0x7d6b5a78      /* xor   %r11,%r11,%r11  */
157 #define ADD_R12_R12_R11 0x7d8c5a14      /* add   %r12,%r12,%r11  */
158 #define ADD_R2_R2_R11   0x7c425a14      /* add   %r2,%r2,%r11    */
159 #define CMPLDI_R2_0     0x28220000      /* cmpldi %r2,0          */
160 #define BNECTR          0x4ca20420      /* bnectr+               */
161 #define BNECTR_P4       0x4ce20420      /* bnectr+               */
162
163 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
164 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
165
166 #define LD_R2_40R1      0xe8410028      /* ld    %r2,40(%r1)     */
167
168 /* glink call stub instructions.  We enter with the index in R0.  */
169 #define GLINK_CALL_STUB_SIZE (16*4)
170                                         /* 0:                           */
171                                         /*  .quad plt0-1f               */
172                                         /* __glink:                     */
173 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
174 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
175                                         /* 1:                           */
176 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
177 #define LD_R2_M16R11    0xe84bfff0      /*  ld %2,(0b-1b)(%11)          */
178 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
179 #define ADD_R12_R2_R11  0x7d825a14      /*  add %12,%2,%11              */
180                                         /*  ld %11,0(%12)               */
181                                         /*  ld %2,8(%12)                */
182                                         /*  mtctr %11                   */
183                                         /*  ld %11,16(%12)              */
184                                         /*  bctr                        */
185
186 /* Pad with this.  */
187 #define NOP             0x60000000
188
189 /* Some other nops.  */
190 #define CROR_151515     0x4def7b82
191 #define CROR_313131     0x4ffffb82
192
193 /* .glink entries for the first 32k functions are two instructions.  */
194 #define LI_R0_0         0x38000000      /* li    %r0,0          */
195 #define B_DOT           0x48000000      /* b     .              */
196
197 /* After that, we need two instructions to load the index, followed by
198    a branch.  */
199 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
200 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
201
202 /* Instructions used by the save and restore reg functions.  */
203 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
204 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
205 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
206 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
207 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
208 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
209 #define LI_R12_0        0x39800000      /* li    %r12,0         */
210 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
211 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
212 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
213 #define BLR             0x4e800020      /* blr                  */
214
215 /* Since .opd is an array of descriptors and each entry will end up
216    with identical R_PPC64_RELATIVE relocs, there is really no need to
217    propagate .opd relocs;  The dynamic linker should be taught to
218    relocate .opd without reloc entries.  */
219 #ifndef NO_OPD_RELOCS
220 #define NO_OPD_RELOCS 0
221 #endif
222 \f
223 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
224
225 /* Relocation HOWTO's.  */
226 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
227
228 static reloc_howto_type ppc64_elf_howto_raw[] = {
229   /* This reloc does nothing.  */
230   HOWTO (R_PPC64_NONE,          /* type */
231          0,                     /* rightshift */
232          2,                     /* size (0 = byte, 1 = short, 2 = long) */
233          32,                    /* bitsize */
234          FALSE,                 /* pc_relative */
235          0,                     /* bitpos */
236          complain_overflow_dont, /* complain_on_overflow */
237          bfd_elf_generic_reloc, /* special_function */
238          "R_PPC64_NONE",        /* name */
239          FALSE,                 /* partial_inplace */
240          0,                     /* src_mask */
241          0,                     /* dst_mask */
242          FALSE),                /* pcrel_offset */
243
244   /* A standard 32 bit relocation.  */
245   HOWTO (R_PPC64_ADDR32,        /* type */
246          0,                     /* rightshift */
247          2,                     /* size (0 = byte, 1 = short, 2 = long) */
248          32,                    /* bitsize */
249          FALSE,                 /* pc_relative */
250          0,                     /* bitpos */
251          complain_overflow_bitfield, /* complain_on_overflow */
252          bfd_elf_generic_reloc, /* special_function */
253          "R_PPC64_ADDR32",      /* name */
254          FALSE,                 /* partial_inplace */
255          0,                     /* src_mask */
256          0xffffffff,            /* dst_mask */
257          FALSE),                /* pcrel_offset */
258
259   /* An absolute 26 bit branch; the lower two bits must be zero.
260      FIXME: we don't check that, we just clear them.  */
261   HOWTO (R_PPC64_ADDR24,        /* type */
262          0,                     /* rightshift */
263          2,                     /* size (0 = byte, 1 = short, 2 = long) */
264          26,                    /* bitsize */
265          FALSE,                 /* pc_relative */
266          0,                     /* bitpos */
267          complain_overflow_bitfield, /* complain_on_overflow */
268          bfd_elf_generic_reloc, /* special_function */
269          "R_PPC64_ADDR24",      /* name */
270          FALSE,                 /* partial_inplace */
271          0,                     /* src_mask */
272          0x03fffffc,            /* dst_mask */
273          FALSE),                /* pcrel_offset */
274
275   /* A standard 16 bit relocation.  */
276   HOWTO (R_PPC64_ADDR16,        /* type */
277          0,                     /* rightshift */
278          1,                     /* size (0 = byte, 1 = short, 2 = long) */
279          16,                    /* bitsize */
280          FALSE,                 /* pc_relative */
281          0,                     /* bitpos */
282          complain_overflow_bitfield, /* complain_on_overflow */
283          bfd_elf_generic_reloc, /* special_function */
284          "R_PPC64_ADDR16",      /* name */
285          FALSE,                 /* partial_inplace */
286          0,                     /* src_mask */
287          0xffff,                /* dst_mask */
288          FALSE),                /* pcrel_offset */
289
290   /* A 16 bit relocation without overflow.  */
291   HOWTO (R_PPC64_ADDR16_LO,     /* type */
292          0,                     /* rightshift */
293          1,                     /* size (0 = byte, 1 = short, 2 = long) */
294          16,                    /* bitsize */
295          FALSE,                 /* pc_relative */
296          0,                     /* bitpos */
297          complain_overflow_dont,/* complain_on_overflow */
298          bfd_elf_generic_reloc, /* special_function */
299          "R_PPC64_ADDR16_LO",   /* name */
300          FALSE,                 /* partial_inplace */
301          0,                     /* src_mask */
302          0xffff,                /* dst_mask */
303          FALSE),                /* pcrel_offset */
304
305   /* Bits 16-31 of an address.  */
306   HOWTO (R_PPC64_ADDR16_HI,     /* type */
307          16,                    /* rightshift */
308          1,                     /* size (0 = byte, 1 = short, 2 = long) */
309          16,                    /* bitsize */
310          FALSE,                 /* pc_relative */
311          0,                     /* bitpos */
312          complain_overflow_dont, /* complain_on_overflow */
313          bfd_elf_generic_reloc, /* special_function */
314          "R_PPC64_ADDR16_HI",   /* name */
315          FALSE,                 /* partial_inplace */
316          0,                     /* src_mask */
317          0xffff,                /* dst_mask */
318          FALSE),                /* pcrel_offset */
319
320   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
321      bits, treated as a signed number, is negative.  */
322   HOWTO (R_PPC64_ADDR16_HA,     /* type */
323          16,                    /* rightshift */
324          1,                     /* size (0 = byte, 1 = short, 2 = long) */
325          16,                    /* bitsize */
326          FALSE,                 /* pc_relative */
327          0,                     /* bitpos */
328          complain_overflow_dont, /* complain_on_overflow */
329          ppc64_elf_ha_reloc,    /* special_function */
330          "R_PPC64_ADDR16_HA",   /* name */
331          FALSE,                 /* partial_inplace */
332          0,                     /* src_mask */
333          0xffff,                /* dst_mask */
334          FALSE),                /* pcrel_offset */
335
336   /* An absolute 16 bit branch; the lower two bits must be zero.
337      FIXME: we don't check that, we just clear them.  */
338   HOWTO (R_PPC64_ADDR14,        /* type */
339          0,                     /* rightshift */
340          2,                     /* size (0 = byte, 1 = short, 2 = long) */
341          16,                    /* bitsize */
342          FALSE,                 /* pc_relative */
343          0,                     /* bitpos */
344          complain_overflow_bitfield, /* complain_on_overflow */
345          ppc64_elf_branch_reloc, /* special_function */
346          "R_PPC64_ADDR14",      /* name */
347          FALSE,                 /* partial_inplace */
348          0,                     /* src_mask */
349          0x0000fffc,            /* dst_mask */
350          FALSE),                /* pcrel_offset */
351
352   /* An absolute 16 bit branch, for which bit 10 should be set to
353      indicate that the branch is expected to be taken.  The lower two
354      bits must be zero.  */
355   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
356          0,                     /* rightshift */
357          2,                     /* size (0 = byte, 1 = short, 2 = long) */
358          16,                    /* bitsize */
359          FALSE,                 /* pc_relative */
360          0,                     /* bitpos */
361          complain_overflow_bitfield, /* complain_on_overflow */
362          ppc64_elf_brtaken_reloc, /* special_function */
363          "R_PPC64_ADDR14_BRTAKEN",/* name */
364          FALSE,                 /* partial_inplace */
365          0,                     /* src_mask */
366          0x0000fffc,            /* dst_mask */
367          FALSE),                /* pcrel_offset */
368
369   /* An absolute 16 bit branch, for which bit 10 should be set to
370      indicate that the branch is not expected to be taken.  The lower
371      two bits must be zero.  */
372   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
373          0,                     /* rightshift */
374          2,                     /* size (0 = byte, 1 = short, 2 = long) */
375          16,                    /* bitsize */
376          FALSE,                 /* pc_relative */
377          0,                     /* bitpos */
378          complain_overflow_bitfield, /* complain_on_overflow */
379          ppc64_elf_brtaken_reloc, /* special_function */
380          "R_PPC64_ADDR14_BRNTAKEN",/* name */
381          FALSE,                 /* partial_inplace */
382          0,                     /* src_mask */
383          0x0000fffc,            /* dst_mask */
384          FALSE),                /* pcrel_offset */
385
386   /* A relative 26 bit branch; the lower two bits must be zero.  */
387   HOWTO (R_PPC64_REL24,         /* type */
388          0,                     /* rightshift */
389          2,                     /* size (0 = byte, 1 = short, 2 = long) */
390          26,                    /* bitsize */
391          TRUE,                  /* pc_relative */
392          0,                     /* bitpos */
393          complain_overflow_signed, /* complain_on_overflow */
394          ppc64_elf_branch_reloc, /* special_function */
395          "R_PPC64_REL24",       /* name */
396          FALSE,                 /* partial_inplace */
397          0,                     /* src_mask */
398          0x03fffffc,            /* dst_mask */
399          TRUE),                 /* pcrel_offset */
400
401   /* A relative 16 bit branch; the lower two bits must be zero.  */
402   HOWTO (R_PPC64_REL14,         /* type */
403          0,                     /* rightshift */
404          2,                     /* size (0 = byte, 1 = short, 2 = long) */
405          16,                    /* bitsize */
406          TRUE,                  /* pc_relative */
407          0,                     /* bitpos */
408          complain_overflow_signed, /* complain_on_overflow */
409          ppc64_elf_branch_reloc, /* special_function */
410          "R_PPC64_REL14",       /* name */
411          FALSE,                 /* partial_inplace */
412          0,                     /* src_mask */
413          0x0000fffc,            /* dst_mask */
414          TRUE),                 /* pcrel_offset */
415
416   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
417      the branch is expected to be taken.  The lower two bits must be
418      zero.  */
419   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
420          0,                     /* rightshift */
421          2,                     /* size (0 = byte, 1 = short, 2 = long) */
422          16,                    /* bitsize */
423          TRUE,                  /* pc_relative */
424          0,                     /* bitpos */
425          complain_overflow_signed, /* complain_on_overflow */
426          ppc64_elf_brtaken_reloc, /* special_function */
427          "R_PPC64_REL14_BRTAKEN", /* name */
428          FALSE,                 /* partial_inplace */
429          0,                     /* src_mask */
430          0x0000fffc,            /* dst_mask */
431          TRUE),                 /* pcrel_offset */
432
433   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
434      the branch is not expected to be taken.  The lower two bits must
435      be zero.  */
436   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
437          0,                     /* rightshift */
438          2,                     /* size (0 = byte, 1 = short, 2 = long) */
439          16,                    /* bitsize */
440          TRUE,                  /* pc_relative */
441          0,                     /* bitpos */
442          complain_overflow_signed, /* complain_on_overflow */
443          ppc64_elf_brtaken_reloc, /* special_function */
444          "R_PPC64_REL14_BRNTAKEN",/* name */
445          FALSE,                 /* partial_inplace */
446          0,                     /* src_mask */
447          0x0000fffc,            /* dst_mask */
448          TRUE),                 /* pcrel_offset */
449
450   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
451      symbol.  */
452   HOWTO (R_PPC64_GOT16,         /* type */
453          0,                     /* rightshift */
454          1,                     /* size (0 = byte, 1 = short, 2 = long) */
455          16,                    /* bitsize */
456          FALSE,                 /* pc_relative */
457          0,                     /* bitpos */
458          complain_overflow_signed, /* complain_on_overflow */
459          ppc64_elf_unhandled_reloc, /* special_function */
460          "R_PPC64_GOT16",       /* name */
461          FALSE,                 /* partial_inplace */
462          0,                     /* src_mask */
463          0xffff,                /* dst_mask */
464          FALSE),                /* pcrel_offset */
465
466   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
467      the symbol.  */
468   HOWTO (R_PPC64_GOT16_LO,      /* type */
469          0,                     /* rightshift */
470          1,                     /* size (0 = byte, 1 = short, 2 = long) */
471          16,                    /* bitsize */
472          FALSE,                 /* pc_relative */
473          0,                     /* bitpos */
474          complain_overflow_dont, /* complain_on_overflow */
475          ppc64_elf_unhandled_reloc, /* special_function */
476          "R_PPC64_GOT16_LO",    /* name */
477          FALSE,                 /* partial_inplace */
478          0,                     /* src_mask */
479          0xffff,                /* dst_mask */
480          FALSE),                /* pcrel_offset */
481
482   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
483      the symbol.  */
484   HOWTO (R_PPC64_GOT16_HI,      /* type */
485          16,                    /* rightshift */
486          1,                     /* size (0 = byte, 1 = short, 2 = long) */
487          16,                    /* bitsize */
488          FALSE,                 /* pc_relative */
489          0,                     /* bitpos */
490          complain_overflow_dont,/* complain_on_overflow */
491          ppc64_elf_unhandled_reloc, /* special_function */
492          "R_PPC64_GOT16_HI",    /* name */
493          FALSE,                 /* partial_inplace */
494          0,                     /* src_mask */
495          0xffff,                /* dst_mask */
496          FALSE),                /* pcrel_offset */
497
498   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
499      the symbol.  */
500   HOWTO (R_PPC64_GOT16_HA,      /* type */
501          16,                    /* rightshift */
502          1,                     /* size (0 = byte, 1 = short, 2 = long) */
503          16,                    /* bitsize */
504          FALSE,                 /* pc_relative */
505          0,                     /* bitpos */
506          complain_overflow_dont,/* complain_on_overflow */
507          ppc64_elf_unhandled_reloc, /* special_function */
508          "R_PPC64_GOT16_HA",    /* name */
509          FALSE,                 /* partial_inplace */
510          0,                     /* src_mask */
511          0xffff,                /* dst_mask */
512          FALSE),                /* pcrel_offset */
513
514   /* This is used only by the dynamic linker.  The symbol should exist
515      both in the object being run and in some shared library.  The
516      dynamic linker copies the data addressed by the symbol from the
517      shared library into the object, because the object being
518      run has to have the data at some particular address.  */
519   HOWTO (R_PPC64_COPY,          /* type */
520          0,                     /* rightshift */
521          0,                     /* this one is variable size */
522          0,                     /* bitsize */
523          FALSE,                 /* pc_relative */
524          0,                     /* bitpos */
525          complain_overflow_dont, /* complain_on_overflow */
526          ppc64_elf_unhandled_reloc, /* special_function */
527          "R_PPC64_COPY",        /* name */
528          FALSE,                 /* partial_inplace */
529          0,                     /* src_mask */
530          0,                     /* dst_mask */
531          FALSE),                /* pcrel_offset */
532
533   /* Like R_PPC64_ADDR64, but used when setting global offset table
534      entries.  */
535   HOWTO (R_PPC64_GLOB_DAT,      /* type */
536          0,                     /* rightshift */
537          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
538          64,                    /* bitsize */
539          FALSE,                 /* pc_relative */
540          0,                     /* bitpos */
541          complain_overflow_dont, /* complain_on_overflow */
542          ppc64_elf_unhandled_reloc,  /* special_function */
543          "R_PPC64_GLOB_DAT",    /* name */
544          FALSE,                 /* partial_inplace */
545          0,                     /* src_mask */
546          ONES (64),             /* dst_mask */
547          FALSE),                /* pcrel_offset */
548
549   /* Created by the link editor.  Marks a procedure linkage table
550      entry for a symbol.  */
551   HOWTO (R_PPC64_JMP_SLOT,      /* type */
552          0,                     /* rightshift */
553          0,                     /* size (0 = byte, 1 = short, 2 = long) */
554          0,                     /* bitsize */
555          FALSE,                 /* pc_relative */
556          0,                     /* bitpos */
557          complain_overflow_dont, /* complain_on_overflow */
558          ppc64_elf_unhandled_reloc, /* special_function */
559          "R_PPC64_JMP_SLOT",    /* name */
560          FALSE,                 /* partial_inplace */
561          0,                     /* src_mask */
562          0,                     /* dst_mask */
563          FALSE),                /* pcrel_offset */
564
565   /* Used only by the dynamic linker.  When the object is run, this
566      doubleword64 is set to the load address of the object, plus the
567      addend.  */
568   HOWTO (R_PPC64_RELATIVE,      /* type */
569          0,                     /* rightshift */
570          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
571          64,                    /* bitsize */
572          FALSE,                 /* pc_relative */
573          0,                     /* bitpos */
574          complain_overflow_dont, /* complain_on_overflow */
575          bfd_elf_generic_reloc, /* special_function */
576          "R_PPC64_RELATIVE",    /* name */
577          FALSE,                 /* partial_inplace */
578          0,                     /* src_mask */
579          ONES (64),             /* dst_mask */
580          FALSE),                /* pcrel_offset */
581
582   /* Like R_PPC64_ADDR32, but may be unaligned.  */
583   HOWTO (R_PPC64_UADDR32,       /* type */
584          0,                     /* rightshift */
585          2,                     /* size (0 = byte, 1 = short, 2 = long) */
586          32,                    /* bitsize */
587          FALSE,                 /* pc_relative */
588          0,                     /* bitpos */
589          complain_overflow_bitfield, /* complain_on_overflow */
590          bfd_elf_generic_reloc, /* special_function */
591          "R_PPC64_UADDR32",     /* name */
592          FALSE,                 /* partial_inplace */
593          0,                     /* src_mask */
594          0xffffffff,            /* dst_mask */
595          FALSE),                /* pcrel_offset */
596
597   /* Like R_PPC64_ADDR16, but may be unaligned.  */
598   HOWTO (R_PPC64_UADDR16,       /* type */
599          0,                     /* rightshift */
600          1,                     /* size (0 = byte, 1 = short, 2 = long) */
601          16,                    /* bitsize */
602          FALSE,                 /* pc_relative */
603          0,                     /* bitpos */
604          complain_overflow_bitfield, /* complain_on_overflow */
605          bfd_elf_generic_reloc, /* special_function */
606          "R_PPC64_UADDR16",     /* name */
607          FALSE,                 /* partial_inplace */
608          0,                     /* src_mask */
609          0xffff,                /* dst_mask */
610          FALSE),                /* pcrel_offset */
611
612   /* 32-bit PC relative.  */
613   HOWTO (R_PPC64_REL32,         /* type */
614          0,                     /* rightshift */
615          2,                     /* size (0 = byte, 1 = short, 2 = long) */
616          32,                    /* bitsize */
617          TRUE,                  /* pc_relative */
618          0,                     /* bitpos */
619          /* FIXME: Verify.  Was complain_overflow_bitfield.  */
620          complain_overflow_signed, /* complain_on_overflow */
621          bfd_elf_generic_reloc, /* special_function */
622          "R_PPC64_REL32",       /* name */
623          FALSE,                 /* partial_inplace */
624          0,                     /* src_mask */
625          0xffffffff,            /* dst_mask */
626          TRUE),                 /* pcrel_offset */
627
628   /* 32-bit relocation to the symbol's procedure linkage table.  */
629   HOWTO (R_PPC64_PLT32,         /* type */
630          0,                     /* rightshift */
631          2,                     /* size (0 = byte, 1 = short, 2 = long) */
632          32,                    /* bitsize */
633          FALSE,                 /* pc_relative */
634          0,                     /* bitpos */
635          complain_overflow_bitfield, /* complain_on_overflow */
636          ppc64_elf_unhandled_reloc, /* special_function */
637          "R_PPC64_PLT32",       /* name */
638          FALSE,                 /* partial_inplace */
639          0,                     /* src_mask */
640          0xffffffff,            /* dst_mask */
641          FALSE),                /* pcrel_offset */
642
643   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
644      FIXME: R_PPC64_PLTREL32 not supported.  */
645   HOWTO (R_PPC64_PLTREL32,      /* type */
646          0,                     /* rightshift */
647          2,                     /* size (0 = byte, 1 = short, 2 = long) */
648          32,                    /* bitsize */
649          TRUE,                  /* pc_relative */
650          0,                     /* bitpos */
651          complain_overflow_signed, /* complain_on_overflow */
652          bfd_elf_generic_reloc, /* special_function */
653          "R_PPC64_PLTREL32",    /* name */
654          FALSE,                 /* partial_inplace */
655          0,                     /* src_mask */
656          0xffffffff,            /* dst_mask */
657          TRUE),                 /* pcrel_offset */
658
659   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
660      the symbol.  */
661   HOWTO (R_PPC64_PLT16_LO,      /* type */
662          0,                     /* rightshift */
663          1,                     /* size (0 = byte, 1 = short, 2 = long) */
664          16,                    /* bitsize */
665          FALSE,                 /* pc_relative */
666          0,                     /* bitpos */
667          complain_overflow_dont, /* complain_on_overflow */
668          ppc64_elf_unhandled_reloc, /* special_function */
669          "R_PPC64_PLT16_LO",    /* name */
670          FALSE,                 /* partial_inplace */
671          0,                     /* src_mask */
672          0xffff,                /* dst_mask */
673          FALSE),                /* pcrel_offset */
674
675   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
676      the symbol.  */
677   HOWTO (R_PPC64_PLT16_HI,      /* type */
678          16,                    /* rightshift */
679          1,                     /* size (0 = byte, 1 = short, 2 = long) */
680          16,                    /* bitsize */
681          FALSE,                 /* pc_relative */
682          0,                     /* bitpos */
683          complain_overflow_dont, /* complain_on_overflow */
684          ppc64_elf_unhandled_reloc, /* special_function */
685          "R_PPC64_PLT16_HI",    /* name */
686          FALSE,                 /* partial_inplace */
687          0,                     /* src_mask */
688          0xffff,                /* dst_mask */
689          FALSE),                /* pcrel_offset */
690
691   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
692      the symbol.  */
693   HOWTO (R_PPC64_PLT16_HA,      /* type */
694          16,                    /* rightshift */
695          1,                     /* size (0 = byte, 1 = short, 2 = long) */
696          16,                    /* bitsize */
697          FALSE,                 /* pc_relative */
698          0,                     /* bitpos */
699          complain_overflow_dont, /* complain_on_overflow */
700          ppc64_elf_unhandled_reloc, /* special_function */
701          "R_PPC64_PLT16_HA",    /* name */
702          FALSE,                 /* partial_inplace */
703          0,                     /* src_mask */
704          0xffff,                /* dst_mask */
705          FALSE),                /* pcrel_offset */
706
707   /* 16-bit section relative relocation.  */
708   HOWTO (R_PPC64_SECTOFF,       /* type */
709          0,                     /* rightshift */
710          1,                     /* size (0 = byte, 1 = short, 2 = long) */
711          16,                    /* bitsize */
712          FALSE,                 /* pc_relative */
713          0,                     /* bitpos */
714          complain_overflow_bitfield, /* complain_on_overflow */
715          ppc64_elf_sectoff_reloc, /* special_function */
716          "R_PPC64_SECTOFF",     /* name */
717          FALSE,                 /* partial_inplace */
718          0,                     /* src_mask */
719          0xffff,                /* dst_mask */
720          FALSE),                /* pcrel_offset */
721
722   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
723   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
724          0,                     /* rightshift */
725          1,                     /* size (0 = byte, 1 = short, 2 = long) */
726          16,                    /* bitsize */
727          FALSE,                 /* pc_relative */
728          0,                     /* bitpos */
729          complain_overflow_dont, /* complain_on_overflow */
730          ppc64_elf_sectoff_reloc, /* special_function */
731          "R_PPC64_SECTOFF_LO",  /* name */
732          FALSE,                 /* partial_inplace */
733          0,                     /* src_mask */
734          0xffff,                /* dst_mask */
735          FALSE),                /* pcrel_offset */
736
737   /* 16-bit upper half section relative relocation.  */
738   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
739          16,                    /* rightshift */
740          1,                     /* size (0 = byte, 1 = short, 2 = long) */
741          16,                    /* bitsize */
742          FALSE,                 /* pc_relative */
743          0,                     /* bitpos */
744          complain_overflow_dont, /* complain_on_overflow */
745          ppc64_elf_sectoff_reloc, /* special_function */
746          "R_PPC64_SECTOFF_HI",  /* name */
747          FALSE,                 /* partial_inplace */
748          0,                     /* src_mask */
749          0xffff,                /* dst_mask */
750          FALSE),                /* pcrel_offset */
751
752   /* 16-bit upper half adjusted section relative relocation.  */
753   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
754          16,                    /* rightshift */
755          1,                     /* size (0 = byte, 1 = short, 2 = long) */
756          16,                    /* bitsize */
757          FALSE,                 /* pc_relative */
758          0,                     /* bitpos */
759          complain_overflow_dont, /* complain_on_overflow */
760          ppc64_elf_sectoff_ha_reloc, /* special_function */
761          "R_PPC64_SECTOFF_HA",  /* name */
762          FALSE,                 /* partial_inplace */
763          0,                     /* src_mask */
764          0xffff,                /* dst_mask */
765          FALSE),                /* pcrel_offset */
766
767   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
768   HOWTO (R_PPC64_REL30,         /* type */
769          2,                     /* rightshift */
770          2,                     /* size (0 = byte, 1 = short, 2 = long) */
771          30,                    /* bitsize */
772          TRUE,                  /* pc_relative */
773          0,                     /* bitpos */
774          complain_overflow_dont, /* complain_on_overflow */
775          bfd_elf_generic_reloc, /* special_function */
776          "R_PPC64_REL30",       /* name */
777          FALSE,                 /* partial_inplace */
778          0,                     /* src_mask */
779          0xfffffffc,            /* dst_mask */
780          TRUE),                 /* pcrel_offset */
781
782   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
783
784   /* A standard 64-bit relocation.  */
785   HOWTO (R_PPC64_ADDR64,        /* type */
786          0,                     /* rightshift */
787          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
788          64,                    /* bitsize */
789          FALSE,                 /* pc_relative */
790          0,                     /* bitpos */
791          complain_overflow_dont, /* complain_on_overflow */
792          bfd_elf_generic_reloc, /* special_function */
793          "R_PPC64_ADDR64",      /* name */
794          FALSE,                 /* partial_inplace */
795          0,                     /* src_mask */
796          ONES (64),             /* dst_mask */
797          FALSE),                /* pcrel_offset */
798
799   /* The bits 32-47 of an address.  */
800   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
801          32,                    /* rightshift */
802          1,                     /* size (0 = byte, 1 = short, 2 = long) */
803          16,                    /* bitsize */
804          FALSE,                 /* pc_relative */
805          0,                     /* bitpos */
806          complain_overflow_dont, /* complain_on_overflow */
807          bfd_elf_generic_reloc, /* special_function */
808          "R_PPC64_ADDR16_HIGHER", /* name */
809          FALSE,                 /* partial_inplace */
810          0,                     /* src_mask */
811          0xffff,                /* dst_mask */
812          FALSE),                /* pcrel_offset */
813
814   /* The bits 32-47 of an address, plus 1 if the contents of the low
815      16 bits, treated as a signed number, is negative.  */
816   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
817          32,                    /* rightshift */
818          1,                     /* size (0 = byte, 1 = short, 2 = long) */
819          16,                    /* bitsize */
820          FALSE,                 /* pc_relative */
821          0,                     /* bitpos */
822          complain_overflow_dont, /* complain_on_overflow */
823          ppc64_elf_ha_reloc,    /* special_function */
824          "R_PPC64_ADDR16_HIGHERA", /* name */
825          FALSE,                 /* partial_inplace */
826          0,                     /* src_mask */
827          0xffff,                /* dst_mask */
828          FALSE),                /* pcrel_offset */
829
830   /* The bits 48-63 of an address.  */
831   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
832          48,                    /* rightshift */
833          1,                     /* size (0 = byte, 1 = short, 2 = long) */
834          16,                    /* bitsize */
835          FALSE,                 /* pc_relative */
836          0,                     /* bitpos */
837          complain_overflow_dont, /* complain_on_overflow */
838          bfd_elf_generic_reloc, /* special_function */
839          "R_PPC64_ADDR16_HIGHEST", /* name */
840          FALSE,                 /* partial_inplace */
841          0,                     /* src_mask */
842          0xffff,                /* dst_mask */
843          FALSE),                /* pcrel_offset */
844
845   /* The bits 48-63 of an address, plus 1 if the contents of the low
846      16 bits, treated as a signed number, is negative.  */
847   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
848          48,                    /* rightshift */
849          1,                     /* size (0 = byte, 1 = short, 2 = long) */
850          16,                    /* bitsize */
851          FALSE,                 /* pc_relative */
852          0,                     /* bitpos */
853          complain_overflow_dont, /* complain_on_overflow */
854          ppc64_elf_ha_reloc,    /* special_function */
855          "R_PPC64_ADDR16_HIGHESTA", /* name */
856          FALSE,                 /* partial_inplace */
857          0,                     /* src_mask */
858          0xffff,                /* dst_mask */
859          FALSE),                /* pcrel_offset */
860
861   /* Like ADDR64, but may be unaligned.  */
862   HOWTO (R_PPC64_UADDR64,       /* type */
863          0,                     /* rightshift */
864          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
865          64,                    /* bitsize */
866          FALSE,                 /* pc_relative */
867          0,                     /* bitpos */
868          complain_overflow_dont, /* complain_on_overflow */
869          bfd_elf_generic_reloc, /* special_function */
870          "R_PPC64_UADDR64",     /* name */
871          FALSE,                 /* partial_inplace */
872          0,                     /* src_mask */
873          ONES (64),             /* dst_mask */
874          FALSE),                /* pcrel_offset */
875
876   /* 64-bit relative relocation.  */
877   HOWTO (R_PPC64_REL64,         /* type */
878          0,                     /* rightshift */
879          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
880          64,                    /* bitsize */
881          TRUE,                  /* pc_relative */
882          0,                     /* bitpos */
883          complain_overflow_dont, /* complain_on_overflow */
884          bfd_elf_generic_reloc, /* special_function */
885          "R_PPC64_REL64",       /* name */
886          FALSE,                 /* partial_inplace */
887          0,                     /* src_mask */
888          ONES (64),             /* dst_mask */
889          TRUE),                 /* pcrel_offset */
890
891   /* 64-bit relocation to the symbol's procedure linkage table.  */
892   HOWTO (R_PPC64_PLT64,         /* type */
893          0,                     /* rightshift */
894          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
895          64,                    /* bitsize */
896          FALSE,                 /* pc_relative */
897          0,                     /* bitpos */
898          complain_overflow_dont, /* complain_on_overflow */
899          ppc64_elf_unhandled_reloc, /* special_function */
900          "R_PPC64_PLT64",       /* name */
901          FALSE,                 /* partial_inplace */
902          0,                     /* src_mask */
903          ONES (64),             /* dst_mask */
904          FALSE),                /* pcrel_offset */
905
906   /* 64-bit PC relative relocation to the symbol's procedure linkage
907      table.  */
908   /* FIXME: R_PPC64_PLTREL64 not supported.  */
909   HOWTO (R_PPC64_PLTREL64,      /* type */
910          0,                     /* rightshift */
911          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
912          64,                    /* bitsize */
913          TRUE,                  /* pc_relative */
914          0,                     /* bitpos */
915          complain_overflow_dont, /* complain_on_overflow */
916          ppc64_elf_unhandled_reloc, /* special_function */
917          "R_PPC64_PLTREL64",    /* name */
918          FALSE,                 /* partial_inplace */
919          0,                     /* src_mask */
920          ONES (64),             /* dst_mask */
921          TRUE),                 /* pcrel_offset */
922
923   /* 16 bit TOC-relative relocation.  */
924
925   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
926   HOWTO (R_PPC64_TOC16,         /* type */
927          0,                     /* rightshift */
928          1,                     /* size (0 = byte, 1 = short, 2 = long) */
929          16,                    /* bitsize */
930          FALSE,                 /* pc_relative */
931          0,                     /* bitpos */
932          complain_overflow_signed, /* complain_on_overflow */
933          ppc64_elf_toc_reloc,   /* special_function */
934          "R_PPC64_TOC16",       /* name */
935          FALSE,                 /* partial_inplace */
936          0,                     /* src_mask */
937          0xffff,                /* dst_mask */
938          FALSE),                /* pcrel_offset */
939
940   /* 16 bit TOC-relative relocation without overflow.  */
941
942   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
943   HOWTO (R_PPC64_TOC16_LO,      /* type */
944          0,                     /* rightshift */
945          1,                     /* size (0 = byte, 1 = short, 2 = long) */
946          16,                    /* bitsize */
947          FALSE,                 /* pc_relative */
948          0,                     /* bitpos */
949          complain_overflow_dont, /* complain_on_overflow */
950          ppc64_elf_toc_reloc,   /* special_function */
951          "R_PPC64_TOC16_LO",    /* name */
952          FALSE,                 /* partial_inplace */
953          0,                     /* src_mask */
954          0xffff,                /* dst_mask */
955          FALSE),                /* pcrel_offset */
956
957   /* 16 bit TOC-relative relocation, high 16 bits.  */
958
959   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
960   HOWTO (R_PPC64_TOC16_HI,      /* type */
961          16,                    /* rightshift */
962          1,                     /* size (0 = byte, 1 = short, 2 = long) */
963          16,                    /* bitsize */
964          FALSE,                 /* pc_relative */
965          0,                     /* bitpos */
966          complain_overflow_dont, /* complain_on_overflow */
967          ppc64_elf_toc_reloc,   /* special_function */
968          "R_PPC64_TOC16_HI",    /* name */
969          FALSE,                 /* partial_inplace */
970          0,                     /* src_mask */
971          0xffff,                /* dst_mask */
972          FALSE),                /* pcrel_offset */
973
974   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
975      contents of the low 16 bits, treated as a signed number, is
976      negative.  */
977
978   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
979   HOWTO (R_PPC64_TOC16_HA,      /* type */
980          16,                    /* rightshift */
981          1,                     /* size (0 = byte, 1 = short, 2 = long) */
982          16,                    /* bitsize */
983          FALSE,                 /* pc_relative */
984          0,                     /* bitpos */
985          complain_overflow_dont, /* complain_on_overflow */
986          ppc64_elf_toc_ha_reloc, /* special_function */
987          "R_PPC64_TOC16_HA",    /* name */
988          FALSE,                 /* partial_inplace */
989          0,                     /* src_mask */
990          0xffff,                /* dst_mask */
991          FALSE),                /* pcrel_offset */
992
993   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
994
995   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
996   HOWTO (R_PPC64_TOC,           /* type */
997          0,                     /* rightshift */
998          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
999          64,                    /* bitsize */
1000          FALSE,                 /* pc_relative */
1001          0,                     /* bitpos */
1002          complain_overflow_bitfield, /* complain_on_overflow */
1003          ppc64_elf_toc64_reloc, /* special_function */
1004          "R_PPC64_TOC",         /* name */
1005          FALSE,                 /* partial_inplace */
1006          0,                     /* src_mask */
1007          ONES (64),             /* dst_mask */
1008          FALSE),                /* pcrel_offset */
1009
1010   /* Like R_PPC64_GOT16, but also informs the link editor that the
1011      value to relocate may (!) refer to a PLT entry which the link
1012      editor (a) may replace with the symbol value.  If the link editor
1013      is unable to fully resolve the symbol, it may (b) create a PLT
1014      entry and store the address to the new PLT entry in the GOT.
1015      This permits lazy resolution of function symbols at run time.
1016      The link editor may also skip all of this and just (c) emit a
1017      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
1018   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
1019     HOWTO (R_PPC64_PLTGOT16,    /* type */
1020          0,                     /* rightshift */
1021          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1022          16,                    /* bitsize */
1023          FALSE,                 /* pc_relative */
1024          0,                     /* bitpos */
1025          complain_overflow_signed, /* complain_on_overflow */
1026          ppc64_elf_unhandled_reloc, /* special_function */
1027          "R_PPC64_PLTGOT16",    /* name */
1028          FALSE,                 /* partial_inplace */
1029          0,                     /* src_mask */
1030          0xffff,                /* dst_mask */
1031          FALSE),                /* pcrel_offset */
1032
1033   /* Like R_PPC64_PLTGOT16, but without overflow.  */
1034   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1035   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1036          0,                     /* rightshift */
1037          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1038          16,                    /* bitsize */
1039          FALSE,                 /* pc_relative */
1040          0,                     /* bitpos */
1041          complain_overflow_dont, /* complain_on_overflow */
1042          ppc64_elf_unhandled_reloc, /* special_function */
1043          "R_PPC64_PLTGOT16_LO", /* name */
1044          FALSE,                 /* partial_inplace */
1045          0,                     /* src_mask */
1046          0xffff,                /* dst_mask */
1047          FALSE),                /* pcrel_offset */
1048
1049   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1050   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1051   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1052          16,                    /* rightshift */
1053          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1054          16,                    /* bitsize */
1055          FALSE,                 /* pc_relative */
1056          0,                     /* bitpos */
1057          complain_overflow_dont, /* complain_on_overflow */
1058          ppc64_elf_unhandled_reloc, /* special_function */
1059          "R_PPC64_PLTGOT16_HI", /* name */
1060          FALSE,                 /* partial_inplace */
1061          0,                     /* src_mask */
1062          0xffff,                /* dst_mask */
1063          FALSE),                /* pcrel_offset */
1064
1065   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1066      1 if the contents of the low 16 bits, treated as a signed number,
1067      is negative.  */
1068   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1069   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1070          16,                    /* rightshift */
1071          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1072          16,                    /* bitsize */
1073          FALSE,                 /* pc_relative */
1074          0,                     /* bitpos */
1075          complain_overflow_dont,/* complain_on_overflow */
1076          ppc64_elf_unhandled_reloc, /* special_function */
1077          "R_PPC64_PLTGOT16_HA", /* name */
1078          FALSE,                 /* partial_inplace */
1079          0,                     /* src_mask */
1080          0xffff,                /* dst_mask */
1081          FALSE),                /* pcrel_offset */
1082
1083   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1084   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1085          0,                     /* rightshift */
1086          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1087          16,                    /* bitsize */
1088          FALSE,                 /* pc_relative */
1089          0,                     /* bitpos */
1090          complain_overflow_bitfield, /* complain_on_overflow */
1091          bfd_elf_generic_reloc, /* special_function */
1092          "R_PPC64_ADDR16_DS",   /* name */
1093          FALSE,                 /* partial_inplace */
1094          0,                     /* src_mask */
1095          0xfffc,                /* dst_mask */
1096          FALSE),                /* pcrel_offset */
1097
1098   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1099   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1100          0,                     /* rightshift */
1101          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1102          16,                    /* bitsize */
1103          FALSE,                 /* pc_relative */
1104          0,                     /* bitpos */
1105          complain_overflow_dont,/* complain_on_overflow */
1106          bfd_elf_generic_reloc, /* special_function */
1107          "R_PPC64_ADDR16_LO_DS",/* name */
1108          FALSE,                 /* partial_inplace */
1109          0,                     /* src_mask */
1110          0xfffc,                /* dst_mask */
1111          FALSE),                /* pcrel_offset */
1112
1113   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1114   HOWTO (R_PPC64_GOT16_DS,      /* type */
1115          0,                     /* rightshift */
1116          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1117          16,                    /* bitsize */
1118          FALSE,                 /* pc_relative */
1119          0,                     /* bitpos */
1120          complain_overflow_signed, /* complain_on_overflow */
1121          ppc64_elf_unhandled_reloc, /* special_function */
1122          "R_PPC64_GOT16_DS",    /* name */
1123          FALSE,                 /* partial_inplace */
1124          0,                     /* src_mask */
1125          0xfffc,                /* dst_mask */
1126          FALSE),                /* pcrel_offset */
1127
1128   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1129   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1130          0,                     /* rightshift */
1131          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1132          16,                    /* bitsize */
1133          FALSE,                 /* pc_relative */
1134          0,                     /* bitpos */
1135          complain_overflow_dont, /* complain_on_overflow */
1136          ppc64_elf_unhandled_reloc, /* special_function */
1137          "R_PPC64_GOT16_LO_DS", /* name */
1138          FALSE,                 /* partial_inplace */
1139          0,                     /* src_mask */
1140          0xfffc,                /* dst_mask */
1141          FALSE),                /* pcrel_offset */
1142
1143   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1144   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1145          0,                     /* rightshift */
1146          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1147          16,                    /* bitsize */
1148          FALSE,                 /* pc_relative */
1149          0,                     /* bitpos */
1150          complain_overflow_dont, /* complain_on_overflow */
1151          ppc64_elf_unhandled_reloc, /* special_function */
1152          "R_PPC64_PLT16_LO_DS", /* name */
1153          FALSE,                 /* partial_inplace */
1154          0,                     /* src_mask */
1155          0xfffc,                /* dst_mask */
1156          FALSE),                /* pcrel_offset */
1157
1158   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1159   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1160          0,                     /* rightshift */
1161          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1162          16,                    /* bitsize */
1163          FALSE,                 /* pc_relative */
1164          0,                     /* bitpos */
1165          complain_overflow_bitfield, /* complain_on_overflow */
1166          ppc64_elf_sectoff_reloc, /* special_function */
1167          "R_PPC64_SECTOFF_DS",  /* name */
1168          FALSE,                 /* partial_inplace */
1169          0,                     /* src_mask */
1170          0xfffc,                /* dst_mask */
1171          FALSE),                /* pcrel_offset */
1172
1173   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1174   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1175          0,                     /* rightshift */
1176          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1177          16,                    /* bitsize */
1178          FALSE,                 /* pc_relative */
1179          0,                     /* bitpos */
1180          complain_overflow_dont, /* complain_on_overflow */
1181          ppc64_elf_sectoff_reloc, /* special_function */
1182          "R_PPC64_SECTOFF_LO_DS",/* name */
1183          FALSE,                 /* partial_inplace */
1184          0,                     /* src_mask */
1185          0xfffc,                /* dst_mask */
1186          FALSE),                /* pcrel_offset */
1187
1188   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1189   HOWTO (R_PPC64_TOC16_DS,      /* type */
1190          0,                     /* rightshift */
1191          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1192          16,                    /* bitsize */
1193          FALSE,                 /* pc_relative */
1194          0,                     /* bitpos */
1195          complain_overflow_signed, /* complain_on_overflow */
1196          ppc64_elf_toc_reloc,   /* special_function */
1197          "R_PPC64_TOC16_DS",    /* name */
1198          FALSE,                 /* partial_inplace */
1199          0,                     /* src_mask */
1200          0xfffc,                /* dst_mask */
1201          FALSE),                /* pcrel_offset */
1202
1203   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1204   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1205          0,                     /* rightshift */
1206          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1207          16,                    /* bitsize */
1208          FALSE,                 /* pc_relative */
1209          0,                     /* bitpos */
1210          complain_overflow_dont, /* complain_on_overflow */
1211          ppc64_elf_toc_reloc,   /* special_function */
1212          "R_PPC64_TOC16_LO_DS", /* name */
1213          FALSE,                 /* partial_inplace */
1214          0,                     /* src_mask */
1215          0xfffc,                /* dst_mask */
1216          FALSE),                /* pcrel_offset */
1217
1218   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1219   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1220   HOWTO (R_PPC64_PLTGOT16_DS,   /* type */
1221          0,                     /* rightshift */
1222          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1223          16,                    /* bitsize */
1224          FALSE,                 /* pc_relative */
1225          0,                     /* bitpos */
1226          complain_overflow_signed, /* complain_on_overflow */
1227          ppc64_elf_unhandled_reloc, /* special_function */
1228          "R_PPC64_PLTGOT16_DS", /* name */
1229          FALSE,                 /* partial_inplace */
1230          0,                     /* src_mask */
1231          0xfffc,                /* dst_mask */
1232          FALSE),                /* pcrel_offset */
1233
1234   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1235   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1236   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1237          0,                     /* rightshift */
1238          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1239          16,                    /* bitsize */
1240          FALSE,                 /* pc_relative */
1241          0,                     /* bitpos */
1242          complain_overflow_dont, /* complain_on_overflow */
1243          ppc64_elf_unhandled_reloc, /* special_function */
1244          "R_PPC64_PLTGOT16_LO_DS",/* name */
1245          FALSE,                 /* partial_inplace */
1246          0,                     /* src_mask */
1247          0xfffc,                /* dst_mask */
1248          FALSE),                /* pcrel_offset */
1249
1250   /* Marker relocs for TLS.  */
1251   HOWTO (R_PPC64_TLS,
1252          0,                     /* rightshift */
1253          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1254          32,                    /* bitsize */
1255          FALSE,                 /* pc_relative */
1256          0,                     /* bitpos */
1257          complain_overflow_dont, /* complain_on_overflow */
1258          bfd_elf_generic_reloc, /* special_function */
1259          "R_PPC64_TLS",         /* name */
1260          FALSE,                 /* partial_inplace */
1261          0,                     /* src_mask */
1262          0,                     /* dst_mask */
1263          FALSE),                /* pcrel_offset */
1264
1265   HOWTO (R_PPC64_TLSGD,
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_TLSGD",       /* name */
1274          FALSE,                 /* partial_inplace */
1275          0,                     /* src_mask */
1276          0,                     /* dst_mask */
1277          FALSE),                /* pcrel_offset */
1278
1279   HOWTO (R_PPC64_TLSLD,
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_TLSLD",       /* name */
1288          FALSE,                 /* partial_inplace */
1289          0,                     /* src_mask */
1290          0,                     /* dst_mask */
1291          FALSE),                /* pcrel_offset */
1292
1293   HOWTO (R_PPC64_TOCSAVE,
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_TOCSAVE",     /* name */
1302          FALSE,                 /* partial_inplace */
1303          0,                     /* src_mask */
1304          0,                     /* dst_mask */
1305          FALSE),                /* pcrel_offset */
1306
1307   /* Computes the load module index of the load module that contains the
1308      definition of its TLS sym.  */
1309   HOWTO (R_PPC64_DTPMOD64,
1310          0,                     /* rightshift */
1311          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1312          64,                    /* bitsize */
1313          FALSE,                 /* pc_relative */
1314          0,                     /* bitpos */
1315          complain_overflow_dont, /* complain_on_overflow */
1316          ppc64_elf_unhandled_reloc, /* special_function */
1317          "R_PPC64_DTPMOD64",    /* name */
1318          FALSE,                 /* partial_inplace */
1319          0,                     /* src_mask */
1320          ONES (64),             /* dst_mask */
1321          FALSE),                /* pcrel_offset */
1322
1323   /* Computes a dtv-relative displacement, the difference between the value
1324      of sym+add and the base address of the thread-local storage block that
1325      contains the definition of sym, minus 0x8000.  */
1326   HOWTO (R_PPC64_DTPREL64,
1327          0,                     /* rightshift */
1328          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1329          64,                    /* bitsize */
1330          FALSE,                 /* pc_relative */
1331          0,                     /* bitpos */
1332          complain_overflow_dont, /* complain_on_overflow */
1333          ppc64_elf_unhandled_reloc, /* special_function */
1334          "R_PPC64_DTPREL64",    /* name */
1335          FALSE,                 /* partial_inplace */
1336          0,                     /* src_mask */
1337          ONES (64),             /* dst_mask */
1338          FALSE),                /* pcrel_offset */
1339
1340   /* A 16 bit dtprel reloc.  */
1341   HOWTO (R_PPC64_DTPREL16,
1342          0,                     /* rightshift */
1343          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1344          16,                    /* bitsize */
1345          FALSE,                 /* pc_relative */
1346          0,                     /* bitpos */
1347          complain_overflow_signed, /* complain_on_overflow */
1348          ppc64_elf_unhandled_reloc, /* special_function */
1349          "R_PPC64_DTPREL16",    /* name */
1350          FALSE,                 /* partial_inplace */
1351          0,                     /* src_mask */
1352          0xffff,                /* dst_mask */
1353          FALSE),                /* pcrel_offset */
1354
1355   /* Like DTPREL16, but no overflow.  */
1356   HOWTO (R_PPC64_DTPREL16_LO,
1357          0,                     /* rightshift */
1358          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1359          16,                    /* bitsize */
1360          FALSE,                 /* pc_relative */
1361          0,                     /* bitpos */
1362          complain_overflow_dont, /* complain_on_overflow */
1363          ppc64_elf_unhandled_reloc, /* special_function */
1364          "R_PPC64_DTPREL16_LO", /* name */
1365          FALSE,                 /* partial_inplace */
1366          0,                     /* src_mask */
1367          0xffff,                /* dst_mask */
1368          FALSE),                /* pcrel_offset */
1369
1370   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1371   HOWTO (R_PPC64_DTPREL16_HI,
1372          16,                    /* rightshift */
1373          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1374          16,                    /* bitsize */
1375          FALSE,                 /* pc_relative */
1376          0,                     /* bitpos */
1377          complain_overflow_dont, /* complain_on_overflow */
1378          ppc64_elf_unhandled_reloc, /* special_function */
1379          "R_PPC64_DTPREL16_HI", /* name */
1380          FALSE,                 /* partial_inplace */
1381          0,                     /* src_mask */
1382          0xffff,                /* dst_mask */
1383          FALSE),                /* pcrel_offset */
1384
1385   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1386   HOWTO (R_PPC64_DTPREL16_HA,
1387          16,                    /* rightshift */
1388          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1389          16,                    /* bitsize */
1390          FALSE,                 /* pc_relative */
1391          0,                     /* bitpos */
1392          complain_overflow_dont, /* complain_on_overflow */
1393          ppc64_elf_unhandled_reloc, /* special_function */
1394          "R_PPC64_DTPREL16_HA", /* name */
1395          FALSE,                 /* partial_inplace */
1396          0,                     /* src_mask */
1397          0xffff,                /* dst_mask */
1398          FALSE),                /* pcrel_offset */
1399
1400   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1401   HOWTO (R_PPC64_DTPREL16_HIGHER,
1402          32,                    /* rightshift */
1403          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1404          16,                    /* bitsize */
1405          FALSE,                 /* pc_relative */
1406          0,                     /* bitpos */
1407          complain_overflow_dont, /* complain_on_overflow */
1408          ppc64_elf_unhandled_reloc, /* special_function */
1409          "R_PPC64_DTPREL16_HIGHER", /* name */
1410          FALSE,                 /* partial_inplace */
1411          0,                     /* src_mask */
1412          0xffff,                /* dst_mask */
1413          FALSE),                /* pcrel_offset */
1414
1415   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1416   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1417          32,                    /* rightshift */
1418          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1419          16,                    /* bitsize */
1420          FALSE,                 /* pc_relative */
1421          0,                     /* bitpos */
1422          complain_overflow_dont, /* complain_on_overflow */
1423          ppc64_elf_unhandled_reloc, /* special_function */
1424          "R_PPC64_DTPREL16_HIGHERA", /* name */
1425          FALSE,                 /* partial_inplace */
1426          0,                     /* src_mask */
1427          0xffff,                /* dst_mask */
1428          FALSE),                /* pcrel_offset */
1429
1430   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1431   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1432          48,                    /* rightshift */
1433          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1434          16,                    /* bitsize */
1435          FALSE,                 /* pc_relative */
1436          0,                     /* bitpos */
1437          complain_overflow_dont, /* complain_on_overflow */
1438          ppc64_elf_unhandled_reloc, /* special_function */
1439          "R_PPC64_DTPREL16_HIGHEST", /* name */
1440          FALSE,                 /* partial_inplace */
1441          0,                     /* src_mask */
1442          0xffff,                /* dst_mask */
1443          FALSE),                /* pcrel_offset */
1444
1445   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1446   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1447          48,                    /* rightshift */
1448          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1449          16,                    /* bitsize */
1450          FALSE,                 /* pc_relative */
1451          0,                     /* bitpos */
1452          complain_overflow_dont, /* complain_on_overflow */
1453          ppc64_elf_unhandled_reloc, /* special_function */
1454          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1455          FALSE,                 /* partial_inplace */
1456          0,                     /* src_mask */
1457          0xffff,                /* dst_mask */
1458          FALSE),                /* pcrel_offset */
1459
1460   /* Like DTPREL16, but for insns with a DS field.  */
1461   HOWTO (R_PPC64_DTPREL16_DS,
1462          0,                     /* rightshift */
1463          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1464          16,                    /* bitsize */
1465          FALSE,                 /* pc_relative */
1466          0,                     /* bitpos */
1467          complain_overflow_signed, /* complain_on_overflow */
1468          ppc64_elf_unhandled_reloc, /* special_function */
1469          "R_PPC64_DTPREL16_DS", /* name */
1470          FALSE,                 /* partial_inplace */
1471          0,                     /* src_mask */
1472          0xfffc,                /* dst_mask */
1473          FALSE),                /* pcrel_offset */
1474
1475   /* Like DTPREL16_DS, but no overflow.  */
1476   HOWTO (R_PPC64_DTPREL16_LO_DS,
1477          0,                     /* rightshift */
1478          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1479          16,                    /* bitsize */
1480          FALSE,                 /* pc_relative */
1481          0,                     /* bitpos */
1482          complain_overflow_dont, /* complain_on_overflow */
1483          ppc64_elf_unhandled_reloc, /* special_function */
1484          "R_PPC64_DTPREL16_LO_DS", /* name */
1485          FALSE,                 /* partial_inplace */
1486          0,                     /* src_mask */
1487          0xfffc,                /* dst_mask */
1488          FALSE),                /* pcrel_offset */
1489
1490   /* Computes a tp-relative displacement, the difference between the value of
1491      sym+add and the value of the thread pointer (r13).  */
1492   HOWTO (R_PPC64_TPREL64,
1493          0,                     /* rightshift */
1494          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1495          64,                    /* bitsize */
1496          FALSE,                 /* pc_relative */
1497          0,                     /* bitpos */
1498          complain_overflow_dont, /* complain_on_overflow */
1499          ppc64_elf_unhandled_reloc, /* special_function */
1500          "R_PPC64_TPREL64",     /* name */
1501          FALSE,                 /* partial_inplace */
1502          0,                     /* src_mask */
1503          ONES (64),             /* dst_mask */
1504          FALSE),                /* pcrel_offset */
1505
1506   /* A 16 bit tprel reloc.  */
1507   HOWTO (R_PPC64_TPREL16,
1508          0,                     /* rightshift */
1509          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1510          16,                    /* bitsize */
1511          FALSE,                 /* pc_relative */
1512          0,                     /* bitpos */
1513          complain_overflow_signed, /* complain_on_overflow */
1514          ppc64_elf_unhandled_reloc, /* special_function */
1515          "R_PPC64_TPREL16",     /* name */
1516          FALSE,                 /* partial_inplace */
1517          0,                     /* src_mask */
1518          0xffff,                /* dst_mask */
1519          FALSE),                /* pcrel_offset */
1520
1521   /* Like TPREL16, but no overflow.  */
1522   HOWTO (R_PPC64_TPREL16_LO,
1523          0,                     /* rightshift */
1524          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1525          16,                    /* bitsize */
1526          FALSE,                 /* pc_relative */
1527          0,                     /* bitpos */
1528          complain_overflow_dont, /* complain_on_overflow */
1529          ppc64_elf_unhandled_reloc, /* special_function */
1530          "R_PPC64_TPREL16_LO",  /* name */
1531          FALSE,                 /* partial_inplace */
1532          0,                     /* src_mask */
1533          0xffff,                /* dst_mask */
1534          FALSE),                /* pcrel_offset */
1535
1536   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1537   HOWTO (R_PPC64_TPREL16_HI,
1538          16,                    /* rightshift */
1539          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1540          16,                    /* bitsize */
1541          FALSE,                 /* pc_relative */
1542          0,                     /* bitpos */
1543          complain_overflow_dont, /* complain_on_overflow */
1544          ppc64_elf_unhandled_reloc, /* special_function */
1545          "R_PPC64_TPREL16_HI",  /* name */
1546          FALSE,                 /* partial_inplace */
1547          0,                     /* src_mask */
1548          0xffff,                /* dst_mask */
1549          FALSE),                /* pcrel_offset */
1550
1551   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1552   HOWTO (R_PPC64_TPREL16_HA,
1553          16,                    /* rightshift */
1554          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1555          16,                    /* bitsize */
1556          FALSE,                 /* pc_relative */
1557          0,                     /* bitpos */
1558          complain_overflow_dont, /* complain_on_overflow */
1559          ppc64_elf_unhandled_reloc, /* special_function */
1560          "R_PPC64_TPREL16_HA",  /* name */
1561          FALSE,                 /* partial_inplace */
1562          0,                     /* src_mask */
1563          0xffff,                /* dst_mask */
1564          FALSE),                /* pcrel_offset */
1565
1566   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1567   HOWTO (R_PPC64_TPREL16_HIGHER,
1568          32,                    /* rightshift */
1569          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1570          16,                    /* bitsize */
1571          FALSE,                 /* pc_relative */
1572          0,                     /* bitpos */
1573          complain_overflow_dont, /* complain_on_overflow */
1574          ppc64_elf_unhandled_reloc, /* special_function */
1575          "R_PPC64_TPREL16_HIGHER",      /* name */
1576          FALSE,                 /* partial_inplace */
1577          0,                     /* src_mask */
1578          0xffff,                /* dst_mask */
1579          FALSE),                /* pcrel_offset */
1580
1581   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1582   HOWTO (R_PPC64_TPREL16_HIGHERA,
1583          32,                    /* rightshift */
1584          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1585          16,                    /* bitsize */
1586          FALSE,                 /* pc_relative */
1587          0,                     /* bitpos */
1588          complain_overflow_dont, /* complain_on_overflow */
1589          ppc64_elf_unhandled_reloc, /* special_function */
1590          "R_PPC64_TPREL16_HIGHERA", /* name */
1591          FALSE,                 /* partial_inplace */
1592          0,                     /* src_mask */
1593          0xffff,                /* dst_mask */
1594          FALSE),                /* pcrel_offset */
1595
1596   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1597   HOWTO (R_PPC64_TPREL16_HIGHEST,
1598          48,                    /* rightshift */
1599          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1600          16,                    /* bitsize */
1601          FALSE,                 /* pc_relative */
1602          0,                     /* bitpos */
1603          complain_overflow_dont, /* complain_on_overflow */
1604          ppc64_elf_unhandled_reloc, /* special_function */
1605          "R_PPC64_TPREL16_HIGHEST", /* name */
1606          FALSE,                 /* partial_inplace */
1607          0,                     /* src_mask */
1608          0xffff,                /* dst_mask */
1609          FALSE),                /* pcrel_offset */
1610
1611   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1612   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1613          48,                    /* rightshift */
1614          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1615          16,                    /* bitsize */
1616          FALSE,                 /* pc_relative */
1617          0,                     /* bitpos */
1618          complain_overflow_dont, /* complain_on_overflow */
1619          ppc64_elf_unhandled_reloc, /* special_function */
1620          "R_PPC64_TPREL16_HIGHESTA", /* name */
1621          FALSE,                 /* partial_inplace */
1622          0,                     /* src_mask */
1623          0xffff,                /* dst_mask */
1624          FALSE),                /* pcrel_offset */
1625
1626   /* Like TPREL16, but for insns with a DS field.  */
1627   HOWTO (R_PPC64_TPREL16_DS,
1628          0,                     /* rightshift */
1629          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1630          16,                    /* bitsize */
1631          FALSE,                 /* pc_relative */
1632          0,                     /* bitpos */
1633          complain_overflow_signed, /* complain_on_overflow */
1634          ppc64_elf_unhandled_reloc, /* special_function */
1635          "R_PPC64_TPREL16_DS",  /* name */
1636          FALSE,                 /* partial_inplace */
1637          0,                     /* src_mask */
1638          0xfffc,                /* dst_mask */
1639          FALSE),                /* pcrel_offset */
1640
1641   /* Like TPREL16_DS, but no overflow.  */
1642   HOWTO (R_PPC64_TPREL16_LO_DS,
1643          0,                     /* rightshift */
1644          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1645          16,                    /* bitsize */
1646          FALSE,                 /* pc_relative */
1647          0,                     /* bitpos */
1648          complain_overflow_dont, /* complain_on_overflow */
1649          ppc64_elf_unhandled_reloc, /* special_function */
1650          "R_PPC64_TPREL16_LO_DS", /* name */
1651          FALSE,                 /* partial_inplace */
1652          0,                     /* src_mask */
1653          0xfffc,                /* dst_mask */
1654          FALSE),                /* pcrel_offset */
1655
1656   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1657      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1658      to the first entry relative to the TOC base (r2).  */
1659   HOWTO (R_PPC64_GOT_TLSGD16,
1660          0,                     /* rightshift */
1661          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1662          16,                    /* bitsize */
1663          FALSE,                 /* pc_relative */
1664          0,                     /* bitpos */
1665          complain_overflow_signed, /* complain_on_overflow */
1666          ppc64_elf_unhandled_reloc, /* special_function */
1667          "R_PPC64_GOT_TLSGD16", /* name */
1668          FALSE,                 /* partial_inplace */
1669          0,                     /* src_mask */
1670          0xffff,                /* dst_mask */
1671          FALSE),                /* pcrel_offset */
1672
1673   /* Like GOT_TLSGD16, but no overflow.  */
1674   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1675          0,                     /* rightshift */
1676          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1677          16,                    /* bitsize */
1678          FALSE,                 /* pc_relative */
1679          0,                     /* bitpos */
1680          complain_overflow_dont, /* complain_on_overflow */
1681          ppc64_elf_unhandled_reloc, /* special_function */
1682          "R_PPC64_GOT_TLSGD16_LO", /* name */
1683          FALSE,                 /* partial_inplace */
1684          0,                     /* src_mask */
1685          0xffff,                /* dst_mask */
1686          FALSE),                /* pcrel_offset */
1687
1688   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1689   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1690          16,                    /* rightshift */
1691          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1692          16,                    /* bitsize */
1693          FALSE,                 /* pc_relative */
1694          0,                     /* bitpos */
1695          complain_overflow_dont, /* complain_on_overflow */
1696          ppc64_elf_unhandled_reloc, /* special_function */
1697          "R_PPC64_GOT_TLSGD16_HI", /* name */
1698          FALSE,                 /* partial_inplace */
1699          0,                     /* src_mask */
1700          0xffff,                /* dst_mask */
1701          FALSE),                /* pcrel_offset */
1702
1703   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1704   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1705          16,                    /* rightshift */
1706          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1707          16,                    /* bitsize */
1708          FALSE,                 /* pc_relative */
1709          0,                     /* bitpos */
1710          complain_overflow_dont, /* complain_on_overflow */
1711          ppc64_elf_unhandled_reloc, /* special_function */
1712          "R_PPC64_GOT_TLSGD16_HA", /* name */
1713          FALSE,                 /* partial_inplace */
1714          0,                     /* src_mask */
1715          0xffff,                /* dst_mask */
1716          FALSE),                /* pcrel_offset */
1717
1718   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1719      with values (sym+add)@dtpmod and zero, and computes the offset to the
1720      first entry relative to the TOC base (r2).  */
1721   HOWTO (R_PPC64_GOT_TLSLD16,
1722          0,                     /* rightshift */
1723          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1724          16,                    /* bitsize */
1725          FALSE,                 /* pc_relative */
1726          0,                     /* bitpos */
1727          complain_overflow_signed, /* complain_on_overflow */
1728          ppc64_elf_unhandled_reloc, /* special_function */
1729          "R_PPC64_GOT_TLSLD16", /* name */
1730          FALSE,                 /* partial_inplace */
1731          0,                     /* src_mask */
1732          0xffff,                /* dst_mask */
1733          FALSE),                /* pcrel_offset */
1734
1735   /* Like GOT_TLSLD16, but no overflow.  */
1736   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1737          0,                     /* rightshift */
1738          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1739          16,                    /* bitsize */
1740          FALSE,                 /* pc_relative */
1741          0,                     /* bitpos */
1742          complain_overflow_dont, /* complain_on_overflow */
1743          ppc64_elf_unhandled_reloc, /* special_function */
1744          "R_PPC64_GOT_TLSLD16_LO", /* name */
1745          FALSE,                 /* partial_inplace */
1746          0,                     /* src_mask */
1747          0xffff,                /* dst_mask */
1748          FALSE),                /* pcrel_offset */
1749
1750   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1751   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1752          16,                    /* rightshift */
1753          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1754          16,                    /* bitsize */
1755          FALSE,                 /* pc_relative */
1756          0,                     /* bitpos */
1757          complain_overflow_dont, /* complain_on_overflow */
1758          ppc64_elf_unhandled_reloc, /* special_function */
1759          "R_PPC64_GOT_TLSLD16_HI", /* name */
1760          FALSE,                 /* partial_inplace */
1761          0,                     /* src_mask */
1762          0xffff,                /* dst_mask */
1763          FALSE),                /* pcrel_offset */
1764
1765   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1766   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1767          16,                    /* rightshift */
1768          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1769          16,                    /* bitsize */
1770          FALSE,                 /* pc_relative */
1771          0,                     /* bitpos */
1772          complain_overflow_dont, /* complain_on_overflow */
1773          ppc64_elf_unhandled_reloc, /* special_function */
1774          "R_PPC64_GOT_TLSLD16_HA", /* name */
1775          FALSE,                 /* partial_inplace */
1776          0,                     /* src_mask */
1777          0xffff,                /* dst_mask */
1778          FALSE),                /* pcrel_offset */
1779
1780   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1781      the offset to the entry relative to the TOC base (r2).  */
1782   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1783          0,                     /* rightshift */
1784          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1785          16,                    /* bitsize */
1786          FALSE,                 /* pc_relative */
1787          0,                     /* bitpos */
1788          complain_overflow_signed, /* complain_on_overflow */
1789          ppc64_elf_unhandled_reloc, /* special_function */
1790          "R_PPC64_GOT_DTPREL16_DS", /* name */
1791          FALSE,                 /* partial_inplace */
1792          0,                     /* src_mask */
1793          0xfffc,                /* dst_mask */
1794          FALSE),                /* pcrel_offset */
1795
1796   /* Like GOT_DTPREL16_DS, but no overflow.  */
1797   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1798          0,                     /* rightshift */
1799          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1800          16,                    /* bitsize */
1801          FALSE,                 /* pc_relative */
1802          0,                     /* bitpos */
1803          complain_overflow_dont, /* complain_on_overflow */
1804          ppc64_elf_unhandled_reloc, /* special_function */
1805          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1806          FALSE,                 /* partial_inplace */
1807          0,                     /* src_mask */
1808          0xfffc,                /* dst_mask */
1809          FALSE),                /* pcrel_offset */
1810
1811   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1812   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1813          16,                    /* rightshift */
1814          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1815          16,                    /* bitsize */
1816          FALSE,                 /* pc_relative */
1817          0,                     /* bitpos */
1818          complain_overflow_dont, /* complain_on_overflow */
1819          ppc64_elf_unhandled_reloc, /* special_function */
1820          "R_PPC64_GOT_DTPREL16_HI", /* name */
1821          FALSE,                 /* partial_inplace */
1822          0,                     /* src_mask */
1823          0xffff,                /* dst_mask */
1824          FALSE),                /* pcrel_offset */
1825
1826   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1827   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1828          16,                    /* rightshift */
1829          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1830          16,                    /* bitsize */
1831          FALSE,                 /* pc_relative */
1832          0,                     /* bitpos */
1833          complain_overflow_dont, /* complain_on_overflow */
1834          ppc64_elf_unhandled_reloc, /* special_function */
1835          "R_PPC64_GOT_DTPREL16_HA", /* name */
1836          FALSE,                 /* partial_inplace */
1837          0,                     /* src_mask */
1838          0xffff,                /* dst_mask */
1839          FALSE),                /* pcrel_offset */
1840
1841   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1842      offset to the entry relative to the TOC base (r2).  */
1843   HOWTO (R_PPC64_GOT_TPREL16_DS,
1844          0,                     /* rightshift */
1845          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1846          16,                    /* bitsize */
1847          FALSE,                 /* pc_relative */
1848          0,                     /* bitpos */
1849          complain_overflow_signed, /* complain_on_overflow */
1850          ppc64_elf_unhandled_reloc, /* special_function */
1851          "R_PPC64_GOT_TPREL16_DS", /* name */
1852          FALSE,                 /* partial_inplace */
1853          0,                     /* src_mask */
1854          0xfffc,                /* dst_mask */
1855          FALSE),                /* pcrel_offset */
1856
1857   /* Like GOT_TPREL16_DS, but no overflow.  */
1858   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1859          0,                     /* rightshift */
1860          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1861          16,                    /* bitsize */
1862          FALSE,                 /* pc_relative */
1863          0,                     /* bitpos */
1864          complain_overflow_dont, /* complain_on_overflow */
1865          ppc64_elf_unhandled_reloc, /* special_function */
1866          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1867          FALSE,                 /* partial_inplace */
1868          0,                     /* src_mask */
1869          0xfffc,                /* dst_mask */
1870          FALSE),                /* pcrel_offset */
1871
1872   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1873   HOWTO (R_PPC64_GOT_TPREL16_HI,
1874          16,                    /* rightshift */
1875          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1876          16,                    /* bitsize */
1877          FALSE,                 /* pc_relative */
1878          0,                     /* bitpos */
1879          complain_overflow_dont, /* complain_on_overflow */
1880          ppc64_elf_unhandled_reloc, /* special_function */
1881          "R_PPC64_GOT_TPREL16_HI", /* name */
1882          FALSE,                 /* partial_inplace */
1883          0,                     /* src_mask */
1884          0xffff,                /* dst_mask */
1885          FALSE),                /* pcrel_offset */
1886
1887   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1888   HOWTO (R_PPC64_GOT_TPREL16_HA,
1889          16,                    /* rightshift */
1890          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1891          16,                    /* bitsize */
1892          FALSE,                 /* pc_relative */
1893          0,                     /* bitpos */
1894          complain_overflow_dont, /* complain_on_overflow */
1895          ppc64_elf_unhandled_reloc, /* special_function */
1896          "R_PPC64_GOT_TPREL16_HA", /* name */
1897          FALSE,                 /* partial_inplace */
1898          0,                     /* src_mask */
1899          0xffff,                /* dst_mask */
1900          FALSE),                /* pcrel_offset */
1901
1902   HOWTO (R_PPC64_JMP_IREL,      /* type */
1903          0,                     /* rightshift */
1904          0,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1905          0,                     /* bitsize */
1906          FALSE,                 /* pc_relative */
1907          0,                     /* bitpos */
1908          complain_overflow_dont, /* complain_on_overflow */
1909          ppc64_elf_unhandled_reloc, /* special_function */
1910          "R_PPC64_JMP_IREL",    /* name */
1911          FALSE,                 /* partial_inplace */
1912          0,                     /* src_mask */
1913          0,                     /* dst_mask */
1914          FALSE),                /* pcrel_offset */
1915
1916   HOWTO (R_PPC64_IRELATIVE,     /* type */
1917          0,                     /* rightshift */
1918          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1919          64,                    /* bitsize */
1920          FALSE,                 /* pc_relative */
1921          0,                     /* bitpos */
1922          complain_overflow_dont, /* complain_on_overflow */
1923          bfd_elf_generic_reloc, /* special_function */
1924          "R_PPC64_IRELATIVE",   /* name */
1925          FALSE,                 /* partial_inplace */
1926          0,                     /* src_mask */
1927          ONES (64),             /* dst_mask */
1928          FALSE),                /* pcrel_offset */
1929
1930   /* A 16 bit relative relocation.  */
1931   HOWTO (R_PPC64_REL16,         /* type */
1932          0,                     /* rightshift */
1933          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1934          16,                    /* bitsize */
1935          TRUE,                  /* pc_relative */
1936          0,                     /* bitpos */
1937          complain_overflow_bitfield, /* complain_on_overflow */
1938          bfd_elf_generic_reloc, /* special_function */
1939          "R_PPC64_REL16",       /* name */
1940          FALSE,                 /* partial_inplace */
1941          0,                     /* src_mask */
1942          0xffff,                /* dst_mask */
1943          TRUE),                 /* pcrel_offset */
1944
1945   /* A 16 bit relative relocation without overflow.  */
1946   HOWTO (R_PPC64_REL16_LO,      /* type */
1947          0,                     /* rightshift */
1948          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1949          16,                    /* bitsize */
1950          TRUE,                  /* pc_relative */
1951          0,                     /* bitpos */
1952          complain_overflow_dont,/* complain_on_overflow */
1953          bfd_elf_generic_reloc, /* special_function */
1954          "R_PPC64_REL16_LO",    /* name */
1955          FALSE,                 /* partial_inplace */
1956          0,                     /* src_mask */
1957          0xffff,                /* dst_mask */
1958          TRUE),                 /* pcrel_offset */
1959
1960   /* The high order 16 bits of a relative address.  */
1961   HOWTO (R_PPC64_REL16_HI,      /* type */
1962          16,                    /* rightshift */
1963          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1964          16,                    /* bitsize */
1965          TRUE,                  /* pc_relative */
1966          0,                     /* bitpos */
1967          complain_overflow_dont, /* complain_on_overflow */
1968          bfd_elf_generic_reloc, /* special_function */
1969          "R_PPC64_REL16_HI",    /* name */
1970          FALSE,                 /* partial_inplace */
1971          0,                     /* src_mask */
1972          0xffff,                /* dst_mask */
1973          TRUE),                 /* pcrel_offset */
1974
1975   /* The high order 16 bits of a relative address, plus 1 if the contents of
1976      the low 16 bits, treated as a signed number, is negative.  */
1977   HOWTO (R_PPC64_REL16_HA,      /* type */
1978          16,                    /* rightshift */
1979          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1980          16,                    /* bitsize */
1981          TRUE,                  /* pc_relative */
1982          0,                     /* bitpos */
1983          complain_overflow_dont, /* complain_on_overflow */
1984          ppc64_elf_ha_reloc,    /* special_function */
1985          "R_PPC64_REL16_HA",    /* name */
1986          FALSE,                 /* partial_inplace */
1987          0,                     /* src_mask */
1988          0xffff,                /* dst_mask */
1989          TRUE),                 /* pcrel_offset */
1990
1991   /* GNU extension to record C++ vtable hierarchy.  */
1992   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1993          0,                     /* rightshift */
1994          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1995          0,                     /* bitsize */
1996          FALSE,                 /* pc_relative */
1997          0,                     /* bitpos */
1998          complain_overflow_dont, /* complain_on_overflow */
1999          NULL,                  /* special_function */
2000          "R_PPC64_GNU_VTINHERIT", /* name */
2001          FALSE,                 /* partial_inplace */
2002          0,                     /* src_mask */
2003          0,                     /* dst_mask */
2004          FALSE),                /* pcrel_offset */
2005
2006   /* GNU extension to record C++ vtable member usage.  */
2007   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
2008          0,                     /* rightshift */
2009          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2010          0,                     /* bitsize */
2011          FALSE,                 /* pc_relative */
2012          0,                     /* bitpos */
2013          complain_overflow_dont, /* complain_on_overflow */
2014          NULL,                  /* special_function */
2015          "R_PPC64_GNU_VTENTRY", /* name */
2016          FALSE,                 /* partial_inplace */
2017          0,                     /* src_mask */
2018          0,                     /* dst_mask */
2019          FALSE),                /* pcrel_offset */
2020 };
2021
2022 \f
2023 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2024    be done.  */
2025
2026 static void
2027 ppc_howto_init (void)
2028 {
2029   unsigned int i, type;
2030
2031   for (i = 0;
2032        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2033        i++)
2034     {
2035       type = ppc64_elf_howto_raw[i].type;
2036       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2037                           / sizeof (ppc64_elf_howto_table[0])));
2038       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2039     }
2040 }
2041
2042 static reloc_howto_type *
2043 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2044                              bfd_reloc_code_real_type code)
2045 {
2046   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2047
2048   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2049     /* Initialize howto table if needed.  */
2050     ppc_howto_init ();
2051
2052   switch (code)
2053     {
2054     default:
2055       return NULL;
2056
2057     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
2058       break;
2059     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
2060       break;
2061     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
2062       break;
2063     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
2064       break;
2065     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
2066       break;
2067     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
2068       break;
2069     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
2070       break;
2071     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
2072       break;
2073     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
2074       break;
2075     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
2076       break;
2077     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
2078       break;
2079     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
2080       break;
2081     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
2082       break;
2083     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
2084       break;
2085     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
2086       break;
2087     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
2088       break;
2089     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
2090       break;
2091     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
2092       break;
2093     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
2094       break;
2095     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
2096       break;
2097     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
2098       break;
2099     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
2100       break;
2101     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
2102       break;
2103     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
2104       break;
2105     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
2106       break;
2107     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
2108       break;
2109     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
2110       break;
2111     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
2112       break;
2113     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
2114       break;
2115     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
2116       break;
2117     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
2118       break;
2119     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
2120       break;
2121     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
2122       break;
2123     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
2124       break;
2125     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
2126       break;
2127     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
2128       break;
2129     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
2130       break;
2131     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
2132       break;
2133     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
2134       break;
2135     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
2136       break;
2137     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
2138       break;
2139     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
2140       break;
2141     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
2142       break;
2143     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
2144       break;
2145     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
2146       break;
2147     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
2148       break;
2149     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
2150       break;
2151     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
2152       break;
2153     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
2154       break;
2155     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
2156       break;
2157     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
2158       break;
2159     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
2160       break;
2161     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
2162       break;
2163     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
2164       break;
2165     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2166       break;
2167     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2168       break;
2169     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2170       break;
2171     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2172       break;
2173     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2174       break;
2175     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2176       break;
2177     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
2178       break;
2179     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
2180       break;
2181     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2182       break;
2183     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2184       break;
2185     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2186       break;
2187     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2188       break;
2189     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2190       break;
2191     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2192       break;
2193     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2194       break;
2195     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2196       break;
2197     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2198       break;
2199     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2200       break;
2201     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2202       break;
2203     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2204       break;
2205     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2206       break;
2207     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2208       break;
2209     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2210       break;
2211     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2212       break;
2213     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2214       break;
2215     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2216       break;
2217     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2218       break;
2219     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2220       break;
2221     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2222       break;
2223     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2224       break;
2225     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2226       break;
2227     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2228       break;
2229     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2230       break;
2231     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2232       break;
2233     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2234       break;
2235     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2236       break;
2237     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2238       break;
2239     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2240       break;
2241     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2242       break;
2243     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2244       break;
2245     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2246       break;
2247     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2248       break;
2249     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2250       break;
2251     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2252       break;
2253     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2254       break;
2255     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2256       break;
2257     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2258       break;
2259     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
2260       break;
2261     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
2262       break;
2263     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
2264       break;
2265     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
2266       break;
2267     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2268       break;
2269     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2270       break;
2271     }
2272
2273   return ppc64_elf_howto_table[r];
2274 };
2275
2276 static reloc_howto_type *
2277 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2278                              const char *r_name)
2279 {
2280   unsigned int i;
2281
2282   for (i = 0;
2283        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2284        i++)
2285     if (ppc64_elf_howto_raw[i].name != NULL
2286         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2287       return &ppc64_elf_howto_raw[i];
2288
2289   return NULL;
2290 }
2291
2292 /* Set the howto pointer for a PowerPC ELF reloc.  */
2293
2294 static void
2295 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2296                          Elf_Internal_Rela *dst)
2297 {
2298   unsigned int type;
2299
2300   /* Initialize howto table if needed.  */
2301   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2302     ppc_howto_init ();
2303
2304   type = ELF64_R_TYPE (dst->r_info);
2305   if (type >= (sizeof (ppc64_elf_howto_table)
2306                / sizeof (ppc64_elf_howto_table[0])))
2307     {
2308       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2309                              abfd, (int) type);
2310       type = R_PPC64_NONE;
2311     }
2312   cache_ptr->howto = ppc64_elf_howto_table[type];
2313 }
2314
2315 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2316
2317 static bfd_reloc_status_type
2318 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2319                     void *data, asection *input_section,
2320                     bfd *output_bfd, char **error_message)
2321 {
2322   /* If this is a relocatable link (output_bfd test tells us), just
2323      call the generic function.  Any adjustment will be done at final
2324      link time.  */
2325   if (output_bfd != NULL)
2326     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2327                                   input_section, output_bfd, error_message);
2328
2329   /* Adjust the addend for sign extension of the low 16 bits.
2330      We won't actually be using the low 16 bits, so trashing them
2331      doesn't matter.  */
2332   reloc_entry->addend += 0x8000;
2333   return bfd_reloc_continue;
2334 }
2335
2336 static bfd_reloc_status_type
2337 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2338                         void *data, asection *input_section,
2339                         bfd *output_bfd, char **error_message)
2340 {
2341   if (output_bfd != NULL)
2342     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2343                                   input_section, output_bfd, error_message);
2344
2345   if (strcmp (symbol->section->name, ".opd") == 0
2346       && (symbol->section->owner->flags & DYNAMIC) == 0)
2347     {
2348       bfd_vma dest = opd_entry_value (symbol->section,
2349                                       symbol->value + reloc_entry->addend,
2350                                       NULL, NULL, FALSE);
2351       if (dest != (bfd_vma) -1)
2352         reloc_entry->addend = dest - (symbol->value
2353                                       + symbol->section->output_section->vma
2354                                       + symbol->section->output_offset);
2355     }
2356   return bfd_reloc_continue;
2357 }
2358
2359 static bfd_reloc_status_type
2360 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2361                          void *data, asection *input_section,
2362                          bfd *output_bfd, char **error_message)
2363 {
2364   long insn;
2365   enum elf_ppc64_reloc_type r_type;
2366   bfd_size_type octets;
2367   /* Assume 'at' branch hints.  */
2368   bfd_boolean is_isa_v2 = TRUE;
2369
2370   /* If this is a relocatable link (output_bfd test tells us), just
2371      call the generic function.  Any adjustment will be done at final
2372      link time.  */
2373   if (output_bfd != NULL)
2374     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2375                                   input_section, output_bfd, error_message);
2376
2377   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2378   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2379   insn &= ~(0x01 << 21);
2380   r_type = reloc_entry->howto->type;
2381   if (r_type == R_PPC64_ADDR14_BRTAKEN
2382       || r_type == R_PPC64_REL14_BRTAKEN)
2383     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2384
2385   if (is_isa_v2)
2386     {
2387       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2388          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2389          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2390       if ((insn & (0x14 << 21)) == (0x04 << 21))
2391         insn |= 0x02 << 21;
2392       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2393         insn |= 0x08 << 21;
2394       else
2395         goto out;
2396     }
2397   else
2398     {
2399       bfd_vma target = 0;
2400       bfd_vma from;
2401
2402       if (!bfd_is_com_section (symbol->section))
2403         target = symbol->value;
2404       target += symbol->section->output_section->vma;
2405       target += symbol->section->output_offset;
2406       target += reloc_entry->addend;
2407
2408       from = (reloc_entry->address
2409               + input_section->output_offset
2410               + input_section->output_section->vma);
2411
2412       /* Invert 'y' bit if not the default.  */
2413       if ((bfd_signed_vma) (target - from) < 0)
2414         insn ^= 0x01 << 21;
2415     }
2416   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2417  out:
2418   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2419                                  input_section, output_bfd, error_message);
2420 }
2421
2422 static bfd_reloc_status_type
2423 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2424                          void *data, asection *input_section,
2425                          bfd *output_bfd, char **error_message)
2426 {
2427   /* If this is a relocatable link (output_bfd test tells us), just
2428      call the generic function.  Any adjustment will be done at final
2429      link time.  */
2430   if (output_bfd != NULL)
2431     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2432                                   input_section, output_bfd, error_message);
2433
2434   /* Subtract the symbol section base address.  */
2435   reloc_entry->addend -= symbol->section->output_section->vma;
2436   return bfd_reloc_continue;
2437 }
2438
2439 static bfd_reloc_status_type
2440 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2441                             void *data, asection *input_section,
2442                             bfd *output_bfd, char **error_message)
2443 {
2444   /* If this is a relocatable link (output_bfd test tells us), just
2445      call the generic function.  Any adjustment will be done at final
2446      link time.  */
2447   if (output_bfd != NULL)
2448     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2449                                   input_section, output_bfd, error_message);
2450
2451   /* Subtract the symbol section base address.  */
2452   reloc_entry->addend -= symbol->section->output_section->vma;
2453
2454   /* Adjust the addend for sign extension of the low 16 bits.  */
2455   reloc_entry->addend += 0x8000;
2456   return bfd_reloc_continue;
2457 }
2458
2459 static bfd_reloc_status_type
2460 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2461                      void *data, asection *input_section,
2462                      bfd *output_bfd, char **error_message)
2463 {
2464   bfd_vma TOCstart;
2465
2466   /* If this is a relocatable link (output_bfd test tells us), just
2467      call the generic function.  Any adjustment will be done at final
2468      link time.  */
2469   if (output_bfd != NULL)
2470     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2471                                   input_section, output_bfd, error_message);
2472
2473   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2474   if (TOCstart == 0)
2475     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2476
2477   /* Subtract the TOC base address.  */
2478   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2479   return bfd_reloc_continue;
2480 }
2481
2482 static bfd_reloc_status_type
2483 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2484                         void *data, asection *input_section,
2485                         bfd *output_bfd, char **error_message)
2486 {
2487   bfd_vma TOCstart;
2488
2489   /* If this is a relocatable link (output_bfd test tells us), just
2490      call the generic function.  Any adjustment will be done at final
2491      link time.  */
2492   if (output_bfd != NULL)
2493     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2494                                   input_section, output_bfd, error_message);
2495
2496   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2497   if (TOCstart == 0)
2498     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2499
2500   /* Subtract the TOC base address.  */
2501   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2502
2503   /* Adjust the addend for sign extension of the low 16 bits.  */
2504   reloc_entry->addend += 0x8000;
2505   return bfd_reloc_continue;
2506 }
2507
2508 static bfd_reloc_status_type
2509 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2510                        void *data, asection *input_section,
2511                        bfd *output_bfd, char **error_message)
2512 {
2513   bfd_vma TOCstart;
2514   bfd_size_type octets;
2515
2516   /* If this is a relocatable link (output_bfd test tells us), just
2517      call the generic function.  Any adjustment will be done at final
2518      link time.  */
2519   if (output_bfd != NULL)
2520     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2521                                   input_section, output_bfd, error_message);
2522
2523   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2524   if (TOCstart == 0)
2525     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2526
2527   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2528   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2529   return bfd_reloc_ok;
2530 }
2531
2532 static bfd_reloc_status_type
2533 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2534                            void *data, asection *input_section,
2535                            bfd *output_bfd, char **error_message)
2536 {
2537   /* If this is a relocatable link (output_bfd test tells us), just
2538      call the generic function.  Any adjustment will be done at final
2539      link time.  */
2540   if (output_bfd != NULL)
2541     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2542                                   input_section, output_bfd, error_message);
2543
2544   if (error_message != NULL)
2545     {
2546       static char buf[60];
2547       sprintf (buf, "generic linker can't handle %s",
2548                reloc_entry->howto->name);
2549       *error_message = buf;
2550     }
2551   return bfd_reloc_dangerous;
2552 }
2553
2554 /* Track GOT entries needed for a given symbol.  We might need more
2555    than one got entry per symbol.  */
2556 struct got_entry
2557 {
2558   struct got_entry *next;
2559
2560   /* The symbol addend that we'll be placing in the GOT.  */
2561   bfd_vma addend;
2562
2563   /* Unlike other ELF targets, we use separate GOT entries for the same
2564      symbol referenced from different input files.  This is to support
2565      automatic multiple TOC/GOT sections, where the TOC base can vary
2566      from one input file to another.  After partitioning into TOC groups
2567      we merge entries within the group.
2568
2569      Point to the BFD owning this GOT entry.  */
2570   bfd *owner;
2571
2572   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2573      TLS_TPREL or TLS_DTPREL for tls entries.  */
2574   unsigned char tls_type;
2575
2576   /* Non-zero if got.ent points to real entry.  */
2577   unsigned char is_indirect;
2578
2579   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2580   union
2581     {
2582       bfd_signed_vma refcount;
2583       bfd_vma offset;
2584       struct got_entry *ent;
2585     } got;
2586 };
2587
2588 /* The same for PLT.  */
2589 struct plt_entry
2590 {
2591   struct plt_entry *next;
2592
2593   bfd_vma addend;
2594
2595   union
2596     {
2597       bfd_signed_vma refcount;
2598       bfd_vma offset;
2599     } plt;
2600 };
2601
2602 struct ppc64_elf_obj_tdata
2603 {
2604   struct elf_obj_tdata elf;
2605
2606   /* Shortcuts to dynamic linker sections.  */
2607   asection *got;
2608   asection *relgot;
2609
2610   /* Used during garbage collection.  We attach global symbols defined
2611      on removed .opd entries to this section so that the sym is removed.  */
2612   asection *deleted_section;
2613
2614   /* TLS local dynamic got entry handling.  Support for multiple GOT
2615      sections means we potentially need one of these for each input bfd.  */
2616   struct got_entry tlsld_got;
2617
2618   /* A copy of relocs before they are modified for --emit-relocs.  */
2619   Elf_Internal_Rela *opd_relocs;
2620
2621   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2622      the reloc to be in the range -32768 to 32767.  */
2623   unsigned int has_small_toc_reloc : 1;
2624
2625   /* Set if toc/got ha relocs detected not using r2, or lo reloc
2626      instruction not one we handle.  */
2627   unsigned int unexpected_toc_insn : 1;
2628 };
2629
2630 #define ppc64_elf_tdata(bfd) \
2631   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2632
2633 #define ppc64_tlsld_got(bfd) \
2634   (&ppc64_elf_tdata (bfd)->tlsld_got)
2635
2636 #define is_ppc64_elf(bfd) \
2637   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2638    && elf_object_id (bfd) == PPC64_ELF_DATA)
2639
2640 /* Override the generic function because we store some extras.  */
2641
2642 static bfd_boolean
2643 ppc64_elf_mkobject (bfd *abfd)
2644 {
2645   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2646                                   PPC64_ELF_DATA);
2647 }
2648
2649 /* Fix bad default arch selected for a 64 bit input bfd when the
2650    default is 32 bit.  */
2651
2652 static bfd_boolean
2653 ppc64_elf_object_p (bfd *abfd)
2654 {
2655   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2656     {
2657       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2658
2659       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2660         {
2661           /* Relies on arch after 32 bit default being 64 bit default.  */
2662           abfd->arch_info = abfd->arch_info->next;
2663           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2664         }
2665     }
2666   return TRUE;
2667 }
2668
2669 /* Support for core dump NOTE sections.  */
2670
2671 static bfd_boolean
2672 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2673 {
2674   size_t offset, size;
2675
2676   if (note->descsz != 504)
2677     return FALSE;
2678
2679   /* pr_cursig */
2680   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2681
2682   /* pr_pid */
2683   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2684
2685   /* pr_reg */
2686   offset = 112;
2687   size = 384;
2688
2689   /* Make a ".reg/999" section.  */
2690   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2691                                           size, note->descpos + offset);
2692 }
2693
2694 static bfd_boolean
2695 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2696 {
2697   if (note->descsz != 136)
2698     return FALSE;
2699
2700   elf_tdata (abfd)->core->pid
2701     = bfd_get_32 (abfd, note->descdata + 24);
2702   elf_tdata (abfd)->core->program
2703     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2704   elf_tdata (abfd)->core->command
2705     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2706
2707   return TRUE;
2708 }
2709
2710 static char *
2711 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2712                            ...)
2713 {
2714   switch (note_type)
2715     {
2716     default:
2717       return NULL;
2718
2719     case NT_PRPSINFO:
2720       {
2721         char data[136];
2722         va_list ap;
2723
2724         va_start (ap, note_type);
2725         memset (data, 0, sizeof (data));
2726         strncpy (data + 40, va_arg (ap, const char *), 16);
2727         strncpy (data + 56, va_arg (ap, const char *), 80);
2728         va_end (ap);
2729         return elfcore_write_note (abfd, buf, bufsiz,
2730                                    "CORE", note_type, data, sizeof (data));
2731       }
2732
2733     case NT_PRSTATUS:
2734       {
2735         char data[504];
2736         va_list ap;
2737         long pid;
2738         int cursig;
2739         const void *greg;
2740
2741         va_start (ap, note_type);
2742         memset (data, 0, 112);
2743         pid = va_arg (ap, long);
2744         bfd_put_32 (abfd, pid, data + 32);
2745         cursig = va_arg (ap, int);
2746         bfd_put_16 (abfd, cursig, data + 12);
2747         greg = va_arg (ap, const void *);
2748         memcpy (data + 112, greg, 384);
2749         memset (data + 496, 0, 8);
2750         va_end (ap);
2751         return elfcore_write_note (abfd, buf, bufsiz,
2752                                    "CORE", note_type, data, sizeof (data));
2753       }
2754     }
2755 }
2756
2757 /* Add extra PPC sections.  */
2758
2759 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2760 {
2761   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
2762   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2763   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2764   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2765   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2766   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2767   { NULL,                     0,  0, 0,            0 }
2768 };
2769
2770 enum _ppc64_sec_type {
2771   sec_normal = 0,
2772   sec_opd = 1,
2773   sec_toc = 2
2774 };
2775
2776 struct _ppc64_elf_section_data
2777 {
2778   struct bfd_elf_section_data elf;
2779
2780   union
2781   {
2782     /* An array with one entry for each opd function descriptor.  */
2783     struct _opd_sec_data
2784     {
2785       /* Points to the function code section for local opd entries.  */
2786       asection **func_sec;
2787
2788       /* After editing .opd, adjust references to opd local syms.  */
2789       long *adjust;
2790     } opd;
2791
2792     /* An array for toc sections, indexed by offset/8.  */
2793     struct _toc_sec_data
2794     {
2795       /* Specifies the relocation symbol index used at a given toc offset.  */
2796       unsigned *symndx;
2797
2798       /* And the relocation addend.  */
2799       bfd_vma *add;
2800     } toc;
2801   } u;
2802
2803   enum _ppc64_sec_type sec_type:2;
2804
2805   /* Flag set when small branches are detected.  Used to
2806      select suitable defaults for the stub group size.  */
2807   unsigned int has_14bit_branch:1;
2808 };
2809
2810 #define ppc64_elf_section_data(sec) \
2811   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2812
2813 static bfd_boolean
2814 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2815 {
2816   if (!sec->used_by_bfd)
2817     {
2818       struct _ppc64_elf_section_data *sdata;
2819       bfd_size_type amt = sizeof (*sdata);
2820
2821       sdata = bfd_zalloc (abfd, amt);
2822       if (sdata == NULL)
2823         return FALSE;
2824       sec->used_by_bfd = sdata;
2825     }
2826
2827   return _bfd_elf_new_section_hook (abfd, sec);
2828 }
2829
2830 static struct _opd_sec_data *
2831 get_opd_info (asection * sec)
2832 {
2833   if (sec != NULL
2834       && ppc64_elf_section_data (sec) != NULL
2835       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2836     return &ppc64_elf_section_data (sec)->u.opd;
2837   return NULL;
2838 }
2839 \f
2840 /* Parameters for the qsort hook.  */
2841 static bfd_boolean synthetic_relocatable;
2842
2843 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
2844
2845 static int
2846 compare_symbols (const void *ap, const void *bp)
2847 {
2848   const asymbol *a = * (const asymbol **) ap;
2849   const asymbol *b = * (const asymbol **) bp;
2850
2851   /* Section symbols first.  */
2852   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2853     return -1;
2854   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2855     return 1;
2856
2857   /* then .opd symbols.  */
2858   if (strcmp (a->section->name, ".opd") == 0
2859       && strcmp (b->section->name, ".opd") != 0)
2860     return -1;
2861   if (strcmp (a->section->name, ".opd") != 0
2862       && strcmp (b->section->name, ".opd") == 0)
2863     return 1;
2864
2865   /* then other code symbols.  */
2866   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2867       == (SEC_CODE | SEC_ALLOC)
2868       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2869          != (SEC_CODE | SEC_ALLOC))
2870     return -1;
2871
2872   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2873       != (SEC_CODE | SEC_ALLOC)
2874       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2875          == (SEC_CODE | SEC_ALLOC))
2876     return 1;
2877
2878   if (synthetic_relocatable)
2879     {
2880       if (a->section->id < b->section->id)
2881         return -1;
2882
2883       if (a->section->id > b->section->id)
2884         return 1;
2885     }
2886
2887   if (a->value + a->section->vma < b->value + b->section->vma)
2888     return -1;
2889
2890   if (a->value + a->section->vma > b->value + b->section->vma)
2891     return 1;
2892
2893   /* For syms with the same value, prefer strong dynamic global function
2894      syms over other syms.  */
2895   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2896     return -1;
2897
2898   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2899     return 1;
2900
2901   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2902     return -1;
2903
2904   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2905     return 1;
2906
2907   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2908     return -1;
2909
2910   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2911     return 1;
2912
2913   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2914     return -1;
2915
2916   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2917     return 1;
2918
2919   return 0;
2920 }
2921
2922 /* Search SYMS for a symbol of the given VALUE.  */
2923
2924 static asymbol *
2925 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
2926 {
2927   long mid;
2928
2929   if (id == -1)
2930     {
2931       while (lo < hi)
2932         {
2933           mid = (lo + hi) >> 1;
2934           if (syms[mid]->value + syms[mid]->section->vma < value)
2935             lo = mid + 1;
2936           else if (syms[mid]->value + syms[mid]->section->vma > value)
2937             hi = mid;
2938           else
2939             return syms[mid];
2940         }
2941     }
2942   else
2943     {
2944       while (lo < hi)
2945         {
2946           mid = (lo + hi) >> 1;
2947           if (syms[mid]->section->id < id)
2948             lo = mid + 1;
2949           else if (syms[mid]->section->id > id)
2950             hi = mid;
2951           else if (syms[mid]->value < value)
2952             lo = mid + 1;
2953           else if (syms[mid]->value > value)
2954             hi = mid;
2955           else
2956             return syms[mid];
2957         }
2958     }
2959   return NULL;
2960 }
2961
2962 static bfd_boolean
2963 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2964 {
2965   bfd_vma vma = *(bfd_vma *) ptr;
2966   return ((section->flags & SEC_ALLOC) != 0
2967           && section->vma <= vma
2968           && vma < section->vma + section->size);
2969 }
2970
2971 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2972    entry syms.  Also generate @plt symbols for the glink branch table.  */
2973
2974 static long
2975 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2976                                 long static_count, asymbol **static_syms,
2977                                 long dyn_count, asymbol **dyn_syms,
2978                                 asymbol **ret)
2979 {
2980   asymbol *s;
2981   long i;
2982   long count;
2983   char *names;
2984   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2985   asection *opd;
2986   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2987   asymbol **syms;
2988
2989   *ret = NULL;
2990
2991   opd = bfd_get_section_by_name (abfd, ".opd");
2992   if (opd == NULL)
2993     return 0;
2994
2995   symcount = static_count;
2996   if (!relocatable)
2997     symcount += dyn_count;
2998   if (symcount == 0)
2999     return 0;
3000
3001   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3002   if (syms == NULL)
3003     return -1;
3004
3005   if (!relocatable && static_count != 0 && dyn_count != 0)
3006     {
3007       /* Use both symbol tables.  */
3008       memcpy (syms, static_syms, static_count * sizeof (*syms));
3009       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3010     }
3011   else if (!relocatable && static_count == 0)
3012     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3013   else
3014     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3015
3016   synthetic_relocatable = relocatable;
3017   qsort (syms, symcount, sizeof (*syms), compare_symbols);
3018
3019   if (!relocatable && symcount > 1)
3020     {
3021       long j;
3022       /* Trim duplicate syms, since we may have merged the normal and
3023          dynamic symbols.  Actually, we only care about syms that have
3024          different values, so trim any with the same value.  */
3025       for (i = 1, j = 1; i < symcount; ++i)
3026         if (syms[i - 1]->value + syms[i - 1]->section->vma
3027             != syms[i]->value + syms[i]->section->vma)
3028           syms[j++] = syms[i];
3029       symcount = j;
3030     }
3031
3032   i = 0;
3033   if (strcmp (syms[i]->section->name, ".opd") == 0)
3034     ++i;
3035   codesecsym = i;
3036
3037   for (; i < symcount; ++i)
3038     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3039          != (SEC_CODE | SEC_ALLOC))
3040         || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3041       break;
3042   codesecsymend = i;
3043
3044   for (; i < symcount; ++i)
3045     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3046       break;
3047   secsymend = i;
3048
3049   for (; i < symcount; ++i)
3050     if (strcmp (syms[i]->section->name, ".opd") != 0)
3051       break;
3052   opdsymend = i;
3053
3054   for (; i < symcount; ++i)
3055     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3056         != (SEC_CODE | SEC_ALLOC))
3057       break;
3058   symcount = i;
3059
3060   count = 0;
3061
3062   if (relocatable)
3063     {
3064       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3065       arelent *r;
3066       size_t size;
3067       long relcount;
3068
3069       if (opdsymend == secsymend)
3070         goto done;
3071
3072       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3073       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3074       if (relcount == 0)
3075         goto done;
3076
3077       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3078         {
3079           count = -1;
3080           goto done;
3081         }
3082
3083       size = 0;
3084       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3085         {
3086           asymbol *sym;
3087
3088           while (r < opd->relocation + relcount
3089                  && r->address < syms[i]->value + opd->vma)
3090             ++r;
3091
3092           if (r == opd->relocation + relcount)
3093             break;
3094
3095           if (r->address != syms[i]->value + opd->vma)
3096             continue;
3097
3098           if (r->howto->type != R_PPC64_ADDR64)
3099             continue;
3100
3101           sym = *r->sym_ptr_ptr;
3102           if (!sym_exists_at (syms, opdsymend, symcount,
3103                               sym->section->id, sym->value + r->addend))
3104             {
3105               ++count;
3106               size += sizeof (asymbol);
3107               size += strlen (syms[i]->name) + 2;
3108             }
3109         }
3110
3111       s = *ret = bfd_malloc (size);
3112       if (s == NULL)
3113         {
3114           count = -1;
3115           goto done;
3116         }
3117
3118       names = (char *) (s + count);
3119
3120       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3121         {
3122           asymbol *sym;
3123
3124           while (r < opd->relocation + relcount
3125                  && r->address < syms[i]->value + opd->vma)
3126             ++r;
3127
3128           if (r == opd->relocation + relcount)
3129             break;
3130
3131           if (r->address != syms[i]->value + opd->vma)
3132             continue;
3133
3134           if (r->howto->type != R_PPC64_ADDR64)
3135             continue;
3136
3137           sym = *r->sym_ptr_ptr;
3138           if (!sym_exists_at (syms, opdsymend, symcount,
3139                               sym->section->id, sym->value + r->addend))
3140             {
3141               size_t len;
3142
3143               *s = *syms[i];
3144               s->flags |= BSF_SYNTHETIC;
3145               s->section = sym->section;
3146               s->value = sym->value + r->addend;
3147               s->name = names;
3148               *names++ = '.';
3149               len = strlen (syms[i]->name);
3150               memcpy (names, syms[i]->name, len + 1);
3151               names += len + 1;
3152               /* Have udata.p point back to the original symbol this
3153                  synthetic symbol was derived from.  */
3154               s->udata.p = syms[i];
3155               s++;
3156             }
3157         }
3158     }
3159   else
3160     {
3161       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3162       bfd_byte *contents;
3163       size_t size;
3164       long plt_count = 0;
3165       bfd_vma glink_vma = 0, resolv_vma = 0;
3166       asection *dynamic, *glink = NULL, *relplt = NULL;
3167       arelent *p;
3168
3169       if (!bfd_malloc_and_get_section (abfd, opd, &contents))
3170         {
3171           if (contents)
3172             {
3173             free_contents_and_exit:
3174               free (contents);
3175             }
3176           count = -1;
3177           goto done;
3178         }
3179
3180       size = 0;
3181       for (i = secsymend; i < opdsymend; ++i)
3182         {
3183           bfd_vma ent;
3184
3185           /* Ignore bogus symbols.  */
3186           if (syms[i]->value > opd->size - 8)
3187             continue;
3188
3189           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3190           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3191             {
3192               ++count;
3193               size += sizeof (asymbol);
3194               size += strlen (syms[i]->name) + 2;
3195             }
3196         }
3197
3198       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
3199       if (dyn_count != 0
3200           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3201         {
3202           bfd_byte *dynbuf, *extdyn, *extdynend;
3203           size_t extdynsize;
3204           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3205
3206           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3207             goto free_contents_and_exit;
3208
3209           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3210           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3211
3212           extdyn = dynbuf;
3213           extdynend = extdyn + dynamic->size;
3214           for (; extdyn < extdynend; extdyn += extdynsize)
3215             {
3216               Elf_Internal_Dyn dyn;
3217               (*swap_dyn_in) (abfd, extdyn, &dyn);
3218
3219               if (dyn.d_tag == DT_NULL)
3220                 break;
3221
3222               if (dyn.d_tag == DT_PPC64_GLINK)
3223                 {
3224                   /* The first glink stub starts at offset 32; see comment in
3225                      ppc64_elf_finish_dynamic_sections. */
3226                   glink_vma = dyn.d_un.d_val + 32;
3227                   /* The .glink section usually does not survive the final
3228                      link; search for the section (usually .text) where the
3229                      glink stubs now reside.  */
3230                   glink = bfd_sections_find_if (abfd, section_covers_vma,
3231                                                 &glink_vma);
3232                   break;
3233                 }
3234             }
3235
3236           free (dynbuf);
3237         }
3238
3239       if (glink != NULL)
3240         {
3241           /* Determine __glink trampoline by reading the relative branch
3242              from the first glink stub.  */
3243           bfd_byte buf[4];
3244           if (bfd_get_section_contents (abfd, glink, buf,
3245                                         glink_vma + 4 - glink->vma, 4))
3246             {
3247               unsigned int insn = bfd_get_32 (abfd, buf);
3248               insn ^= B_DOT;
3249               if ((insn & ~0x3fffffc) == 0)
3250                 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3251             }
3252
3253           if (resolv_vma)
3254             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3255
3256           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3257           if (relplt != NULL)
3258             {
3259               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3260               if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3261                 goto free_contents_and_exit;
3262
3263               plt_count = relplt->size / sizeof (Elf64_External_Rela);
3264               size += plt_count * sizeof (asymbol);
3265
3266               p = relplt->relocation;
3267               for (i = 0; i < plt_count; i++, p++)
3268                 {
3269                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3270                   if (p->addend != 0)
3271                     size += sizeof ("+0x") - 1 + 16;
3272                 }
3273             }
3274         }
3275
3276       s = *ret = bfd_malloc (size);
3277       if (s == NULL)
3278         goto free_contents_and_exit;
3279
3280       names = (char *) (s + count + plt_count + (resolv_vma != 0));
3281
3282       for (i = secsymend; i < opdsymend; ++i)
3283         {
3284           bfd_vma ent;
3285
3286           if (syms[i]->value > opd->size - 8)
3287             continue;
3288
3289           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3290           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3291             {
3292               long lo, hi;
3293               size_t len;
3294               asection *sec = abfd->sections;
3295
3296               *s = *syms[i];
3297               lo = codesecsym;
3298               hi = codesecsymend;
3299               while (lo < hi)
3300                 {
3301                   long mid = (lo + hi) >> 1;
3302                   if (syms[mid]->section->vma < ent)
3303                     lo = mid + 1;
3304                   else if (syms[mid]->section->vma > ent)
3305                     hi = mid;
3306                   else
3307                     {
3308                       sec = syms[mid]->section;
3309                       break;
3310                     }
3311                 }
3312
3313               if (lo >= hi && lo > codesecsym)
3314                 sec = syms[lo - 1]->section;
3315
3316               for (; sec != NULL; sec = sec->next)
3317                 {
3318                   if (sec->vma > ent)
3319                     break;
3320                   /* SEC_LOAD may not be set if SEC is from a separate debug
3321                      info file.  */
3322                   if ((sec->flags & SEC_ALLOC) == 0)
3323                     break;
3324                   if ((sec->flags & SEC_CODE) != 0)
3325                     s->section = sec;
3326                 }
3327               s->flags |= BSF_SYNTHETIC;
3328               s->value = ent - s->section->vma;
3329               s->name = names;
3330               *names++ = '.';
3331               len = strlen (syms[i]->name);
3332               memcpy (names, syms[i]->name, len + 1);
3333               names += len + 1;
3334               /* Have udata.p point back to the original symbol this
3335                  synthetic symbol was derived from.  */
3336               s->udata.p = syms[i];
3337               s++;
3338             }
3339         }
3340       free (contents);
3341
3342       if (glink != NULL && relplt != NULL)
3343         {
3344           if (resolv_vma)
3345             {
3346               /* Add a symbol for the main glink trampoline.  */
3347               memset (s, 0, sizeof *s);
3348               s->the_bfd = abfd;
3349               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3350               s->section = glink;
3351               s->value = resolv_vma - glink->vma;
3352               s->name = names;
3353               memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3354               names += sizeof ("__glink_PLTresolve");
3355               s++;
3356               count++;
3357             }
3358
3359           /* FIXME: It would be very much nicer to put sym@plt on the
3360              stub rather than on the glink branch table entry.  The
3361              objdump disassembler would then use a sensible symbol
3362              name on plt calls.  The difficulty in doing so is
3363              a) finding the stubs, and,
3364              b) matching stubs against plt entries, and,
3365              c) there can be multiple stubs for a given plt entry.
3366
3367              Solving (a) could be done by code scanning, but older
3368              ppc64 binaries used different stubs to current code.
3369              (b) is the tricky one since you need to known the toc
3370              pointer for at least one function that uses a pic stub to
3371              be able to calculate the plt address referenced.
3372              (c) means gdb would need to set multiple breakpoints (or
3373              find the glink branch itself) when setting breakpoints
3374              for pending shared library loads.  */
3375           p = relplt->relocation;
3376           for (i = 0; i < plt_count; i++, p++)
3377             {
3378               size_t len;
3379
3380               *s = **p->sym_ptr_ptr;
3381               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
3382                  we are defining a symbol, ensure one of them is set.  */
3383               if ((s->flags & BSF_LOCAL) == 0)
3384                 s->flags |= BSF_GLOBAL;
3385               s->flags |= BSF_SYNTHETIC;
3386               s->section = glink;
3387               s->value = glink_vma - glink->vma;
3388               s->name = names;
3389               s->udata.p = NULL;
3390               len = strlen ((*p->sym_ptr_ptr)->name);
3391               memcpy (names, (*p->sym_ptr_ptr)->name, len);
3392               names += len;
3393               if (p->addend != 0)
3394                 {
3395                   memcpy (names, "+0x", sizeof ("+0x") - 1);
3396                   names += sizeof ("+0x") - 1;
3397                   bfd_sprintf_vma (abfd, names, p->addend);
3398                   names += strlen (names);
3399                 }
3400               memcpy (names, "@plt", sizeof ("@plt"));
3401               names += sizeof ("@plt");
3402               s++;
3403               glink_vma += 8;
3404               if (i >= 0x8000)
3405                 glink_vma += 4;
3406             }
3407           count += plt_count;
3408         }
3409     }
3410
3411  done:
3412   free (syms);
3413   return count;
3414 }
3415 \f
3416 /* The following functions are specific to the ELF linker, while
3417    functions above are used generally.  Those named ppc64_elf_* are
3418    called by the main ELF linker code.  They appear in this file more
3419    or less in the order in which they are called.  eg.
3420    ppc64_elf_check_relocs is called early in the link process,
3421    ppc64_elf_finish_dynamic_sections is one of the last functions
3422    called.
3423
3424    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3425    functions have both a function code symbol and a function descriptor
3426    symbol.  A call to foo in a relocatable object file looks like:
3427
3428    .            .text
3429    .    x:
3430    .            bl      .foo
3431    .            nop
3432
3433    The function definition in another object file might be:
3434
3435    .            .section .opd
3436    .    foo:    .quad   .foo
3437    .            .quad   .TOC.@tocbase
3438    .            .quad   0
3439    .
3440    .            .text
3441    .    .foo:   blr
3442
3443    When the linker resolves the call during a static link, the branch
3444    unsurprisingly just goes to .foo and the .opd information is unused.
3445    If the function definition is in a shared library, things are a little
3446    different:  The call goes via a plt call stub, the opd information gets
3447    copied to the plt, and the linker patches the nop.
3448
3449    .    x:
3450    .            bl      .foo_stub
3451    .            ld      2,40(1)
3452    .
3453    .
3454    .    .foo_stub:
3455    .            addis   12,2,Lfoo@toc@ha        # in practice, the call stub
3456    .            addi    12,12,Lfoo@toc@l        # is slightly optimized, but
3457    .            std     2,40(1)                 # this is the general idea
3458    .            ld      11,0(12)
3459    .            ld      2,8(12)
3460    .            mtctr   11
3461    .            ld      11,16(12)
3462    .            bctr
3463    .
3464    .            .section .plt
3465    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
3466
3467    The "reloc ()" notation is supposed to indicate that the linker emits
3468    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3469    copying.
3470
3471    What are the difficulties here?  Well, firstly, the relocations
3472    examined by the linker in check_relocs are against the function code
3473    sym .foo, while the dynamic relocation in the plt is emitted against
3474    the function descriptor symbol, foo.  Somewhere along the line, we need
3475    to carefully copy dynamic link information from one symbol to the other.
3476    Secondly, the generic part of the elf linker will make .foo a dynamic
3477    symbol as is normal for most other backends.  We need foo dynamic
3478    instead, at least for an application final link.  However, when
3479    creating a shared library containing foo, we need to have both symbols
3480    dynamic so that references to .foo are satisfied during the early
3481    stages of linking.  Otherwise the linker might decide to pull in a
3482    definition from some other object, eg. a static library.
3483
3484    Update: As of August 2004, we support a new convention.  Function
3485    calls may use the function descriptor symbol, ie. "bl foo".  This
3486    behaves exactly as "bl .foo".  */
3487
3488 /* Of those relocs that might be copied as dynamic relocs, this function
3489    selects those that must be copied when linking a shared library,
3490    even when the symbol is local.  */
3491
3492 static int
3493 must_be_dyn_reloc (struct bfd_link_info *info,
3494                    enum elf_ppc64_reloc_type r_type)
3495 {
3496   switch (r_type)
3497     {
3498     default:
3499       return 1;
3500
3501     case R_PPC64_REL32:
3502     case R_PPC64_REL64:
3503     case R_PPC64_REL30:
3504       return 0;
3505
3506     case R_PPC64_TPREL16:
3507     case R_PPC64_TPREL16_LO:
3508     case R_PPC64_TPREL16_HI:
3509     case R_PPC64_TPREL16_HA:
3510     case R_PPC64_TPREL16_DS:
3511     case R_PPC64_TPREL16_LO_DS:
3512     case R_PPC64_TPREL16_HIGHER:
3513     case R_PPC64_TPREL16_HIGHERA:
3514     case R_PPC64_TPREL16_HIGHEST:
3515     case R_PPC64_TPREL16_HIGHESTA:
3516     case R_PPC64_TPREL64:
3517       return !info->executable;
3518     }
3519 }
3520
3521 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3522    copying dynamic variables from a shared lib into an app's dynbss
3523    section, and instead use a dynamic relocation to point into the
3524    shared lib.  With code that gcc generates, it's vital that this be
3525    enabled;  In the PowerPC64 ABI, the address of a function is actually
3526    the address of a function descriptor, which resides in the .opd
3527    section.  gcc uses the descriptor directly rather than going via the
3528    GOT as some other ABI's do, which means that initialized function
3529    pointers must reference the descriptor.  Thus, a function pointer
3530    initialized to the address of a function in a shared library will
3531    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3532    redefines the function descriptor symbol to point to the copy.  This
3533    presents a problem as a plt entry for that function is also
3534    initialized from the function descriptor symbol and the copy reloc
3535    may not be initialized first.  */
3536 #define ELIMINATE_COPY_RELOCS 1
3537
3538 /* Section name for stubs is the associated section name plus this
3539    string.  */
3540 #define STUB_SUFFIX ".stub"
3541
3542 /* Linker stubs.
3543    ppc_stub_long_branch:
3544    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3545    destination, but a 24 bit branch in a stub section will reach.
3546    .    b       dest
3547
3548    ppc_stub_plt_branch:
3549    Similar to the above, but a 24 bit branch in the stub section won't
3550    reach its destination.
3551    .    addis   %r12,%r2,xxx@toc@ha
3552    .    ld      %r11,xxx@toc@l(%r12)
3553    .    mtctr   %r11
3554    .    bctr
3555
3556    ppc_stub_plt_call:
3557    Used to call a function in a shared library.  If it so happens that
3558    the plt entry referenced crosses a 64k boundary, then an extra
3559    "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
3560    .    addis   %r12,%r2,xxx@toc@ha
3561    .    std     %r2,40(%r1)
3562    .    ld      %r11,xxx+0@toc@l(%r12)
3563    .    mtctr   %r11
3564    .    ld      %r2,xxx+8@toc@l(%r12)
3565    .    ld      %r11,xxx+16@toc@l(%r12)
3566    .    bctr
3567
3568    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3569    code to adjust the value and save r2 to support multiple toc sections.
3570    A ppc_stub_long_branch with an r2 offset looks like:
3571    .    std     %r2,40(%r1)
3572    .    addis   %r2,%r2,off@ha
3573    .    addi    %r2,%r2,off@l
3574    .    b       dest
3575
3576    A ppc_stub_plt_branch with an r2 offset looks like:
3577    .    std     %r2,40(%r1)
3578    .    addis   %r12,%r2,xxx@toc@ha
3579    .    ld      %r11,xxx@toc@l(%r12)
3580    .    addis   %r2,%r2,off@ha
3581    .    addi    %r2,%r2,off@l
3582    .    mtctr   %r11
3583    .    bctr
3584
3585    In cases where the "addis" instruction would add zero, the "addis" is
3586    omitted and following instructions modified slightly in some cases.
3587 */
3588
3589 enum ppc_stub_type {
3590   ppc_stub_none,
3591   ppc_stub_long_branch,
3592   ppc_stub_long_branch_r2off,
3593   ppc_stub_plt_branch,
3594   ppc_stub_plt_branch_r2off,
3595   ppc_stub_plt_call,
3596   ppc_stub_plt_call_r2save
3597 };
3598
3599 struct ppc_stub_hash_entry {
3600
3601   /* Base hash table entry structure.  */
3602   struct bfd_hash_entry root;
3603
3604   enum ppc_stub_type stub_type;
3605
3606   /* The stub section.  */
3607   asection *stub_sec;
3608
3609   /* Offset within stub_sec of the beginning of this stub.  */
3610   bfd_vma stub_offset;
3611
3612   /* Given the symbol's value and its section we can determine its final
3613      value when building the stubs (so the stub knows where to jump.  */
3614   bfd_vma target_value;
3615   asection *target_section;
3616
3617   /* The symbol table entry, if any, that this was derived from.  */
3618   struct ppc_link_hash_entry *h;
3619   struct plt_entry *plt_ent;
3620
3621   /* Where this stub is being called from, or, in the case of combined
3622      stub sections, the first input section in the group.  */
3623   asection *id_sec;
3624 };
3625
3626 struct ppc_branch_hash_entry {
3627
3628   /* Base hash table entry structure.  */
3629   struct bfd_hash_entry root;
3630
3631   /* Offset within branch lookup table.  */
3632   unsigned int offset;
3633
3634   /* Generation marker.  */
3635   unsigned int iter;
3636 };
3637
3638 /* Used to track dynamic relocations for local symbols.  */
3639 struct ppc_dyn_relocs
3640 {
3641   struct ppc_dyn_relocs *next;
3642
3643   /* The input section of the reloc.  */
3644   asection *sec;
3645
3646   /* Total number of relocs copied for the input section.  */
3647   unsigned int count : 31;
3648
3649   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
3650   unsigned int ifunc : 1;
3651 };
3652
3653 struct ppc_link_hash_entry
3654 {
3655   struct elf_link_hash_entry elf;
3656
3657   union {
3658     /* A pointer to the most recently used stub hash entry against this
3659        symbol.  */
3660     struct ppc_stub_hash_entry *stub_cache;
3661
3662     /* A pointer to the next symbol starting with a '.'  */
3663     struct ppc_link_hash_entry *next_dot_sym;
3664   } u;
3665
3666   /* Track dynamic relocs copied for this symbol.  */
3667   struct elf_dyn_relocs *dyn_relocs;
3668
3669   /* Link between function code and descriptor symbols.  */
3670   struct ppc_link_hash_entry *oh;
3671
3672   /* Flag function code and descriptor symbols.  */
3673   unsigned int is_func:1;
3674   unsigned int is_func_descriptor:1;
3675   unsigned int fake:1;
3676
3677   /* Whether global opd/toc sym has been adjusted or not.
3678      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3679      should be set for all globals defined in any opd/toc section.  */
3680   unsigned int adjust_done:1;
3681
3682   /* Set if we twiddled this symbol to weak at some stage.  */
3683   unsigned int was_undefined:1;
3684
3685   /* Contexts in which symbol is used in the GOT (or TOC).
3686      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3687      corresponding relocs are encountered during check_relocs.
3688      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3689      indicate the corresponding GOT entry type is not needed.
3690      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3691      a TPREL one.  We use a separate flag rather than setting TPREL
3692      just for convenience in distinguishing the two cases.  */
3693 #define TLS_GD           1      /* GD reloc. */
3694 #define TLS_LD           2      /* LD reloc. */
3695 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
3696 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
3697 #define TLS_TLS         16      /* Any TLS reloc.  */
3698 #define TLS_EXPLICIT    32      /* Marks TOC section TLS relocs. */
3699 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
3700 #define PLT_IFUNC      128      /* STT_GNU_IFUNC.  */
3701   unsigned char tls_mask;
3702 };
3703
3704 /* ppc64 ELF linker hash table.  */
3705
3706 struct ppc_link_hash_table
3707 {
3708   struct elf_link_hash_table elf;
3709
3710   /* The stub hash table.  */
3711   struct bfd_hash_table stub_hash_table;
3712
3713   /* Another hash table for plt_branch stubs.  */
3714   struct bfd_hash_table branch_hash_table;
3715
3716   /* Hash table for function prologue tocsave.  */
3717   htab_t tocsave_htab;
3718
3719   /* Linker stub bfd.  */
3720   bfd *stub_bfd;
3721
3722   /* Linker call-backs.  */
3723   asection * (*add_stub_section) (const char *, asection *);
3724   void (*layout_sections_again) (void);
3725
3726   /* Array to keep track of which stub sections have been created, and
3727      information on stub grouping.  */
3728   struct map_stub {
3729     /* This is the section to which stubs in the group will be attached.  */
3730     asection *link_sec;
3731     /* The stub section.  */
3732     asection *stub_sec;
3733     /* Along with elf_gp, specifies the TOC pointer used in this group.  */
3734     bfd_vma toc_off;
3735   } *stub_group;
3736
3737   /* Temp used when calculating TOC pointers.  */
3738   bfd_vma toc_curr;
3739   bfd *toc_bfd;
3740   asection *toc_first_sec;
3741
3742   /* Highest input section id.  */
3743   int top_id;
3744
3745   /* Highest output section index.  */
3746   int top_index;
3747
3748   /* Used when adding symbols.  */
3749   struct ppc_link_hash_entry *dot_syms;
3750
3751   /* List of input sections for each output section.  */
3752   asection **input_list;
3753
3754   /* Short-cuts to get to dynamic linker sections.  */
3755   asection *got;
3756   asection *plt;
3757   asection *relplt;
3758   asection *iplt;
3759   asection *reliplt;
3760   asection *dynbss;
3761   asection *relbss;
3762   asection *glink;
3763   asection *sfpr;
3764   asection *brlt;
3765   asection *relbrlt;
3766   asection *glink_eh_frame;
3767
3768   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
3769   struct ppc_link_hash_entry *tls_get_addr;
3770   struct ppc_link_hash_entry *tls_get_addr_fd;
3771
3772   /* The size of reliplt used by got entry relocs.  */
3773   bfd_size_type got_reli_size;
3774
3775   /* Statistics.  */
3776   unsigned long stub_count[ppc_stub_plt_call_r2save];
3777
3778   /* Number of stubs against global syms.  */
3779   unsigned long stub_globals;
3780
3781   /* Alignment of PLT call stubs.  */
3782   unsigned int plt_stub_align:4;
3783
3784   /* Set if PLT call stubs should load r11.  */
3785   unsigned int plt_static_chain:1;
3786
3787   /* Set if PLT call stubs need a read-read barrier.  */
3788   unsigned int plt_thread_safe:1;
3789
3790   /* Set if we should emit symbols for stubs.  */
3791   unsigned int emit_stub_syms:1;
3792
3793   /* Set if __tls_get_addr optimization should not be done.  */
3794   unsigned int no_tls_get_addr_opt:1;
3795
3796   /* Support for multiple toc sections.  */
3797   unsigned int do_multi_toc:1;
3798   unsigned int multi_toc_needed:1;
3799   unsigned int second_toc_pass:1;
3800   unsigned int do_toc_opt:1;
3801
3802   /* Set on error.  */
3803   unsigned int stub_error:1;
3804
3805   /* Temp used by ppc64_elf_process_dot_syms.  */
3806   unsigned int twiddled_syms:1;
3807
3808   /* Incremented every time we size stubs.  */
3809   unsigned int stub_iteration;
3810
3811   /* Small local sym cache.  */
3812   struct sym_cache sym_cache;
3813 };
3814
3815 /* Rename some of the generic section flags to better document how they
3816    are used here.  */
3817
3818 /* Nonzero if this section has TLS related relocations.  */
3819 #define has_tls_reloc sec_flg0
3820
3821 /* Nonzero if this section has a call to __tls_get_addr.  */
3822 #define has_tls_get_addr_call sec_flg1
3823
3824 /* Nonzero if this section has any toc or got relocs.  */
3825 #define has_toc_reloc sec_flg2
3826
3827 /* Nonzero if this section has a call to another section that uses
3828    the toc or got.  */
3829 #define makes_toc_func_call sec_flg3
3830
3831 /* Recursion protection when determining above flag.  */
3832 #define call_check_in_progress sec_flg4
3833 #define call_check_done sec_flg5
3834
3835 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
3836
3837 #define ppc_hash_table(p) \
3838   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3839   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3840
3841 #define ppc_stub_hash_lookup(table, string, create, copy) \
3842   ((struct ppc_stub_hash_entry *) \
3843    bfd_hash_lookup ((table), (string), (create), (copy)))
3844
3845 #define ppc_branch_hash_lookup(table, string, create, copy) \
3846   ((struct ppc_branch_hash_entry *) \
3847    bfd_hash_lookup ((table), (string), (create), (copy)))
3848
3849 /* Create an entry in the stub hash table.  */
3850
3851 static struct bfd_hash_entry *
3852 stub_hash_newfunc (struct bfd_hash_entry *entry,
3853                    struct bfd_hash_table *table,
3854                    const char *string)
3855 {
3856   /* Allocate the structure if it has not already been allocated by a
3857      subclass.  */
3858   if (entry == NULL)
3859     {
3860       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3861       if (entry == NULL)
3862         return entry;
3863     }
3864
3865   /* Call the allocation method of the superclass.  */
3866   entry = bfd_hash_newfunc (entry, table, string);
3867   if (entry != NULL)
3868     {
3869       struct ppc_stub_hash_entry *eh;
3870
3871       /* Initialize the local fields.  */
3872       eh = (struct ppc_stub_hash_entry *) entry;
3873       eh->stub_type = ppc_stub_none;
3874       eh->stub_sec = NULL;
3875       eh->stub_offset = 0;
3876       eh->target_value = 0;
3877       eh->target_section = NULL;
3878       eh->h = NULL;
3879       eh->id_sec = NULL;
3880     }
3881
3882   return entry;
3883 }
3884
3885 /* Create an entry in the branch hash table.  */
3886
3887 static struct bfd_hash_entry *
3888 branch_hash_newfunc (struct bfd_hash_entry *entry,
3889                      struct bfd_hash_table *table,
3890                      const char *string)
3891 {
3892   /* Allocate the structure if it has not already been allocated by a
3893      subclass.  */
3894   if (entry == NULL)
3895     {
3896       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3897       if (entry == NULL)
3898         return entry;
3899     }
3900
3901   /* Call the allocation method of the superclass.  */
3902   entry = bfd_hash_newfunc (entry, table, string);
3903   if (entry != NULL)
3904     {
3905       struct ppc_branch_hash_entry *eh;
3906
3907       /* Initialize the local fields.  */
3908       eh = (struct ppc_branch_hash_entry *) entry;
3909       eh->offset = 0;
3910       eh->iter = 0;
3911     }
3912
3913   return entry;
3914 }
3915
3916 /* Create an entry in a ppc64 ELF linker hash table.  */
3917
3918 static struct bfd_hash_entry *
3919 link_hash_newfunc (struct bfd_hash_entry *entry,
3920                    struct bfd_hash_table *table,
3921                    const char *string)
3922 {
3923   /* Allocate the structure if it has not already been allocated by a
3924      subclass.  */
3925   if (entry == NULL)
3926     {
3927       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3928       if (entry == NULL)
3929         return entry;
3930     }
3931
3932   /* Call the allocation method of the superclass.  */
3933   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3934   if (entry != NULL)
3935     {
3936       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3937
3938       memset (&eh->u.stub_cache, 0,
3939               (sizeof (struct ppc_link_hash_entry)
3940                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3941
3942       /* When making function calls, old ABI code references function entry
3943          points (dot symbols), while new ABI code references the function
3944          descriptor symbol.  We need to make any combination of reference and
3945          definition work together, without breaking archive linking.
3946
3947          For a defined function "foo" and an undefined call to "bar":
3948          An old object defines "foo" and ".foo", references ".bar" (possibly
3949          "bar" too).
3950          A new object defines "foo" and references "bar".
3951
3952          A new object thus has no problem with its undefined symbols being
3953          satisfied by definitions in an old object.  On the other hand, the
3954          old object won't have ".bar" satisfied by a new object.
3955
3956          Keep a list of newly added dot-symbols.  */
3957
3958       if (string[0] == '.')
3959         {
3960           struct ppc_link_hash_table *htab;
3961
3962           htab = (struct ppc_link_hash_table *) table;
3963           eh->u.next_dot_sym = htab->dot_syms;
3964           htab->dot_syms = eh;
3965         }
3966     }
3967
3968   return entry;
3969 }
3970
3971 struct tocsave_entry {
3972   asection *sec;
3973   bfd_vma offset;
3974 };
3975
3976 static hashval_t
3977 tocsave_htab_hash (const void *p)
3978 {
3979   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3980   return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
3981 }
3982
3983 static int
3984 tocsave_htab_eq (const void *p1, const void *p2)
3985 {
3986   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3987   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3988   return e1->sec == e2->sec && e1->offset == e2->offset;
3989 }
3990
3991 /* Create a ppc64 ELF linker hash table.  */
3992
3993 static struct bfd_link_hash_table *
3994 ppc64_elf_link_hash_table_create (bfd *abfd)
3995 {
3996   struct ppc_link_hash_table *htab;
3997   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3998
3999   htab = bfd_zmalloc (amt);
4000   if (htab == NULL)
4001     return NULL;
4002
4003   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4004                                       sizeof (struct ppc_link_hash_entry),
4005                                       PPC64_ELF_DATA))
4006     {
4007       free (htab);
4008       return NULL;
4009     }
4010
4011   /* Init the stub hash table too.  */
4012   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4013                             sizeof (struct ppc_stub_hash_entry)))
4014     return NULL;
4015
4016   /* And the branch hash table.  */
4017   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4018                             sizeof (struct ppc_branch_hash_entry)))
4019     return NULL;
4020
4021   htab->tocsave_htab = htab_try_create (1024,
4022                                         tocsave_htab_hash,
4023                                         tocsave_htab_eq,
4024                                         NULL);
4025   if (htab->tocsave_htab == NULL)
4026     return NULL;
4027
4028   /* Initializing two fields of the union is just cosmetic.  We really
4029      only care about glist, but when compiled on a 32-bit host the
4030      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
4031      debugger inspection of these fields look nicer.  */
4032   htab->elf.init_got_refcount.refcount = 0;
4033   htab->elf.init_got_refcount.glist = NULL;
4034   htab->elf.init_plt_refcount.refcount = 0;
4035   htab->elf.init_plt_refcount.glist = NULL;
4036   htab->elf.init_got_offset.offset = 0;
4037   htab->elf.init_got_offset.glist = NULL;
4038   htab->elf.init_plt_offset.offset = 0;
4039   htab->elf.init_plt_offset.glist = NULL;
4040
4041   return &htab->elf.root;
4042 }
4043
4044 /* Free the derived linker hash table.  */
4045
4046 static void
4047 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
4048 {
4049   struct ppc_link_hash_table *htab = (struct ppc_link_hash_table *) hash;
4050
4051   bfd_hash_table_free (&htab->stub_hash_table);
4052   bfd_hash_table_free (&htab->branch_hash_table);
4053   if (htab->tocsave_htab)
4054     htab_delete (htab->tocsave_htab);
4055   _bfd_elf_link_hash_table_free (hash);
4056 }
4057
4058 /* Create sections for linker generated code.  */
4059
4060 static bfd_boolean
4061 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4062 {
4063   struct ppc_link_hash_table *htab;
4064   flagword flags;
4065
4066   htab = ppc_hash_table (info);
4067
4068   /* Create .sfpr for code to save and restore fp regs.  */
4069   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4070            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4071   htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4072                                                    flags);
4073   if (htab->sfpr == NULL
4074       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4075     return FALSE;
4076
4077   /* Create .glink for lazy dynamic linking support.  */
4078   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4079                                                     flags);
4080   if (htab->glink == NULL
4081       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4082     return FALSE;
4083
4084   if (!info->no_ld_generated_unwind_info)
4085     {
4086       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4087                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4088       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4089                                                                  ".eh_frame",
4090                                                                  flags);
4091       if (htab->glink_eh_frame == NULL
4092           || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4093         return FALSE;
4094     }
4095
4096   flags = SEC_ALLOC | SEC_LINKER_CREATED;
4097   htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4098   if (htab->iplt == NULL
4099       || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4100     return FALSE;
4101
4102   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4103            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4104   htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4105                                                       ".rela.iplt",
4106                                                       flags);
4107   if (htab->reliplt == NULL
4108       || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4109     return FALSE;
4110
4111   /* Create branch lookup table for plt_branch stubs.  */
4112   flags = (SEC_ALLOC | SEC_LOAD
4113            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4114   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4115                                                    flags);
4116   if (htab->brlt == NULL
4117       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4118     return FALSE;
4119
4120   if (!info->shared)
4121     return TRUE;
4122
4123   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4124            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4125   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4126                                                       ".rela.branch_lt",
4127                                                       flags);
4128   if (htab->relbrlt == NULL
4129       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4130     return FALSE;
4131
4132   return TRUE;
4133 }
4134
4135 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
4136
4137 bfd_boolean
4138 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4139 {
4140   struct ppc_link_hash_table *htab;
4141
4142   elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4143
4144 /* Always hook our dynamic sections into the first bfd, which is the
4145    linker created stub bfd.  This ensures that the GOT header is at
4146    the start of the output TOC section.  */
4147   htab = ppc_hash_table (info);
4148   if (htab == NULL)
4149     return FALSE;
4150   htab->stub_bfd = abfd;
4151   htab->elf.dynobj = abfd;
4152
4153   if (info->relocatable)
4154     return TRUE;
4155
4156   return create_linkage_sections (htab->elf.dynobj, info);
4157 }
4158
4159 /* Build a name for an entry in the stub hash table.  */
4160
4161 static char *
4162 ppc_stub_name (const asection *input_section,
4163                const asection *sym_sec,
4164                const struct ppc_link_hash_entry *h,
4165                const Elf_Internal_Rela *rel)
4166 {
4167   char *stub_name;
4168   ssize_t len;
4169
4170   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4171      offsets from a sym as a branch target?  In fact, we could
4172      probably assume the addend is always zero.  */
4173   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4174
4175   if (h)
4176     {
4177       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4178       stub_name = bfd_malloc (len);
4179       if (stub_name == NULL)
4180         return stub_name;
4181
4182       len = sprintf (stub_name, "%08x.%s+%x",
4183                      input_section->id & 0xffffffff,
4184                      h->elf.root.root.string,
4185                      (int) rel->r_addend & 0xffffffff);
4186     }
4187   else
4188     {
4189       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4190       stub_name = bfd_malloc (len);
4191       if (stub_name == NULL)
4192         return stub_name;
4193
4194       len = sprintf (stub_name, "%08x.%x:%x+%x",
4195                      input_section->id & 0xffffffff,
4196                      sym_sec->id & 0xffffffff,
4197                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4198                      (int) rel->r_addend & 0xffffffff);
4199     }
4200   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4201     stub_name[len - 2] = 0;
4202   return stub_name;
4203 }
4204
4205 /* Look up an entry in the stub hash.  Stub entries are cached because
4206    creating the stub name takes a bit of time.  */
4207
4208 static struct ppc_stub_hash_entry *
4209 ppc_get_stub_entry (const asection *input_section,
4210                     const asection *sym_sec,
4211                     struct ppc_link_hash_entry *h,
4212                     const Elf_Internal_Rela *rel,
4213                     struct ppc_link_hash_table *htab)
4214 {
4215   struct ppc_stub_hash_entry *stub_entry;
4216   const asection *id_sec;
4217
4218   /* If this input section is part of a group of sections sharing one
4219      stub section, then use the id of the first section in the group.
4220      Stub names need to include a section id, as there may well be
4221      more than one stub used to reach say, printf, and we need to
4222      distinguish between them.  */
4223   id_sec = htab->stub_group[input_section->id].link_sec;
4224
4225   if (h != NULL && h->u.stub_cache != NULL
4226       && h->u.stub_cache->h == h
4227       && h->u.stub_cache->id_sec == id_sec)
4228     {
4229       stub_entry = h->u.stub_cache;
4230     }
4231   else
4232     {
4233       char *stub_name;
4234
4235       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4236       if (stub_name == NULL)
4237         return NULL;
4238
4239       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4240                                          stub_name, FALSE, FALSE);
4241       if (h != NULL)
4242         h->u.stub_cache = stub_entry;
4243
4244       free (stub_name);
4245     }
4246
4247   return stub_entry;
4248 }
4249
4250 /* Add a new stub entry to the stub hash.  Not all fields of the new
4251    stub entry are initialised.  */
4252
4253 static struct ppc_stub_hash_entry *
4254 ppc_add_stub (const char *stub_name,
4255               asection *section,
4256               struct bfd_link_info *info)
4257 {
4258   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4259   asection *link_sec;
4260   asection *stub_sec;
4261   struct ppc_stub_hash_entry *stub_entry;
4262
4263   link_sec = htab->stub_group[section->id].link_sec;
4264   stub_sec = htab->stub_group[section->id].stub_sec;
4265   if (stub_sec == NULL)
4266     {
4267       stub_sec = htab->stub_group[link_sec->id].stub_sec;
4268       if (stub_sec == NULL)
4269         {
4270           size_t namelen;
4271           bfd_size_type len;
4272           char *s_name;
4273
4274           namelen = strlen (link_sec->name);
4275           len = namelen + sizeof (STUB_SUFFIX);
4276           s_name = bfd_alloc (htab->stub_bfd, len);
4277           if (s_name == NULL)
4278             return NULL;
4279
4280           memcpy (s_name, link_sec->name, namelen);
4281           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4282           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4283           if (stub_sec == NULL)
4284             return NULL;
4285           htab->stub_group[link_sec->id].stub_sec = stub_sec;
4286         }
4287       htab->stub_group[section->id].stub_sec = stub_sec;
4288     }
4289
4290   /* Enter this entry into the linker stub hash table.  */
4291   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4292                                      TRUE, FALSE);
4293   if (stub_entry == NULL)
4294     {
4295       info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4296                               section->owner, stub_name);
4297       return NULL;
4298     }
4299
4300   stub_entry->stub_sec = stub_sec;
4301   stub_entry->stub_offset = 0;
4302   stub_entry->id_sec = link_sec;
4303   return stub_entry;
4304 }
4305
4306 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4307    not already done.  */
4308
4309 static bfd_boolean
4310 create_got_section (bfd *abfd, struct bfd_link_info *info)
4311 {
4312   asection *got, *relgot;
4313   flagword flags;
4314   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4315
4316   if (!is_ppc64_elf (abfd))
4317     return FALSE;
4318   if (htab == NULL)
4319     return FALSE;
4320
4321   if (!htab->got)
4322     {
4323       if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4324         return FALSE;
4325
4326       htab->got = bfd_get_linker_section (htab->elf.dynobj, ".got");
4327       if (!htab->got)
4328         abort ();
4329     }
4330
4331   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4332            | SEC_LINKER_CREATED);
4333
4334   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4335   if (!got
4336       || !bfd_set_section_alignment (abfd, got, 3))
4337     return FALSE;
4338
4339   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4340                                                flags | SEC_READONLY);
4341   if (!relgot
4342       || ! bfd_set_section_alignment (abfd, relgot, 3))
4343     return FALSE;
4344
4345   ppc64_elf_tdata (abfd)->got = got;
4346   ppc64_elf_tdata (abfd)->relgot = relgot;
4347   return TRUE;
4348 }
4349
4350 /* Create the dynamic sections, and set up shortcuts.  */
4351
4352 static bfd_boolean
4353 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4354 {
4355   struct ppc_link_hash_table *htab;
4356
4357   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4358     return FALSE;
4359
4360   htab = ppc_hash_table (info);
4361   if (htab == NULL)
4362     return FALSE;
4363
4364   if (!htab->got)
4365     htab->got = bfd_get_linker_section (dynobj, ".got");
4366   htab->plt = bfd_get_linker_section (dynobj, ".plt");
4367   htab->relplt = bfd_get_linker_section (dynobj, ".rela.plt");
4368   htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
4369   if (!info->shared)
4370     htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
4371
4372   if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4373       || (!info->shared && !htab->relbss))
4374     abort ();
4375
4376   return TRUE;
4377 }
4378
4379 /* Follow indirect and warning symbol links.  */
4380
4381 static inline struct bfd_link_hash_entry *
4382 follow_link (struct bfd_link_hash_entry *h)
4383 {
4384   while (h->type == bfd_link_hash_indirect
4385          || h->type == bfd_link_hash_warning)
4386     h = h->u.i.link;
4387   return h;
4388 }
4389
4390 static inline struct elf_link_hash_entry *
4391 elf_follow_link (struct elf_link_hash_entry *h)
4392 {
4393   return (struct elf_link_hash_entry *) follow_link (&h->root);
4394 }
4395
4396 static inline struct ppc_link_hash_entry *
4397 ppc_follow_link (struct ppc_link_hash_entry *h)
4398 {
4399   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4400 }
4401
4402 /* Merge PLT info on FROM with that on TO.  */
4403
4404 static void
4405 move_plt_plist (struct ppc_link_hash_entry *from,
4406                 struct ppc_link_hash_entry *to)
4407 {
4408   if (from->elf.plt.plist != NULL)
4409     {
4410       if (to->elf.plt.plist != NULL)
4411         {
4412           struct plt_entry **entp;
4413           struct plt_entry *ent;
4414
4415           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4416             {
4417               struct plt_entry *dent;
4418
4419               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4420                 if (dent->addend == ent->addend)
4421                   {
4422                     dent->plt.refcount += ent->plt.refcount;
4423                     *entp = ent->next;
4424                     break;
4425                   }
4426               if (dent == NULL)
4427                 entp = &ent->next;
4428             }
4429           *entp = to->elf.plt.plist;
4430         }
4431
4432       to->elf.plt.plist = from->elf.plt.plist;
4433       from->elf.plt.plist = NULL;
4434     }
4435 }
4436
4437 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
4438
4439 static void
4440 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4441                                 struct elf_link_hash_entry *dir,
4442                                 struct elf_link_hash_entry *ind)
4443 {
4444   struct ppc_link_hash_entry *edir, *eind;
4445
4446   edir = (struct ppc_link_hash_entry *) dir;
4447   eind = (struct ppc_link_hash_entry *) ind;
4448
4449   edir->is_func |= eind->is_func;
4450   edir->is_func_descriptor |= eind->is_func_descriptor;
4451   edir->tls_mask |= eind->tls_mask;
4452   if (eind->oh != NULL)
4453     edir->oh = ppc_follow_link (eind->oh);
4454
4455   /* If called to transfer flags for a weakdef during processing
4456      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4457      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
4458   if (!(ELIMINATE_COPY_RELOCS
4459         && eind->elf.root.type != bfd_link_hash_indirect
4460         && edir->elf.dynamic_adjusted))
4461     edir->elf.non_got_ref |= eind->elf.non_got_ref;
4462
4463   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4464   edir->elf.ref_regular |= eind->elf.ref_regular;
4465   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4466   edir->elf.needs_plt |= eind->elf.needs_plt;
4467
4468   /* Copy over any dynamic relocs we may have on the indirect sym.  */
4469   if (eind->dyn_relocs != NULL)
4470     {
4471       if (edir->dyn_relocs != NULL)
4472         {
4473           struct elf_dyn_relocs **pp;
4474           struct elf_dyn_relocs *p;
4475
4476           /* Add reloc counts against the indirect sym to the direct sym
4477              list.  Merge any entries against the same section.  */
4478           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4479             {
4480               struct elf_dyn_relocs *q;
4481
4482               for (q = edir->dyn_relocs; q != NULL; q = q->next)
4483                 if (q->sec == p->sec)
4484                   {
4485                     q->pc_count += p->pc_count;
4486                     q->count += p->count;
4487                     *pp = p->next;
4488                     break;
4489                   }
4490               if (q == NULL)
4491                 pp = &p->next;
4492             }
4493           *pp = edir->dyn_relocs;
4494         }
4495
4496       edir->dyn_relocs = eind->dyn_relocs;
4497       eind->dyn_relocs = NULL;
4498     }
4499
4500   /* If we were called to copy over info for a weak sym, that's all.
4501      You might think dyn_relocs need not be copied over;  After all,
4502      both syms will be dynamic or both non-dynamic so we're just
4503      moving reloc accounting around.  However, ELIMINATE_COPY_RELOCS
4504      code in ppc64_elf_adjust_dynamic_symbol needs to check for
4505      dyn_relocs in read-only sections, and it does so on what is the
4506      DIR sym here.  */
4507   if (eind->elf.root.type != bfd_link_hash_indirect)
4508     return;
4509
4510   /* Copy over got entries that we may have already seen to the
4511      symbol which just became indirect.  */
4512   if (eind->elf.got.glist != NULL)
4513     {
4514       if (edir->elf.got.glist != NULL)
4515         {
4516           struct got_entry **entp;
4517           struct got_entry *ent;
4518
4519           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4520             {
4521               struct got_entry *dent;
4522
4523               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4524                 if (dent->addend == ent->addend
4525                     && dent->owner == ent->owner
4526                     && dent->tls_type == ent->tls_type)
4527                   {
4528                     dent->got.refcount += ent->got.refcount;
4529                     *entp = ent->next;
4530                     break;
4531                   }
4532               if (dent == NULL)
4533                 entp = &ent->next;
4534             }
4535           *entp = edir->elf.got.glist;
4536         }
4537
4538       edir->elf.got.glist = eind->elf.got.glist;
4539       eind->elf.got.glist = NULL;
4540     }
4541
4542   /* And plt entries.  */
4543   move_plt_plist (eind, edir);
4544
4545   if (eind->elf.dynindx != -1)
4546     {
4547       if (edir->elf.dynindx != -1)
4548         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4549                                 edir->elf.dynstr_index);
4550       edir->elf.dynindx = eind->elf.dynindx;
4551       edir->elf.dynstr_index = eind->elf.dynstr_index;
4552       eind->elf.dynindx = -1;
4553       eind->elf.dynstr_index = 0;
4554     }
4555 }
4556
4557 /* Find the function descriptor hash entry from the given function code
4558    hash entry FH.  Link the entries via their OH fields.  */
4559
4560 static struct ppc_link_hash_entry *
4561 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4562 {
4563   struct ppc_link_hash_entry *fdh = fh->oh;
4564
4565   if (fdh == NULL)
4566     {
4567       const char *fd_name = fh->elf.root.root.string + 1;
4568
4569       fdh = (struct ppc_link_hash_entry *)
4570         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4571       if (fdh == NULL)
4572         return fdh;
4573
4574       fdh->is_func_descriptor = 1;
4575       fdh->oh = fh;
4576       fh->is_func = 1;
4577       fh->oh = fdh;
4578     }
4579
4580   return ppc_follow_link (fdh);
4581 }
4582
4583 /* Make a fake function descriptor sym for the code sym FH.  */
4584
4585 static struct ppc_link_hash_entry *
4586 make_fdh (struct bfd_link_info *info,
4587           struct ppc_link_hash_entry *fh)
4588 {
4589   bfd *abfd;
4590   asymbol *newsym;
4591   struct bfd_link_hash_entry *bh;
4592   struct ppc_link_hash_entry *fdh;
4593
4594   abfd = fh->elf.root.u.undef.abfd;
4595   newsym = bfd_make_empty_symbol (abfd);
4596   newsym->name = fh->elf.root.root.string + 1;
4597   newsym->section = bfd_und_section_ptr;
4598   newsym->value = 0;
4599   newsym->flags = BSF_WEAK;
4600
4601   bh = NULL;
4602   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4603                                          newsym->flags, newsym->section,
4604                                          newsym->value, NULL, FALSE, FALSE,
4605                                          &bh))
4606     return NULL;
4607
4608   fdh = (struct ppc_link_hash_entry *) bh;
4609   fdh->elf.non_elf = 0;
4610   fdh->fake = 1;
4611   fdh->is_func_descriptor = 1;
4612   fdh->oh = fh;
4613   fh->is_func = 1;
4614   fh->oh = fdh;
4615   return fdh;
4616 }
4617
4618 /* Fix function descriptor symbols defined in .opd sections to be
4619    function type.  */
4620
4621 static bfd_boolean
4622 ppc64_elf_add_symbol_hook (bfd *ibfd,
4623                            struct bfd_link_info *info,
4624                            Elf_Internal_Sym *isym,
4625                            const char **name ATTRIBUTE_UNUSED,
4626                            flagword *flags ATTRIBUTE_UNUSED,
4627                            asection **sec,
4628                            bfd_vma *value ATTRIBUTE_UNUSED)
4629 {
4630   if ((ibfd->flags & DYNAMIC) == 0
4631       && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4632     elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4633
4634   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4635     {
4636       if ((ibfd->flags & DYNAMIC) == 0)
4637         elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4638     }
4639   else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4640     ;
4641   else if (*sec != NULL
4642            && strcmp ((*sec)->name, ".opd") == 0)
4643     isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4644
4645   return TRUE;
4646 }
4647
4648 /* This function makes an old ABI object reference to ".bar" cause the
4649    inclusion of a new ABI object archive that defines "bar".
4650    NAME is a symbol defined in an archive.  Return a symbol in the hash
4651    table that might be satisfied by the archive symbols.  */
4652
4653 static struct elf_link_hash_entry *
4654 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4655                                  struct bfd_link_info *info,
4656                                  const char *name)
4657 {
4658   struct elf_link_hash_entry *h;
4659   char *dot_name;
4660   size_t len;
4661
4662   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4663   if (h != NULL
4664       /* Don't return this sym if it is a fake function descriptor
4665          created by add_symbol_adjust.  */
4666       && !(h->root.type == bfd_link_hash_undefweak
4667            && ((struct ppc_link_hash_entry *) h)->fake))
4668     return h;
4669
4670   if (name[0] == '.')
4671     return h;
4672
4673   len = strlen (name);
4674   dot_name = bfd_alloc (abfd, len + 2);
4675   if (dot_name == NULL)
4676     return (struct elf_link_hash_entry *) 0 - 1;
4677   dot_name[0] = '.';
4678   memcpy (dot_name + 1, name, len + 1);
4679   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4680   bfd_release (abfd, dot_name);
4681   return h;
4682 }
4683
4684 /* This function satisfies all old ABI object references to ".bar" if a
4685    new ABI object defines "bar".  Well, at least, undefined dot symbols
4686    are made weak.  This stops later archive searches from including an
4687    object if we already have a function descriptor definition.  It also
4688    prevents the linker complaining about undefined symbols.
4689    We also check and correct mismatched symbol visibility here.  The
4690    most restrictive visibility of the function descriptor and the
4691    function entry symbol is used.  */
4692
4693 static bfd_boolean
4694 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4695 {
4696   struct ppc_link_hash_table *htab;
4697   struct ppc_link_hash_entry *fdh;
4698
4699   if (eh->elf.root.type == bfd_link_hash_indirect)
4700     return TRUE;
4701
4702   if (eh->elf.root.type == bfd_link_hash_warning)
4703     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4704
4705   if (eh->elf.root.root.string[0] != '.')
4706     abort ();
4707
4708   htab = ppc_hash_table (info);
4709   if (htab == NULL)
4710     return FALSE;
4711
4712   fdh = lookup_fdh (eh, htab);
4713   if (fdh == NULL)
4714     {
4715       if (!info->relocatable
4716           && (eh->elf.root.type == bfd_link_hash_undefined
4717               || eh->elf.root.type == bfd_link_hash_undefweak)
4718           && eh->elf.ref_regular)
4719         {
4720           /* Make an undefweak function descriptor sym, which is enough to
4721              pull in an --as-needed shared lib, but won't cause link
4722              errors.  Archives are handled elsewhere.  */
4723           fdh = make_fdh (info, eh);
4724           if (fdh == NULL)
4725             return FALSE;
4726           fdh->elf.ref_regular = 1;
4727         }
4728     }
4729   else
4730     {
4731       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4732       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4733       if (entry_vis < descr_vis)
4734         fdh->elf.other += entry_vis - descr_vis;
4735       else if (entry_vis > descr_vis)
4736         eh->elf.other += descr_vis - entry_vis;
4737
4738       if ((fdh->elf.root.type == bfd_link_hash_defined
4739            || fdh->elf.root.type == bfd_link_hash_defweak)
4740           && eh->elf.root.type == bfd_link_hash_undefined)
4741         {
4742           eh->elf.root.type = bfd_link_hash_undefweak;
4743           eh->was_undefined = 1;
4744           htab->twiddled_syms = 1;
4745         }
4746     }
4747
4748   return TRUE;
4749 }
4750
4751 /* Process list of dot-symbols we made in link_hash_newfunc.  */
4752
4753 static bfd_boolean
4754 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
4755 {
4756   struct ppc_link_hash_table *htab;
4757   struct ppc_link_hash_entry **p, *eh;
4758
4759   if (!is_ppc64_elf (info->output_bfd))
4760     return TRUE;
4761   htab = ppc_hash_table (info);
4762   if (htab == NULL)
4763     return FALSE;
4764
4765   if (is_ppc64_elf (ibfd))
4766     {
4767       p = &htab->dot_syms;
4768       while ((eh = *p) != NULL)
4769         {
4770           *p = NULL;
4771           if (&eh->elf == htab->elf.hgot)
4772             ;
4773           else if (htab->elf.hgot == NULL
4774                    && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4775             htab->elf.hgot = &eh->elf;
4776           else if (!add_symbol_adjust (eh, info))
4777             return FALSE;
4778           p = &eh->u.next_dot_sym;
4779         }
4780     }
4781
4782   /* Clear the list for non-ppc64 input files.  */
4783   p = &htab->dot_syms;
4784   while ((eh = *p) != NULL)
4785     {
4786       *p = NULL;
4787       p = &eh->u.next_dot_sym;
4788     }
4789
4790   /* We need to fix the undefs list for any syms we have twiddled to
4791      undef_weak.  */
4792   if (htab->twiddled_syms)
4793     {
4794       bfd_link_repair_undef_list (&htab->elf.root);
4795       htab->twiddled_syms = 0;
4796     }
4797   return TRUE;
4798 }
4799
4800 /* Undo hash table changes when an --as-needed input file is determined
4801    not to be needed.  */
4802
4803 static bfd_boolean
4804 ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4805                              struct bfd_link_info *info)
4806 {
4807   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4808
4809   if (htab == NULL)
4810     return FALSE;
4811
4812   htab->dot_syms = NULL;
4813   return TRUE;
4814 }
4815
4816 /* If --just-symbols against a final linked binary, then assume we need
4817    toc adjusting stubs when calling functions defined there.  */
4818
4819 static void
4820 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4821 {
4822   if ((sec->flags & SEC_CODE) != 0
4823       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4824       && is_ppc64_elf (sec->owner))
4825     {
4826       asection *got = bfd_get_section_by_name (sec->owner, ".got");
4827       if (got != NULL
4828           && got->size >= elf_backend_got_header_size
4829           && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4830         sec->has_toc_reloc = 1;
4831     }
4832   _bfd_elf_link_just_syms (sec, info);
4833 }
4834
4835 static struct plt_entry **
4836 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4837                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4838 {
4839   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4840   struct plt_entry **local_plt;
4841   unsigned char *local_got_tls_masks;
4842
4843   if (local_got_ents == NULL)
4844     {
4845       bfd_size_type size = symtab_hdr->sh_info;
4846
4847       size *= (sizeof (*local_got_ents)
4848                + sizeof (*local_plt)
4849                + sizeof (*local_got_tls_masks));
4850       local_got_ents = bfd_zalloc (abfd, size);
4851       if (local_got_ents == NULL)
4852         return NULL;
4853       elf_local_got_ents (abfd) = local_got_ents;
4854     }
4855
4856   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
4857     {
4858       struct got_entry *ent;
4859
4860       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4861         if (ent->addend == r_addend
4862             && ent->owner == abfd
4863             && ent->tls_type == tls_type)
4864           break;
4865       if (ent == NULL)
4866         {
4867           bfd_size_type amt = sizeof (*ent);
4868           ent = bfd_alloc (abfd, amt);
4869           if (ent == NULL)
4870             return FALSE;
4871           ent->next = local_got_ents[r_symndx];
4872           ent->addend = r_addend;
4873           ent->owner = abfd;
4874           ent->tls_type = tls_type;
4875           ent->is_indirect = FALSE;
4876           ent->got.refcount = 0;
4877           local_got_ents[r_symndx] = ent;
4878         }
4879       ent->got.refcount += 1;
4880     }
4881
4882   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4883   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4884   local_got_tls_masks[r_symndx] |= tls_type;
4885
4886   return local_plt + r_symndx;
4887 }
4888
4889 static bfd_boolean
4890 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4891 {
4892   struct plt_entry *ent;
4893
4894   for (ent = *plist; ent != NULL; ent = ent->next)
4895     if (ent->addend == addend)
4896       break;
4897   if (ent == NULL)
4898     {
4899       bfd_size_type amt = sizeof (*ent);
4900       ent = bfd_alloc (abfd, amt);
4901       if (ent == NULL)
4902         return FALSE;
4903       ent->next = *plist;
4904       ent->addend = addend;
4905       ent->plt.refcount = 0;
4906       *plist = ent;
4907     }
4908   ent->plt.refcount += 1;
4909   return TRUE;
4910 }
4911
4912 static bfd_boolean
4913 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4914 {
4915   return (r_type == R_PPC64_REL24
4916           || r_type == R_PPC64_REL14
4917           || r_type == R_PPC64_REL14_BRTAKEN
4918           || r_type == R_PPC64_REL14_BRNTAKEN
4919           || r_type == R_PPC64_ADDR24
4920           || r_type == R_PPC64_ADDR14
4921           || r_type == R_PPC64_ADDR14_BRTAKEN
4922           || r_type == R_PPC64_ADDR14_BRNTAKEN);
4923 }
4924
4925 /* Look through the relocs for a section during the first phase, and
4926    calculate needed space in the global offset table, procedure
4927    linkage table, and dynamic reloc sections.  */
4928
4929 static bfd_boolean
4930 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4931                         asection *sec, const Elf_Internal_Rela *relocs)
4932 {
4933   struct ppc_link_hash_table *htab;
4934   Elf_Internal_Shdr *symtab_hdr;
4935   struct elf_link_hash_entry **sym_hashes;
4936   const Elf_Internal_Rela *rel;
4937   const Elf_Internal_Rela *rel_end;
4938   asection *sreloc;
4939   asection **opd_sym_map;
4940   struct elf_link_hash_entry *tga, *dottga;
4941
4942   if (info->relocatable)
4943     return TRUE;
4944
4945   /* Don't do anything special with non-loaded, non-alloced sections.
4946      In particular, any relocs in such sections should not affect GOT
4947      and PLT reference counting (ie. we don't allow them to create GOT
4948      or PLT entries), there's no possibility or desire to optimize TLS
4949      relocs, and there's not much point in propagating relocs to shared
4950      libs that the dynamic linker won't relocate.  */
4951   if ((sec->flags & SEC_ALLOC) == 0)
4952     return TRUE;
4953
4954   BFD_ASSERT (is_ppc64_elf (abfd));
4955
4956   htab = ppc_hash_table (info);
4957   if (htab == NULL)
4958     return FALSE;
4959
4960   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4961                               FALSE, FALSE, TRUE);
4962   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4963                                  FALSE, FALSE, TRUE);
4964   symtab_hdr = &elf_symtab_hdr (abfd);
4965   sym_hashes = elf_sym_hashes (abfd);
4966   sreloc = NULL;
4967   opd_sym_map = NULL;
4968   if (strcmp (sec->name, ".opd") == 0)
4969     {
4970       /* Garbage collection needs some extra help with .opd sections.
4971          We don't want to necessarily keep everything referenced by
4972          relocs in .opd, as that would keep all functions.  Instead,
4973          if we reference an .opd symbol (a function descriptor), we
4974          want to keep the function code symbol's section.  This is
4975          easy for global symbols, but for local syms we need to keep
4976          information about the associated function section.  */
4977       bfd_size_type amt;
4978
4979       amt = sec->size * sizeof (*opd_sym_map) / 8;
4980       opd_sym_map = bfd_zalloc (abfd, amt);
4981       if (opd_sym_map == NULL)
4982         return FALSE;
4983       ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
4984       BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4985       ppc64_elf_section_data (sec)->sec_type = sec_opd;
4986     }
4987
4988   rel_end = relocs + sec->reloc_count;
4989   for (rel = relocs; rel < rel_end; rel++)
4990     {
4991       unsigned long r_symndx;
4992       struct elf_link_hash_entry *h;
4993       enum elf_ppc64_reloc_type r_type;
4994       int tls_type;
4995       struct _ppc64_elf_section_data *ppc64_sec;
4996       struct plt_entry **ifunc;
4997
4998       r_symndx = ELF64_R_SYM (rel->r_info);
4999       if (r_symndx < symtab_hdr->sh_info)
5000         h = NULL;
5001       else
5002         {
5003           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5004           h = elf_follow_link (h);
5005
5006           /* PR15323, ref flags aren't set for references in the same
5007              object.  */
5008           h->root.non_ir_ref = 1;
5009
5010           if (h == htab->elf.hgot)
5011             sec->has_toc_reloc = 1;
5012         }
5013
5014       tls_type = 0;
5015       ifunc = NULL;
5016       if (h != NULL)
5017         {
5018           if (h->type == STT_GNU_IFUNC)
5019             {
5020               h->needs_plt = 1;
5021               ifunc = &h->plt.plist;
5022             }
5023         }
5024       else
5025         {
5026           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5027                                                           abfd, r_symndx);
5028           if (isym == NULL)
5029             return FALSE;
5030
5031           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5032             {
5033               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5034                                              rel->r_addend, PLT_IFUNC);
5035               if (ifunc == NULL)
5036                 return FALSE;
5037             }
5038         }
5039       r_type = ELF64_R_TYPE (rel->r_info);
5040       if (is_branch_reloc (r_type))
5041         {
5042           if (h != NULL && (h == tga || h == dottga))
5043             {
5044               if (rel != relocs
5045                   && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5046                       || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5047                 /* We have a new-style __tls_get_addr call with a marker
5048                    reloc.  */
5049                 ;
5050               else
5051                 /* Mark this section as having an old-style call.  */
5052                 sec->has_tls_get_addr_call = 1;
5053             }
5054
5055           /* STT_GNU_IFUNC symbols must have a PLT entry.  */
5056           if (ifunc != NULL
5057               && !update_plt_info (abfd, ifunc, rel->r_addend))
5058             return FALSE;
5059         }
5060
5061       switch (r_type)
5062         {
5063         case R_PPC64_TLSGD:
5064         case R_PPC64_TLSLD:
5065           /* These special tls relocs tie a call to __tls_get_addr with
5066              its parameter symbol.  */
5067           break;
5068
5069         case R_PPC64_GOT_TLSLD16:
5070         case R_PPC64_GOT_TLSLD16_LO:
5071         case R_PPC64_GOT_TLSLD16_HI:
5072         case R_PPC64_GOT_TLSLD16_HA:
5073           tls_type = TLS_TLS | TLS_LD;
5074           goto dogottls;
5075
5076         case R_PPC64_GOT_TLSGD16:
5077         case R_PPC64_GOT_TLSGD16_LO:
5078         case R_PPC64_GOT_TLSGD16_HI:
5079         case R_PPC64_GOT_TLSGD16_HA:
5080           tls_type = TLS_TLS | TLS_GD;
5081           goto dogottls;
5082
5083         case R_PPC64_GOT_TPREL16_DS:
5084         case R_PPC64_GOT_TPREL16_LO_DS:
5085         case R_PPC64_GOT_TPREL16_HI:
5086         case R_PPC64_GOT_TPREL16_HA:
5087           if (!info->executable)
5088             info->flags |= DF_STATIC_TLS;
5089           tls_type = TLS_TLS | TLS_TPREL;
5090           goto dogottls;
5091
5092         case R_PPC64_GOT_DTPREL16_DS:
5093         case R_PPC64_GOT_DTPREL16_LO_DS:
5094         case R_PPC64_GOT_DTPREL16_HI:
5095         case R_PPC64_GOT_DTPREL16_HA:
5096           tls_type = TLS_TLS | TLS_DTPREL;
5097         dogottls:
5098           sec->has_tls_reloc = 1;
5099           /* Fall thru */
5100
5101         case R_PPC64_GOT16:
5102         case R_PPC64_GOT16_DS:
5103         case R_PPC64_GOT16_HA:
5104         case R_PPC64_GOT16_HI:
5105         case R_PPC64_GOT16_LO:
5106         case R_PPC64_GOT16_LO_DS:
5107           /* This symbol requires a global offset table entry.  */
5108           sec->has_toc_reloc = 1;
5109           if (r_type == R_PPC64_GOT_TLSLD16
5110               || r_type == R_PPC64_GOT_TLSGD16
5111               || r_type == R_PPC64_GOT_TPREL16_DS
5112               || r_type == R_PPC64_GOT_DTPREL16_DS
5113               || r_type == R_PPC64_GOT16
5114               || r_type == R_PPC64_GOT16_DS)
5115             {
5116               htab->do_multi_toc = 1;
5117               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5118             }
5119
5120           if (ppc64_elf_tdata (abfd)->got == NULL
5121               && !create_got_section (abfd, info))
5122             return FALSE;
5123
5124           if (h != NULL)
5125             {
5126               struct ppc_link_hash_entry *eh;
5127               struct got_entry *ent;
5128
5129               eh = (struct ppc_link_hash_entry *) h;
5130               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5131                 if (ent->addend == rel->r_addend
5132                     && ent->owner == abfd
5133                     && ent->tls_type == tls_type)
5134                   break;
5135               if (ent == NULL)
5136                 {
5137                   bfd_size_type amt = sizeof (*ent);
5138                   ent = bfd_alloc (abfd, amt);
5139                   if (ent == NULL)
5140                     return FALSE;
5141                   ent->next = eh->elf.got.glist;
5142                   ent->addend = rel->r_addend;
5143                   ent->owner = abfd;
5144                   ent->tls_type = tls_type;
5145                   ent->is_indirect = FALSE;
5146                   ent->got.refcount = 0;
5147                   eh->elf.got.glist = ent;
5148                 }
5149               ent->got.refcount += 1;
5150               eh->tls_mask |= tls_type;
5151             }
5152           else
5153             /* This is a global offset table entry for a local symbol.  */
5154             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5155                                         rel->r_addend, tls_type))
5156               return FALSE;
5157           break;
5158
5159         case R_PPC64_PLT16_HA:
5160         case R_PPC64_PLT16_HI:
5161         case R_PPC64_PLT16_LO:
5162         case R_PPC64_PLT32:
5163         case R_PPC64_PLT64:
5164           /* This symbol requires a procedure linkage table entry.  We
5165              actually build the entry in adjust_dynamic_symbol,
5166              because this might be a case of linking PIC code without
5167              linking in any dynamic objects, in which case we don't
5168              need to generate a procedure linkage table after all.  */
5169           if (h == NULL)
5170             {
5171               /* It does not make sense to have a procedure linkage
5172                  table entry for a local symbol.  */
5173               bfd_set_error (bfd_error_bad_value);
5174               return FALSE;
5175             }
5176           else
5177             {
5178               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5179                 return FALSE;
5180               h->needs_plt = 1;
5181               if (h->root.root.string[0] == '.'
5182                   && h->root.root.string[1] != '\0')
5183                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5184             }
5185           break;
5186
5187           /* The following relocations don't need to propagate the
5188              relocation if linking a shared object since they are
5189              section relative.  */
5190         case R_PPC64_SECTOFF:
5191         case R_PPC64_SECTOFF_LO:
5192         case R_PPC64_SECTOFF_HI:
5193         case R_PPC64_SECTOFF_HA:
5194         case R_PPC64_SECTOFF_DS:
5195         case R_PPC64_SECTOFF_LO_DS:
5196         case R_PPC64_DTPREL16:
5197         case R_PPC64_DTPREL16_LO:
5198         case R_PPC64_DTPREL16_HI:
5199         case R_PPC64_DTPREL16_HA:
5200         case R_PPC64_DTPREL16_DS:
5201         case R_PPC64_DTPREL16_LO_DS:
5202         case R_PPC64_DTPREL16_HIGHER:
5203         case R_PPC64_DTPREL16_HIGHERA:
5204         case R_PPC64_DTPREL16_HIGHEST:
5205         case R_PPC64_DTPREL16_HIGHESTA:
5206           break;
5207
5208           /* Nor do these.  */
5209         case R_PPC64_REL16:
5210         case R_PPC64_REL16_LO:
5211         case R_PPC64_REL16_HI:
5212         case R_PPC64_REL16_HA:
5213           break;
5214
5215         case R_PPC64_TOC16:
5216         case R_PPC64_TOC16_DS:
5217           htab->do_multi_toc = 1;
5218           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5219         case R_PPC64_TOC16_LO:
5220         case R_PPC64_TOC16_HI:
5221         case R_PPC64_TOC16_HA:
5222         case R_PPC64_TOC16_LO_DS:
5223           sec->has_toc_reloc = 1;
5224           break;
5225
5226           /* This relocation describes the C++ object vtable hierarchy.
5227              Reconstruct it for later use during GC.  */
5228         case R_PPC64_GNU_VTINHERIT:
5229           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5230             return FALSE;
5231           break;
5232
5233           /* This relocation describes which C++ vtable entries are actually
5234              used.  Record for later use during GC.  */
5235         case R_PPC64_GNU_VTENTRY:
5236           BFD_ASSERT (h != NULL);
5237           if (h != NULL
5238               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5239             return FALSE;
5240           break;
5241
5242         case R_PPC64_REL14:
5243         case R_PPC64_REL14_BRTAKEN:
5244         case R_PPC64_REL14_BRNTAKEN:
5245           {
5246             asection *dest = NULL;
5247
5248             /* Heuristic: If jumping outside our section, chances are
5249                we are going to need a stub.  */
5250             if (h != NULL)
5251               {
5252                 /* If the sym is weak it may be overridden later, so
5253                    don't assume we know where a weak sym lives.  */
5254                 if (h->root.type == bfd_link_hash_defined)
5255                   dest = h->root.u.def.section;
5256               }
5257             else
5258               {
5259                 Elf_Internal_Sym *isym;
5260
5261                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5262                                               abfd, r_symndx);
5263                 if (isym == NULL)
5264                   return FALSE;
5265
5266                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5267               }
5268
5269             if (dest != sec)
5270               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5271           }
5272           /* Fall through.  */
5273
5274         case R_PPC64_REL24:
5275           if (h != NULL && ifunc == NULL)
5276             {
5277               /* We may need a .plt entry if the function this reloc
5278                  refers to is in a shared lib.  */
5279               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5280                 return FALSE;
5281               h->needs_plt = 1;
5282               if (h->root.root.string[0] == '.'
5283                   && h->root.root.string[1] != '\0')
5284                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5285               if (h == tga || h == dottga)
5286                 sec->has_tls_reloc = 1;
5287             }
5288           break;
5289
5290         case R_PPC64_TPREL64:
5291           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5292           if (!info->executable)
5293             info->flags |= DF_STATIC_TLS;
5294           goto dotlstoc;
5295
5296         case R_PPC64_DTPMOD64:
5297           if (rel + 1 < rel_end
5298               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5299               && rel[1].r_offset == rel->r_offset + 8)
5300             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5301           else
5302             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5303           goto dotlstoc;
5304
5305         case R_PPC64_DTPREL64:
5306           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5307           if (rel != relocs
5308               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5309               && rel[-1].r_offset == rel->r_offset - 8)
5310             /* This is the second reloc of a dtpmod, dtprel pair.
5311                Don't mark with TLS_DTPREL.  */
5312             goto dodyn;
5313
5314         dotlstoc:
5315           sec->has_tls_reloc = 1;
5316           if (h != NULL)
5317             {
5318               struct ppc_link_hash_entry *eh;
5319               eh = (struct ppc_link_hash_entry *) h;
5320               eh->tls_mask |= tls_type;
5321             }
5322           else
5323             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5324                                         rel->r_addend, tls_type))
5325               return FALSE;
5326
5327           ppc64_sec = ppc64_elf_section_data (sec);
5328           if (ppc64_sec->sec_type != sec_toc)
5329             {
5330               bfd_size_type amt;
5331
5332               /* One extra to simplify get_tls_mask.  */
5333               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5334               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5335               if (ppc64_sec->u.toc.symndx == NULL)
5336                 return FALSE;
5337               amt = sec->size * sizeof (bfd_vma) / 8;
5338               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5339               if (ppc64_sec->u.toc.add == NULL)
5340                 return FALSE;
5341               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5342               ppc64_sec->sec_type = sec_toc;
5343             }
5344           BFD_ASSERT (rel->r_offset % 8 == 0);
5345           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5346           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5347
5348           /* Mark the second slot of a GD or LD entry.
5349              -1 to indicate GD and -2 to indicate LD.  */
5350           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5351             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5352           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5353             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5354           goto dodyn;
5355
5356         case R_PPC64_TPREL16:
5357         case R_PPC64_TPREL16_LO:
5358         case R_PPC64_TPREL16_HI:
5359         case R_PPC64_TPREL16_HA:
5360         case R_PPC64_TPREL16_DS:
5361         case R_PPC64_TPREL16_LO_DS:
5362         case R_PPC64_TPREL16_HIGHER:
5363         case R_PPC64_TPREL16_HIGHERA:
5364         case R_PPC64_TPREL16_HIGHEST:
5365         case R_PPC64_TPREL16_HIGHESTA:
5366           if (info->shared)
5367             {
5368               if (!info->executable)
5369                 info->flags |= DF_STATIC_TLS;
5370               goto dodyn;
5371             }
5372           break;
5373
5374         case R_PPC64_ADDR64:
5375           if (opd_sym_map != NULL
5376               && rel + 1 < rel_end
5377               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5378             {
5379               if (h != NULL)
5380                 {
5381                   if (h->root.root.string[0] == '.'
5382                       && h->root.root.string[1] != 0
5383                       && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5384                     ;
5385                   else
5386                     ((struct ppc_link_hash_entry *) h)->is_func = 1;
5387                 }
5388               else
5389                 {
5390                   asection *s;
5391                   Elf_Internal_Sym *isym;
5392
5393                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5394                                                 abfd, r_symndx);
5395                   if (isym == NULL)
5396                     return FALSE;
5397
5398                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5399                   if (s != NULL && s != sec)
5400                     opd_sym_map[rel->r_offset / 8] = s;
5401                 }
5402             }
5403           /* Fall through.  */
5404
5405         case R_PPC64_REL30:
5406         case R_PPC64_REL32:
5407         case R_PPC64_REL64:
5408         case R_PPC64_ADDR14:
5409         case R_PPC64_ADDR14_BRNTAKEN:
5410         case R_PPC64_ADDR14_BRTAKEN:
5411         case R_PPC64_ADDR16:
5412         case R_PPC64_ADDR16_DS:
5413         case R_PPC64_ADDR16_HA:
5414         case R_PPC64_ADDR16_HI:
5415         case R_PPC64_ADDR16_HIGHER:
5416         case R_PPC64_ADDR16_HIGHERA:
5417         case R_PPC64_ADDR16_HIGHEST:
5418         case R_PPC64_ADDR16_HIGHESTA:
5419         case R_PPC64_ADDR16_LO:
5420         case R_PPC64_ADDR16_LO_DS:
5421         case R_PPC64_ADDR24:
5422         case R_PPC64_ADDR32:
5423         case R_PPC64_UADDR16:
5424         case R_PPC64_UADDR32:
5425         case R_PPC64_UADDR64:
5426         case R_PPC64_TOC:
5427           if (h != NULL && !info->shared)
5428             /* We may need a copy reloc.  */
5429             h->non_got_ref = 1;
5430
5431           /* Don't propagate .opd relocs.  */
5432           if (NO_OPD_RELOCS && opd_sym_map != NULL)
5433             break;
5434
5435           /* If we are creating a shared library, and this is a reloc
5436              against a global symbol, or a non PC relative reloc
5437              against a local symbol, then we need to copy the reloc
5438              into the shared library.  However, if we are linking with
5439              -Bsymbolic, we do not need to copy a reloc against a
5440              global symbol which is defined in an object we are
5441              including in the link (i.e., DEF_REGULAR is set).  At
5442              this point we have not seen all the input files, so it is
5443              possible that DEF_REGULAR is not set now but will be set
5444              later (it is never cleared).  In case of a weak definition,
5445              DEF_REGULAR may be cleared later by a strong definition in
5446              a shared library.  We account for that possibility below by
5447              storing information in the dyn_relocs field of the hash
5448              table entry.  A similar situation occurs when creating
5449              shared libraries and symbol visibility changes render the
5450              symbol local.
5451
5452              If on the other hand, we are creating an executable, we
5453              may need to keep relocations for symbols satisfied by a
5454              dynamic library if we manage to avoid copy relocs for the
5455              symbol.  */
5456         dodyn:
5457           if ((info->shared
5458                && (must_be_dyn_reloc (info, r_type)
5459                    || (h != NULL
5460                        && (!SYMBOLIC_BIND (info, h)
5461                            || h->root.type == bfd_link_hash_defweak
5462                            || !h->def_regular))))
5463               || (ELIMINATE_COPY_RELOCS
5464                   && !info->shared
5465                   && h != NULL
5466                   && (h->root.type == bfd_link_hash_defweak
5467                       || !h->def_regular))
5468               || (!info->shared
5469                   && ifunc != NULL))
5470             {
5471               /* We must copy these reloc types into the output file.
5472                  Create a reloc section in dynobj and make room for
5473                  this reloc.  */
5474               if (sreloc == NULL)
5475                 {
5476                   sreloc = _bfd_elf_make_dynamic_reloc_section
5477                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5478
5479                   if (sreloc == NULL)
5480                     return FALSE;
5481                 }
5482
5483               /* If this is a global symbol, we count the number of
5484                  relocations we need for this symbol.  */
5485               if (h != NULL)
5486                 {
5487                   struct elf_dyn_relocs *p;
5488                   struct elf_dyn_relocs **head;
5489
5490                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5491                   p = *head;
5492                   if (p == NULL || p->sec != sec)
5493                     {
5494                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5495                       if (p == NULL)
5496                         return FALSE;
5497                       p->next = *head;
5498                       *head = p;
5499                       p->sec = sec;
5500                       p->count = 0;
5501                       p->pc_count = 0;
5502                     }
5503                   p->count += 1;
5504                   if (!must_be_dyn_reloc (info, r_type))
5505                     p->pc_count += 1;
5506                 }
5507               else
5508                 {
5509                   /* Track dynamic relocs needed for local syms too.
5510                      We really need local syms available to do this
5511                      easily.  Oh well.  */
5512                   struct ppc_dyn_relocs *p;
5513                   struct ppc_dyn_relocs **head;
5514                   bfd_boolean is_ifunc;
5515                   asection *s;
5516                   void *vpp;
5517                   Elf_Internal_Sym *isym;
5518
5519                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5520                                                 abfd, r_symndx);
5521                   if (isym == NULL)
5522                     return FALSE;
5523
5524                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5525                   if (s == NULL)
5526                     s = sec;
5527
5528                   vpp = &elf_section_data (s)->local_dynrel;
5529                   head = (struct ppc_dyn_relocs **) vpp;
5530                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5531                   p = *head;
5532                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5533                     p = p->next;
5534                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5535                     {
5536                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5537                       if (p == NULL)
5538                         return FALSE;
5539                       p->next = *head;
5540                       *head = p;
5541                       p->sec = sec;
5542                       p->ifunc = is_ifunc;
5543                       p->count = 0;
5544                     }
5545                   p->count += 1;
5546                 }
5547             }
5548           break;
5549
5550         default:
5551           break;
5552         }
5553     }
5554
5555   return TRUE;
5556 }
5557
5558 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
5559    of the code entry point, and its section.  */
5560
5561 static bfd_vma
5562 opd_entry_value (asection *opd_sec,
5563                  bfd_vma offset,
5564                  asection **code_sec,
5565                  bfd_vma *code_off,
5566                  bfd_boolean in_code_sec)
5567 {
5568   bfd *opd_bfd = opd_sec->owner;
5569   Elf_Internal_Rela *relocs;
5570   Elf_Internal_Rela *lo, *hi, *look;
5571   bfd_vma val;
5572
5573   /* No relocs implies we are linking a --just-symbols object, or looking
5574      at a final linked executable with addr2line or somesuch.  */
5575   if (opd_sec->reloc_count == 0)
5576     {
5577       char buf[8];
5578
5579       if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
5580         return (bfd_vma) -1;
5581
5582       val = bfd_get_64 (opd_bfd, buf);
5583       if (code_sec != NULL)
5584         {
5585           asection *sec, *likely = NULL;
5586
5587           if (in_code_sec)
5588             {
5589               sec = *code_sec;
5590               if (sec->vma <= val
5591                   && val < sec->vma + sec->size)
5592                 likely = sec;
5593               else
5594                 val = -1;
5595             }
5596           else
5597             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5598               if (sec->vma <= val
5599                   && (sec->flags & SEC_LOAD) != 0
5600                   && (sec->flags & SEC_ALLOC) != 0)
5601                 likely = sec;
5602           if (likely != NULL)
5603             {
5604               *code_sec = likely;
5605               if (code_off != NULL)
5606                 *code_off = val - likely->vma;
5607             }
5608         }
5609       return val;
5610     }
5611
5612   BFD_ASSERT (is_ppc64_elf (opd_bfd));
5613
5614   relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
5615   if (relocs == NULL)
5616     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5617
5618   /* Go find the opd reloc at the sym address.  */
5619   lo = relocs;
5620   BFD_ASSERT (lo != NULL);
5621   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5622   val = (bfd_vma) -1;
5623   while (lo < hi)
5624     {
5625       look = lo + (hi - lo) / 2;
5626       if (look->r_offset < offset)
5627         lo = look + 1;
5628       else if (look->r_offset > offset)
5629         hi = look;
5630       else
5631         {
5632           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5633
5634           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5635               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5636             {
5637               unsigned long symndx = ELF64_R_SYM (look->r_info);
5638               asection *sec;
5639
5640               if (symndx < symtab_hdr->sh_info
5641                   || elf_sym_hashes (opd_bfd) == NULL)
5642                 {
5643                   Elf_Internal_Sym *sym;
5644
5645                   sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5646                   if (sym == NULL)
5647                     {
5648                       size_t symcnt = symtab_hdr->sh_info;
5649                       if (elf_sym_hashes (opd_bfd) == NULL)
5650                         symcnt = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
5651                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, symcnt,
5652                                                   0, NULL, NULL, NULL);
5653                       if (sym == NULL)
5654                         break;
5655                       symtab_hdr->contents = (bfd_byte *) sym;
5656                     }
5657
5658                   sym += symndx;
5659                   val = sym->st_value;
5660                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5661                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5662                 }
5663               else
5664                 {
5665                   struct elf_link_hash_entry **sym_hashes;
5666                   struct elf_link_hash_entry *rh;
5667
5668                   sym_hashes = elf_sym_hashes (opd_bfd);
5669                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
5670                   if (rh != NULL)
5671                     {
5672                       rh = elf_follow_link (rh);
5673                       BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5674                                   || rh->root.type == bfd_link_hash_defweak);
5675                       val = rh->root.u.def.value;
5676                       sec = rh->root.u.def.section;
5677                     }
5678                   else
5679                     {
5680                       /* Handle the odd case where we can be called
5681                          during bfd_elf_link_add_symbols before the
5682                          symbol hashes have been fully populated.  */
5683                       Elf_Internal_Sym *sym;
5684
5685                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, 1,
5686                                                   symndx, NULL, NULL, NULL);
5687                       if (sym == NULL)
5688                         break;
5689
5690                       val = sym->st_value;
5691                       sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5692                       free (sym);
5693                     }
5694                 }
5695               val += look->r_addend;
5696               if (code_off != NULL)
5697                 *code_off = val;
5698               if (code_sec != NULL)
5699                 {
5700                   if (in_code_sec && *code_sec != sec)
5701                     return -1;
5702                   else
5703                     *code_sec = sec;
5704                 }
5705               if (sec != NULL && sec->output_section != NULL)
5706                 val += sec->output_section->vma + sec->output_offset;
5707             }
5708           break;
5709         }
5710     }
5711
5712   return val;
5713 }
5714
5715 /* If the ELF symbol SYM might be a function in SEC, return the
5716    function size and set *CODE_OFF to the function's entry point,
5717    otherwise return zero.  */
5718
5719 static bfd_size_type
5720 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5721                               bfd_vma *code_off)
5722 {
5723   bfd_size_type size;
5724
5725   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5726                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5727     return 0;
5728
5729   size = 0;
5730   if (!(sym->flags & BSF_SYNTHETIC))
5731     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5732
5733   if (strcmp (sym->section->name, ".opd") == 0)
5734     {
5735       if (opd_entry_value (sym->section, sym->value,
5736                            &sec, code_off, TRUE) == (bfd_vma) -1)
5737         return 0;
5738       /* An old ABI binary with dot-syms has a size of 24 on the .opd
5739          symbol.  This size has nothing to do with the code size of the
5740          function, which is what we're supposed to return, but the
5741          code size isn't available without looking up the dot-sym.
5742          However, doing that would be a waste of time particularly
5743          since elf_find_function will look at the dot-sym anyway.
5744          Now, elf_find_function will keep the largest size of any
5745          function sym found at the code address of interest, so return
5746          1 here to avoid it incorrectly caching a larger function size
5747          for a small function.  This does mean we return the wrong
5748          size for a new-ABI function of size 24, but all that does is
5749          disable caching for such functions.  */
5750       if (size == 24)
5751         size = 1;
5752     }
5753   else
5754     {
5755       if (sym->section != sec)
5756         return 0;
5757       *code_off = sym->value;
5758     }
5759   if (size == 0)
5760     size = 1;
5761   return size;
5762 }
5763
5764 /* Return true if symbol is defined in a regular object file.  */
5765
5766 static bfd_boolean
5767 is_static_defined (struct elf_link_hash_entry *h)
5768 {
5769   return ((h->root.type == bfd_link_hash_defined
5770            || h->root.type == bfd_link_hash_defweak)
5771           && h->root.u.def.section != NULL
5772           && h->root.u.def.section->output_section != NULL);
5773 }
5774
5775 /* If FDH is a function descriptor symbol, return the associated code
5776    entry symbol if it is defined.  Return NULL otherwise.  */
5777
5778 static struct ppc_link_hash_entry *
5779 defined_code_entry (struct ppc_link_hash_entry *fdh)
5780 {
5781   if (fdh->is_func_descriptor)
5782     {
5783       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5784       if (fh->elf.root.type == bfd_link_hash_defined
5785           || fh->elf.root.type == bfd_link_hash_defweak)
5786         return fh;
5787     }
5788   return NULL;
5789 }
5790
5791 /* If FH is a function code entry symbol, return the associated
5792    function descriptor symbol if it is defined.  Return NULL otherwise.  */
5793
5794 static struct ppc_link_hash_entry *
5795 defined_func_desc (struct ppc_link_hash_entry *fh)
5796 {
5797   if (fh->oh != NULL
5798       && fh->oh->is_func_descriptor)
5799     {
5800       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5801       if (fdh->elf.root.type == bfd_link_hash_defined
5802           || fdh->elf.root.type == bfd_link_hash_defweak)
5803         return fdh;
5804     }
5805   return NULL;
5806 }
5807
5808 /* Mark all our entry sym sections, both opd and code section.  */
5809
5810 static void
5811 ppc64_elf_gc_keep (struct bfd_link_info *info)
5812 {
5813   struct ppc_link_hash_table *htab = ppc_hash_table (info);
5814   struct bfd_sym_chain *sym;
5815
5816   if (htab == NULL)
5817     return;
5818
5819   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5820     {
5821       struct ppc_link_hash_entry *eh, *fh;
5822       asection *sec;
5823
5824       eh = (struct ppc_link_hash_entry *)
5825         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5826       if (eh == NULL)
5827         continue;
5828       if (eh->elf.root.type != bfd_link_hash_defined
5829           && eh->elf.root.type != bfd_link_hash_defweak)
5830         continue;
5831
5832       fh = defined_code_entry (eh);
5833       if (fh != NULL)
5834         {
5835           sec = fh->elf.root.u.def.section;
5836           sec->flags |= SEC_KEEP;
5837         }
5838       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5839                && opd_entry_value (eh->elf.root.u.def.section,
5840                                    eh->elf.root.u.def.value,
5841                                    &sec, NULL, FALSE) != (bfd_vma) -1)
5842         sec->flags |= SEC_KEEP;
5843
5844       sec = eh->elf.root.u.def.section;
5845       sec->flags |= SEC_KEEP;
5846     }
5847 }
5848
5849 /* Mark sections containing dynamically referenced symbols.  When
5850    building shared libraries, we must assume that any visible symbol is
5851    referenced.  */
5852
5853 static bfd_boolean
5854 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5855 {
5856   struct bfd_link_info *info = (struct bfd_link_info *) inf;
5857   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5858   struct ppc_link_hash_entry *fdh;
5859
5860   /* Dynamic linking info is on the func descriptor sym.  */
5861   fdh = defined_func_desc (eh);
5862   if (fdh != NULL)
5863     eh = fdh;
5864
5865   if ((eh->elf.root.type == bfd_link_hash_defined
5866        || eh->elf.root.type == bfd_link_hash_defweak)
5867       && (eh->elf.ref_dynamic
5868           || (!info->executable
5869               && eh->elf.def_regular
5870               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5871               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5872               && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
5873                   || !bfd_hide_sym_by_version (info->version_info,
5874                                                eh->elf.root.root.string)))))
5875     {
5876       asection *code_sec;
5877       struct ppc_link_hash_entry *fh;
5878
5879       eh->elf.root.u.def.section->flags |= SEC_KEEP;
5880
5881       /* Function descriptor syms cause the associated
5882          function code sym section to be marked.  */
5883       fh = defined_code_entry (eh);
5884       if (fh != NULL)
5885         {
5886           code_sec = fh->elf.root.u.def.section;
5887           code_sec->flags |= SEC_KEEP;
5888         }
5889       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5890                && opd_entry_value (eh->elf.root.u.def.section,
5891                                    eh->elf.root.u.def.value,
5892                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
5893         code_sec->flags |= SEC_KEEP;
5894     }
5895
5896   return TRUE;
5897 }
5898
5899 /* Return the section that should be marked against GC for a given
5900    relocation.  */
5901
5902 static asection *
5903 ppc64_elf_gc_mark_hook (asection *sec,
5904                         struct bfd_link_info *info,
5905                         Elf_Internal_Rela *rel,
5906                         struct elf_link_hash_entry *h,
5907                         Elf_Internal_Sym *sym)
5908 {
5909   asection *rsec;
5910
5911   /* Syms return NULL if we're marking .opd, so we avoid marking all
5912      function sections, as all functions are referenced in .opd.  */
5913   rsec = NULL;
5914   if (get_opd_info (sec) != NULL)
5915     return rsec;
5916
5917   if (h != NULL)
5918     {
5919       enum elf_ppc64_reloc_type r_type;
5920       struct ppc_link_hash_entry *eh, *fh, *fdh;
5921
5922       r_type = ELF64_R_TYPE (rel->r_info);
5923       switch (r_type)
5924         {
5925         case R_PPC64_GNU_VTINHERIT:
5926         case R_PPC64_GNU_VTENTRY:
5927           break;
5928
5929         default:
5930           switch (h->root.type)
5931             {
5932             case bfd_link_hash_defined:
5933             case bfd_link_hash_defweak:
5934               eh = (struct ppc_link_hash_entry *) h;
5935               fdh = defined_func_desc (eh);
5936               if (fdh != NULL)
5937                 eh = fdh;
5938
5939               /* Function descriptor syms cause the associated
5940                  function code sym section to be marked.  */
5941               fh = defined_code_entry (eh);
5942               if (fh != NULL)
5943                 {
5944                   /* They also mark their opd section.  */
5945                   eh->elf.root.u.def.section->gc_mark = 1;
5946
5947                   rsec = fh->elf.root.u.def.section;
5948                 }
5949               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5950                        && opd_entry_value (eh->elf.root.u.def.section,
5951                                            eh->elf.root.u.def.value,
5952                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
5953                 eh->elf.root.u.def.section->gc_mark = 1;
5954               else
5955                 rsec = h->root.u.def.section;
5956               break;
5957
5958             case bfd_link_hash_common:
5959               rsec = h->root.u.c.p->section;
5960               break;
5961
5962             default:
5963               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5964             }
5965         }
5966     }
5967   else
5968     {
5969       struct _opd_sec_data *opd;
5970
5971       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5972       opd = get_opd_info (rsec);
5973       if (opd != NULL && opd->func_sec != NULL)
5974         {
5975           rsec->gc_mark = 1;
5976
5977           rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
5978         }
5979     }
5980
5981   return rsec;
5982 }
5983
5984 /* Update the .got, .plt. and dynamic reloc reference counts for the
5985    section being removed.  */
5986
5987 static bfd_boolean
5988 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5989                          asection *sec, const Elf_Internal_Rela *relocs)
5990 {
5991   struct ppc_link_hash_table *htab;
5992   Elf_Internal_Shdr *symtab_hdr;
5993   struct elf_link_hash_entry **sym_hashes;
5994   struct got_entry **local_got_ents;
5995   const Elf_Internal_Rela *rel, *relend;
5996
5997   if (info->relocatable)
5998     return TRUE;
5999
6000   if ((sec->flags & SEC_ALLOC) == 0)
6001     return TRUE;
6002
6003   elf_section_data (sec)->local_dynrel = NULL;
6004
6005   htab = ppc_hash_table (info);
6006   if (htab == NULL)
6007     return FALSE;
6008
6009   symtab_hdr = &elf_symtab_hdr (abfd);
6010   sym_hashes = elf_sym_hashes (abfd);
6011   local_got_ents = elf_local_got_ents (abfd);
6012
6013   relend = relocs + sec->reloc_count;
6014   for (rel = relocs; rel < relend; rel++)
6015     {
6016       unsigned long r_symndx;
6017       enum elf_ppc64_reloc_type r_type;
6018       struct elf_link_hash_entry *h = NULL;
6019       unsigned char tls_type = 0;
6020
6021       r_symndx = ELF64_R_SYM (rel->r_info);
6022       r_type = ELF64_R_TYPE (rel->r_info);
6023       if (r_symndx >= symtab_hdr->sh_info)
6024         {
6025           struct ppc_link_hash_entry *eh;
6026           struct elf_dyn_relocs **pp;
6027           struct elf_dyn_relocs *p;
6028
6029           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6030           h = elf_follow_link (h);
6031           eh = (struct ppc_link_hash_entry *) h;
6032
6033           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6034             if (p->sec == sec)
6035               {
6036                 /* Everything must go for SEC.  */
6037                 *pp = p->next;
6038                 break;
6039               }
6040         }
6041
6042       if (is_branch_reloc (r_type))
6043         {
6044           struct plt_entry **ifunc = NULL;
6045           if (h != NULL)
6046             {
6047               if (h->type == STT_GNU_IFUNC)
6048                 ifunc = &h->plt.plist;
6049             }
6050           else if (local_got_ents != NULL)
6051             {
6052               struct plt_entry **local_plt = (struct plt_entry **)
6053                 (local_got_ents + symtab_hdr->sh_info);
6054               unsigned char *local_got_tls_masks = (unsigned char *)
6055                 (local_plt + symtab_hdr->sh_info);
6056               if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6057                 ifunc = local_plt + r_symndx;
6058             }
6059           if (ifunc != NULL)
6060             {
6061               struct plt_entry *ent;
6062
6063               for (ent = *ifunc; ent != NULL; ent = ent->next)
6064                 if (ent->addend == rel->r_addend)
6065                   break;
6066               if (ent == NULL)
6067                 abort ();
6068               if (ent->plt.refcount > 0)
6069                 ent->plt.refcount -= 1;
6070               continue;
6071             }
6072         }
6073
6074       switch (r_type)
6075         {
6076         case R_PPC64_GOT_TLSLD16:
6077         case R_PPC64_GOT_TLSLD16_LO:
6078         case R_PPC64_GOT_TLSLD16_HI:
6079         case R_PPC64_GOT_TLSLD16_HA:
6080           tls_type = TLS_TLS | TLS_LD;
6081           goto dogot;
6082
6083         case R_PPC64_GOT_TLSGD16:
6084         case R_PPC64_GOT_TLSGD16_LO:
6085         case R_PPC64_GOT_TLSGD16_HI:
6086         case R_PPC64_GOT_TLSGD16_HA:
6087           tls_type = TLS_TLS | TLS_GD;
6088           goto dogot;
6089
6090         case R_PPC64_GOT_TPREL16_DS:
6091         case R_PPC64_GOT_TPREL16_LO_DS:
6092         case R_PPC64_GOT_TPREL16_HI:
6093         case R_PPC64_GOT_TPREL16_HA:
6094           tls_type = TLS_TLS | TLS_TPREL;
6095           goto dogot;
6096
6097         case R_PPC64_GOT_DTPREL16_DS:
6098         case R_PPC64_GOT_DTPREL16_LO_DS:
6099         case R_PPC64_GOT_DTPREL16_HI:
6100         case R_PPC64_GOT_DTPREL16_HA:
6101           tls_type = TLS_TLS | TLS_DTPREL;
6102           goto dogot;
6103
6104         case R_PPC64_GOT16:
6105         case R_PPC64_GOT16_DS:
6106         case R_PPC64_GOT16_HA:
6107         case R_PPC64_GOT16_HI:
6108         case R_PPC64_GOT16_LO:
6109         case R_PPC64_GOT16_LO_DS:
6110         dogot:
6111           {
6112             struct got_entry *ent;
6113
6114             if (h != NULL)
6115               ent = h->got.glist;
6116             else
6117               ent = local_got_ents[r_symndx];
6118
6119             for (; ent != NULL; ent = ent->next)
6120               if (ent->addend == rel->r_addend
6121                   && ent->owner == abfd
6122                   && ent->tls_type == tls_type)
6123                 break;
6124             if (ent == NULL)
6125               abort ();
6126             if (ent->got.refcount > 0)
6127               ent->got.refcount -= 1;
6128           }
6129           break;
6130
6131         case R_PPC64_PLT16_HA:
6132         case R_PPC64_PLT16_HI:
6133         case R_PPC64_PLT16_LO:
6134         case R_PPC64_PLT32:
6135         case R_PPC64_PLT64:
6136         case R_PPC64_REL14:
6137         case R_PPC64_REL14_BRNTAKEN:
6138         case R_PPC64_REL14_BRTAKEN:
6139         case R_PPC64_REL24:
6140           if (h != NULL)
6141             {
6142               struct plt_entry *ent;
6143
6144               for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6145                 if (ent->addend == rel->r_addend)
6146                   break;
6147               if (ent != NULL && ent->plt.refcount > 0)
6148                 ent->plt.refcount -= 1;
6149             }
6150           break;
6151
6152         default:
6153           break;
6154         }
6155     }
6156   return TRUE;
6157 }
6158
6159 /* The maximum size of .sfpr.  */
6160 #define SFPR_MAX (218*4)
6161
6162 struct sfpr_def_parms
6163 {
6164   const char name[12];
6165   unsigned char lo, hi;
6166   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6167   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6168 };
6169
6170 /* Auto-generate _save*, _rest* functions in .sfpr.  */
6171
6172 static bfd_boolean
6173 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6174 {
6175   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6176   unsigned int i;
6177   size_t len = strlen (parm->name);
6178   bfd_boolean writing = FALSE;
6179   char sym[16];
6180
6181   if (htab == NULL)
6182     return FALSE;
6183
6184   memcpy (sym, parm->name, len);
6185   sym[len + 2] = 0;
6186
6187   for (i = parm->lo; i <= parm->hi; i++)
6188     {
6189       struct elf_link_hash_entry *h;
6190
6191       sym[len + 0] = i / 10 + '0';
6192       sym[len + 1] = i % 10 + '0';
6193       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6194       if (h != NULL
6195           && !h->def_regular)
6196         {
6197           h->root.type = bfd_link_hash_defined;
6198           h->root.u.def.section = htab->sfpr;
6199           h->root.u.def.value = htab->sfpr->size;
6200           h->type = STT_FUNC;
6201           h->def_regular = 1;
6202           _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6203           writing = TRUE;
6204           if (htab->sfpr->contents == NULL)
6205             {
6206               htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6207               if (htab->sfpr->contents == NULL)
6208                 return FALSE;
6209             }
6210         }
6211       if (writing)
6212         {
6213           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6214           if (i != parm->hi)
6215             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6216           else
6217             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6218           htab->sfpr->size = p - htab->sfpr->contents;
6219         }
6220     }
6221
6222   return TRUE;
6223 }
6224
6225 static bfd_byte *
6226 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6227 {
6228   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6229   return p + 4;
6230 }
6231
6232 static bfd_byte *
6233 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6234 {
6235   p = savegpr0 (abfd, p, r);
6236   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6237   p = p + 4;
6238   bfd_put_32 (abfd, BLR, p);
6239   return p + 4;
6240 }
6241
6242 static bfd_byte *
6243 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6244 {
6245   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6246   return p + 4;
6247 }
6248
6249 static bfd_byte *
6250 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6251 {
6252   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6253   p = p + 4;
6254   p = restgpr0 (abfd, p, r);
6255   bfd_put_32 (abfd, MTLR_R0, p);
6256   p = p + 4;
6257   if (r == 29)
6258     {
6259       p = restgpr0 (abfd, p, 30);
6260       p = restgpr0 (abfd, p, 31);
6261     }
6262   bfd_put_32 (abfd, BLR, p);
6263   return p + 4;
6264 }
6265
6266 static bfd_byte *
6267 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6268 {
6269   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6270   return p + 4;
6271 }
6272
6273 static bfd_byte *
6274 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6275 {
6276   p = savegpr1 (abfd, p, r);
6277   bfd_put_32 (abfd, BLR, p);
6278   return p + 4;
6279 }
6280
6281 static bfd_byte *
6282 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6283 {
6284   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6285   return p + 4;
6286 }
6287
6288 static bfd_byte *
6289 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6290 {
6291   p = restgpr1 (abfd, p, r);
6292   bfd_put_32 (abfd, BLR, p);
6293   return p + 4;
6294 }
6295
6296 static bfd_byte *
6297 savefpr (bfd *abfd, bfd_byte *p, int r)
6298 {
6299   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6300   return p + 4;
6301 }
6302
6303 static bfd_byte *
6304 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6305 {
6306   p = savefpr (abfd, p, r);
6307   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6308   p = p + 4;
6309   bfd_put_32 (abfd, BLR, p);
6310   return p + 4;
6311 }
6312
6313 static bfd_byte *
6314 restfpr (bfd *abfd, bfd_byte *p, int r)
6315 {
6316   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6317   return p + 4;
6318 }
6319
6320 static bfd_byte *
6321 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6322 {
6323   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6324   p = p + 4;
6325   p = restfpr (abfd, p, r);
6326   bfd_put_32 (abfd, MTLR_R0, p);
6327   p = p + 4;
6328   if (r == 29)
6329     {
6330       p = restfpr (abfd, p, 30);
6331       p = restfpr (abfd, p, 31);
6332     }
6333   bfd_put_32 (abfd, BLR, p);
6334   return p + 4;
6335 }
6336
6337 static bfd_byte *
6338 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6339 {
6340   p = savefpr (abfd, p, r);
6341   bfd_put_32 (abfd, BLR, p);
6342   return p + 4;
6343 }
6344
6345 static bfd_byte *
6346 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6347 {
6348   p = restfpr (abfd, p, r);
6349   bfd_put_32 (abfd, BLR, p);
6350   return p + 4;
6351 }
6352
6353 static bfd_byte *
6354 savevr (bfd *abfd, bfd_byte *p, int r)
6355 {
6356   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6357   p = p + 4;
6358   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6359   return p + 4;
6360 }
6361
6362 static bfd_byte *
6363 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6364 {
6365   p = savevr (abfd, p, r);
6366   bfd_put_32 (abfd, BLR, p);
6367   return p + 4;
6368 }
6369
6370 static bfd_byte *
6371 restvr (bfd *abfd, bfd_byte *p, int r)
6372 {
6373   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6374   p = p + 4;
6375   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6376   return p + 4;
6377 }
6378
6379 static bfd_byte *
6380 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6381 {
6382   p = restvr (abfd, p, r);
6383   bfd_put_32 (abfd, BLR, p);
6384   return p + 4;
6385 }
6386
6387 /* Called via elf_link_hash_traverse to transfer dynamic linking
6388    information on function code symbol entries to their corresponding
6389    function descriptor symbol entries.  */
6390
6391 static bfd_boolean
6392 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6393 {
6394   struct bfd_link_info *info;
6395   struct ppc_link_hash_table *htab;
6396   struct plt_entry *ent;
6397   struct ppc_link_hash_entry *fh;
6398   struct ppc_link_hash_entry *fdh;
6399   bfd_boolean force_local;
6400
6401   fh = (struct ppc_link_hash_entry *) h;
6402   if (fh->elf.root.type == bfd_link_hash_indirect)
6403     return TRUE;
6404
6405   info = inf;
6406   htab = ppc_hash_table (info);
6407   if (htab == NULL)
6408     return FALSE;
6409
6410   /* Resolve undefined references to dot-symbols as the value
6411      in the function descriptor, if we have one in a regular object.
6412      This is to satisfy cases like ".quad .foo".  Calls to functions
6413      in dynamic objects are handled elsewhere.  */
6414   if (fh->elf.root.type == bfd_link_hash_undefweak
6415       && fh->was_undefined
6416       && (fdh = defined_func_desc (fh)) != NULL
6417       && get_opd_info (fdh->elf.root.u.def.section) != NULL
6418       && opd_entry_value (fdh->elf.root.u.def.section,
6419                           fdh->elf.root.u.def.value,
6420                           &fh->elf.root.u.def.section,
6421                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6422     {
6423       fh->elf.root.type = fdh->elf.root.type;
6424       fh->elf.forced_local = 1;
6425       fh->elf.def_regular = fdh->elf.def_regular;
6426       fh->elf.def_dynamic = fdh->elf.def_dynamic;
6427     }
6428
6429   /* If this is a function code symbol, transfer dynamic linking
6430      information to the function descriptor symbol.  */
6431   if (!fh->is_func)
6432     return TRUE;
6433
6434   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6435     if (ent->plt.refcount > 0)
6436       break;
6437   if (ent == NULL
6438       || fh->elf.root.root.string[0] != '.'
6439       || fh->elf.root.root.string[1] == '\0')
6440     return TRUE;
6441
6442   /* Find the corresponding function descriptor symbol.  Create it
6443      as undefined if necessary.  */
6444
6445   fdh = lookup_fdh (fh, htab);
6446   if (fdh == NULL
6447       && !info->executable
6448       && (fh->elf.root.type == bfd_link_hash_undefined
6449           || fh->elf.root.type == bfd_link_hash_undefweak))
6450     {
6451       fdh = make_fdh (info, fh);
6452       if (fdh == NULL)
6453         return FALSE;
6454     }
6455
6456   /* Fake function descriptors are made undefweak.  If the function
6457      code symbol is strong undefined, make the fake sym the same.
6458      If the function code symbol is defined, then force the fake
6459      descriptor local;  We can't support overriding of symbols in a
6460      shared library on a fake descriptor.  */
6461
6462   if (fdh != NULL
6463       && fdh->fake
6464       && fdh->elf.root.type == bfd_link_hash_undefweak)
6465     {
6466       if (fh->elf.root.type == bfd_link_hash_undefined)
6467         {
6468           fdh->elf.root.type = bfd_link_hash_undefined;
6469           bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6470         }
6471       else if (fh->elf.root.type == bfd_link_hash_defined
6472                || fh->elf.root.type == bfd_link_hash_defweak)
6473         {
6474           _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6475         }
6476     }
6477
6478   if (fdh != NULL
6479       && !fdh->elf.forced_local
6480       && (!info->executable
6481           || fdh->elf.def_dynamic
6482           || fdh->elf.ref_dynamic
6483           || (fdh->elf.root.type == bfd_link_hash_undefweak
6484               && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6485     {
6486       if (fdh->elf.dynindx == -1)
6487         if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6488           return FALSE;
6489       fdh->elf.ref_regular |= fh->elf.ref_regular;
6490       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6491       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6492       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6493       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6494         {
6495           move_plt_plist (fh, fdh);
6496           fdh->elf.needs_plt = 1;
6497         }
6498       fdh->is_func_descriptor = 1;
6499       fdh->oh = fh;
6500       fh->oh = fdh;
6501     }
6502
6503   /* Now that the info is on the function descriptor, clear the
6504      function code sym info.  Any function code syms for which we
6505      don't have a definition in a regular file, we force local.
6506      This prevents a shared library from exporting syms that have
6507      been imported from another library.  Function code syms that
6508      are really in the library we must leave global to prevent the
6509      linker dragging in a definition from a static library.  */
6510   force_local = (!fh->elf.def_regular
6511                  || fdh == NULL
6512                  || !fdh->elf.def_regular
6513                  || fdh->elf.forced_local);
6514   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6515
6516   return TRUE;
6517 }
6518
6519 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
6520    this hook to a) provide some gcc support functions, and b) transfer
6521    dynamic linking information gathered so far on function code symbol
6522    entries, to their corresponding function descriptor symbol entries.  */
6523
6524 static bfd_boolean
6525 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6526                             struct bfd_link_info *info)
6527 {
6528   struct ppc_link_hash_table *htab;
6529   unsigned int i;
6530   static const struct sfpr_def_parms funcs[] =
6531     {
6532       { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6533       { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6534       { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6535       { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6536       { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6537       { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6538       { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6539       { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6540       { "._savef", 14, 31, savefpr, savefpr1_tail },
6541       { "._restf", 14, 31, restfpr, restfpr1_tail },
6542       { "_savevr_", 20, 31, savevr, savevr_tail },
6543       { "_restvr_", 20, 31, restvr, restvr_tail }
6544     };
6545
6546   htab = ppc_hash_table (info);
6547   if (htab == NULL)
6548     return FALSE;
6549
6550   if (!info->relocatable
6551       && htab->elf.hgot != NULL)
6552     _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6553
6554   if (htab->sfpr == NULL)
6555     /* We don't have any relocs.  */
6556     return TRUE;
6557
6558   /* Provide any missing _save* and _rest* functions.  */
6559   htab->sfpr->size = 0;
6560   if (!info->relocatable)
6561     for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6562       if (!sfpr_define (info, &funcs[i]))
6563         return FALSE;
6564
6565   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6566
6567   if (htab->sfpr->size == 0)
6568     htab->sfpr->flags |= SEC_EXCLUDE;
6569
6570   return TRUE;
6571 }
6572
6573 /* Adjust a symbol defined by a dynamic object and referenced by a
6574    regular object.  The current definition is in some section of the
6575    dynamic object, but we're not including those sections.  We have to
6576    change the definition to something the rest of the link can
6577    understand.  */
6578
6579 static bfd_boolean
6580 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6581                                  struct elf_link_hash_entry *h)
6582 {
6583   struct ppc_link_hash_table *htab;
6584   asection *s;
6585
6586   htab = ppc_hash_table (info);
6587   if (htab == NULL)
6588     return FALSE;
6589
6590   /* Deal with function syms.  */
6591   if (h->type == STT_FUNC
6592       || h->type == STT_GNU_IFUNC
6593       || h->needs_plt)
6594     {
6595       /* Clear procedure linkage table information for any symbol that
6596          won't need a .plt entry.  */
6597       struct plt_entry *ent;
6598       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6599         if (ent->plt.refcount > 0)
6600           break;
6601       if (ent == NULL
6602           || (h->type != STT_GNU_IFUNC
6603               && (SYMBOL_CALLS_LOCAL (info, h)
6604                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6605                       && h->root.type == bfd_link_hash_undefweak))))
6606         {
6607           h->plt.plist = NULL;
6608           h->needs_plt = 0;
6609         }
6610     }
6611   else
6612     h->plt.plist = NULL;
6613
6614   /* If this is a weak symbol, and there is a real definition, the
6615      processor independent code will have arranged for us to see the
6616      real definition first, and we can just use the same value.  */
6617   if (h->u.weakdef != NULL)
6618     {
6619       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6620                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
6621       h->root.u.def.section = h->u.weakdef->root.u.def.section;
6622       h->root.u.def.value = h->u.weakdef->root.u.def.value;
6623       if (ELIMINATE_COPY_RELOCS)
6624         h->non_got_ref = h->u.weakdef->non_got_ref;
6625       return TRUE;
6626     }
6627
6628   /* If we are creating a shared library, we must presume that the
6629      only references to the symbol are via the global offset table.
6630      For such cases we need not do anything here; the relocations will
6631      be handled correctly by relocate_section.  */
6632   if (info->shared)
6633     return TRUE;
6634
6635   /* If there are no references to this symbol that do not use the
6636      GOT, we don't need to generate a copy reloc.  */
6637   if (!h->non_got_ref)
6638     return TRUE;
6639
6640   /* Don't generate a copy reloc for symbols defined in the executable.  */
6641   if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6642     return TRUE;
6643
6644   if (ELIMINATE_COPY_RELOCS)
6645     {
6646       struct ppc_link_hash_entry * eh;
6647       struct elf_dyn_relocs *p;
6648
6649       eh = (struct ppc_link_hash_entry *) h;
6650       for (p = eh->dyn_relocs; p != NULL; p = p->next)
6651         {
6652           s = p->sec->output_section;
6653           if (s != NULL && (s->flags & SEC_READONLY) != 0)
6654             break;
6655         }
6656
6657       /* If we didn't find any dynamic relocs in read-only sections, then
6658          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
6659       if (p == NULL)
6660         {
6661           h->non_got_ref = 0;
6662           return TRUE;
6663         }
6664     }
6665
6666   if (h->plt.plist != NULL)
6667     {
6668       /* We should never get here, but unfortunately there are versions
6669          of gcc out there that improperly (for this ABI) put initialized
6670          function pointers, vtable refs and suchlike in read-only
6671          sections.  Allow them to proceed, but warn that this might
6672          break at runtime.  */
6673       info->callbacks->einfo
6674         (_("%P: copy reloc against `%T' requires lazy plt linking; "
6675            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6676          h->root.root.string);
6677     }
6678
6679   /* This is a reference to a symbol defined by a dynamic object which
6680      is not a function.  */
6681
6682   /* We must allocate the symbol in our .dynbss section, which will
6683      become part of the .bss section of the executable.  There will be
6684      an entry for this symbol in the .dynsym section.  The dynamic
6685      object will contain position independent code, so all references
6686      from the dynamic object to this symbol will go through the global
6687      offset table.  The dynamic linker will use the .dynsym entry to
6688      determine the address it must put in the global offset table, so
6689      both the dynamic object and the regular object will refer to the
6690      same memory location for the variable.  */
6691
6692   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6693      to copy the initial value out of the dynamic object and into the
6694      runtime process image.  We need to remember the offset into the
6695      .rela.bss section we are going to use.  */
6696   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6697     {
6698       htab->relbss->size += sizeof (Elf64_External_Rela);
6699       h->needs_copy = 1;
6700     }
6701
6702   s = htab->dynbss;
6703
6704   return _bfd_elf_adjust_dynamic_copy (h, s);
6705 }
6706
6707 /* If given a function descriptor symbol, hide both the function code
6708    sym and the descriptor.  */
6709 static void
6710 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6711                        struct elf_link_hash_entry *h,
6712                        bfd_boolean force_local)
6713 {
6714   struct ppc_link_hash_entry *eh;
6715   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6716
6717   eh = (struct ppc_link_hash_entry *) h;
6718   if (eh->is_func_descriptor)
6719     {
6720       struct ppc_link_hash_entry *fh = eh->oh;
6721
6722       if (fh == NULL)
6723         {
6724           const char *p, *q;
6725           struct ppc_link_hash_table *htab;
6726           char save;
6727
6728           /* We aren't supposed to use alloca in BFD because on
6729              systems which do not have alloca the version in libiberty
6730              calls xmalloc, which might cause the program to crash
6731              when it runs out of memory.  This function doesn't have a
6732              return status, so there's no way to gracefully return an
6733              error.  So cheat.  We know that string[-1] can be safely
6734              accessed;  It's either a string in an ELF string table,
6735              or allocated in an objalloc structure.  */
6736
6737           p = eh->elf.root.root.string - 1;
6738           save = *p;
6739           *(char *) p = '.';
6740           htab = ppc_hash_table (info);
6741           if (htab == NULL)
6742             return;
6743
6744           fh = (struct ppc_link_hash_entry *)
6745             elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6746           *(char *) p = save;
6747
6748           /* Unfortunately, if it so happens that the string we were
6749              looking for was allocated immediately before this string,
6750              then we overwrote the string terminator.  That's the only
6751              reason the lookup should fail.  */
6752           if (fh == NULL)
6753             {
6754               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6755               while (q >= eh->elf.root.root.string && *q == *p)
6756                 --q, --p;
6757               if (q < eh->elf.root.root.string && *p == '.')
6758                 fh = (struct ppc_link_hash_entry *)
6759                   elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6760             }
6761           if (fh != NULL)
6762             {
6763               eh->oh = fh;
6764               fh->oh = eh;
6765             }
6766         }
6767       if (fh != NULL)
6768         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6769     }
6770 }
6771
6772 static bfd_boolean
6773 get_sym_h (struct elf_link_hash_entry **hp,
6774            Elf_Internal_Sym **symp,
6775            asection **symsecp,
6776            unsigned char **tls_maskp,
6777            Elf_Internal_Sym **locsymsp,
6778            unsigned long r_symndx,
6779            bfd *ibfd)
6780 {
6781   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6782
6783   if (r_symndx >= symtab_hdr->sh_info)
6784     {
6785       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6786       struct elf_link_hash_entry *h;
6787
6788       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6789       h = elf_follow_link (h);
6790
6791       if (hp != NULL)
6792         *hp = h;
6793
6794       if (symp != NULL)
6795         *symp = NULL;
6796
6797       if (symsecp != NULL)
6798         {
6799           asection *symsec = NULL;
6800           if (h->root.type == bfd_link_hash_defined
6801               || h->root.type == bfd_link_hash_defweak)
6802             symsec = h->root.u.def.section;
6803           *symsecp = symsec;
6804         }
6805
6806       if (tls_maskp != NULL)
6807         {
6808           struct ppc_link_hash_entry *eh;
6809
6810           eh = (struct ppc_link_hash_entry *) h;
6811           *tls_maskp = &eh->tls_mask;
6812         }
6813     }
6814   else
6815     {
6816       Elf_Internal_Sym *sym;
6817       Elf_Internal_Sym *locsyms = *locsymsp;
6818
6819       if (locsyms == NULL)
6820         {
6821           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6822           if (locsyms == NULL)
6823             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6824                                             symtab_hdr->sh_info,
6825                                             0, NULL, NULL, NULL);
6826           if (locsyms == NULL)
6827             return FALSE;
6828           *locsymsp = locsyms;
6829         }
6830       sym = locsyms + r_symndx;
6831
6832       if (hp != NULL)
6833         *hp = NULL;
6834
6835       if (symp != NULL)
6836         *symp = sym;
6837
6838       if (symsecp != NULL)
6839         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6840
6841       if (tls_maskp != NULL)
6842         {
6843           struct got_entry **lgot_ents;
6844           unsigned char *tls_mask;
6845
6846           tls_mask = NULL;
6847           lgot_ents = elf_local_got_ents (ibfd);
6848           if (lgot_ents != NULL)
6849             {
6850               struct plt_entry **local_plt = (struct plt_entry **)
6851                 (lgot_ents + symtab_hdr->sh_info);
6852               unsigned char *lgot_masks = (unsigned char *)
6853                 (local_plt + symtab_hdr->sh_info);
6854               tls_mask = &lgot_masks[r_symndx];
6855             }
6856           *tls_maskp = tls_mask;
6857         }
6858     }
6859   return TRUE;
6860 }
6861
6862 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
6863    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6864    type suitable for optimization, and 1 otherwise.  */
6865
6866 static int
6867 get_tls_mask (unsigned char **tls_maskp,
6868               unsigned long *toc_symndx,
6869               bfd_vma *toc_addend,
6870               Elf_Internal_Sym **locsymsp,
6871               const Elf_Internal_Rela *rel,
6872               bfd *ibfd)
6873 {
6874   unsigned long r_symndx;
6875   int next_r;
6876   struct elf_link_hash_entry *h;
6877   Elf_Internal_Sym *sym;
6878   asection *sec;
6879   bfd_vma off;
6880
6881   r_symndx = ELF64_R_SYM (rel->r_info);
6882   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6883     return 0;
6884
6885   if ((*tls_maskp != NULL && **tls_maskp != 0)
6886       || sec == NULL
6887       || ppc64_elf_section_data (sec) == NULL
6888       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6889     return 1;
6890
6891   /* Look inside a TOC section too.  */
6892   if (h != NULL)
6893     {
6894       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6895       off = h->root.u.def.value;
6896     }
6897   else
6898     off = sym->st_value;
6899   off += rel->r_addend;
6900   BFD_ASSERT (off % 8 == 0);
6901   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6902   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6903   if (toc_symndx != NULL)
6904     *toc_symndx = r_symndx;
6905   if (toc_addend != NULL)
6906     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6907   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6908     return 0;
6909   if ((h == NULL || is_static_defined (h))
6910       && (next_r == -1 || next_r == -2))
6911     return 1 - next_r;
6912   return 1;
6913 }
6914
6915 /* Find (or create) an entry in the tocsave hash table.  */
6916
6917 static struct tocsave_entry *
6918 tocsave_find (struct ppc_link_hash_table *htab,
6919               enum insert_option insert,
6920               Elf_Internal_Sym **local_syms,
6921               const Elf_Internal_Rela *irela,
6922               bfd *ibfd)
6923 {
6924   unsigned long r_indx;
6925   struct elf_link_hash_entry *h;
6926   Elf_Internal_Sym *sym;
6927   struct tocsave_entry ent, *p;
6928   hashval_t hash;
6929   struct tocsave_entry **slot;
6930
6931   r_indx = ELF64_R_SYM (irela->r_info);
6932   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6933     return NULL;
6934   if (ent.sec == NULL || ent.sec->output_section == NULL)
6935     {
6936       (*_bfd_error_handler)
6937         (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
6938       return NULL;
6939     }
6940
6941   if (h != NULL)
6942     ent.offset = h->root.u.def.value;
6943   else
6944     ent.offset = sym->st_value;
6945   ent.offset += irela->r_addend;
6946
6947   hash = tocsave_htab_hash (&ent);
6948   slot = ((struct tocsave_entry **)
6949           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6950   if (slot == NULL)
6951     return NULL;
6952
6953   if (*slot == NULL)
6954     {
6955       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6956       if (p == NULL)
6957         return NULL;
6958       *p = ent;
6959       *slot = p;
6960     }
6961   return *slot;
6962 }
6963
6964 /* Adjust all global syms defined in opd sections.  In gcc generated
6965    code for the old ABI, these will already have been done.  */
6966
6967 static bfd_boolean
6968 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6969 {
6970   struct ppc_link_hash_entry *eh;
6971   asection *sym_sec;
6972   struct _opd_sec_data *opd;
6973
6974   if (h->root.type == bfd_link_hash_indirect)
6975     return TRUE;
6976
6977   if (h->root.type != bfd_link_hash_defined
6978       && h->root.type != bfd_link_hash_defweak)
6979     return TRUE;
6980
6981   eh = (struct ppc_link_hash_entry *) h;
6982   if (eh->adjust_done)
6983     return TRUE;
6984
6985   sym_sec = eh->elf.root.u.def.section;
6986   opd = get_opd_info (sym_sec);
6987   if (opd != NULL && opd->adjust != NULL)
6988     {
6989       long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
6990       if (adjust == -1)
6991         {
6992           /* This entry has been deleted.  */
6993           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6994           if (dsec == NULL)
6995             {
6996               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6997                 if (discarded_section (dsec))
6998                   {
6999                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7000                     break;
7001                   }
7002             }
7003           eh->elf.root.u.def.value = 0;
7004           eh->elf.root.u.def.section = dsec;
7005         }
7006       else
7007         eh->elf.root.u.def.value += adjust;
7008       eh->adjust_done = 1;
7009     }
7010   return TRUE;
7011 }
7012
7013 /* Handles decrementing dynamic reloc counts for the reloc specified by
7014    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
7015    have already been determined.  */
7016
7017 static bfd_boolean
7018 dec_dynrel_count (bfd_vma r_info,
7019                   asection *sec,
7020                   struct bfd_link_info *info,
7021                   Elf_Internal_Sym **local_syms,
7022                   struct elf_link_hash_entry *h,
7023                   Elf_Internal_Sym *sym)
7024 {
7025   enum elf_ppc64_reloc_type r_type;
7026   asection *sym_sec = NULL;
7027
7028   /* Can this reloc be dynamic?  This switch, and later tests here
7029      should be kept in sync with the code in check_relocs.  */
7030   r_type = ELF64_R_TYPE (r_info);
7031   switch (r_type)
7032     {
7033     default:
7034       return TRUE;
7035
7036     case R_PPC64_TPREL16:
7037     case R_PPC64_TPREL16_LO:
7038     case R_PPC64_TPREL16_HI:
7039     case R_PPC64_TPREL16_HA:
7040     case R_PPC64_TPREL16_DS:
7041     case R_PPC64_TPREL16_LO_DS:
7042     case R_PPC64_TPREL16_HIGHER:
7043     case R_PPC64_TPREL16_HIGHERA:
7044     case R_PPC64_TPREL16_HIGHEST:
7045     case R_PPC64_TPREL16_HIGHESTA:
7046       if (!info->shared)
7047         return TRUE;
7048
7049     case R_PPC64_TPREL64:
7050     case R_PPC64_DTPMOD64:
7051     case R_PPC64_DTPREL64:
7052     case R_PPC64_ADDR64:
7053     case R_PPC64_REL30:
7054     case R_PPC64_REL32:
7055     case R_PPC64_REL64:
7056     case R_PPC64_ADDR14:
7057     case R_PPC64_ADDR14_BRNTAKEN:
7058     case R_PPC64_ADDR14_BRTAKEN:
7059     case R_PPC64_ADDR16:
7060     case R_PPC64_ADDR16_DS:
7061     case R_PPC64_ADDR16_HA:
7062     case R_PPC64_ADDR16_HI:
7063     case R_PPC64_ADDR16_HIGHER:
7064     case R_PPC64_ADDR16_HIGHERA:
7065     case R_PPC64_ADDR16_HIGHEST:
7066     case R_PPC64_ADDR16_HIGHESTA:
7067     case R_PPC64_ADDR16_LO:
7068     case R_PPC64_ADDR16_LO_DS:
7069     case R_PPC64_ADDR24:
7070     case R_PPC64_ADDR32:
7071     case R_PPC64_UADDR16:
7072     case R_PPC64_UADDR32:
7073     case R_PPC64_UADDR64:
7074     case R_PPC64_TOC:
7075       break;
7076     }
7077
7078   if (local_syms != NULL)
7079     {
7080       unsigned long r_symndx;
7081       bfd *ibfd = sec->owner;
7082
7083       r_symndx = ELF64_R_SYM (r_info);
7084       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7085         return FALSE;
7086     }
7087
7088   if ((info->shared
7089        && (must_be_dyn_reloc (info, r_type)
7090            || (h != NULL
7091                && (!SYMBOLIC_BIND (info, h)
7092                    || h->root.type == bfd_link_hash_defweak
7093                    || !h->def_regular))))
7094       || (ELIMINATE_COPY_RELOCS
7095           && !info->shared
7096           && h != NULL
7097           && (h->root.type == bfd_link_hash_defweak
7098               || !h->def_regular)))
7099     ;
7100   else
7101     return TRUE;
7102
7103   if (h != NULL)
7104     {
7105       struct elf_dyn_relocs *p;
7106       struct elf_dyn_relocs **pp;
7107       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7108
7109       /* elf_gc_sweep may have already removed all dyn relocs associated
7110          with local syms for a given section.  Also, symbol flags are
7111          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
7112          report a dynreloc miscount.  */
7113       if (*pp == NULL && info->gc_sections)
7114         return TRUE;
7115
7116       while ((p = *pp) != NULL)
7117         {
7118           if (p->sec == sec)
7119             {
7120               if (!must_be_dyn_reloc (info, r_type))
7121                 p->pc_count -= 1;
7122               p->count -= 1;
7123               if (p->count == 0)
7124                 *pp = p->next;
7125               return TRUE;
7126             }
7127           pp = &p->next;
7128         }
7129     }
7130   else
7131     {
7132       struct ppc_dyn_relocs *p;
7133       struct ppc_dyn_relocs **pp;
7134       void *vpp;
7135       bfd_boolean is_ifunc;
7136
7137       if (local_syms == NULL)
7138         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7139       if (sym_sec == NULL)
7140         sym_sec = sec;
7141
7142       vpp = &elf_section_data (sym_sec)->local_dynrel;
7143       pp = (struct ppc_dyn_relocs **) vpp;
7144
7145       if (*pp == NULL && info->gc_sections)
7146         return TRUE;
7147
7148       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7149       while ((p = *pp) != NULL)
7150         {
7151           if (p->sec == sec && p->ifunc == is_ifunc)
7152             {
7153               p->count -= 1;
7154               if (p->count == 0)
7155                 *pp = p->next;
7156               return TRUE;
7157             }
7158           pp = &p->next;
7159         }
7160     }
7161
7162   info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7163                           sec->owner, sec);
7164   bfd_set_error (bfd_error_bad_value);
7165   return FALSE;
7166 }
7167
7168 /* Remove unused Official Procedure Descriptor entries.  Currently we
7169    only remove those associated with functions in discarded link-once
7170    sections, or weakly defined functions that have been overridden.  It
7171    would be possible to remove many more entries for statically linked
7172    applications.  */
7173
7174 bfd_boolean
7175 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
7176 {
7177   bfd *ibfd;
7178   bfd_boolean some_edited = FALSE;
7179   asection *need_pad = NULL;
7180
7181   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7182     {
7183       asection *sec;
7184       Elf_Internal_Rela *relstart, *rel, *relend;
7185       Elf_Internal_Shdr *symtab_hdr;
7186       Elf_Internal_Sym *local_syms;
7187       bfd_vma offset;
7188       struct _opd_sec_data *opd;
7189       bfd_boolean need_edit, add_aux_fields;
7190       bfd_size_type cnt_16b = 0;
7191
7192       if (!is_ppc64_elf (ibfd))
7193         continue;
7194
7195       sec = bfd_get_section_by_name (ibfd, ".opd");
7196       if (sec == NULL || sec->size == 0)
7197         continue;
7198
7199       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7200         continue;
7201
7202       if (sec->output_section == bfd_abs_section_ptr)
7203         continue;
7204
7205       /* Look through the section relocs.  */
7206       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7207         continue;
7208
7209       local_syms = NULL;
7210       symtab_hdr = &elf_symtab_hdr (ibfd);
7211
7212       /* Read the relocations.  */
7213       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7214                                             info->keep_memory);
7215       if (relstart == NULL)
7216         return FALSE;
7217
7218       /* First run through the relocs to check they are sane, and to
7219          determine whether we need to edit this opd section.  */
7220       need_edit = FALSE;
7221       need_pad = sec;
7222       offset = 0;
7223       relend = relstart + sec->reloc_count;
7224       for (rel = relstart; rel < relend; )
7225         {
7226           enum elf_ppc64_reloc_type r_type;
7227           unsigned long r_symndx;
7228           asection *sym_sec;
7229           struct elf_link_hash_entry *h;
7230           Elf_Internal_Sym *sym;
7231
7232           /* .opd contains a regular array of 16 or 24 byte entries.  We're
7233              only interested in the reloc pointing to a function entry
7234              point.  */
7235           if (rel->r_offset != offset
7236               || rel + 1 >= relend
7237               || (rel + 1)->r_offset != offset + 8)
7238             {
7239               /* If someone messes with .opd alignment then after a
7240                  "ld -r" we might have padding in the middle of .opd.
7241                  Also, there's nothing to prevent someone putting
7242                  something silly in .opd with the assembler.  No .opd
7243                  optimization for them!  */
7244             broken_opd:
7245               (*_bfd_error_handler)
7246                 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7247               need_edit = FALSE;
7248               break;
7249             }
7250
7251           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7252               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7253             {
7254               (*_bfd_error_handler)
7255                 (_("%B: unexpected reloc type %u in .opd section"),
7256                  ibfd, r_type);
7257               need_edit = FALSE;
7258               break;
7259             }
7260
7261           r_symndx = ELF64_R_SYM (rel->r_info);
7262           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7263                           r_symndx, ibfd))
7264             goto error_ret;
7265
7266           if (sym_sec == NULL || sym_sec->owner == NULL)
7267             {
7268               const char *sym_name;
7269               if (h != NULL)
7270                 sym_name = h->root.root.string;
7271               else
7272                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7273                                              sym_sec);
7274
7275               (*_bfd_error_handler)
7276                 (_("%B: undefined sym `%s' in .opd section"),
7277                  ibfd, sym_name);
7278               need_edit = FALSE;
7279               break;
7280             }
7281
7282           /* opd entries are always for functions defined in the
7283              current input bfd.  If the symbol isn't defined in the
7284              input bfd, then we won't be using the function in this
7285              bfd;  It must be defined in a linkonce section in another
7286              bfd, or is weak.  It's also possible that we are
7287              discarding the function due to a linker script /DISCARD/,
7288              which we test for via the output_section.  */
7289           if (sym_sec->owner != ibfd
7290               || sym_sec->output_section == bfd_abs_section_ptr)
7291             need_edit = TRUE;
7292
7293           rel += 2;
7294           if (rel == relend
7295               || (rel + 1 == relend && rel->r_offset == offset + 16))
7296             {
7297               if (sec->size == offset + 24)
7298                 {
7299                   need_pad = NULL;
7300                   break;
7301                 }
7302               if (rel == relend && sec->size == offset + 16)
7303                 {
7304                   cnt_16b++;
7305                   break;
7306                 }
7307               goto broken_opd;
7308             }
7309
7310           if (rel->r_offset == offset + 24)
7311             offset += 24;
7312           else if (rel->r_offset != offset + 16)
7313             goto broken_opd;
7314           else if (rel + 1 < relend
7315                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7316                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7317             {
7318               offset += 16;
7319               cnt_16b++;
7320             }
7321           else if (rel + 2 < relend
7322                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7323                    && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7324             {
7325               offset += 24;
7326               rel += 1;
7327             }
7328           else
7329             goto broken_opd;
7330         }
7331
7332       add_aux_fields = non_overlapping && cnt_16b > 0;
7333
7334       if (need_edit || add_aux_fields)
7335         {
7336           Elf_Internal_Rela *write_rel;
7337           Elf_Internal_Shdr *rel_hdr;
7338           bfd_byte *rptr, *wptr;
7339           bfd_byte *new_contents;
7340           bfd_boolean skip;
7341           long opd_ent_size;
7342           bfd_size_type amt;
7343
7344           new_contents = NULL;
7345           amt = sec->size * sizeof (long) / 8;
7346           opd = &ppc64_elf_section_data (sec)->u.opd;
7347           opd->adjust = bfd_zalloc (sec->owner, amt);
7348           if (opd->adjust == NULL)
7349             return FALSE;
7350           ppc64_elf_section_data (sec)->sec_type = sec_opd;
7351
7352           /* This seems a waste of time as input .opd sections are all
7353              zeros as generated by gcc, but I suppose there's no reason
7354              this will always be so.  We might start putting something in
7355              the third word of .opd entries.  */
7356           if ((sec->flags & SEC_IN_MEMORY) == 0)
7357             {
7358               bfd_byte *loc;
7359               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7360                 {
7361                   if (loc != NULL)
7362                     free (loc);
7363                 error_ret:
7364                   if (local_syms != NULL
7365                       && symtab_hdr->contents != (unsigned char *) local_syms)
7366                     free (local_syms);
7367                   if (elf_section_data (sec)->relocs != relstart)
7368                     free (relstart);
7369                   return FALSE;
7370                 }
7371               sec->contents = loc;
7372               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7373             }
7374
7375           elf_section_data (sec)->relocs = relstart;
7376
7377           new_contents = sec->contents;
7378           if (add_aux_fields)
7379             {
7380               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7381               if (new_contents == NULL)
7382                 return FALSE;
7383               need_pad = FALSE;
7384             }
7385           wptr = new_contents;
7386           rptr = sec->contents;
7387
7388           write_rel = relstart;
7389           skip = FALSE;
7390           offset = 0;
7391           opd_ent_size = 0;
7392           for (rel = relstart; rel < relend; rel++)
7393             {
7394               unsigned long r_symndx;
7395               asection *sym_sec;
7396               struct elf_link_hash_entry *h;
7397               Elf_Internal_Sym *sym;
7398
7399               r_symndx = ELF64_R_SYM (rel->r_info);
7400               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7401                               r_symndx, ibfd))
7402                 goto error_ret;
7403
7404               if (rel->r_offset == offset)
7405                 {
7406                   struct ppc_link_hash_entry *fdh = NULL;
7407
7408                   /* See if the .opd entry is full 24 byte or
7409                      16 byte (with fd_aux entry overlapped with next
7410                      fd_func).  */
7411                   opd_ent_size = 24;
7412                   if ((rel + 2 == relend && sec->size == offset + 16)
7413                       || (rel + 3 < relend
7414                           && rel[2].r_offset == offset + 16
7415                           && rel[3].r_offset == offset + 24
7416                           && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7417                           && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7418                     opd_ent_size = 16;
7419
7420                   if (h != NULL
7421                       && h->root.root.string[0] == '.')
7422                     {
7423                       struct ppc_link_hash_table *htab;
7424
7425                       htab = ppc_hash_table (info);
7426                       if (htab != NULL)
7427                         fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7428                                           htab);
7429                       if (fdh != NULL
7430                           && fdh->elf.root.type != bfd_link_hash_defined
7431                           && fdh->elf.root.type != bfd_link_hash_defweak)
7432                         fdh = NULL;
7433                     }
7434
7435                   skip = (sym_sec->owner != ibfd
7436                           || sym_sec->output_section == bfd_abs_section_ptr);
7437                   if (skip)
7438                     {
7439                       if (fdh != NULL && sym_sec->owner == ibfd)
7440                         {
7441                           /* Arrange for the function descriptor sym
7442                              to be dropped.  */
7443                           fdh->elf.root.u.def.value = 0;
7444                           fdh->elf.root.u.def.section = sym_sec;
7445                         }
7446                       opd->adjust[rel->r_offset / 8] = -1;
7447                     }
7448                   else
7449                     {
7450                       /* We'll be keeping this opd entry.  */
7451
7452                       if (fdh != NULL)
7453                         {
7454                           /* Redefine the function descriptor symbol to
7455                              this location in the opd section.  It is
7456                              necessary to update the value here rather
7457                              than using an array of adjustments as we do
7458                              for local symbols, because various places
7459                              in the generic ELF code use the value
7460                              stored in u.def.value.  */
7461                           fdh->elf.root.u.def.value = wptr - new_contents;
7462                           fdh->adjust_done = 1;
7463                         }
7464
7465                       /* Local syms are a bit tricky.  We could
7466                          tweak them as they can be cached, but
7467                          we'd need to look through the local syms
7468                          for the function descriptor sym which we
7469                          don't have at the moment.  So keep an
7470                          array of adjustments.  */
7471                       opd->adjust[rel->r_offset / 8]
7472                         = (wptr - new_contents) - (rptr - sec->contents);
7473
7474                       if (wptr != rptr)
7475                         memcpy (wptr, rptr, opd_ent_size);
7476                       wptr += opd_ent_size;
7477                       if (add_aux_fields && opd_ent_size == 16)
7478                         {
7479                           memset (wptr, '\0', 8);
7480                           wptr += 8;
7481                         }
7482                     }
7483                   rptr += opd_ent_size;
7484                   offset += opd_ent_size;
7485                 }
7486
7487               if (skip)
7488                 {
7489                   if (!NO_OPD_RELOCS
7490                       && !info->relocatable
7491                       && !dec_dynrel_count (rel->r_info, sec, info,
7492                                             NULL, h, sym))
7493                     goto error_ret;
7494                 }
7495               else
7496                 {
7497                   /* We need to adjust any reloc offsets to point to the
7498                      new opd entries.  While we're at it, we may as well
7499                      remove redundant relocs.  */
7500                   rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
7501                   if (write_rel != rel)
7502                     memcpy (write_rel, rel, sizeof (*rel));
7503                   ++write_rel;
7504                 }
7505             }
7506
7507           sec->size = wptr - new_contents;
7508           sec->reloc_count = write_rel - relstart;
7509           if (add_aux_fields)
7510             {
7511               free (sec->contents);
7512               sec->contents = new_contents;
7513             }
7514
7515           /* Fudge the header size too, as this is used later in
7516              elf_bfd_final_link if we are emitting relocs.  */
7517           rel_hdr = _bfd_elf_single_rel_hdr (sec);
7518           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7519           some_edited = TRUE;
7520         }
7521       else if (elf_section_data (sec)->relocs != relstart)
7522         free (relstart);
7523
7524       if (local_syms != NULL
7525           && symtab_hdr->contents != (unsigned char *) local_syms)
7526         {
7527           if (!info->keep_memory)
7528             free (local_syms);
7529           else
7530             symtab_hdr->contents = (unsigned char *) local_syms;
7531         }
7532     }
7533
7534   if (some_edited)
7535     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7536
7537   /* If we are doing a final link and the last .opd entry is just 16 byte
7538      long, add a 8 byte padding after it.  */
7539   if (need_pad != NULL && !info->relocatable)
7540     {
7541       bfd_byte *p;
7542
7543       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7544         {
7545           BFD_ASSERT (need_pad->size > 0);
7546
7547           p = bfd_malloc (need_pad->size + 8);
7548           if (p == NULL)
7549             return FALSE;
7550
7551           if (! bfd_get_section_contents (need_pad->owner, need_pad,
7552                                           p, 0, need_pad->size))
7553             return FALSE;
7554
7555           need_pad->contents = p;
7556           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7557         }
7558       else
7559         {
7560           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7561           if (p == NULL)
7562             return FALSE;
7563
7564           need_pad->contents = p;
7565         }
7566
7567       memset (need_pad->contents + need_pad->size, 0, 8);
7568       need_pad->size += 8;
7569     }
7570
7571   return TRUE;
7572 }
7573
7574 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
7575
7576 asection *
7577 ppc64_elf_tls_setup (struct bfd_link_info *info,
7578                      int no_tls_get_addr_opt,
7579                      int *no_multi_toc)
7580 {
7581   struct ppc_link_hash_table *htab;
7582
7583   htab = ppc_hash_table (info);
7584   if (htab == NULL)
7585     return NULL;
7586
7587   if (*no_multi_toc)
7588     htab->do_multi_toc = 0;
7589   else if (!htab->do_multi_toc)
7590     *no_multi_toc = 1;
7591
7592   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7593                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7594                                               FALSE, FALSE, TRUE));
7595   /* Move dynamic linking info to the function descriptor sym.  */
7596   if (htab->tls_get_addr != NULL)
7597     func_desc_adjust (&htab->tls_get_addr->elf, info);
7598   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7599                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7600                                                  FALSE, FALSE, TRUE));
7601   if (!no_tls_get_addr_opt)
7602     {
7603       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7604
7605       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7606                                   FALSE, FALSE, TRUE);
7607       if (opt != NULL)
7608         func_desc_adjust (opt, info);
7609       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7610                                      FALSE, FALSE, TRUE);
7611       if (opt_fd != NULL
7612           && (opt_fd->root.type == bfd_link_hash_defined
7613               || opt_fd->root.type == bfd_link_hash_defweak))
7614         {
7615           /* If glibc supports an optimized __tls_get_addr call stub,
7616              signalled by the presence of __tls_get_addr_opt, and we'll
7617              be calling __tls_get_addr via a plt call stub, then
7618              make __tls_get_addr point to __tls_get_addr_opt.  */
7619           tga_fd = &htab->tls_get_addr_fd->elf;
7620           if (htab->elf.dynamic_sections_created
7621               && tga_fd != NULL
7622               && (tga_fd->type == STT_FUNC
7623                   || tga_fd->needs_plt)
7624               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7625                    || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7626                        && tga_fd->root.type == bfd_link_hash_undefweak)))
7627             {
7628               struct plt_entry *ent;
7629
7630               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7631                 if (ent->plt.refcount > 0)
7632                   break;
7633               if (ent != NULL)
7634                 {
7635                   tga_fd->root.type = bfd_link_hash_indirect;
7636                   tga_fd->root.u.i.link = &opt_fd->root;
7637                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7638                   if (opt_fd->dynindx != -1)
7639                     {
7640                       /* Use __tls_get_addr_opt in dynamic relocations.  */
7641                       opt_fd->dynindx = -1;
7642                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7643                                               opt_fd->dynstr_index);
7644                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7645                         return NULL;
7646                     }
7647                   htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7648                   tga = &htab->tls_get_addr->elf;
7649                   if (opt != NULL && tga != NULL)
7650                     {
7651                       tga->root.type = bfd_link_hash_indirect;
7652                       tga->root.u.i.link = &opt->root;
7653                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
7654                       _bfd_elf_link_hash_hide_symbol (info, opt,
7655                                                       tga->forced_local);
7656                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7657                     }
7658                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7659                   htab->tls_get_addr_fd->is_func_descriptor = 1;
7660                   if (htab->tls_get_addr != NULL)
7661                     {
7662                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7663                       htab->tls_get_addr->is_func = 1;
7664                     }
7665                 }
7666             }
7667         }
7668       else
7669         no_tls_get_addr_opt = TRUE;
7670     }
7671   htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
7672   return _bfd_elf_tls_setup (info->output_bfd, info);
7673 }
7674
7675 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7676    HASH1 or HASH2.  */
7677
7678 static bfd_boolean
7679 branch_reloc_hash_match (const bfd *ibfd,
7680                          const Elf_Internal_Rela *rel,
7681                          const struct ppc_link_hash_entry *hash1,
7682                          const struct ppc_link_hash_entry *hash2)
7683 {
7684   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7685   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7686   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7687
7688   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7689     {
7690       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7691       struct elf_link_hash_entry *h;
7692
7693       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7694       h = elf_follow_link (h);
7695       if (h == &hash1->elf || h == &hash2->elf)
7696         return TRUE;
7697     }
7698   return FALSE;
7699 }
7700
7701 /* Run through all the TLS relocs looking for optimization
7702    opportunities.  The linker has been hacked (see ppc64elf.em) to do
7703    a preliminary section layout so that we know the TLS segment
7704    offsets.  We can't optimize earlier because some optimizations need
7705    to know the tp offset, and we need to optimize before allocating
7706    dynamic relocations.  */
7707
7708 bfd_boolean
7709 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7710 {
7711   bfd *ibfd;
7712   asection *sec;
7713   struct ppc_link_hash_table *htab;
7714   unsigned char *toc_ref;
7715   int pass;
7716
7717   if (info->relocatable || !info->executable)
7718     return TRUE;
7719
7720   htab = ppc_hash_table (info);
7721   if (htab == NULL)
7722     return FALSE;
7723
7724   /* Make two passes over the relocs.  On the first pass, mark toc
7725      entries involved with tls relocs, and check that tls relocs
7726      involved in setting up a tls_get_addr call are indeed followed by
7727      such a call.  If they are not, we can't do any tls optimization.
7728      On the second pass twiddle tls_mask flags to notify
7729      relocate_section that optimization can be done, and adjust got
7730      and plt refcounts.  */
7731   toc_ref = NULL;
7732   for (pass = 0; pass < 2; ++pass)
7733     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7734       {
7735         Elf_Internal_Sym *locsyms = NULL;
7736         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7737
7738         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7739           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7740             {
7741               Elf_Internal_Rela *relstart, *rel, *relend;
7742               bfd_boolean found_tls_get_addr_arg = 0;
7743
7744               /* Read the relocations.  */
7745               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7746                                                     info->keep_memory);
7747               if (relstart == NULL)
7748                 return FALSE;
7749
7750               relend = relstart + sec->reloc_count;
7751               for (rel = relstart; rel < relend; rel++)
7752                 {
7753                   enum elf_ppc64_reloc_type r_type;
7754                   unsigned long r_symndx;
7755                   struct elf_link_hash_entry *h;
7756                   Elf_Internal_Sym *sym;
7757                   asection *sym_sec;
7758                   unsigned char *tls_mask;
7759                   unsigned char tls_set, tls_clear, tls_type = 0;
7760                   bfd_vma value;
7761                   bfd_boolean ok_tprel, is_local;
7762                   long toc_ref_index = 0;
7763                   int expecting_tls_get_addr = 0;
7764                   bfd_boolean ret = FALSE;
7765
7766                   r_symndx = ELF64_R_SYM (rel->r_info);
7767                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7768                                   r_symndx, ibfd))
7769                     {
7770                     err_free_rel:
7771                       if (elf_section_data (sec)->relocs != relstart)
7772                         free (relstart);
7773                       if (toc_ref != NULL)
7774                         free (toc_ref);
7775                       if (locsyms != NULL
7776                           && (elf_symtab_hdr (ibfd).contents
7777                               != (unsigned char *) locsyms))
7778                         free (locsyms);
7779                       return ret;
7780                     }
7781
7782                   if (h != NULL)
7783                     {
7784                       if (h->root.type == bfd_link_hash_defined
7785                           || h->root.type == bfd_link_hash_defweak)
7786                         value = h->root.u.def.value;
7787                       else if (h->root.type == bfd_link_hash_undefweak)
7788                         value = 0;
7789                       else
7790                         {
7791                           found_tls_get_addr_arg = 0;
7792                           continue;
7793                         }
7794                     }
7795                   else
7796                     /* Symbols referenced by TLS relocs must be of type
7797                        STT_TLS.  So no need for .opd local sym adjust.  */
7798                     value = sym->st_value;
7799
7800                   ok_tprel = FALSE;
7801                   is_local = FALSE;
7802                   if (h == NULL
7803                       || !h->def_dynamic)
7804                     {
7805                       is_local = TRUE;
7806                       if (h != NULL
7807                           && h->root.type == bfd_link_hash_undefweak)
7808                         ok_tprel = TRUE;
7809                       else
7810                         {
7811                           value += sym_sec->output_offset;
7812                           value += sym_sec->output_section->vma;
7813                           value -= htab->elf.tls_sec->vma;
7814                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7815                                       < (bfd_vma) 1 << 32);
7816                         }
7817                     }
7818
7819                   r_type = ELF64_R_TYPE (rel->r_info);
7820                   /* If this section has old-style __tls_get_addr calls
7821                      without marker relocs, then check that each
7822                      __tls_get_addr call reloc is preceded by a reloc
7823                      that conceivably belongs to the __tls_get_addr arg
7824                      setup insn.  If we don't find matching arg setup
7825                      relocs, don't do any tls optimization.  */
7826                   if (pass == 0
7827                       && sec->has_tls_get_addr_call
7828                       && h != NULL
7829                       && (h == &htab->tls_get_addr->elf
7830                           || h == &htab->tls_get_addr_fd->elf)
7831                       && !found_tls_get_addr_arg
7832                       && is_branch_reloc (r_type))
7833                     {
7834                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7835                                                 "TLS optimization disabled\n"),
7836                                               ibfd, sec, rel->r_offset);
7837                       ret = TRUE;
7838                       goto err_free_rel;
7839                     }
7840
7841                   found_tls_get_addr_arg = 0;
7842                   switch (r_type)
7843                     {
7844                     case R_PPC64_GOT_TLSLD16:
7845                     case R_PPC64_GOT_TLSLD16_LO:
7846                       expecting_tls_get_addr = 1;
7847                       found_tls_get_addr_arg = 1;
7848                       /* Fall thru */
7849
7850                     case R_PPC64_GOT_TLSLD16_HI:
7851                     case R_PPC64_GOT_TLSLD16_HA:
7852                       /* These relocs should never be against a symbol
7853                          defined in a shared lib.  Leave them alone if
7854                          that turns out to be the case.  */
7855                       if (!is_local)
7856                         continue;
7857
7858                       /* LD -> LE */
7859                       tls_set = 0;
7860                       tls_clear = TLS_LD;
7861                       tls_type = TLS_TLS | TLS_LD;
7862                       break;
7863
7864                     case R_PPC64_GOT_TLSGD16:
7865                     case R_PPC64_GOT_TLSGD16_LO:
7866                       expecting_tls_get_addr = 1;
7867                       found_tls_get_addr_arg = 1;
7868                       /* Fall thru */
7869
7870                     case R_PPC64_GOT_TLSGD16_HI:
7871                     case R_PPC64_GOT_TLSGD16_HA:
7872                       if (ok_tprel)
7873                         /* GD -> LE */
7874                         tls_set = 0;
7875                       else
7876                         /* GD -> IE */
7877                         tls_set = TLS_TLS | TLS_TPRELGD;
7878                       tls_clear = TLS_GD;
7879                       tls_type = TLS_TLS | TLS_GD;
7880                       break;
7881
7882                     case R_PPC64_GOT_TPREL16_DS:
7883                     case R_PPC64_GOT_TPREL16_LO_DS:
7884                     case R_PPC64_GOT_TPREL16_HI:
7885                     case R_PPC64_GOT_TPREL16_HA:
7886                       if (ok_tprel)
7887                         {
7888                           /* IE -> LE */
7889                           tls_set = 0;
7890                           tls_clear = TLS_TPREL;
7891                           tls_type = TLS_TLS | TLS_TPREL;
7892                           break;
7893                         }
7894                       continue;
7895
7896                     case R_PPC64_TLSGD:
7897                     case R_PPC64_TLSLD:
7898                       found_tls_get_addr_arg = 1;
7899                       /* Fall thru */
7900
7901                     case R_PPC64_TLS:
7902                     case R_PPC64_TOC16:
7903                     case R_PPC64_TOC16_LO:
7904                       if (sym_sec == NULL || sym_sec != toc)
7905                         continue;
7906
7907                       /* Mark this toc entry as referenced by a TLS
7908                          code sequence.  We can do that now in the
7909                          case of R_PPC64_TLS, and after checking for
7910                          tls_get_addr for the TOC16 relocs.  */
7911                       if (toc_ref == NULL)
7912                         toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
7913                       if (toc_ref == NULL)
7914                         goto err_free_rel;
7915
7916                       if (h != NULL)
7917                         value = h->root.u.def.value;
7918                       else
7919                         value = sym->st_value;
7920                       value += rel->r_addend;
7921                       BFD_ASSERT (value < toc->size && value % 8 == 0);
7922                       toc_ref_index = (value + toc->output_offset) / 8;
7923                       if (r_type == R_PPC64_TLS
7924                           || r_type == R_PPC64_TLSGD
7925                           || r_type == R_PPC64_TLSLD)
7926                         {
7927                           toc_ref[toc_ref_index] = 1;
7928                           continue;
7929                         }
7930
7931                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
7932                         continue;
7933
7934                       tls_set = 0;
7935                       tls_clear = 0;
7936                       expecting_tls_get_addr = 2;
7937                       break;
7938
7939                     case R_PPC64_TPREL64:
7940                       if (pass == 0
7941                           || sec != toc
7942                           || toc_ref == NULL
7943                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7944                         continue;
7945                       if (ok_tprel)
7946                         {
7947                           /* IE -> LE */
7948                           tls_set = TLS_EXPLICIT;
7949                           tls_clear = TLS_TPREL;
7950                           break;
7951                         }
7952                       continue;
7953
7954                     case R_PPC64_DTPMOD64:
7955                       if (pass == 0
7956                           || sec != toc
7957                           || toc_ref == NULL
7958                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7959                         continue;
7960                       if (rel + 1 < relend
7961                           && (rel[1].r_info
7962                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7963                           && rel[1].r_offset == rel->r_offset + 8)
7964                         {
7965                           if (ok_tprel)
7966                             /* GD -> LE */
7967                             tls_set = TLS_EXPLICIT | TLS_GD;
7968                           else
7969                             /* GD -> IE */
7970                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7971                           tls_clear = TLS_GD;
7972                         }
7973                       else
7974                         {
7975                           if (!is_local)
7976                             continue;
7977
7978                           /* LD -> LE */
7979                           tls_set = TLS_EXPLICIT;
7980                           tls_clear = TLS_LD;
7981                         }
7982                       break;
7983
7984                     default:
7985                       continue;
7986                     }
7987
7988                   if (pass == 0)
7989                     {
7990                       if (!expecting_tls_get_addr
7991                           || !sec->has_tls_get_addr_call)
7992                         continue;
7993
7994                       if (rel + 1 < relend
7995                           && branch_reloc_hash_match (ibfd, rel + 1,
7996                                                       htab->tls_get_addr,
7997                                                       htab->tls_get_addr_fd))
7998                         {
7999                           if (expecting_tls_get_addr == 2)
8000                             {
8001                               /* Check for toc tls entries.  */
8002                               unsigned char *toc_tls;
8003                               int retval;
8004
8005                               retval = get_tls_mask (&toc_tls, NULL, NULL,
8006                                                      &locsyms,
8007                                                      rel, ibfd);
8008                               if (retval == 0)
8009                                 goto err_free_rel;
8010                               if (toc_tls != NULL)
8011                                 {
8012                                   if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8013                                     found_tls_get_addr_arg = 1;
8014                                   if (retval > 1)
8015                                     toc_ref[toc_ref_index] = 1;
8016                                 }
8017                             }
8018                           continue;
8019                         }
8020
8021                       if (expecting_tls_get_addr != 1)
8022                         continue;
8023
8024                       /* Uh oh, we didn't find the expected call.  We
8025                          could just mark this symbol to exclude it
8026                          from tls optimization but it's safer to skip
8027                          the entire optimization.  */
8028                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8029                                                 "TLS optimization disabled\n"),
8030                                               ibfd, sec, rel->r_offset);
8031                       ret = TRUE;
8032                       goto err_free_rel;
8033                     }
8034
8035                   if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8036                     {
8037                       struct plt_entry *ent;
8038                       for (ent = htab->tls_get_addr->elf.plt.plist;
8039                            ent != NULL;
8040                            ent = ent->next)
8041                         if (ent->addend == 0)
8042                           {
8043                             if (ent->plt.refcount > 0)
8044                               {
8045                                 ent->plt.refcount -= 1;
8046                                 expecting_tls_get_addr = 0;
8047                               }
8048                             break;
8049                           }
8050                     }
8051
8052                   if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8053                     {
8054                       struct plt_entry *ent;
8055                       for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8056                            ent != NULL;
8057                            ent = ent->next)
8058                         if (ent->addend == 0)
8059                           {
8060                             if (ent->plt.refcount > 0)
8061                               ent->plt.refcount -= 1;
8062                             break;
8063                           }
8064                     }
8065
8066                   if (tls_clear == 0)
8067                     continue;
8068
8069                   if ((tls_set & TLS_EXPLICIT) == 0)
8070                     {
8071                       struct got_entry *ent;
8072
8073                       /* Adjust got entry for this reloc.  */
8074                       if (h != NULL)
8075                         ent = h->got.glist;
8076                       else
8077                         ent = elf_local_got_ents (ibfd)[r_symndx];
8078
8079                       for (; ent != NULL; ent = ent->next)
8080                         if (ent->addend == rel->r_addend
8081                             && ent->owner == ibfd
8082                             && ent->tls_type == tls_type)
8083                           break;
8084                       if (ent == NULL)
8085                         abort ();
8086
8087                       if (tls_set == 0)
8088                         {
8089                           /* We managed to get rid of a got entry.  */
8090                           if (ent->got.refcount > 0)
8091                             ent->got.refcount -= 1;
8092                         }
8093                     }
8094                   else
8095                     {
8096                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
8097                          we'll lose one or two dyn relocs.  */
8098                       if (!dec_dynrel_count (rel->r_info, sec, info,
8099                                              NULL, h, sym))
8100                         return FALSE;
8101
8102                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
8103                         {
8104                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8105                                                  NULL, h, sym))
8106                             return FALSE;
8107                         }
8108                     }
8109
8110                   *tls_mask |= tls_set;
8111                   *tls_mask &= ~tls_clear;
8112                 }
8113
8114               if (elf_section_data (sec)->relocs != relstart)
8115                 free (relstart);
8116             }
8117
8118         if (locsyms != NULL
8119             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8120           {
8121             if (!info->keep_memory)
8122               free (locsyms);
8123             else
8124               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8125           }
8126       }
8127
8128   if (toc_ref != NULL)
8129     free (toc_ref);
8130   return TRUE;
8131 }
8132
8133 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8134    the values of any global symbols in a toc section that has been
8135    edited.  Globals in toc sections should be a rarity, so this function
8136    sets a flag if any are found in toc sections other than the one just
8137    edited, so that futher hash table traversals can be avoided.  */
8138
8139 struct adjust_toc_info
8140 {
8141   asection *toc;
8142   unsigned long *skip;
8143   bfd_boolean global_toc_syms;
8144 };
8145
8146 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8147
8148 static bfd_boolean
8149 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8150 {
8151   struct ppc_link_hash_entry *eh;
8152   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8153   unsigned long i;
8154
8155   if (h->root.type != bfd_link_hash_defined
8156       && h->root.type != bfd_link_hash_defweak)
8157     return TRUE;
8158
8159   eh = (struct ppc_link_hash_entry *) h;
8160   if (eh->adjust_done)
8161     return TRUE;
8162
8163   if (eh->elf.root.u.def.section == toc_inf->toc)
8164     {
8165       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8166         i = toc_inf->toc->rawsize >> 3;
8167       else
8168         i = eh->elf.root.u.def.value >> 3;
8169
8170       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8171         {
8172           (*_bfd_error_handler)
8173             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8174           do
8175             ++i;
8176           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8177           eh->elf.root.u.def.value = (bfd_vma) i << 3;
8178         }
8179
8180       eh->elf.root.u.def.value -= toc_inf->skip[i];
8181       eh->adjust_done = 1;
8182     }
8183   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8184     toc_inf->global_toc_syms = TRUE;
8185
8186   return TRUE;
8187 }
8188
8189 /* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc.  */
8190
8191 static bfd_boolean
8192 ok_lo_toc_insn (unsigned int insn)
8193 {
8194   return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8195           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8196           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8197           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8198           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8199           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8200           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8201           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8202           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8203           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8204           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8205           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8206           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8207           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8208           || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8209               && (insn & 3) != 1)
8210           || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8211               && ((insn & 3) == 0 || (insn & 3) == 3))
8212           || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8213 }
8214
8215 /* Examine all relocs referencing .toc sections in order to remove
8216    unused .toc entries.  */
8217
8218 bfd_boolean
8219 ppc64_elf_edit_toc (struct bfd_link_info *info)
8220 {
8221   bfd *ibfd;
8222   struct adjust_toc_info toc_inf;
8223   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8224
8225   htab->do_toc_opt = 1;
8226   toc_inf.global_toc_syms = TRUE;
8227   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8228     {
8229       asection *toc, *sec;
8230       Elf_Internal_Shdr *symtab_hdr;
8231       Elf_Internal_Sym *local_syms;
8232       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8233       unsigned long *skip, *drop;
8234       unsigned char *used;
8235       unsigned char *keep, last, some_unused;
8236
8237       if (!is_ppc64_elf (ibfd))
8238         continue;
8239
8240       toc = bfd_get_section_by_name (ibfd, ".toc");
8241       if (toc == NULL
8242           || toc->size == 0
8243           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8244           || discarded_section (toc))
8245         continue;
8246
8247       toc_relocs = NULL;
8248       local_syms = NULL;
8249       symtab_hdr = &elf_symtab_hdr (ibfd);
8250
8251       /* Look at sections dropped from the final link.  */
8252       skip = NULL;
8253       relstart = NULL;
8254       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8255         {
8256           if (sec->reloc_count == 0
8257               || !discarded_section (sec)
8258               || get_opd_info (sec)
8259               || (sec->flags & SEC_ALLOC) == 0
8260               || (sec->flags & SEC_DEBUGGING) != 0)
8261             continue;
8262
8263           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8264           if (relstart == NULL)
8265             goto error_ret;
8266
8267           /* Run through the relocs to see which toc entries might be
8268              unused.  */
8269           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8270             {
8271               enum elf_ppc64_reloc_type r_type;
8272               unsigned long r_symndx;
8273               asection *sym_sec;
8274               struct elf_link_hash_entry *h;
8275               Elf_Internal_Sym *sym;
8276               bfd_vma val;
8277
8278               r_type = ELF64_R_TYPE (rel->r_info);
8279               switch (r_type)
8280                 {
8281                 default:
8282                   continue;
8283
8284                 case R_PPC64_TOC16:
8285                 case R_PPC64_TOC16_LO:
8286                 case R_PPC64_TOC16_HI:
8287                 case R_PPC64_TOC16_HA:
8288                 case R_PPC64_TOC16_DS:
8289                 case R_PPC64_TOC16_LO_DS:
8290                   break;
8291                 }
8292
8293               r_symndx = ELF64_R_SYM (rel->r_info);
8294               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8295                               r_symndx, ibfd))
8296                 goto error_ret;
8297
8298               if (sym_sec != toc)
8299                 continue;
8300
8301               if (h != NULL)
8302                 val = h->root.u.def.value;
8303               else
8304                 val = sym->st_value;
8305               val += rel->r_addend;
8306
8307               if (val >= toc->size)
8308                 continue;
8309
8310               /* Anything in the toc ought to be aligned to 8 bytes.
8311                  If not, don't mark as unused.  */
8312               if (val & 7)
8313                 continue;
8314
8315               if (skip == NULL)
8316                 {
8317                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8318                   if (skip == NULL)
8319                     goto error_ret;
8320                 }
8321
8322               skip[val >> 3] = ref_from_discarded;
8323             }
8324
8325           if (elf_section_data (sec)->relocs != relstart)
8326             free (relstart);
8327         }
8328
8329       /* For largetoc loads of address constants, we can convert
8330          .  addis rx,2,addr@got@ha
8331          .  ld ry,addr@got@l(rx)
8332          to
8333          .  addis rx,2,addr@toc@ha
8334          .  addi ry,rx,addr@toc@l
8335          when addr is within 2G of the toc pointer.  This then means
8336          that the word storing "addr" in the toc is no longer needed.  */
8337
8338       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8339           && toc->output_section->rawsize < (bfd_vma) 1 << 31
8340           && toc->reloc_count != 0)
8341         {
8342           /* Read toc relocs.  */
8343           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8344                                                   info->keep_memory);
8345           if (toc_relocs == NULL)
8346             goto error_ret;
8347
8348           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8349             {
8350               enum elf_ppc64_reloc_type r_type;
8351               unsigned long r_symndx;
8352               asection *sym_sec;
8353               struct elf_link_hash_entry *h;
8354               Elf_Internal_Sym *sym;
8355               bfd_vma val, addr;
8356
8357               r_type = ELF64_R_TYPE (rel->r_info);
8358               if (r_type != R_PPC64_ADDR64)
8359                 continue;
8360
8361               r_symndx = ELF64_R_SYM (rel->r_info);
8362               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8363                               r_symndx, ibfd))
8364                 goto error_ret;
8365
8366               if (sym_sec == NULL
8367                   || discarded_section (sym_sec))
8368                 continue;
8369
8370               if (!SYMBOL_CALLS_LOCAL (info, h))
8371                 continue;
8372
8373               if (h != NULL)
8374                 {
8375                   if (h->type == STT_GNU_IFUNC)
8376                     continue;
8377                   val = h->root.u.def.value;
8378                 }
8379               else
8380                 {
8381                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8382                     continue;
8383                   val = sym->st_value;
8384                 }
8385               val += rel->r_addend;
8386               val += sym_sec->output_section->vma + sym_sec->output_offset;
8387
8388               /* We don't yet know the exact toc pointer value, but we
8389                  know it will be somewhere in the toc section.  Don't
8390                  optimize if the difference from any possible toc
8391                  pointer is outside [ff..f80008000, 7fff7fff].  */
8392               addr = toc->output_section->vma + TOC_BASE_OFF;
8393               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8394                 continue;
8395
8396               addr = toc->output_section->vma + toc->output_section->rawsize;
8397               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8398                 continue;
8399
8400               if (skip == NULL)
8401                 {
8402                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8403                   if (skip == NULL)
8404                     goto error_ret;
8405                 }
8406
8407               skip[rel->r_offset >> 3]
8408                 |= can_optimize | ((rel - toc_relocs) << 2);
8409             }
8410         }
8411
8412       if (skip == NULL)
8413         continue;
8414
8415       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8416       if (used == NULL)
8417         {
8418         error_ret:
8419           if (local_syms != NULL
8420               && symtab_hdr->contents != (unsigned char *) local_syms)
8421             free (local_syms);
8422           if (sec != NULL
8423               && relstart != NULL
8424               && elf_section_data (sec)->relocs != relstart)
8425             free (relstart);
8426           if (toc_relocs != NULL
8427               && elf_section_data (toc)->relocs != toc_relocs)
8428             free (toc_relocs);
8429           if (skip != NULL)
8430             free (skip);
8431           return FALSE;
8432         }
8433
8434       /* Now check all kept sections that might reference the toc.
8435          Check the toc itself last.  */
8436       for (sec = (ibfd->sections == toc && toc->next ? toc->next
8437                   : ibfd->sections);
8438            sec != NULL;
8439            sec = (sec == toc ? NULL
8440                   : sec->next == NULL ? toc
8441                   : sec->next == toc && toc->next ? toc->next
8442                   : sec->next))
8443         {
8444           int repeat;
8445
8446           if (sec->reloc_count == 0
8447               || discarded_section (sec)
8448               || get_opd_info (sec)
8449               || (sec->flags & SEC_ALLOC) == 0
8450               || (sec->flags & SEC_DEBUGGING) != 0)
8451             continue;
8452
8453           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8454                                                 info->keep_memory);
8455           if (relstart == NULL)
8456             goto error_ret;
8457
8458           /* Mark toc entries referenced as used.  */
8459           do
8460             {
8461               repeat = 0;
8462               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8463                 {
8464                   enum elf_ppc64_reloc_type r_type;
8465                   unsigned long r_symndx;
8466                   asection *sym_sec;
8467                   struct elf_link_hash_entry *h;
8468                   Elf_Internal_Sym *sym;
8469                   bfd_vma val;
8470                   enum {no_check, check_lo, check_ha} insn_check;
8471
8472                   r_type = ELF64_R_TYPE (rel->r_info);
8473                   switch (r_type)
8474                     {
8475                     default:
8476                       insn_check = no_check;
8477                       break;
8478
8479                     case R_PPC64_GOT_TLSLD16_HA:
8480                     case R_PPC64_GOT_TLSGD16_HA:
8481                     case R_PPC64_GOT_TPREL16_HA:
8482                     case R_PPC64_GOT_DTPREL16_HA:
8483                     case R_PPC64_GOT16_HA:
8484                     case R_PPC64_TOC16_HA:
8485                       insn_check = check_ha;
8486                       break;
8487
8488                     case R_PPC64_GOT_TLSLD16_LO:
8489                     case R_PPC64_GOT_TLSGD16_LO:
8490                     case R_PPC64_GOT_TPREL16_LO_DS:
8491                     case R_PPC64_GOT_DTPREL16_LO_DS:
8492                     case R_PPC64_GOT16_LO:
8493                     case R_PPC64_GOT16_LO_DS:
8494                     case R_PPC64_TOC16_LO:
8495                     case R_PPC64_TOC16_LO_DS:
8496                       insn_check = check_lo;
8497                       break;
8498                     }
8499
8500                   if (insn_check != no_check)
8501                     {
8502                       bfd_vma off = rel->r_offset & ~3;
8503                       unsigned char buf[4];
8504                       unsigned int insn;
8505
8506                       if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8507                         {
8508                           free (used);
8509                           goto error_ret;
8510                         }
8511                       insn = bfd_get_32 (ibfd, buf);
8512                       if (insn_check == check_lo
8513                           ? !ok_lo_toc_insn (insn)
8514                           : ((insn & ((0x3f << 26) | 0x1f << 16))
8515                              != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8516                         {
8517                           char str[12];
8518
8519                           ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8520                           sprintf (str, "%#08x", insn);
8521                           info->callbacks->einfo
8522                             (_("%P: %H: toc optimization is not supported for"
8523                                " %s instruction.\n"),
8524                              ibfd, sec, rel->r_offset & ~3, str);
8525                         }
8526                     }
8527
8528                   switch (r_type)
8529                     {
8530                     case R_PPC64_TOC16:
8531                     case R_PPC64_TOC16_LO:
8532                     case R_PPC64_TOC16_HI:
8533                     case R_PPC64_TOC16_HA:
8534                     case R_PPC64_TOC16_DS:
8535                     case R_PPC64_TOC16_LO_DS:
8536                       /* In case we're taking addresses of toc entries.  */
8537                     case R_PPC64_ADDR64:
8538                       break;
8539
8540                     default:
8541                       continue;
8542                     }
8543
8544                   r_symndx = ELF64_R_SYM (rel->r_info);
8545                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8546                                   r_symndx, ibfd))
8547                     {
8548                       free (used);
8549                       goto error_ret;
8550                     }
8551
8552                   if (sym_sec != toc)
8553                     continue;
8554
8555                   if (h != NULL)
8556                     val = h->root.u.def.value;
8557                   else
8558                     val = sym->st_value;
8559                   val += rel->r_addend;
8560
8561                   if (val >= toc->size)
8562                     continue;
8563
8564                   if ((skip[val >> 3] & can_optimize) != 0)
8565                     {
8566                       bfd_vma off;
8567                       unsigned char opc;
8568
8569                       switch (r_type)
8570                         {
8571                         case R_PPC64_TOC16_HA:
8572                           break;
8573
8574                         case R_PPC64_TOC16_LO_DS:
8575                           off = rel->r_offset;
8576                           off += (bfd_big_endian (ibfd) ? -2 : 3);
8577                           if (!bfd_get_section_contents (ibfd, sec, &opc,
8578                                                          off, 1))
8579                             {
8580                               free (used);
8581                               goto error_ret;
8582                             }
8583                           if ((opc & (0x3f << 2)) == (58u << 2))
8584                             break;
8585                           /* Fall thru */
8586
8587                         default:
8588                           /* Wrong sort of reloc, or not a ld.  We may
8589                              as well clear ref_from_discarded too.  */
8590                           skip[val >> 3] = 0;
8591                         }
8592                     }
8593
8594                   if (sec != toc)
8595                     used[val >> 3] = 1;
8596                   /* For the toc section, we only mark as used if this
8597                      entry itself isn't unused.  */
8598                   else if ((used[rel->r_offset >> 3]
8599                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8600                            && !used[val >> 3])
8601                     {
8602                       /* Do all the relocs again, to catch reference
8603                          chains.  */
8604                       repeat = 1;
8605                       used[val >> 3] = 1;
8606                     }
8607                 }
8608             }
8609           while (repeat);
8610
8611           if (elf_section_data (sec)->relocs != relstart)
8612             free (relstart);
8613         }
8614
8615       /* Merge the used and skip arrays.  Assume that TOC
8616          doublewords not appearing as either used or unused belong
8617          to to an entry more than one doubleword in size.  */
8618       for (drop = skip, keep = used, last = 0, some_unused = 0;
8619            drop < skip + (toc->size + 7) / 8;
8620            ++drop, ++keep)
8621         {
8622           if (*keep)
8623             {
8624               *drop &= ~ref_from_discarded;
8625               if ((*drop & can_optimize) != 0)
8626                 some_unused = 1;
8627               last = 0;
8628             }
8629           else if ((*drop & ref_from_discarded) != 0)
8630             {
8631               some_unused = 1;
8632               last = ref_from_discarded;
8633             }
8634           else
8635             *drop = last;
8636         }
8637
8638       free (used);
8639
8640       if (some_unused)
8641         {
8642           bfd_byte *contents, *src;
8643           unsigned long off;
8644           Elf_Internal_Sym *sym;
8645           bfd_boolean local_toc_syms = FALSE;
8646
8647           /* Shuffle the toc contents, and at the same time convert the
8648              skip array from booleans into offsets.  */
8649           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8650             goto error_ret;
8651
8652           elf_section_data (toc)->this_hdr.contents = contents;
8653
8654           for (src = contents, off = 0, drop = skip;
8655                src < contents + toc->size;
8656                src += 8, ++drop)
8657             {
8658               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8659                 off += 8;
8660               else if (off != 0)
8661                 {
8662                   *drop = off;
8663                   memcpy (src - off, src, 8);
8664                 }
8665             }
8666           *drop = off;
8667           toc->rawsize = toc->size;
8668           toc->size = src - contents - off;
8669
8670           /* Adjust addends for relocs against the toc section sym,
8671              and optimize any accesses we can.  */
8672           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8673             {
8674               if (sec->reloc_count == 0
8675                   || discarded_section (sec))
8676                 continue;
8677
8678               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8679                                                     info->keep_memory);
8680               if (relstart == NULL)
8681                 goto error_ret;
8682
8683               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8684                 {
8685                   enum elf_ppc64_reloc_type r_type;
8686                   unsigned long r_symndx;
8687                   asection *sym_sec;
8688                   struct elf_link_hash_entry *h;
8689                   bfd_vma val;
8690
8691                   r_type = ELF64_R_TYPE (rel->r_info);
8692                   switch (r_type)
8693                     {
8694                     default:
8695                       continue;
8696
8697                     case R_PPC64_TOC16:
8698                     case R_PPC64_TOC16_LO:
8699                     case R_PPC64_TOC16_HI:
8700                     case R_PPC64_TOC16_HA:
8701                     case R_PPC64_TOC16_DS:
8702                     case R_PPC64_TOC16_LO_DS:
8703                     case R_PPC64_ADDR64:
8704                       break;
8705                     }
8706
8707                   r_symndx = ELF64_R_SYM (rel->r_info);
8708                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8709                                   r_symndx, ibfd))
8710                     goto error_ret;
8711
8712                   if (sym_sec != toc)
8713                     continue;
8714
8715                   if (h != NULL)
8716                     val = h->root.u.def.value;
8717                   else
8718                     {
8719                       val = sym->st_value;
8720                       if (val != 0)
8721                         local_toc_syms = TRUE;
8722                     }
8723
8724                   val += rel->r_addend;
8725
8726                   if (val > toc->rawsize)
8727                     val = toc->rawsize;
8728                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
8729                     continue;
8730                   else if ((skip[val >> 3] & can_optimize) != 0)
8731                     {
8732                       Elf_Internal_Rela *tocrel
8733                         = toc_relocs + (skip[val >> 3] >> 2);
8734                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8735
8736                       switch (r_type)
8737                         {
8738                         case R_PPC64_TOC16_HA:
8739                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8740                           break;
8741
8742                         case R_PPC64_TOC16_LO_DS:
8743                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8744                           break;
8745
8746                         default:
8747                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8748                             ppc_howto_init ();
8749                           info->callbacks->einfo
8750                             (_("%P: %H: %s references "
8751                                "optimized away TOC entry\n"),
8752                              ibfd, sec, rel->r_offset,
8753                              ppc64_elf_howto_table[r_type]->name);
8754                           bfd_set_error (bfd_error_bad_value);
8755                           goto error_ret;
8756                         }
8757                       rel->r_addend = tocrel->r_addend;
8758                       elf_section_data (sec)->relocs = relstart;
8759                       continue;
8760                     }
8761
8762                   if (h != NULL || sym->st_value != 0)
8763                     continue;
8764
8765                   rel->r_addend -= skip[val >> 3];
8766                   elf_section_data (sec)->relocs = relstart;
8767                 }
8768
8769               if (elf_section_data (sec)->relocs != relstart)
8770                 free (relstart);
8771             }
8772
8773           /* We shouldn't have local or global symbols defined in the TOC,
8774              but handle them anyway.  */
8775           if (local_syms != NULL)
8776             for (sym = local_syms;
8777                  sym < local_syms + symtab_hdr->sh_info;
8778                  ++sym)
8779               if (sym->st_value != 0
8780                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8781                 {
8782                   unsigned long i;
8783
8784                   if (sym->st_value > toc->rawsize)
8785                     i = toc->rawsize >> 3;
8786                   else
8787                     i = sym->st_value >> 3;
8788
8789                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8790                     {
8791                       if (local_toc_syms)
8792                         (*_bfd_error_handler)
8793                           (_("%s defined on removed toc entry"),
8794                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8795                       do
8796                         ++i;
8797                       while ((skip[i] & (ref_from_discarded | can_optimize)));
8798                       sym->st_value = (bfd_vma) i << 3;
8799                     }
8800
8801                   sym->st_value -= skip[i];
8802                   symtab_hdr->contents = (unsigned char *) local_syms;
8803                 }
8804
8805           /* Adjust any global syms defined in this toc input section.  */
8806           if (toc_inf.global_toc_syms)
8807             {
8808               toc_inf.toc = toc;
8809               toc_inf.skip = skip;
8810               toc_inf.global_toc_syms = FALSE;
8811               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8812                                       &toc_inf);
8813             }
8814
8815           if (toc->reloc_count != 0)
8816             {
8817               Elf_Internal_Shdr *rel_hdr;
8818               Elf_Internal_Rela *wrel;
8819               bfd_size_type sz;
8820
8821               /* Remove unused toc relocs, and adjust those we keep.  */
8822               if (toc_relocs == NULL)
8823                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8824                                                         info->keep_memory);
8825               if (toc_relocs == NULL)
8826                 goto error_ret;
8827
8828               wrel = toc_relocs;
8829               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8830                 if ((skip[rel->r_offset >> 3]
8831                      & (ref_from_discarded | can_optimize)) == 0)
8832                   {
8833                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8834                     wrel->r_info = rel->r_info;
8835                     wrel->r_addend = rel->r_addend;
8836                     ++wrel;
8837                   }
8838                 else if (!dec_dynrel_count (rel->r_info, toc, info,
8839                                             &local_syms, NULL, NULL))
8840                   goto error_ret;
8841
8842               elf_section_data (toc)->relocs = toc_relocs;
8843               toc->reloc_count = wrel - toc_relocs;
8844               rel_hdr = _bfd_elf_single_rel_hdr (toc);
8845               sz = rel_hdr->sh_entsize;
8846               rel_hdr->sh_size = toc->reloc_count * sz;
8847             }
8848         }
8849       else if (toc_relocs != NULL
8850                && elf_section_data (toc)->relocs != toc_relocs)
8851         free (toc_relocs);
8852
8853       if (local_syms != NULL
8854           && symtab_hdr->contents != (unsigned char *) local_syms)
8855         {
8856           if (!info->keep_memory)
8857             free (local_syms);
8858           else
8859             symtab_hdr->contents = (unsigned char *) local_syms;
8860         }
8861       free (skip);
8862     }
8863
8864   return TRUE;
8865 }
8866
8867 /* Return true iff input section I references the TOC using
8868    instructions limited to +/-32k offsets.  */
8869
8870 bfd_boolean
8871 ppc64_elf_has_small_toc_reloc (asection *i)
8872 {
8873   return (is_ppc64_elf (i->owner)
8874           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8875 }
8876
8877 /* Allocate space for one GOT entry.  */
8878
8879 static void
8880 allocate_got (struct elf_link_hash_entry *h,
8881               struct bfd_link_info *info,
8882               struct got_entry *gent)
8883 {
8884   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8885   bfd_boolean dyn;
8886   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8887   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8888                  ? 16 : 8);
8889   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8890                   ? 2 : 1) * sizeof (Elf64_External_Rela);
8891   asection *got = ppc64_elf_tdata (gent->owner)->got;
8892
8893   gent->got.offset = got->size;
8894   got->size += entsize;
8895
8896   dyn = htab->elf.dynamic_sections_created;
8897   if (h->type == STT_GNU_IFUNC)
8898     {
8899       htab->reliplt->size += rentsize;
8900       htab->got_reli_size += rentsize;
8901     }
8902   else if ((info->shared
8903             || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8904            && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8905                || h->root.type != bfd_link_hash_undefweak))
8906     {
8907       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8908       relgot->size += rentsize;
8909     }
8910 }
8911
8912 /* This function merges got entries in the same toc group.  */
8913
8914 static void
8915 merge_got_entries (struct got_entry **pent)
8916 {
8917   struct got_entry *ent, *ent2;
8918
8919   for (ent = *pent; ent != NULL; ent = ent->next)
8920     if (!ent->is_indirect)
8921       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8922         if (!ent2->is_indirect
8923             && ent2->addend == ent->addend
8924             && ent2->tls_type == ent->tls_type
8925             && elf_gp (ent2->owner) == elf_gp (ent->owner))
8926           {
8927             ent2->is_indirect = TRUE;
8928             ent2->got.ent = ent;
8929           }
8930 }
8931
8932 /* Allocate space in .plt, .got and associated reloc sections for
8933    dynamic relocs.  */
8934
8935 static bfd_boolean
8936 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8937 {
8938   struct bfd_link_info *info;
8939   struct ppc_link_hash_table *htab;
8940   asection *s;
8941   struct ppc_link_hash_entry *eh;
8942   struct elf_dyn_relocs *p;
8943   struct got_entry **pgent, *gent;
8944
8945   if (h->root.type == bfd_link_hash_indirect)
8946     return TRUE;
8947
8948   info = (struct bfd_link_info *) inf;
8949   htab = ppc_hash_table (info);
8950   if (htab == NULL)
8951     return FALSE;
8952
8953   if ((htab->elf.dynamic_sections_created
8954        && h->dynindx != -1
8955        && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
8956       || h->type == STT_GNU_IFUNC)
8957     {
8958       struct plt_entry *pent;
8959       bfd_boolean doneone = FALSE;
8960       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8961         if (pent->plt.refcount > 0)
8962           {
8963             if (!htab->elf.dynamic_sections_created
8964                 || h->dynindx == -1)
8965               {
8966                 s = htab->iplt;
8967                 pent->plt.offset = s->size;
8968                 s->size += PLT_ENTRY_SIZE;
8969                 s = htab->reliplt;
8970               }
8971             else
8972               {
8973                 /* If this is the first .plt entry, make room for the special
8974                    first entry.  */
8975                 s = htab->plt;
8976                 if (s->size == 0)
8977                   s->size += PLT_INITIAL_ENTRY_SIZE;
8978
8979                 pent->plt.offset = s->size;
8980
8981                 /* Make room for this entry.  */
8982                 s->size += PLT_ENTRY_SIZE;
8983
8984                 /* Make room for the .glink code.  */
8985                 s = htab->glink;
8986                 if (s->size == 0)
8987                   s->size += GLINK_CALL_STUB_SIZE;
8988                 /* We need bigger stubs past index 32767.  */
8989                 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
8990                   s->size += 4;
8991                 s->size += 2*4;
8992
8993                 /* We also need to make an entry in the .rela.plt section.  */
8994                 s = htab->relplt;
8995               }
8996             s->size += sizeof (Elf64_External_Rela);
8997             doneone = TRUE;
8998           }
8999         else
9000           pent->plt.offset = (bfd_vma) -1;
9001       if (!doneone)
9002         {
9003           h->plt.plist = NULL;
9004           h->needs_plt = 0;
9005         }
9006     }
9007   else
9008     {
9009       h->plt.plist = NULL;
9010       h->needs_plt = 0;
9011     }
9012
9013   eh = (struct ppc_link_hash_entry *) h;
9014   /* Run through the TLS GD got entries first if we're changing them
9015      to TPREL.  */
9016   if ((eh->tls_mask & TLS_TPRELGD) != 0)
9017     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9018       if (gent->got.refcount > 0
9019           && (gent->tls_type & TLS_GD) != 0)
9020         {
9021           /* This was a GD entry that has been converted to TPREL.  If
9022              there happens to be a TPREL entry we can use that one.  */
9023           struct got_entry *ent;
9024           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9025             if (ent->got.refcount > 0
9026                 && (ent->tls_type & TLS_TPREL) != 0
9027                 && ent->addend == gent->addend
9028                 && ent->owner == gent->owner)
9029               {
9030                 gent->got.refcount = 0;
9031                 break;
9032               }
9033
9034           /* If not, then we'll be using our own TPREL entry.  */
9035           if (gent->got.refcount != 0)
9036             gent->tls_type = TLS_TLS | TLS_TPREL;
9037         }
9038
9039   /* Remove any list entry that won't generate a word in the GOT before
9040      we call merge_got_entries.  Otherwise we risk merging to empty
9041      entries.  */
9042   pgent = &h->got.glist;
9043   while ((gent = *pgent) != NULL)
9044     if (gent->got.refcount > 0)
9045       {
9046         if ((gent->tls_type & TLS_LD) != 0
9047             && !h->def_dynamic)
9048           {
9049             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9050             *pgent = gent->next;
9051           }
9052         else
9053           pgent = &gent->next;
9054       }
9055     else
9056       *pgent = gent->next;
9057
9058   if (!htab->do_multi_toc)
9059     merge_got_entries (&h->got.glist);
9060
9061   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9062     if (!gent->is_indirect)
9063       {
9064         /* Make sure this symbol is output as a dynamic symbol.
9065            Undefined weak syms won't yet be marked as dynamic,
9066            nor will all TLS symbols.  */
9067         if (h->dynindx == -1
9068             && !h->forced_local
9069             && h->type != STT_GNU_IFUNC
9070             && htab->elf.dynamic_sections_created)
9071           {
9072             if (! bfd_elf_link_record_dynamic_symbol (info, h))
9073               return FALSE;
9074           }
9075
9076         if (!is_ppc64_elf (gent->owner))
9077           abort ();
9078
9079         allocate_got (h, info, gent);
9080       }
9081
9082   if (eh->dyn_relocs == NULL
9083       || (!htab->elf.dynamic_sections_created
9084           && h->type != STT_GNU_IFUNC))
9085     return TRUE;
9086
9087   /* In the shared -Bsymbolic case, discard space allocated for
9088      dynamic pc-relative relocs against symbols which turn out to be
9089      defined in regular objects.  For the normal shared case, discard
9090      space for relocs that have become local due to symbol visibility
9091      changes.  */
9092
9093   if (info->shared)
9094     {
9095       /* Relocs that use pc_count are those that appear on a call insn,
9096          or certain REL relocs (see must_be_dyn_reloc) that can be
9097          generated via assembly.  We want calls to protected symbols to
9098          resolve directly to the function rather than going via the plt.
9099          If people want function pointer comparisons to work as expected
9100          then they should avoid writing weird assembly.  */
9101       if (SYMBOL_CALLS_LOCAL (info, h))
9102         {
9103           struct elf_dyn_relocs **pp;
9104
9105           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9106             {
9107               p->count -= p->pc_count;
9108               p->pc_count = 0;
9109               if (p->count == 0)
9110                 *pp = p->next;
9111               else
9112                 pp = &p->next;
9113             }
9114         }
9115
9116       /* Also discard relocs on undefined weak syms with non-default
9117          visibility.  */
9118       if (eh->dyn_relocs != NULL
9119           && h->root.type == bfd_link_hash_undefweak)
9120         {
9121           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9122             eh->dyn_relocs = NULL;
9123
9124           /* Make sure this symbol is output as a dynamic symbol.
9125              Undefined weak syms won't yet be marked as dynamic.  */
9126           else if (h->dynindx == -1
9127                    && !h->forced_local)
9128             {
9129               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9130                 return FALSE;
9131             }
9132         }
9133     }
9134   else if (h->type == STT_GNU_IFUNC)
9135     {
9136       if (!h->non_got_ref)
9137         eh->dyn_relocs = NULL;
9138     }
9139   else if (ELIMINATE_COPY_RELOCS)
9140     {
9141       /* For the non-shared case, discard space for relocs against
9142          symbols which turn out to need copy relocs or are not
9143          dynamic.  */
9144
9145       if (!h->non_got_ref
9146           && !h->def_regular)
9147         {
9148           /* Make sure this symbol is output as a dynamic symbol.
9149              Undefined weak syms won't yet be marked as dynamic.  */
9150           if (h->dynindx == -1
9151               && !h->forced_local)
9152             {
9153               if (! bfd_elf_link_record_dynamic_symbol (info, h))
9154                 return FALSE;
9155             }
9156
9157           /* If that succeeded, we know we'll be keeping all the
9158              relocs.  */
9159           if (h->dynindx != -1)
9160             goto keep;
9161         }
9162
9163       eh->dyn_relocs = NULL;
9164
9165     keep: ;
9166     }
9167
9168   /* Finally, allocate space.  */
9169   for (p = eh->dyn_relocs; p != NULL; p = p->next)
9170     {
9171       asection *sreloc = elf_section_data (p->sec)->sreloc;
9172       if (eh->elf.type == STT_GNU_IFUNC)
9173         sreloc = htab->reliplt;
9174       sreloc->size += p->count * sizeof (Elf64_External_Rela);
9175     }
9176
9177   return TRUE;
9178 }
9179
9180 /* Find any dynamic relocs that apply to read-only sections.  */
9181
9182 static bfd_boolean
9183 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9184 {
9185   struct ppc_link_hash_entry *eh;
9186   struct elf_dyn_relocs *p;
9187
9188   eh = (struct ppc_link_hash_entry *) h;
9189   for (p = eh->dyn_relocs; p != NULL; p = p->next)
9190     {
9191       asection *s = p->sec->output_section;
9192
9193       if (s != NULL && (s->flags & SEC_READONLY) != 0)
9194         {
9195           struct bfd_link_info *info = inf;
9196
9197           info->flags |= DF_TEXTREL;
9198
9199           /* Not an error, just cut short the traversal.  */
9200           return FALSE;
9201         }
9202     }
9203   return TRUE;
9204 }
9205
9206 /* Set the sizes of the dynamic sections.  */
9207
9208 static bfd_boolean
9209 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
9210                                  struct bfd_link_info *info)
9211 {
9212   struct ppc_link_hash_table *htab;
9213   bfd *dynobj;
9214   asection *s;
9215   bfd_boolean relocs;
9216   bfd *ibfd;
9217   struct got_entry *first_tlsld;
9218
9219   htab = ppc_hash_table (info);
9220   if (htab == NULL)
9221     return FALSE;
9222
9223   dynobj = htab->elf.dynobj;
9224   if (dynobj == NULL)
9225     abort ();
9226
9227   if (htab->elf.dynamic_sections_created)
9228     {
9229       /* Set the contents of the .interp section to the interpreter.  */
9230       if (info->executable)
9231         {
9232           s = bfd_get_linker_section (dynobj, ".interp");
9233           if (s == NULL)
9234             abort ();
9235           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9236           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9237         }
9238     }
9239
9240   /* Set up .got offsets for local syms, and space for local dynamic
9241      relocs.  */
9242   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9243     {
9244       struct got_entry **lgot_ents;
9245       struct got_entry **end_lgot_ents;
9246       struct plt_entry **local_plt;
9247       struct plt_entry **end_local_plt;
9248       unsigned char *lgot_masks;
9249       bfd_size_type locsymcount;
9250       Elf_Internal_Shdr *symtab_hdr;
9251
9252       if (!is_ppc64_elf (ibfd))
9253         continue;
9254
9255       for (s = ibfd->sections; s != NULL; s = s->next)
9256         {
9257           struct ppc_dyn_relocs *p;
9258
9259           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9260             {
9261               if (!bfd_is_abs_section (p->sec)
9262                   && bfd_is_abs_section (p->sec->output_section))
9263                 {
9264                   /* Input section has been discarded, either because
9265                      it is a copy of a linkonce section or due to
9266                      linker script /DISCARD/, so we'll be discarding
9267                      the relocs too.  */
9268                 }
9269               else if (p->count != 0)
9270                 {
9271                   asection *srel = elf_section_data (p->sec)->sreloc;
9272                   if (p->ifunc)
9273                     srel = htab->reliplt;
9274                   srel->size += p->count * sizeof (Elf64_External_Rela);
9275                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9276                     info->flags |= DF_TEXTREL;
9277                 }
9278             }
9279         }
9280
9281       lgot_ents = elf_local_got_ents (ibfd);
9282       if (!lgot_ents)
9283         continue;
9284
9285       symtab_hdr = &elf_symtab_hdr (ibfd);
9286       locsymcount = symtab_hdr->sh_info;
9287       end_lgot_ents = lgot_ents + locsymcount;
9288       local_plt = (struct plt_entry **) end_lgot_ents;
9289       end_local_plt = local_plt + locsymcount;
9290       lgot_masks = (unsigned char *) end_local_plt;
9291       s = ppc64_elf_tdata (ibfd)->got;
9292       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9293         {
9294           struct got_entry **pent, *ent;
9295
9296           pent = lgot_ents;
9297           while ((ent = *pent) != NULL)
9298             if (ent->got.refcount > 0)
9299               {
9300                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9301                   {
9302                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
9303                     *pent = ent->next;
9304                   }
9305                 else
9306                   {
9307                     unsigned int ent_size = 8;
9308                     unsigned int rel_size = sizeof (Elf64_External_Rela);
9309
9310                     ent->got.offset = s->size;
9311                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9312                       {
9313                         ent_size *= 2;
9314                         rel_size *= 2;
9315                       }
9316                     s->size += ent_size;
9317                     if ((*lgot_masks & PLT_IFUNC) != 0)
9318                       {
9319                         htab->reliplt->size += rel_size;
9320                         htab->got_reli_size += rel_size;
9321                       }
9322                     else if (info->shared)
9323                       {
9324                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9325                         srel->size += rel_size;
9326                       }
9327                     pent = &ent->next;
9328                   }
9329               }
9330             else
9331               *pent = ent->next;
9332         }
9333
9334       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
9335       for (; local_plt < end_local_plt; ++local_plt)
9336         {
9337           struct plt_entry *ent;
9338
9339           for (ent = *local_plt; ent != NULL; ent = ent->next)
9340             if (ent->plt.refcount > 0)
9341               {
9342                 s = htab->iplt;
9343                 ent->plt.offset = s->size;
9344                 s->size += PLT_ENTRY_SIZE;
9345
9346                 htab->reliplt->size += sizeof (Elf64_External_Rela);
9347               }
9348             else
9349               ent->plt.offset = (bfd_vma) -1;
9350         }
9351     }
9352
9353   /* Allocate global sym .plt and .got entries, and space for global
9354      sym dynamic relocs.  */
9355   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9356
9357   first_tlsld = NULL;
9358   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9359     {
9360       struct got_entry *ent;
9361
9362       if (!is_ppc64_elf (ibfd))
9363         continue;
9364
9365       ent = ppc64_tlsld_got (ibfd);
9366       if (ent->got.refcount > 0)
9367         {
9368           if (!htab->do_multi_toc && first_tlsld != NULL)
9369             {
9370               ent->is_indirect = TRUE;
9371               ent->got.ent = first_tlsld;
9372             }
9373           else
9374             {
9375               if (first_tlsld == NULL)
9376                 first_tlsld = ent;
9377               s = ppc64_elf_tdata (ibfd)->got;
9378               ent->got.offset = s->size;
9379               ent->owner = ibfd;
9380               s->size += 16;
9381               if (info->shared)
9382                 {
9383                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9384                   srel->size += sizeof (Elf64_External_Rela);
9385                 }
9386             }
9387         }
9388       else
9389         ent->got.offset = (bfd_vma) -1;
9390     }
9391
9392   /* We now have determined the sizes of the various dynamic sections.
9393      Allocate memory for them.  */
9394   relocs = FALSE;
9395   for (s = dynobj->sections; s != NULL; s = s->next)
9396     {
9397       if ((s->flags & SEC_LINKER_CREATED) == 0)
9398         continue;
9399
9400       if (s == htab->brlt || s == htab->relbrlt)
9401         /* These haven't been allocated yet;  don't strip.  */
9402         continue;
9403       else if (s == htab->got
9404                || s == htab->plt
9405                || s == htab->iplt
9406                || s == htab->glink
9407                || s == htab->dynbss)
9408         {
9409           /* Strip this section if we don't need it; see the
9410              comment below.  */
9411         }
9412       else if (s == htab->glink_eh_frame)
9413         {
9414           if (!bfd_is_abs_section (s->output_section))
9415             /* Not sized yet.  */
9416             continue;
9417         }
9418       else if (CONST_STRNEQ (s->name, ".rela"))
9419         {
9420           if (s->size != 0)
9421             {
9422               if (s != htab->relplt)
9423                 relocs = TRUE;
9424
9425               /* We use the reloc_count field as a counter if we need
9426                  to copy relocs into the output file.  */
9427               s->reloc_count = 0;
9428             }
9429         }
9430       else
9431         {
9432           /* It's not one of our sections, so don't allocate space.  */
9433           continue;
9434         }
9435
9436       if (s->size == 0)
9437         {
9438           /* If we don't need this section, strip it from the
9439              output file.  This is mostly to handle .rela.bss and
9440              .rela.plt.  We must create both sections in
9441              create_dynamic_sections, because they must be created
9442              before the linker maps input sections to output
9443              sections.  The linker does that before
9444              adjust_dynamic_symbol is called, and it is that
9445              function which decides whether anything needs to go
9446              into these sections.  */
9447           s->flags |= SEC_EXCLUDE;
9448           continue;
9449         }
9450
9451       if ((s->flags & SEC_HAS_CONTENTS) == 0)
9452         continue;
9453
9454       /* Allocate memory for the section contents.  We use bfd_zalloc
9455          here in case unused entries are not reclaimed before the
9456          section's contents are written out.  This should not happen,
9457          but this way if it does we get a R_PPC64_NONE reloc in .rela
9458          sections instead of garbage.
9459          We also rely on the section contents being zero when writing
9460          the GOT.  */
9461       s->contents = bfd_zalloc (dynobj, s->size);
9462       if (s->contents == NULL)
9463         return FALSE;
9464     }
9465
9466   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9467     {
9468       if (!is_ppc64_elf (ibfd))
9469         continue;
9470
9471       s = ppc64_elf_tdata (ibfd)->got;
9472       if (s != NULL && s != htab->got)
9473         {
9474           if (s->size == 0)
9475             s->flags |= SEC_EXCLUDE;
9476           else
9477             {
9478               s->contents = bfd_zalloc (ibfd, s->size);
9479               if (s->contents == NULL)
9480                 return FALSE;
9481             }
9482         }
9483       s = ppc64_elf_tdata (ibfd)->relgot;
9484       if (s != NULL)
9485         {
9486           if (s->size == 0)
9487             s->flags |= SEC_EXCLUDE;
9488           else
9489             {
9490               s->contents = bfd_zalloc (ibfd, s->size);
9491               if (s->contents == NULL)
9492                 return FALSE;
9493               relocs = TRUE;
9494               s->reloc_count = 0;
9495             }
9496         }
9497     }
9498
9499   if (htab->elf.dynamic_sections_created)
9500     {
9501       /* Add some entries to the .dynamic section.  We fill in the
9502          values later, in ppc64_elf_finish_dynamic_sections, but we
9503          must add the entries now so that we get the correct size for
9504          the .dynamic section.  The DT_DEBUG entry is filled in by the
9505          dynamic linker and used by the debugger.  */
9506 #define add_dynamic_entry(TAG, VAL) \
9507   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9508
9509       if (info->executable)
9510         {
9511           if (!add_dynamic_entry (DT_DEBUG, 0))
9512             return FALSE;
9513         }
9514
9515       if (htab->plt != NULL && htab->plt->size != 0)
9516         {
9517           if (!add_dynamic_entry (DT_PLTGOT, 0)
9518               || !add_dynamic_entry (DT_PLTRELSZ, 0)
9519               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9520               || !add_dynamic_entry (DT_JMPREL, 0)
9521               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9522             return FALSE;
9523         }
9524
9525       if (NO_OPD_RELOCS)
9526         {
9527           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9528               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9529             return FALSE;
9530         }
9531
9532       if (!htab->no_tls_get_addr_opt
9533           && htab->tls_get_addr_fd != NULL
9534           && htab->tls_get_addr_fd->elf.plt.plist != NULL
9535           && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9536         return FALSE;
9537
9538       if (relocs)
9539         {
9540           if (!add_dynamic_entry (DT_RELA, 0)
9541               || !add_dynamic_entry (DT_RELASZ, 0)
9542               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9543             return FALSE;
9544
9545           /* If any dynamic relocs apply to a read-only section,
9546              then we need a DT_TEXTREL entry.  */
9547           if ((info->flags & DF_TEXTREL) == 0)
9548             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
9549
9550           if ((info->flags & DF_TEXTREL) != 0)
9551             {
9552               if (!add_dynamic_entry (DT_TEXTREL, 0))
9553                 return FALSE;
9554             }
9555         }
9556     }
9557 #undef add_dynamic_entry
9558
9559   return TRUE;
9560 }
9561
9562 /* Determine the type of stub needed, if any, for a call.  */
9563
9564 static inline enum ppc_stub_type
9565 ppc_type_of_stub (asection *input_sec,
9566                   const Elf_Internal_Rela *rel,
9567                   struct ppc_link_hash_entry **hash,
9568                   struct plt_entry **plt_ent,
9569                   bfd_vma destination)
9570 {
9571   struct ppc_link_hash_entry *h = *hash;
9572   bfd_vma location;
9573   bfd_vma branch_offset;
9574   bfd_vma max_branch_offset;
9575   enum elf_ppc64_reloc_type r_type;
9576
9577   if (h != NULL)
9578     {
9579       struct plt_entry *ent;
9580       struct ppc_link_hash_entry *fdh = h;
9581       if (h->oh != NULL
9582           && h->oh->is_func_descriptor)
9583         {
9584           fdh = ppc_follow_link (h->oh);
9585           *hash = fdh;
9586         }
9587
9588       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9589         if (ent->addend == rel->r_addend
9590             && ent->plt.offset != (bfd_vma) -1)
9591           {
9592             *plt_ent = ent;
9593             return ppc_stub_plt_call;
9594           }
9595
9596       /* Here, we know we don't have a plt entry.  If we don't have a
9597          either a defined function descriptor or a defined entry symbol
9598          in a regular object file, then it is pointless trying to make
9599          any other type of stub.  */
9600       if (!is_static_defined (&fdh->elf)
9601           && !is_static_defined (&h->elf))
9602         return ppc_stub_none;
9603     }
9604   else if (elf_local_got_ents (input_sec->owner) != NULL)
9605     {
9606       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9607       struct plt_entry **local_plt = (struct plt_entry **)
9608         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9609       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9610
9611       if (local_plt[r_symndx] != NULL)
9612         {
9613           struct plt_entry *ent;
9614
9615           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9616             if (ent->addend == rel->r_addend
9617                 && ent->plt.offset != (bfd_vma) -1)
9618               {
9619                 *plt_ent = ent;
9620                 return ppc_stub_plt_call;
9621               }
9622         }
9623     }
9624
9625   /* Determine where the call point is.  */
9626   location = (input_sec->output_offset
9627               + input_sec->output_section->vma
9628               + rel->r_offset);
9629
9630   branch_offset = destination - location;
9631   r_type = ELF64_R_TYPE (rel->r_info);
9632
9633   /* Determine if a long branch stub is needed.  */
9634   max_branch_offset = 1 << 25;
9635   if (r_type != R_PPC64_REL24)
9636     max_branch_offset = 1 << 15;
9637
9638   if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
9639     /* We need a stub.  Figure out whether a long_branch or plt_branch
9640        is needed later.  */
9641     return ppc_stub_long_branch;
9642
9643   return ppc_stub_none;
9644 }
9645
9646 /* With power7 weakly ordered memory model, it is possible for ld.so
9647    to update a plt entry in one thread and have another thread see a
9648    stale zero toc entry.  To avoid this we need some sort of acquire
9649    barrier in the call stub.  One solution is to make the load of the
9650    toc word seem to appear to depend on the load of the function entry
9651    word.  Another solution is to test for r2 being zero, and branch to
9652    the appropriate glink entry if so.
9653
9654    .    fake dep barrier        compare
9655    .    ld 11,xxx(2)            ld 11,xxx(2)
9656    .    mtctr 11                mtctr 11
9657    .    xor 11,11,11            ld 2,xxx+8(2)
9658    .    add 2,2,11              cmpldi 2,0
9659    .    ld 2,xxx+8(2)           bnectr+
9660    .    bctr                    b <glink_entry>
9661
9662    The solution involving the compare turns out to be faster, so
9663    that's what we use unless the branch won't reach.  */
9664
9665 #define ALWAYS_USE_FAKE_DEP 0
9666 #define ALWAYS_EMIT_R2SAVE 0
9667
9668 #define PPC_LO(v) ((v) & 0xffff)
9669 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9670 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9671
9672 static inline unsigned int
9673 plt_stub_size (struct ppc_link_hash_table *htab,
9674                struct ppc_stub_hash_entry *stub_entry,
9675                bfd_vma off)
9676 {
9677   unsigned size = PLT_CALL_STUB_SIZE;
9678
9679   if (!(ALWAYS_EMIT_R2SAVE
9680         || stub_entry->stub_type == ppc_stub_plt_call_r2save))
9681     size -= 4;
9682   if (!htab->plt_static_chain)
9683     size -= 4;
9684   if (htab->plt_thread_safe)
9685     size += 8;
9686   if (PPC_HA (off) == 0)
9687     size -= 4;
9688   if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
9689     size += 4;
9690   if (stub_entry->h != NULL
9691       && (stub_entry->h == htab->tls_get_addr_fd
9692           || stub_entry->h == htab->tls_get_addr)
9693       && !htab->no_tls_get_addr_opt)
9694     size += 13 * 4;
9695   return size;
9696 }
9697
9698 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
9699    then return the padding needed to do so.  */
9700 static inline unsigned int
9701 plt_stub_pad (struct ppc_link_hash_table *htab,
9702               struct ppc_stub_hash_entry *stub_entry,
9703               bfd_vma plt_off)
9704 {
9705   int stub_align = 1 << htab->plt_stub_align;
9706   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
9707   bfd_vma stub_off = stub_entry->stub_sec->size;
9708
9709   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
9710       > (stub_size & -stub_align))
9711     return stub_align - (stub_off & (stub_align - 1));
9712   return 0;
9713 }
9714
9715 /* Build a .plt call stub.  */
9716
9717 static inline bfd_byte *
9718 build_plt_stub (struct ppc_link_hash_table *htab,
9719                 struct ppc_stub_hash_entry *stub_entry,
9720                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9721 {
9722   bfd *obfd = htab->stub_bfd;
9723   bfd_boolean plt_static_chain = htab->plt_static_chain;
9724   bfd_boolean plt_thread_safe = htab->plt_thread_safe;
9725   bfd_boolean use_fake_dep = plt_thread_safe;
9726   bfd_vma cmp_branch_off = 0;
9727
9728   if (!ALWAYS_USE_FAKE_DEP
9729       && plt_thread_safe
9730       && !(stub_entry->h != NULL
9731            && (stub_entry->h == htab->tls_get_addr_fd
9732                || stub_entry->h == htab->tls_get_addr)
9733            && !htab->no_tls_get_addr_opt))
9734     {
9735       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
9736       bfd_vma pltindex = (pltoff - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE;
9737       bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
9738       bfd_vma to, from;
9739
9740       if (pltindex > 32768)
9741         glinkoff += (pltindex - 32768) * 4;
9742       to = (glinkoff
9743             + htab->glink->output_offset
9744             + htab->glink->output_section->vma);
9745       from = (p - stub_entry->stub_sec->contents
9746               + 4 * (ALWAYS_EMIT_R2SAVE
9747                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9748               + 4 * (PPC_HA (offset) != 0)
9749               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
9750                      != PPC_HA (offset))
9751               + 4 * (plt_static_chain != 0)
9752               + 20
9753               + stub_entry->stub_sec->output_offset
9754               + stub_entry->stub_sec->output_section->vma);
9755       cmp_branch_off = to - from;
9756       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
9757     }
9758
9759   if (PPC_HA (offset) != 0)
9760     {
9761       if (r != NULL)
9762         {
9763           if (ALWAYS_EMIT_R2SAVE
9764               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9765             r[0].r_offset += 4;
9766           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9767           r[1].r_offset = r[0].r_offset + 4;
9768           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9769           r[1].r_addend = r[0].r_addend;
9770           if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9771             {
9772               r[2].r_offset = r[1].r_offset + 4;
9773               r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9774               r[2].r_addend = r[0].r_addend;
9775             }
9776           else
9777             {
9778               r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
9779               r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9780               r[2].r_addend = r[0].r_addend + 8;
9781               if (plt_static_chain)
9782                 {
9783                   r[3].r_offset = r[2].r_offset + 4;
9784                   r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9785                   r[3].r_addend = r[0].r_addend + 16;
9786                 }
9787             }
9788         }
9789       if (ALWAYS_EMIT_R2SAVE
9790           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9791         bfd_put_32 (obfd, STD_R2_40R1, p),                      p += 4;
9792       bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p),     p += 4;
9793       bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),      p += 4;
9794       if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9795         {
9796           bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p), p += 4;
9797           offset = 0;
9798         }
9799       bfd_put_32 (obfd, MTCTR_R11, p),                          p += 4;
9800       if (use_fake_dep)
9801         {
9802           bfd_put_32 (obfd, XOR_R11_R11_R11, p),                p += 4;
9803           bfd_put_32 (obfd, ADD_R12_R12_R11, p),                p += 4;
9804         }
9805       bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p),   p += 4;
9806       if (plt_static_chain)
9807         bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
9808     }
9809   else
9810     {
9811       if (r != NULL)
9812         {
9813           if (ALWAYS_EMIT_R2SAVE
9814               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9815             r[0].r_offset += 4;
9816           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9817           if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9818             {
9819               r[1].r_offset = r[0].r_offset + 4;
9820               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9821               r[1].r_addend = r[0].r_addend;
9822             }
9823           else
9824             {
9825               r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
9826               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9827               r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
9828               if (plt_static_chain)
9829                 {
9830                   r[2].r_offset = r[1].r_offset + 4;
9831                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9832                   r[2].r_addend = r[0].r_addend + 8;
9833                 }
9834             }
9835         }
9836       if (ALWAYS_EMIT_R2SAVE
9837           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9838         bfd_put_32 (obfd, STD_R2_40R1, p),                      p += 4;
9839       bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p),       p += 4;
9840       if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9841         {
9842           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
9843           offset = 0;
9844         }
9845       bfd_put_32 (obfd, MTCTR_R11, p),                          p += 4;
9846       if (use_fake_dep)
9847         {
9848           bfd_put_32 (obfd, XOR_R11_R11_R11, p),                p += 4;
9849           bfd_put_32 (obfd, ADD_R2_R2_R11, p),                  p += 4;
9850         }
9851       if (plt_static_chain)
9852         bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
9853       bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p),    p += 4;
9854     }
9855   if (plt_thread_safe && !use_fake_dep)
9856     {
9857       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
9858       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
9859       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
9860     }
9861   else
9862     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
9863   return p;
9864 }
9865
9866 /* Build a special .plt call stub for __tls_get_addr.  */
9867
9868 #define LD_R11_0R3      0xe9630000
9869 #define LD_R12_0R3      0xe9830000
9870 #define MR_R0_R3        0x7c601b78
9871 #define CMPDI_R11_0     0x2c2b0000
9872 #define ADD_R3_R12_R13  0x7c6c6a14
9873 #define BEQLR           0x4d820020
9874 #define MR_R3_R0        0x7c030378
9875 #define MFLR_R11        0x7d6802a6
9876 #define STD_R11_0R1     0xf9610000
9877 #define BCTRL           0x4e800421
9878 #define LD_R11_0R1      0xe9610000
9879 #define LD_R2_0R1       0xe8410000
9880 #define MTLR_R11        0x7d6803a6
9881
9882 static inline bfd_byte *
9883 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
9884                          struct ppc_stub_hash_entry *stub_entry,
9885                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9886 {
9887   bfd *obfd = htab->stub_bfd;
9888
9889   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
9890   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
9891   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
9892   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
9893   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
9894   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
9895   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
9896   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
9897   bfd_put_32 (obfd, STD_R11_0R1 + 32, p),       p += 4;
9898
9899   if (r != NULL)
9900     r[0].r_offset += 9 * 4;
9901   p = build_plt_stub (htab, stub_entry, p, offset, r);
9902   bfd_put_32 (obfd, BCTRL, p - 4);
9903
9904   bfd_put_32 (obfd, LD_R11_0R1 + 32, p),        p += 4;
9905   bfd_put_32 (obfd, LD_R2_0R1 + 40, p),         p += 4;
9906   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
9907   bfd_put_32 (obfd, BLR, p),                    p += 4;
9908
9909   return p;
9910 }
9911
9912 static Elf_Internal_Rela *
9913 get_relocs (asection *sec, int count)
9914 {
9915   Elf_Internal_Rela *relocs;
9916   struct bfd_elf_section_data *elfsec_data;
9917
9918   elfsec_data = elf_section_data (sec);
9919   relocs = elfsec_data->relocs;
9920   if (relocs == NULL)
9921     {
9922       bfd_size_type relsize;
9923       relsize = sec->reloc_count * sizeof (*relocs);
9924       relocs = bfd_alloc (sec->owner, relsize);
9925       if (relocs == NULL)
9926         return NULL;
9927       elfsec_data->relocs = relocs;
9928       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9929                                           sizeof (Elf_Internal_Shdr));
9930       if (elfsec_data->rela.hdr == NULL)
9931         return NULL;
9932       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9933                                         * sizeof (Elf64_External_Rela));
9934       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
9935       sec->reloc_count = 0;
9936     }
9937   relocs += sec->reloc_count;
9938   sec->reloc_count += count;
9939   return relocs;
9940 }
9941
9942 static bfd_vma
9943 get_r2off (struct bfd_link_info *info,
9944            struct ppc_stub_hash_entry *stub_entry)
9945 {
9946   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9947   bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
9948
9949   if (r2off == 0)
9950     {
9951       /* Support linking -R objects.  Get the toc pointer from the
9952          opd entry.  */
9953       char buf[8];
9954       asection *opd = stub_entry->h->elf.root.u.def.section;
9955       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
9956
9957       if (strcmp (opd->name, ".opd") != 0
9958           || opd->reloc_count != 0)
9959         {
9960           info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
9961                                   stub_entry->h->elf.root.root.string);
9962           bfd_set_error (bfd_error_bad_value);
9963           return 0;
9964         }
9965       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
9966         return 0;
9967       r2off = bfd_get_64 (opd->owner, buf);
9968       r2off -= elf_gp (info->output_bfd);
9969     }
9970   r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
9971   return r2off;
9972 }
9973
9974 static bfd_boolean
9975 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
9976 {
9977   struct ppc_stub_hash_entry *stub_entry;
9978   struct ppc_branch_hash_entry *br_entry;
9979   struct bfd_link_info *info;
9980   struct ppc_link_hash_table *htab;
9981   bfd_byte *loc;
9982   bfd_byte *p;
9983   bfd_vma dest, off;
9984   int size;
9985   Elf_Internal_Rela *r;
9986   asection *plt;
9987
9988   /* Massage our args to the form they really have.  */
9989   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
9990   info = in_arg;
9991
9992   htab = ppc_hash_table (info);
9993   if (htab == NULL)
9994     return FALSE;
9995
9996   /* Make a note of the offset within the stubs for this entry.  */
9997   stub_entry->stub_offset = stub_entry->stub_sec->size;
9998   loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
9999
10000   htab->stub_count[stub_entry->stub_type - 1] += 1;
10001   switch (stub_entry->stub_type)
10002     {
10003     case ppc_stub_long_branch:
10004     case ppc_stub_long_branch_r2off:
10005       /* Branches are relative.  This is where we are going to.  */
10006       off = dest = (stub_entry->target_value
10007                     + stub_entry->target_section->output_offset
10008                     + stub_entry->target_section->output_section->vma);
10009
10010       /* And this is where we are coming from.  */
10011       off -= (stub_entry->stub_offset
10012               + stub_entry->stub_sec->output_offset
10013               + stub_entry->stub_sec->output_section->vma);
10014
10015       size = 4;
10016       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10017         {
10018           bfd_vma r2off = get_r2off (info, stub_entry);
10019
10020           if (r2off == 0)
10021             {
10022               htab->stub_error = TRUE;
10023               return FALSE;
10024             }
10025           bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
10026           loc += 4;
10027           size = 12;
10028           if (PPC_HA (r2off) != 0)
10029             {
10030               size = 16;
10031               bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10032               loc += 4;
10033             }
10034           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
10035           loc += 4;
10036           off -= size - 4;
10037         }
10038       bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
10039
10040       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10041         {
10042           info->callbacks->einfo
10043             (_("%P: long branch stub `%s' offset overflow\n"),
10044              stub_entry->root.string);
10045           htab->stub_error = TRUE;
10046           return FALSE;
10047         }
10048
10049       if (info->emitrelocations)
10050         {
10051           r = get_relocs (stub_entry->stub_sec, 1);
10052           if (r == NULL)
10053             return FALSE;
10054           r->r_offset = loc - stub_entry->stub_sec->contents;
10055           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10056           r->r_addend = dest;
10057           if (stub_entry->h != NULL)
10058             {
10059               struct elf_link_hash_entry **hashes;
10060               unsigned long symndx;
10061               struct ppc_link_hash_entry *h;
10062
10063               hashes = elf_sym_hashes (htab->stub_bfd);
10064               if (hashes == NULL)
10065                 {
10066                   bfd_size_type hsize;
10067
10068                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10069                   hashes = bfd_zalloc (htab->stub_bfd, hsize);
10070                   if (hashes == NULL)
10071                     return FALSE;
10072                   elf_sym_hashes (htab->stub_bfd) = hashes;
10073                   htab->stub_globals = 1;
10074                 }
10075               symndx = htab->stub_globals++;
10076               h = stub_entry->h;
10077               hashes[symndx] = &h->elf;
10078               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10079               if (h->oh != NULL && h->oh->is_func)
10080                 h = ppc_follow_link (h->oh);
10081               if (h->elf.root.u.def.section != stub_entry->target_section)
10082                 /* H is an opd symbol.  The addend must be zero.  */
10083                 r->r_addend = 0;
10084               else
10085                 {
10086                   off = (h->elf.root.u.def.value
10087                          + h->elf.root.u.def.section->output_offset
10088                          + h->elf.root.u.def.section->output_section->vma);
10089                   r->r_addend -= off;
10090                 }
10091             }
10092         }
10093       break;
10094
10095     case ppc_stub_plt_branch:
10096     case ppc_stub_plt_branch_r2off:
10097       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10098                                          stub_entry->root.string + 9,
10099                                          FALSE, FALSE);
10100       if (br_entry == NULL)
10101         {
10102           info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10103                                   stub_entry->root.string);
10104           htab->stub_error = TRUE;
10105           return FALSE;
10106         }
10107
10108       dest = (stub_entry->target_value
10109               + stub_entry->target_section->output_offset
10110               + stub_entry->target_section->output_section->vma);
10111
10112       bfd_put_64 (htab->brlt->owner, dest,
10113                   htab->brlt->contents + br_entry->offset);
10114
10115       if (br_entry->iter == htab->stub_iteration)
10116         {
10117           br_entry->iter = 0;
10118
10119           if (htab->relbrlt != NULL)
10120             {
10121               /* Create a reloc for the branch lookup table entry.  */
10122               Elf_Internal_Rela rela;
10123               bfd_byte *rl;
10124
10125               rela.r_offset = (br_entry->offset
10126                                + htab->brlt->output_offset
10127                                + htab->brlt->output_section->vma);
10128               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10129               rela.r_addend = dest;
10130
10131               rl = htab->relbrlt->contents;
10132               rl += (htab->relbrlt->reloc_count++
10133                      * sizeof (Elf64_External_Rela));
10134               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10135             }
10136           else if (info->emitrelocations)
10137             {
10138               r = get_relocs (htab->brlt, 1);
10139               if (r == NULL)
10140                 return FALSE;
10141               /* brlt, being SEC_LINKER_CREATED does not go through the
10142                  normal reloc processing.  Symbols and offsets are not
10143                  translated from input file to output file form, so
10144                  set up the offset per the output file.  */
10145               r->r_offset = (br_entry->offset
10146                              + htab->brlt->output_offset
10147                              + htab->brlt->output_section->vma);
10148               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10149               r->r_addend = dest;
10150             }
10151         }
10152
10153       dest = (br_entry->offset
10154               + htab->brlt->output_offset
10155               + htab->brlt->output_section->vma);
10156
10157       off = (dest
10158              - elf_gp (htab->brlt->output_section->owner)
10159              - htab->stub_group[stub_entry->id_sec->id].toc_off);
10160
10161       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10162         {
10163           info->callbacks->einfo
10164             (_("%P: linkage table error against `%T'\n"),
10165              stub_entry->root.string);
10166           bfd_set_error (bfd_error_bad_value);
10167           htab->stub_error = TRUE;
10168           return FALSE;
10169         }
10170
10171       if (info->emitrelocations)
10172         {
10173           r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
10174           if (r == NULL)
10175             return FALSE;
10176           r[0].r_offset = loc - stub_entry->stub_sec->contents;
10177           if (bfd_big_endian (info->output_bfd))
10178             r[0].r_offset += 2;
10179           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10180             r[0].r_offset += 4;
10181           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10182           r[0].r_addend = dest;
10183           if (PPC_HA (off) != 0)
10184             {
10185               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10186               r[1].r_offset = r[0].r_offset + 4;
10187               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10188               r[1].r_addend = r[0].r_addend;
10189             }
10190         }
10191
10192       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10193         {
10194           if (PPC_HA (off) != 0)
10195             {
10196               size = 16;
10197               bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
10198               loc += 4;
10199               bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
10200             }
10201           else
10202             {
10203               size = 12;
10204               bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
10205             }
10206         }
10207       else
10208         {
10209           bfd_vma r2off = get_r2off (info, stub_entry);
10210
10211           if (r2off == 0)
10212             {
10213               htab->stub_error = TRUE;
10214               return FALSE;
10215             }
10216
10217           bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
10218           loc += 4;
10219           size = 20;
10220           if (PPC_HA (off) != 0)
10221             {
10222               size += 4;
10223               bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
10224               loc += 4;
10225               bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
10226               loc += 4;
10227             }
10228           else
10229             {
10230               bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
10231               loc += 4;
10232             }
10233
10234           if (PPC_HA (r2off) != 0)
10235             {
10236               size += 4;
10237               bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10238               loc += 4;
10239             }
10240           bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
10241         }
10242       loc += 4;
10243       bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
10244       loc += 4;
10245       bfd_put_32 (htab->stub_bfd, BCTR, loc);
10246       break;
10247
10248     case ppc_stub_plt_call:
10249     case ppc_stub_plt_call_r2save:
10250       if (stub_entry->h != NULL
10251           && stub_entry->h->is_func_descriptor
10252           && stub_entry->h->oh != NULL)
10253         {
10254           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10255
10256           /* If the old-ABI "dot-symbol" is undefined make it weak so
10257              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
10258              FIXME: We used to define the symbol on one of the call
10259              stubs instead, which is why we test symbol section id
10260              against htab->top_id in various places.  Likely all
10261              these checks could now disappear.  */
10262           if (fh->elf.root.type == bfd_link_hash_undefined)
10263             fh->elf.root.type = bfd_link_hash_undefweak;
10264           /* Stop undo_symbol_twiddle changing it back to undefined.  */
10265           fh->was_undefined = 0;
10266         }
10267
10268       /* Now build the stub.  */
10269       dest = stub_entry->plt_ent->plt.offset & ~1;
10270       if (dest >= (bfd_vma) -2)
10271         abort ();
10272
10273       plt = htab->plt;
10274       if (!htab->elf.dynamic_sections_created
10275           || stub_entry->h == NULL
10276           || stub_entry->h->elf.dynindx == -1)
10277         plt = htab->iplt;
10278
10279       dest += plt->output_offset + plt->output_section->vma;
10280
10281       if (stub_entry->h == NULL
10282           && (stub_entry->plt_ent->plt.offset & 1) == 0)
10283         {
10284           Elf_Internal_Rela rela;
10285           bfd_byte *rl;
10286
10287           rela.r_offset = dest;
10288           rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
10289           rela.r_addend = (stub_entry->target_value
10290                            + stub_entry->target_section->output_offset
10291                            + stub_entry->target_section->output_section->vma);
10292
10293           rl = (htab->reliplt->contents
10294                 + (htab->reliplt->reloc_count++
10295                    * sizeof (Elf64_External_Rela)));
10296           bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
10297           stub_entry->plt_ent->plt.offset |= 1;
10298         }
10299
10300       off = (dest
10301              - elf_gp (plt->output_section->owner)
10302              - htab->stub_group[stub_entry->id_sec->id].toc_off);
10303
10304       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10305         {
10306           info->callbacks->einfo
10307             (_("%P: linkage table error against `%T'\n"),
10308              stub_entry->h != NULL
10309              ? stub_entry->h->elf.root.root.string
10310              : "<local sym>");
10311           bfd_set_error (bfd_error_bad_value);
10312           htab->stub_error = TRUE;
10313           return FALSE;
10314         }
10315
10316       if (htab->plt_stub_align != 0)
10317         {
10318           unsigned pad = plt_stub_pad (htab, stub_entry, off);
10319
10320           stub_entry->stub_sec->size += pad;
10321           stub_entry->stub_offset = stub_entry->stub_sec->size;
10322           loc += pad;
10323         }
10324
10325       r = NULL;
10326       if (info->emitrelocations)
10327         {
10328           r = get_relocs (stub_entry->stub_sec,
10329                           (2
10330                            + (PPC_HA (off) != 0)
10331                            + (htab->plt_static_chain
10332                               && PPC_HA (off + 16) == PPC_HA (off))));
10333           if (r == NULL)
10334             return FALSE;
10335           r[0].r_offset = loc - stub_entry->stub_sec->contents;
10336           if (bfd_big_endian (info->output_bfd))
10337             r[0].r_offset += 2;
10338           r[0].r_addend = dest;
10339         }
10340       if (stub_entry->h != NULL
10341           && (stub_entry->h == htab->tls_get_addr_fd
10342               || stub_entry->h == htab->tls_get_addr)
10343           && !htab->no_tls_get_addr_opt)
10344         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
10345       else
10346         p = build_plt_stub (htab, stub_entry, loc, off, r);
10347       size = p - loc;
10348       break;
10349
10350     default:
10351       BFD_FAIL ();
10352       return FALSE;
10353     }
10354
10355   stub_entry->stub_sec->size += size;
10356
10357   if (htab->emit_stub_syms)
10358     {
10359       struct elf_link_hash_entry *h;
10360       size_t len1, len2;
10361       char *name;
10362       const char *const stub_str[] = { "long_branch",
10363                                        "long_branch_r2off",
10364                                        "plt_branch",
10365                                        "plt_branch_r2off",
10366                                        "plt_call",
10367                                        "plt_call" };
10368
10369       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10370       len2 = strlen (stub_entry->root.string);
10371       name = bfd_malloc (len1 + len2 + 2);
10372       if (name == NULL)
10373         return FALSE;
10374       memcpy (name, stub_entry->root.string, 9);
10375       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10376       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10377       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
10378       if (h == NULL)
10379         return FALSE;
10380       if (h->root.type == bfd_link_hash_new)
10381         {
10382           h->root.type = bfd_link_hash_defined;
10383           h->root.u.def.section = stub_entry->stub_sec;
10384           h->root.u.def.value = stub_entry->stub_offset;
10385           h->ref_regular = 1;
10386           h->def_regular = 1;
10387           h->ref_regular_nonweak = 1;
10388           h->forced_local = 1;
10389           h->non_elf = 0;
10390         }
10391     }
10392
10393   return TRUE;
10394 }
10395
10396 /* As above, but don't actually build the stub.  Just bump offset so
10397    we know stub section sizes, and select plt_branch stubs where
10398    long_branch stubs won't do.  */
10399
10400 static bfd_boolean
10401 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10402 {
10403   struct ppc_stub_hash_entry *stub_entry;
10404   struct bfd_link_info *info;
10405   struct ppc_link_hash_table *htab;
10406   bfd_vma off;
10407   int size;
10408
10409   /* Massage our args to the form they really have.  */
10410   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10411   info = in_arg;
10412
10413   htab = ppc_hash_table (info);
10414   if (htab == NULL)
10415     return FALSE;
10416
10417   if (stub_entry->stub_type == ppc_stub_plt_call
10418       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10419     {
10420       asection *plt;
10421       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
10422       if (off >= (bfd_vma) -2)
10423         abort ();
10424       plt = htab->plt;
10425       if (!htab->elf.dynamic_sections_created
10426           || stub_entry->h == NULL
10427           || stub_entry->h->elf.dynindx == -1)
10428         plt = htab->iplt;
10429       off += (plt->output_offset
10430               + plt->output_section->vma
10431               - elf_gp (plt->output_section->owner)
10432               - htab->stub_group[stub_entry->id_sec->id].toc_off);
10433
10434       size = plt_stub_size (htab, stub_entry, off);
10435       if (htab->plt_stub_align)
10436         size += plt_stub_pad (htab, stub_entry, off);
10437       if (info->emitrelocations)
10438         {
10439           stub_entry->stub_sec->reloc_count
10440             += (2
10441                 + (PPC_HA (off) != 0)
10442                 + (htab->plt_static_chain
10443                    && PPC_HA (off + 16) == PPC_HA (off)));
10444           stub_entry->stub_sec->flags |= SEC_RELOC;
10445         }
10446     }
10447   else
10448     {
10449       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10450          variants.  */
10451       bfd_vma r2off = 0;
10452
10453       off = (stub_entry->target_value
10454              + stub_entry->target_section->output_offset
10455              + stub_entry->target_section->output_section->vma);
10456       off -= (stub_entry->stub_sec->size
10457               + stub_entry->stub_sec->output_offset
10458               + stub_entry->stub_sec->output_section->vma);
10459
10460       /* Reset the stub type from the plt variant in case we now
10461          can reach with a shorter stub.  */
10462       if (stub_entry->stub_type >= ppc_stub_plt_branch)
10463         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10464
10465       size = 4;
10466       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10467         {
10468           r2off = get_r2off (info, stub_entry);
10469           if (r2off == 0)
10470             {
10471               htab->stub_error = TRUE;
10472               return FALSE;
10473             }
10474           size = 12;
10475           if (PPC_HA (r2off) != 0)
10476             size = 16;
10477           off -= size - 4;
10478         }
10479
10480       /* If the branch offset if too big, use a ppc_stub_plt_branch.  */
10481       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10482         {
10483           struct ppc_branch_hash_entry *br_entry;
10484
10485           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10486                                              stub_entry->root.string + 9,
10487                                              TRUE, FALSE);
10488           if (br_entry == NULL)
10489             {
10490               info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
10491                                       stub_entry->root.string);
10492               htab->stub_error = TRUE;
10493               return FALSE;
10494             }
10495
10496           if (br_entry->iter != htab->stub_iteration)
10497             {
10498               br_entry->iter = htab->stub_iteration;
10499               br_entry->offset = htab->brlt->size;
10500               htab->brlt->size += 8;
10501
10502               if (htab->relbrlt != NULL)
10503                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10504               else if (info->emitrelocations)
10505                 {
10506                   htab->brlt->reloc_count += 1;
10507                   htab->brlt->flags |= SEC_RELOC;
10508                 }
10509             }
10510
10511           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
10512           off = (br_entry->offset
10513                  + htab->brlt->output_offset
10514                  + htab->brlt->output_section->vma
10515                  - elf_gp (htab->brlt->output_section->owner)
10516                  - htab->stub_group[stub_entry->id_sec->id].toc_off);
10517
10518           if (info->emitrelocations)
10519             {
10520               stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10521               stub_entry->stub_sec->flags |= SEC_RELOC;
10522             }
10523
10524           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10525             {
10526               size = 12;
10527               if (PPC_HA (off) != 0)
10528                 size = 16;
10529             }
10530           else
10531             {
10532               size = 20;
10533               if (PPC_HA (off) != 0)
10534                 size += 4;
10535
10536               if (PPC_HA (r2off) != 0)
10537                 size += 4;
10538             }
10539         }
10540       else if (info->emitrelocations)
10541         {
10542           stub_entry->stub_sec->reloc_count += 1;
10543           stub_entry->stub_sec->flags |= SEC_RELOC;
10544         }
10545     }
10546
10547   stub_entry->stub_sec->size += size;
10548   return TRUE;
10549 }
10550
10551 /* Set up various things so that we can make a list of input sections
10552    for each output section included in the link.  Returns -1 on error,
10553    0 when no stubs will be needed, and 1 on success.  */
10554
10555 int
10556 ppc64_elf_setup_section_lists
10557   (struct bfd_link_info *info,
10558    asection *(*add_stub_section) (const char *, asection *),
10559    void (*layout_sections_again) (void))
10560 {
10561   bfd *input_bfd;
10562   int top_id, top_index, id;
10563   asection *section;
10564   asection **input_list;
10565   bfd_size_type amt;
10566   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10567
10568   if (htab == NULL)
10569     return -1;
10570   /* Stash our params away.  */
10571   htab->add_stub_section = add_stub_section;
10572   htab->layout_sections_again = layout_sections_again;
10573
10574   /* Find the top input section id.  */
10575   for (input_bfd = info->input_bfds, top_id = 3;
10576        input_bfd != NULL;
10577        input_bfd = input_bfd->link_next)
10578     {
10579       for (section = input_bfd->sections;
10580            section != NULL;
10581            section = section->next)
10582         {
10583           if (top_id < section->id)
10584             top_id = section->id;
10585         }
10586     }
10587
10588   htab->top_id = top_id;
10589   amt = sizeof (struct map_stub) * (top_id + 1);
10590   htab->stub_group = bfd_zmalloc (amt);
10591   if (htab->stub_group == NULL)
10592     return -1;
10593
10594   /* Set toc_off for com, und, abs and ind sections.  */
10595   for (id = 0; id < 3; id++)
10596     htab->stub_group[id].toc_off = TOC_BASE_OFF;
10597
10598   /* We can't use output_bfd->section_count here to find the top output
10599      section index as some sections may have been removed, and
10600      strip_excluded_output_sections doesn't renumber the indices.  */
10601   for (section = info->output_bfd->sections, top_index = 0;
10602        section != NULL;
10603        section = section->next)
10604     {
10605       if (top_index < section->index)
10606         top_index = section->index;
10607     }
10608
10609   htab->top_index = top_index;
10610   amt = sizeof (asection *) * (top_index + 1);
10611   input_list = bfd_zmalloc (amt);
10612   htab->input_list = input_list;
10613   if (input_list == NULL)
10614     return -1;
10615
10616   return 1;
10617 }
10618
10619 /* Set up for first pass at multitoc partitioning.  */
10620
10621 void
10622 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10623 {
10624   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10625
10626   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
10627   htab->toc_bfd = NULL;
10628   htab->toc_first_sec = NULL;
10629 }
10630
10631 /* The linker repeatedly calls this function for each TOC input section
10632    and linker generated GOT section.  Group input bfds such that the toc
10633    within a group is less than 64k in size.  */
10634
10635 bfd_boolean
10636 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10637 {
10638   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10639   bfd_vma addr, off, limit;
10640
10641   if (htab == NULL)
10642     return FALSE;
10643
10644   if (!htab->second_toc_pass)
10645     {
10646       /* Keep track of the first .toc or .got section for this input bfd.  */
10647       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
10648
10649       if (new_bfd)
10650         {
10651           htab->toc_bfd = isec->owner;
10652           htab->toc_first_sec = isec;
10653         }
10654
10655       addr = isec->output_offset + isec->output_section->vma;
10656       off = addr - htab->toc_curr;
10657       limit = 0x80008000;
10658       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10659         limit = 0x10000;
10660       if (off + isec->size > limit)
10661         {
10662           addr = (htab->toc_first_sec->output_offset
10663                   + htab->toc_first_sec->output_section->vma);
10664           htab->toc_curr = addr;
10665         }
10666
10667       /* toc_curr is the base address of this toc group.  Set elf_gp
10668          for the input section to be the offset relative to the
10669          output toc base plus 0x8000.  Making the input elf_gp an
10670          offset allows us to move the toc as a whole without
10671          recalculating input elf_gp.  */
10672       off = htab->toc_curr - elf_gp (isec->output_section->owner);
10673       off += TOC_BASE_OFF;
10674
10675       /* Die if someone uses a linker script that doesn't keep input
10676          file .toc and .got together.  */
10677       if (new_bfd
10678           && elf_gp (isec->owner) != 0
10679           && elf_gp (isec->owner) != off)
10680         return FALSE;
10681
10682       elf_gp (isec->owner) = off;
10683       return TRUE;
10684     }
10685
10686   /* During the second pass toc_first_sec points to the start of
10687      a toc group, and toc_curr is used to track the old elf_gp.
10688      We use toc_bfd to ensure we only look at each bfd once.  */
10689   if (htab->toc_bfd == isec->owner)
10690     return TRUE;
10691   htab->toc_bfd = isec->owner;
10692
10693   if (htab->toc_first_sec == NULL
10694       || htab->toc_curr != elf_gp (isec->owner))
10695     {
10696       htab->toc_curr = elf_gp (isec->owner);
10697       htab->toc_first_sec = isec;
10698     }
10699   addr = (htab->toc_first_sec->output_offset
10700           + htab->toc_first_sec->output_section->vma);
10701   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10702   elf_gp (isec->owner) = off;
10703
10704   return TRUE;
10705 }
10706
10707 /* Called via elf_link_hash_traverse to merge GOT entries for global
10708    symbol H.  */
10709
10710 static bfd_boolean
10711 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10712 {
10713   if (h->root.type == bfd_link_hash_indirect)
10714     return TRUE;
10715
10716   merge_got_entries (&h->got.glist);
10717
10718   return TRUE;
10719 }
10720
10721 /* Called via elf_link_hash_traverse to allocate GOT entries for global
10722    symbol H.  */
10723
10724 static bfd_boolean
10725 reallocate_got (struct elf_link_hash_entry *h, void *inf)
10726 {
10727   struct got_entry *gent;
10728
10729   if (h->root.type == bfd_link_hash_indirect)
10730     return TRUE;
10731
10732   for (gent = h->got.glist; gent != NULL; gent = gent->next)
10733     if (!gent->is_indirect)
10734       allocate_got (h, (struct bfd_link_info *) inf, gent);
10735   return TRUE;
10736 }
10737
10738 /* Called on the first multitoc pass after the last call to
10739    ppc64_elf_next_toc_section.  This function removes duplicate GOT
10740    entries.  */
10741
10742 bfd_boolean
10743 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
10744 {
10745   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10746   struct bfd *ibfd, *ibfd2;
10747   bfd_boolean done_something;
10748
10749   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
10750
10751   if (!htab->do_multi_toc)
10752     return FALSE;
10753
10754   /* Merge global sym got entries within a toc group.  */
10755   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10756
10757   /* And tlsld_got.  */
10758   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10759     {
10760       struct got_entry *ent, *ent2;
10761
10762       if (!is_ppc64_elf (ibfd))
10763         continue;
10764
10765       ent = ppc64_tlsld_got (ibfd);
10766       if (!ent->is_indirect
10767           && ent->got.offset != (bfd_vma) -1)
10768         {
10769           for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
10770             {
10771               if (!is_ppc64_elf (ibfd2))
10772                 continue;
10773
10774               ent2 = ppc64_tlsld_got (ibfd2);
10775               if (!ent2->is_indirect
10776                   && ent2->got.offset != (bfd_vma) -1
10777                   && elf_gp (ibfd2) == elf_gp (ibfd))
10778                 {
10779                   ent2->is_indirect = TRUE;
10780                   ent2->got.ent = ent;
10781                 }
10782             }
10783         }
10784     }
10785
10786   /* Zap sizes of got sections.  */
10787   htab->reliplt->rawsize = htab->reliplt->size;
10788   htab->reliplt->size -= htab->got_reli_size;
10789   htab->got_reli_size = 0;
10790
10791   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10792     {
10793       asection *got, *relgot;
10794
10795       if (!is_ppc64_elf (ibfd))
10796         continue;
10797
10798       got = ppc64_elf_tdata (ibfd)->got;
10799       if (got != NULL)
10800         {
10801           got->rawsize = got->size;
10802           got->size = 0;
10803           relgot = ppc64_elf_tdata (ibfd)->relgot;
10804           relgot->rawsize = relgot->size;
10805           relgot->size = 0;
10806         }
10807     }
10808
10809   /* Now reallocate the got, local syms first.  We don't need to
10810      allocate section contents again since we never increase size.  */
10811   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10812     {
10813       struct got_entry **lgot_ents;
10814       struct got_entry **end_lgot_ents;
10815       struct plt_entry **local_plt;
10816       struct plt_entry **end_local_plt;
10817       unsigned char *lgot_masks;
10818       bfd_size_type locsymcount;
10819       Elf_Internal_Shdr *symtab_hdr;
10820       asection *s;
10821
10822       if (!is_ppc64_elf (ibfd))
10823         continue;
10824
10825       lgot_ents = elf_local_got_ents (ibfd);
10826       if (!lgot_ents)
10827         continue;
10828
10829       symtab_hdr = &elf_symtab_hdr (ibfd);
10830       locsymcount = symtab_hdr->sh_info;
10831       end_lgot_ents = lgot_ents + locsymcount;
10832       local_plt = (struct plt_entry **) end_lgot_ents;
10833       end_local_plt = local_plt + locsymcount;
10834       lgot_masks = (unsigned char *) end_local_plt;
10835       s = ppc64_elf_tdata (ibfd)->got;
10836       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10837         {
10838           struct got_entry *ent;
10839
10840           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
10841             {
10842               unsigned int ent_size = 8;
10843               unsigned int rel_size = sizeof (Elf64_External_Rela);
10844
10845               ent->got.offset = s->size;
10846               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10847                 {
10848                   ent_size *= 2;
10849                   rel_size *= 2;
10850                 }
10851               s->size += ent_size;
10852               if ((*lgot_masks & PLT_IFUNC) != 0)
10853                 {
10854                   htab->reliplt->size += rel_size;
10855                   htab->got_reli_size += rel_size;
10856                 }
10857               else if (info->shared)
10858                 {
10859                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10860                   srel->size += rel_size;
10861                 }
10862             }
10863         }
10864     }
10865
10866   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
10867
10868   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10869     {
10870       struct got_entry *ent;
10871
10872       if (!is_ppc64_elf (ibfd))
10873         continue;
10874
10875       ent = ppc64_tlsld_got (ibfd);
10876       if (!ent->is_indirect
10877           && ent->got.offset != (bfd_vma) -1)
10878         {
10879           asection *s = ppc64_elf_tdata (ibfd)->got;
10880           ent->got.offset = s->size;
10881           s->size += 16;
10882           if (info->shared)
10883             {
10884               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10885               srel->size += sizeof (Elf64_External_Rela);
10886             }
10887         }
10888     }
10889
10890   done_something = htab->reliplt->rawsize != htab->reliplt->size;
10891   if (!done_something)
10892     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10893       {
10894         asection *got;
10895
10896         if (!is_ppc64_elf (ibfd))
10897           continue;
10898
10899         got = ppc64_elf_tdata (ibfd)->got;
10900         if (got != NULL)
10901           {
10902             done_something = got->rawsize != got->size;
10903             if (done_something)
10904               break;
10905           }
10906       }
10907
10908   if (done_something)
10909     (*htab->layout_sections_again) ();
10910
10911   /* Set up for second pass over toc sections to recalculate elf_gp
10912      on input sections.  */
10913   htab->toc_bfd = NULL;
10914   htab->toc_first_sec = NULL;
10915   htab->second_toc_pass = TRUE;
10916   return done_something;
10917 }
10918
10919 /* Called after second pass of multitoc partitioning.  */
10920
10921 void
10922 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
10923 {
10924   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10925
10926   /* After the second pass, toc_curr tracks the TOC offset used
10927      for code sections below in ppc64_elf_next_input_section.  */
10928   htab->toc_curr = TOC_BASE_OFF;
10929 }
10930
10931 /* No toc references were found in ISEC.  If the code in ISEC makes no
10932    calls, then there's no need to use toc adjusting stubs when branching
10933    into ISEC.  Actually, indirect calls from ISEC are OK as they will
10934    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
10935    needed, and 2 if a cyclical call-graph was found but no other reason
10936    for a stub was detected.  If called from the top level, a return of
10937    2 means the same as a return of 0.  */
10938
10939 static int
10940 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
10941 {
10942   int ret;
10943
10944   /* Mark this section as checked.  */
10945   isec->call_check_done = 1;
10946
10947   /* We know none of our code bearing sections will need toc stubs.  */
10948   if ((isec->flags & SEC_LINKER_CREATED) != 0)
10949     return 0;
10950
10951   if (isec->size == 0)
10952     return 0;
10953
10954   if (isec->output_section == NULL)
10955     return 0;
10956
10957   ret = 0;
10958   if (isec->reloc_count != 0)
10959     {
10960       Elf_Internal_Rela *relstart, *rel;
10961       Elf_Internal_Sym *local_syms;
10962       struct ppc_link_hash_table *htab;
10963
10964       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
10965                                             info->keep_memory);
10966       if (relstart == NULL)
10967         return -1;
10968
10969       /* Look for branches to outside of this section.  */
10970       local_syms = NULL;
10971       htab = ppc_hash_table (info);
10972       if (htab == NULL)
10973         return -1;
10974
10975       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
10976         {
10977           enum elf_ppc64_reloc_type r_type;
10978           unsigned long r_symndx;
10979           struct elf_link_hash_entry *h;
10980           struct ppc_link_hash_entry *eh;
10981           Elf_Internal_Sym *sym;
10982           asection *sym_sec;
10983           struct _opd_sec_data *opd;
10984           bfd_vma sym_value;
10985           bfd_vma dest;
10986
10987           r_type = ELF64_R_TYPE (rel->r_info);
10988           if (r_type != R_PPC64_REL24
10989               && r_type != R_PPC64_REL14
10990               && r_type != R_PPC64_REL14_BRTAKEN
10991               && r_type != R_PPC64_REL14_BRNTAKEN)
10992             continue;
10993
10994           r_symndx = ELF64_R_SYM (rel->r_info);
10995           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
10996                           isec->owner))
10997             {
10998               ret = -1;
10999               break;
11000             }
11001
11002           /* Calls to dynamic lib functions go through a plt call stub
11003              that uses r2.  */
11004           eh = (struct ppc_link_hash_entry *) h;
11005           if (eh != NULL
11006               && (eh->elf.plt.plist != NULL
11007                   || (eh->oh != NULL
11008                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11009             {
11010               ret = 1;
11011               break;
11012             }
11013
11014           if (sym_sec == NULL)
11015             /* Ignore other undefined symbols.  */
11016             continue;
11017
11018           /* Assume branches to other sections not included in the
11019              link need stubs too, to cover -R and absolute syms.  */
11020           if (sym_sec->output_section == NULL)
11021             {
11022               ret = 1;
11023               break;
11024             }
11025
11026           if (h == NULL)
11027             sym_value = sym->st_value;
11028           else
11029             {
11030               if (h->root.type != bfd_link_hash_defined
11031                   && h->root.type != bfd_link_hash_defweak)
11032                 abort ();
11033               sym_value = h->root.u.def.value;
11034             }
11035           sym_value += rel->r_addend;
11036
11037           /* If this branch reloc uses an opd sym, find the code section.  */
11038           opd = get_opd_info (sym_sec);
11039           if (opd != NULL)
11040             {
11041               if (h == NULL && opd->adjust != NULL)
11042                 {
11043                   long adjust;
11044
11045                   adjust = opd->adjust[sym->st_value / 8];
11046                   if (adjust == -1)
11047                     /* Assume deleted functions won't ever be called.  */
11048                     continue;
11049                   sym_value += adjust;
11050                 }
11051
11052               dest = opd_entry_value (sym_sec, sym_value,
11053                                       &sym_sec, NULL, FALSE);
11054               if (dest == (bfd_vma) -1)
11055                 continue;
11056             }
11057           else
11058             dest = (sym_value
11059                     + sym_sec->output_offset
11060                     + sym_sec->output_section->vma);
11061
11062           /* Ignore branch to self.  */
11063           if (sym_sec == isec)
11064             continue;
11065
11066           /* If the called function uses the toc, we need a stub.  */
11067           if (sym_sec->has_toc_reloc
11068               || sym_sec->makes_toc_func_call)
11069             {
11070               ret = 1;
11071               break;
11072             }
11073
11074           /* Assume any branch that needs a long branch stub might in fact
11075              need a plt_branch stub.  A plt_branch stub uses r2.  */
11076           else if (dest - (isec->output_offset
11077                            + isec->output_section->vma
11078                            + rel->r_offset) + (1 << 25) >= (2 << 25))
11079             {
11080               ret = 1;
11081               break;
11082             }
11083
11084           /* If calling back to a section in the process of being
11085              tested, we can't say for sure that no toc adjusting stubs
11086              are needed, so don't return zero.  */
11087           else if (sym_sec->call_check_in_progress)
11088             ret = 2;
11089
11090           /* Branches to another section that itself doesn't have any TOC
11091              references are OK.  Recursively call ourselves to check.  */
11092           else if (!sym_sec->call_check_done)
11093             {
11094               int recur;
11095
11096               /* Mark current section as indeterminate, so that other
11097                  sections that call back to current won't be marked as
11098                  known.  */
11099               isec->call_check_in_progress = 1;
11100               recur = toc_adjusting_stub_needed (info, sym_sec);
11101               isec->call_check_in_progress = 0;
11102
11103               if (recur != 0)
11104                 {
11105                   ret = recur;
11106                   if (recur != 2)
11107                     break;
11108                 }
11109             }
11110         }
11111
11112       if (local_syms != NULL
11113           && (elf_symtab_hdr (isec->owner).contents
11114               != (unsigned char *) local_syms))
11115         free (local_syms);
11116       if (elf_section_data (isec)->relocs != relstart)
11117         free (relstart);
11118     }
11119
11120   if ((ret & 1) == 0
11121       && isec->map_head.s != NULL
11122       && (strcmp (isec->output_section->name, ".init") == 0
11123           || strcmp (isec->output_section->name, ".fini") == 0))
11124     {
11125       if (isec->map_head.s->has_toc_reloc
11126           || isec->map_head.s->makes_toc_func_call)
11127         ret = 1;
11128       else if (!isec->map_head.s->call_check_done)
11129         {
11130           int recur;
11131           isec->call_check_in_progress = 1;
11132           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11133           isec->call_check_in_progress = 0;
11134           if (recur != 0)
11135             ret = recur;
11136         }
11137     }
11138
11139   if (ret == 1)
11140     isec->makes_toc_func_call = 1;
11141
11142   return ret;
11143 }
11144
11145 /* The linker repeatedly calls this function for each input section,
11146    in the order that input sections are linked into output sections.
11147    Build lists of input sections to determine groupings between which
11148    we may insert linker stubs.  */
11149
11150 bfd_boolean
11151 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11152 {
11153   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11154
11155   if (htab == NULL)
11156     return FALSE;
11157
11158   if ((isec->output_section->flags & SEC_CODE) != 0
11159       && isec->output_section->index <= htab->top_index)
11160     {
11161       asection **list = htab->input_list + isec->output_section->index;
11162       /* Steal the link_sec pointer for our list.  */
11163 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
11164       /* This happens to make the list in reverse order,
11165          which is what we want.  */
11166       PREV_SEC (isec) = *list;
11167       *list = isec;
11168     }
11169
11170   if (htab->multi_toc_needed)
11171     {
11172       /* If a code section has a function that uses the TOC then we need
11173          to use the right TOC (obviously).  Also, make sure that .opd gets
11174          the correct TOC value for R_PPC64_TOC relocs that don't have or
11175          can't find their function symbol (shouldn't ever happen now).
11176          Also specially treat .fixup for the linux kernel.  .fixup
11177          contains branches, but only back to the function that hit an
11178          exception.  */
11179       if (isec->has_toc_reloc
11180           || (isec->flags & SEC_CODE) == 0
11181           || strcmp (isec->name, ".fixup") == 0)
11182         {
11183           if (elf_gp (isec->owner) != 0)
11184             htab->toc_curr = elf_gp (isec->owner);
11185         }
11186       else
11187         {
11188           if (!isec->call_check_done
11189               && toc_adjusting_stub_needed (info, isec) < 0)
11190             return FALSE;
11191           /* If we make a local call from this section, ie. a branch
11192              without a following nop, then we have no place to put a
11193              toc restoring insn.  We must use the same toc group as
11194              the callee.
11195              Testing makes_toc_func_call actually tests for *any*
11196              calls to functions that need a good toc pointer.  A more
11197              precise test would be better, as this one will set
11198              incorrect values for pasted .init/.fini fragments.
11199              (Fixed later in check_pasted_section.)  */
11200           if (isec->makes_toc_func_call
11201               && elf_gp (isec->owner) != 0)
11202             htab->toc_curr = elf_gp (isec->owner);
11203         }
11204     }
11205
11206   /* Functions that don't use the TOC can belong in any TOC group.
11207      Use the last TOC base.  */
11208   htab->stub_group[isec->id].toc_off = htab->toc_curr;
11209   return TRUE;
11210 }
11211
11212 /* Check that all .init and .fini sections use the same toc, if they
11213    have toc relocs.  */
11214
11215 static bfd_boolean
11216 check_pasted_section (struct bfd_link_info *info, const char *name)
11217 {
11218   asection *o = bfd_get_section_by_name (info->output_bfd, name);
11219
11220   if (o != NULL)
11221     {
11222       struct ppc_link_hash_table *htab = ppc_hash_table (info);
11223       bfd_vma toc_off = 0;
11224       asection *i;
11225
11226       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11227         if (i->has_toc_reloc)
11228           {
11229             if (toc_off == 0)
11230               toc_off = htab->stub_group[i->id].toc_off;
11231             else if (toc_off != htab->stub_group[i->id].toc_off)
11232               return FALSE;
11233           }
11234
11235       if (toc_off == 0)
11236         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11237           if (i->makes_toc_func_call)
11238             {
11239               toc_off = htab->stub_group[i->id].toc_off;
11240               break;
11241             }
11242
11243       /* Make sure the whole pasted function uses the same toc offset.  */
11244       if (toc_off != 0)
11245         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11246           htab->stub_group[i->id].toc_off = toc_off;
11247     }
11248   return TRUE;
11249 }
11250
11251 bfd_boolean
11252 ppc64_elf_check_init_fini (struct bfd_link_info *info)
11253 {
11254   return (check_pasted_section (info, ".init")
11255           & check_pasted_section (info, ".fini"));
11256 }
11257
11258 /* See whether we can group stub sections together.  Grouping stub
11259    sections may result in fewer stubs.  More importantly, we need to
11260    put all .init* and .fini* stubs at the beginning of the .init or
11261    .fini output sections respectively, because glibc splits the
11262    _init and _fini functions into multiple parts.  Putting a stub in
11263    the middle of a function is not a good idea.  */
11264
11265 static void
11266 group_sections (struct ppc_link_hash_table *htab,
11267                 bfd_size_type stub_group_size,
11268                 bfd_boolean stubs_always_before_branch)
11269 {
11270   asection **list;
11271   bfd_size_type stub14_group_size;
11272   bfd_boolean suppress_size_errors;
11273
11274   suppress_size_errors = FALSE;
11275   stub14_group_size = stub_group_size;
11276   if (stub_group_size == 1)
11277     {
11278       /* Default values.  */
11279       if (stubs_always_before_branch)
11280         {
11281           stub_group_size = 0x1e00000;
11282           stub14_group_size = 0x7800;
11283         }
11284       else
11285         {
11286           stub_group_size = 0x1c00000;
11287           stub14_group_size = 0x7000;
11288         }
11289       suppress_size_errors = TRUE;
11290     }
11291
11292   list = htab->input_list + htab->top_index;
11293   do
11294     {
11295       asection *tail = *list;
11296       while (tail != NULL)
11297         {
11298           asection *curr;
11299           asection *prev;
11300           bfd_size_type total;
11301           bfd_boolean big_sec;
11302           bfd_vma curr_toc;
11303
11304           curr = tail;
11305           total = tail->size;
11306           big_sec = total > (ppc64_elf_section_data (tail) != NULL
11307                              && ppc64_elf_section_data (tail)->has_14bit_branch
11308                              ? stub14_group_size : stub_group_size);
11309           if (big_sec && !suppress_size_errors)
11310             (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
11311                                      tail->owner, tail);
11312           curr_toc = htab->stub_group[tail->id].toc_off;
11313
11314           while ((prev = PREV_SEC (curr)) != NULL
11315                  && ((total += curr->output_offset - prev->output_offset)
11316                      < (ppc64_elf_section_data (prev) != NULL
11317                         && ppc64_elf_section_data (prev)->has_14bit_branch
11318                         ? stub14_group_size : stub_group_size))
11319                  && htab->stub_group[prev->id].toc_off == curr_toc)
11320             curr = prev;
11321
11322           /* OK, the size from the start of CURR to the end is less
11323              than stub_group_size and thus can be handled by one stub
11324              section.  (or the tail section is itself larger than
11325              stub_group_size, in which case we may be toast.)  We
11326              should really be keeping track of the total size of stubs
11327              added here, as stubs contribute to the final output
11328              section size.  That's a little tricky, and this way will
11329              only break if stubs added make the total size more than
11330              2^25, ie. for the default stub_group_size, if stubs total
11331              more than 2097152 bytes, or nearly 75000 plt call stubs.  */
11332           do
11333             {
11334               prev = PREV_SEC (tail);
11335               /* Set up this stub group.  */
11336               htab->stub_group[tail->id].link_sec = curr;
11337             }
11338           while (tail != curr && (tail = prev) != NULL);
11339
11340           /* But wait, there's more!  Input sections up to stub_group_size
11341              bytes before the stub section can be handled by it too.
11342              Don't do this if we have a really large section after the
11343              stubs, as adding more stubs increases the chance that
11344              branches may not reach into the stub section.  */
11345           if (!stubs_always_before_branch && !big_sec)
11346             {
11347               total = 0;
11348               while (prev != NULL
11349                      && ((total += tail->output_offset - prev->output_offset)
11350                          < (ppc64_elf_section_data (prev) != NULL
11351                             && ppc64_elf_section_data (prev)->has_14bit_branch
11352                             ? stub14_group_size : stub_group_size))
11353                      && htab->stub_group[prev->id].toc_off == curr_toc)
11354                 {
11355                   tail = prev;
11356                   prev = PREV_SEC (tail);
11357                   htab->stub_group[tail->id].link_sec = curr;
11358                 }
11359             }
11360           tail = prev;
11361         }
11362     }
11363   while (list-- != htab->input_list);
11364   free (htab->input_list);
11365 #undef PREV_SEC
11366 }
11367
11368 static const unsigned char glink_eh_frame_cie[] =
11369 {
11370   0, 0, 0, 16,                          /* length.  */
11371   0, 0, 0, 0,                           /* id.  */
11372   1,                                    /* CIE version.  */
11373   'z', 'R', 0,                          /* Augmentation string.  */
11374   4,                                    /* Code alignment.  */
11375   0x78,                                 /* Data alignment.  */
11376   65,                                   /* RA reg.  */
11377   1,                                    /* Augmentation size.  */
11378   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
11379   DW_CFA_def_cfa, 1, 0                  /* def_cfa: r1 offset 0.  */
11380 };
11381
11382 /* Stripping output sections is normally done before dynamic section
11383    symbols have been allocated.  This function is called later, and
11384    handles cases like htab->brlt which is mapped to its own output
11385    section.  */
11386
11387 static void
11388 maybe_strip_output (struct bfd_link_info *info, asection *isec)
11389 {
11390   if (isec->size == 0
11391       && isec->output_section->size == 0
11392       && !(isec->output_section->flags & SEC_KEEP)
11393       && !bfd_section_removed_from_list (info->output_bfd,
11394                                          isec->output_section)
11395       && elf_section_data (isec->output_section)->dynindx == 0)
11396     {
11397       isec->output_section->flags |= SEC_EXCLUDE;
11398       bfd_section_list_remove (info->output_bfd, isec->output_section);
11399       info->output_bfd->section_count--;
11400     }
11401 }
11402
11403 /* Determine and set the size of the stub section for a final link.
11404
11405    The basic idea here is to examine all the relocations looking for
11406    PC-relative calls to a target that is unreachable with a "bl"
11407    instruction.  */
11408
11409 bfd_boolean
11410 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
11411                       bfd_boolean plt_static_chain, int plt_thread_safe,
11412                       int plt_stub_align)
11413 {
11414   bfd_size_type stub_group_size;
11415   bfd_boolean stubs_always_before_branch;
11416   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11417
11418   if (htab == NULL)
11419     return FALSE;
11420
11421   htab->plt_static_chain = plt_static_chain;
11422   htab->plt_stub_align = plt_stub_align;
11423   if (plt_thread_safe == -1 && !info->executable)
11424     plt_thread_safe = 1;
11425   if (plt_thread_safe == -1)
11426     {
11427       static const char *const thread_starter[] =
11428         {
11429           "pthread_create",
11430           /* libstdc++ */
11431           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11432           /* librt */
11433           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11434           "mq_notify", "create_timer",
11435           /* libanl */
11436           "getaddrinfo_a",
11437           /* libgomp */
11438           "GOMP_parallel_start",
11439           "GOMP_parallel_loop_static_start",
11440           "GOMP_parallel_loop_dynamic_start",
11441           "GOMP_parallel_loop_guided_start",
11442           "GOMP_parallel_loop_runtime_start",
11443           "GOMP_parallel_sections_start",
11444         };
11445       unsigned i;
11446
11447       for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
11448         {
11449           struct elf_link_hash_entry *h;
11450           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11451                                     FALSE, FALSE, TRUE);
11452           plt_thread_safe = h != NULL && h->ref_regular;
11453           if (plt_thread_safe)
11454             break;
11455         }
11456     }
11457   htab->plt_thread_safe = plt_thread_safe;
11458   stubs_always_before_branch = group_size < 0;
11459   if (group_size < 0)
11460     stub_group_size = -group_size;
11461   else
11462     stub_group_size = group_size;
11463
11464   group_sections (htab, stub_group_size, stubs_always_before_branch);
11465
11466   while (1)
11467     {
11468       bfd *input_bfd;
11469       unsigned int bfd_indx;
11470       asection *stub_sec;
11471
11472       htab->stub_iteration += 1;
11473
11474       for (input_bfd = info->input_bfds, bfd_indx = 0;
11475            input_bfd != NULL;
11476            input_bfd = input_bfd->link_next, bfd_indx++)
11477         {
11478           Elf_Internal_Shdr *symtab_hdr;
11479           asection *section;
11480           Elf_Internal_Sym *local_syms = NULL;
11481
11482           if (!is_ppc64_elf (input_bfd))
11483             continue;
11484
11485           /* We'll need the symbol table in a second.  */
11486           symtab_hdr = &elf_symtab_hdr (input_bfd);
11487           if (symtab_hdr->sh_info == 0)
11488             continue;
11489
11490           /* Walk over each section attached to the input bfd.  */
11491           for (section = input_bfd->sections;
11492                section != NULL;
11493                section = section->next)
11494             {
11495               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
11496
11497               /* If there aren't any relocs, then there's nothing more
11498                  to do.  */
11499               if ((section->flags & SEC_RELOC) == 0
11500                   || (section->flags & SEC_ALLOC) == 0
11501                   || (section->flags & SEC_LOAD) == 0
11502                   || (section->flags & SEC_CODE) == 0
11503                   || section->reloc_count == 0)
11504                 continue;
11505
11506               /* If this section is a link-once section that will be
11507                  discarded, then don't create any stubs.  */
11508               if (section->output_section == NULL
11509                   || section->output_section->owner != info->output_bfd)
11510                 continue;
11511
11512               /* Get the relocs.  */
11513               internal_relocs
11514                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
11515                                              info->keep_memory);
11516               if (internal_relocs == NULL)
11517                 goto error_ret_free_local;
11518
11519               /* Now examine each relocation.  */
11520               irela = internal_relocs;
11521               irelaend = irela + section->reloc_count;
11522               for (; irela < irelaend; irela++)
11523                 {
11524                   enum elf_ppc64_reloc_type r_type;
11525                   unsigned int r_indx;
11526                   enum ppc_stub_type stub_type;
11527                   struct ppc_stub_hash_entry *stub_entry;
11528                   asection *sym_sec, *code_sec;
11529                   bfd_vma sym_value, code_value;
11530                   bfd_vma destination;
11531                   bfd_boolean ok_dest;
11532                   struct ppc_link_hash_entry *hash;
11533                   struct ppc_link_hash_entry *fdh;
11534                   struct elf_link_hash_entry *h;
11535                   Elf_Internal_Sym *sym;
11536                   char *stub_name;
11537                   const asection *id_sec;
11538                   struct _opd_sec_data *opd;
11539                   struct plt_entry *plt_ent;
11540
11541                   r_type = ELF64_R_TYPE (irela->r_info);
11542                   r_indx = ELF64_R_SYM (irela->r_info);
11543
11544                   if (r_type >= R_PPC64_max)
11545                     {
11546                       bfd_set_error (bfd_error_bad_value);
11547                       goto error_ret_free_internal;
11548                     }
11549
11550                   /* Only look for stubs on branch instructions.  */
11551                   if (r_type != R_PPC64_REL24
11552                       && r_type != R_PPC64_REL14
11553                       && r_type != R_PPC64_REL14_BRTAKEN
11554                       && r_type != R_PPC64_REL14_BRNTAKEN)
11555                     continue;
11556
11557                   /* Now determine the call target, its name, value,
11558                      section.  */
11559                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11560                                   r_indx, input_bfd))
11561                     goto error_ret_free_internal;
11562                   hash = (struct ppc_link_hash_entry *) h;
11563
11564                   ok_dest = FALSE;
11565                   fdh = NULL;
11566                   sym_value = 0;
11567                   if (hash == NULL)
11568                     {
11569                       sym_value = sym->st_value;
11570                       ok_dest = TRUE;
11571                     }
11572                   else if (hash->elf.root.type == bfd_link_hash_defined
11573                            || hash->elf.root.type == bfd_link_hash_defweak)
11574                     {
11575                       sym_value = hash->elf.root.u.def.value;
11576                       if (sym_sec->output_section != NULL)
11577                         ok_dest = TRUE;
11578                     }
11579                   else if (hash->elf.root.type == bfd_link_hash_undefweak
11580                            || hash->elf.root.type == bfd_link_hash_undefined)
11581                     {
11582                       /* Recognise an old ABI func code entry sym, and
11583                          use the func descriptor sym instead if it is
11584                          defined.  */
11585                       if (hash->elf.root.root.string[0] == '.'
11586                           && (fdh = lookup_fdh (hash, htab)) != NULL)
11587                         {
11588                           if (fdh->elf.root.type == bfd_link_hash_defined
11589                               || fdh->elf.root.type == bfd_link_hash_defweak)
11590                             {
11591                               sym_sec = fdh->elf.root.u.def.section;
11592                               sym_value = fdh->elf.root.u.def.value;
11593                               if (sym_sec->output_section != NULL)
11594                                 ok_dest = TRUE;
11595                             }
11596                           else
11597                             fdh = NULL;
11598                         }
11599                     }
11600                   else
11601                     {
11602                       bfd_set_error (bfd_error_bad_value);
11603                       goto error_ret_free_internal;
11604                     }
11605
11606                   destination = 0;
11607                   if (ok_dest)
11608                     {
11609                       sym_value += irela->r_addend;
11610                       destination = (sym_value
11611                                      + sym_sec->output_offset
11612                                      + sym_sec->output_section->vma);
11613                     }
11614
11615                   code_sec = sym_sec;
11616                   code_value = sym_value;
11617                   opd = get_opd_info (sym_sec);
11618                   if (opd != NULL)
11619                     {
11620                       bfd_vma dest;
11621
11622                       if (hash == NULL && opd->adjust != NULL)
11623                         {
11624                           long adjust = opd->adjust[sym_value / 8];
11625                           if (adjust == -1)
11626                             continue;
11627                           code_value += adjust;
11628                           sym_value += adjust;
11629                         }
11630                       dest = opd_entry_value (sym_sec, sym_value,
11631                                               &code_sec, &code_value, FALSE);
11632                       if (dest != (bfd_vma) -1)
11633                         {
11634                           destination = dest;
11635                           if (fdh != NULL)
11636                             {
11637                               /* Fixup old ABI sym to point at code
11638                                  entry.  */
11639                               hash->elf.root.type = bfd_link_hash_defweak;
11640                               hash->elf.root.u.def.section = code_sec;
11641                               hash->elf.root.u.def.value = code_value;
11642                             }
11643                         }
11644                     }
11645
11646                   /* Determine what (if any) linker stub is needed.  */
11647                   plt_ent = NULL;
11648                   stub_type = ppc_type_of_stub (section, irela, &hash,
11649                                                 &plt_ent, destination);
11650
11651                   if (stub_type != ppc_stub_plt_call)
11652                     {
11653                       /* Check whether we need a TOC adjusting stub.
11654                          Since the linker pastes together pieces from
11655                          different object files when creating the
11656                          _init and _fini functions, it may be that a
11657                          call to what looks like a local sym is in
11658                          fact a call needing a TOC adjustment.  */
11659                       if (code_sec != NULL
11660                           && code_sec->output_section != NULL
11661                           && (htab->stub_group[code_sec->id].toc_off
11662                               != htab->stub_group[section->id].toc_off)
11663                           && (code_sec->has_toc_reloc
11664                               || code_sec->makes_toc_func_call))
11665                         stub_type = ppc_stub_long_branch_r2off;
11666                     }
11667
11668                   if (stub_type == ppc_stub_none)
11669                     continue;
11670
11671                   /* __tls_get_addr calls might be eliminated.  */
11672                   if (stub_type != ppc_stub_plt_call
11673                       && hash != NULL
11674                       && (hash == htab->tls_get_addr
11675                           || hash == htab->tls_get_addr_fd)
11676                       && section->has_tls_reloc
11677                       && irela != internal_relocs)
11678                     {
11679                       /* Get tls info.  */
11680                       unsigned char *tls_mask;
11681
11682                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
11683                                          irela - 1, input_bfd))
11684                         goto error_ret_free_internal;
11685                       if (*tls_mask != 0)
11686                         continue;
11687                     }
11688
11689                   if (stub_type == ppc_stub_plt_call
11690                       && irela + 1 < irelaend
11691                       && irela[1].r_offset == irela->r_offset + 4
11692                       && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
11693                     {
11694                       if (!tocsave_find (htab, INSERT,
11695                                          &local_syms, irela + 1, input_bfd))
11696                         goto error_ret_free_internal;
11697                     }
11698                   else if (stub_type == ppc_stub_plt_call)
11699                     stub_type = ppc_stub_plt_call_r2save;
11700
11701                   /* Support for grouping stub sections.  */
11702                   id_sec = htab->stub_group[section->id].link_sec;
11703
11704                   /* Get the name of this stub.  */
11705                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11706                   if (!stub_name)
11707                     goto error_ret_free_internal;
11708
11709                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
11710                                                      stub_name, FALSE, FALSE);
11711                   if (stub_entry != NULL)
11712                     {
11713                       /* The proper stub has already been created.  */
11714                       free (stub_name);
11715                       if (stub_type == ppc_stub_plt_call_r2save)
11716                         stub_entry->stub_type = stub_type;
11717                       continue;
11718                     }
11719
11720                   stub_entry = ppc_add_stub (stub_name, section, info);
11721                   if (stub_entry == NULL)
11722                     {
11723                       free (stub_name);
11724                     error_ret_free_internal:
11725                       if (elf_section_data (section)->relocs == NULL)
11726                         free (internal_relocs);
11727                     error_ret_free_local:
11728                       if (local_syms != NULL
11729                           && (symtab_hdr->contents
11730                               != (unsigned char *) local_syms))
11731                         free (local_syms);
11732                       return FALSE;
11733                     }
11734
11735                   stub_entry->stub_type = stub_type;
11736                   if (stub_type != ppc_stub_plt_call
11737                       && stub_type != ppc_stub_plt_call_r2save)
11738                     {
11739                       stub_entry->target_value = code_value;
11740                       stub_entry->target_section = code_sec;
11741                     }
11742                   else
11743                     {
11744                       stub_entry->target_value = sym_value;
11745                       stub_entry->target_section = sym_sec;
11746                     }
11747                   stub_entry->h = hash;
11748                   stub_entry->plt_ent = plt_ent;
11749
11750                   if (stub_entry->h != NULL)
11751                     htab->stub_globals += 1;
11752                 }
11753
11754               /* We're done with the internal relocs, free them.  */
11755               if (elf_section_data (section)->relocs != internal_relocs)
11756                 free (internal_relocs);
11757             }
11758
11759           if (local_syms != NULL
11760               && symtab_hdr->contents != (unsigned char *) local_syms)
11761             {
11762               if (!info->keep_memory)
11763                 free (local_syms);
11764               else
11765                 symtab_hdr->contents = (unsigned char *) local_syms;
11766             }
11767         }
11768
11769       /* We may have added some stubs.  Find out the new size of the
11770          stub sections.  */
11771       for (stub_sec = htab->stub_bfd->sections;
11772            stub_sec != NULL;
11773            stub_sec = stub_sec->next)
11774         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11775           {
11776             stub_sec->rawsize = stub_sec->size;
11777             stub_sec->size = 0;
11778             stub_sec->reloc_count = 0;
11779             stub_sec->flags &= ~SEC_RELOC;
11780           }
11781
11782       htab->brlt->size = 0;
11783       htab->brlt->reloc_count = 0;
11784       htab->brlt->flags &= ~SEC_RELOC;
11785       if (htab->relbrlt != NULL)
11786         htab->relbrlt->size = 0;
11787
11788       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
11789
11790       if (info->emitrelocations
11791           && htab->glink != NULL && htab->glink->size != 0)
11792         {
11793           htab->glink->reloc_count = 1;
11794           htab->glink->flags |= SEC_RELOC;
11795         }
11796
11797       if (htab->glink_eh_frame != NULL
11798           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
11799           && htab->glink_eh_frame->output_section->size != 0)
11800         {
11801           size_t size = 0, align;
11802
11803           for (stub_sec = htab->stub_bfd->sections;
11804                stub_sec != NULL;
11805                stub_sec = stub_sec->next)
11806             if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11807               size += 20;
11808           if (htab->glink != NULL && htab->glink->size != 0)
11809             size += 24;
11810           if (size != 0)
11811             size += sizeof (glink_eh_frame_cie);
11812           align = 1;
11813           align <<= htab->glink_eh_frame->output_section->alignment_power;
11814           align -= 1;
11815           size = (size + align) & ~align;
11816           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
11817           htab->glink_eh_frame->size = size;
11818         }
11819
11820       if (htab->plt_stub_align != 0)
11821         for (stub_sec = htab->stub_bfd->sections;
11822              stub_sec != NULL;
11823              stub_sec = stub_sec->next)
11824           if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11825             stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
11826                               & (-1 << htab->plt_stub_align));
11827
11828       for (stub_sec = htab->stub_bfd->sections;
11829            stub_sec != NULL;
11830            stub_sec = stub_sec->next)
11831         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11832             && stub_sec->rawsize != stub_sec->size)
11833           break;
11834
11835       /* Exit from this loop when no stubs have been added, and no stubs
11836          have changed size.  */
11837       if (stub_sec == NULL
11838           && (htab->glink_eh_frame == NULL
11839               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
11840         break;
11841
11842       /* Ask the linker to do its stuff.  */
11843       (*htab->layout_sections_again) ();
11844     }
11845
11846   maybe_strip_output (info, htab->brlt);
11847   if (htab->glink_eh_frame != NULL)
11848     maybe_strip_output (info, htab->glink_eh_frame);
11849
11850   return TRUE;
11851 }
11852
11853 /* Called after we have determined section placement.  If sections
11854    move, we'll be called again.  Provide a value for TOCstart.  */
11855
11856 bfd_vma
11857 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
11858 {
11859   asection *s;
11860   bfd_vma TOCstart;
11861
11862   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
11863      order.  The TOC starts where the first of these sections starts.  */
11864   s = bfd_get_section_by_name (obfd, ".got");
11865   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11866     s = bfd_get_section_by_name (obfd, ".toc");
11867   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11868     s = bfd_get_section_by_name (obfd, ".tocbss");
11869   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11870     s = bfd_get_section_by_name (obfd, ".plt");
11871   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11872     {
11873       /* This may happen for
11874          o  references to TOC base (SYM@toc / TOC[tc0]) without a
11875          .toc directive
11876          o  bad linker script
11877          o --gc-sections and empty TOC sections
11878
11879          FIXME: Warn user?  */
11880
11881       /* Look for a likely section.  We probably won't even be
11882          using TOCstart.  */
11883       for (s = obfd->sections; s != NULL; s = s->next)
11884         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
11885                          | SEC_EXCLUDE))
11886             == (SEC_ALLOC | SEC_SMALL_DATA))
11887           break;
11888       if (s == NULL)
11889         for (s = obfd->sections; s != NULL; s = s->next)
11890           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
11891               == (SEC_ALLOC | SEC_SMALL_DATA))
11892             break;
11893       if (s == NULL)
11894         for (s = obfd->sections; s != NULL; s = s->next)
11895           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
11896               == SEC_ALLOC)
11897             break;
11898       if (s == NULL)
11899         for (s = obfd->sections; s != NULL; s = s->next)
11900           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
11901             break;
11902     }
11903
11904   TOCstart = 0;
11905   if (s != NULL)
11906     TOCstart = s->output_section->vma + s->output_offset;
11907
11908   _bfd_set_gp_value (obfd, TOCstart);
11909
11910   if (info != NULL && s != NULL && is_ppc64_elf (obfd))
11911     {
11912       struct ppc_link_hash_table *htab = ppc_hash_table (info);
11913
11914       if (htab != NULL
11915           && htab->elf.hgot != NULL)
11916         {
11917           htab->elf.hgot->type = STT_OBJECT;
11918           htab->elf.hgot->root.type = bfd_link_hash_defined;
11919           htab->elf.hgot->root.u.def.value = TOC_BASE_OFF;
11920           htab->elf.hgot->root.u.def.section = s;
11921         }
11922     }
11923   return TOCstart;
11924 }
11925
11926 /* Build all the stubs associated with the current output file.
11927    The stubs are kept in a hash table attached to the main linker
11928    hash table.  This function is called via gldelf64ppc_finish.  */
11929
11930 bfd_boolean
11931 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
11932                        struct bfd_link_info *info,
11933                        char **stats)
11934 {
11935   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11936   asection *stub_sec;
11937   bfd_byte *p;
11938   int stub_sec_count = 0;
11939
11940   if (htab == NULL)
11941     return FALSE;
11942
11943   htab->emit_stub_syms = emit_stub_syms;
11944
11945   /* Allocate memory to hold the linker stubs.  */
11946   for (stub_sec = htab->stub_bfd->sections;
11947        stub_sec != NULL;
11948        stub_sec = stub_sec->next)
11949     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11950         && stub_sec->size != 0)
11951       {
11952         stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
11953         if (stub_sec->contents == NULL)
11954           return FALSE;
11955         /* We want to check that built size is the same as calculated
11956            size.  rawsize is a convenient location to use.  */
11957         stub_sec->rawsize = stub_sec->size;
11958         stub_sec->size = 0;
11959       }
11960
11961   if (htab->glink != NULL && htab->glink->size != 0)
11962     {
11963       unsigned int indx;
11964       bfd_vma plt0;
11965
11966       /* Build the .glink plt call stub.  */
11967       if (htab->emit_stub_syms)
11968         {
11969           struct elf_link_hash_entry *h;
11970           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
11971                                     TRUE, FALSE, FALSE);
11972           if (h == NULL)
11973             return FALSE;
11974           if (h->root.type == bfd_link_hash_new)
11975             {
11976               h->root.type = bfd_link_hash_defined;
11977               h->root.u.def.section = htab->glink;
11978               h->root.u.def.value = 8;
11979               h->ref_regular = 1;
11980               h->def_regular = 1;
11981               h->ref_regular_nonweak = 1;
11982               h->forced_local = 1;
11983               h->non_elf = 0;
11984             }
11985         }
11986       plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
11987       if (info->emitrelocations)
11988         {
11989           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
11990           if (r == NULL)
11991             return FALSE;
11992           r->r_offset = (htab->glink->output_offset
11993                          + htab->glink->output_section->vma);
11994           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
11995           r->r_addend = plt0;
11996         }
11997       p = htab->glink->contents;
11998       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
11999       bfd_put_64 (htab->glink->owner, plt0, p);
12000       p += 8;
12001       bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12002       p += 4;
12003       bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12004       p += 4;
12005       bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12006       p += 4;
12007       bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
12008       p += 4;
12009       bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12010       p += 4;
12011       bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
12012       p += 4;
12013       bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
12014       p += 4;
12015       bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
12016       p += 4;
12017       bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
12018       p += 4;
12019       bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
12020       p += 4;
12021       bfd_put_32 (htab->glink->owner, BCTR, p);
12022       p += 4;
12023       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
12024         {
12025           bfd_put_32 (htab->glink->owner, NOP, p);
12026           p += 4;
12027         }
12028
12029       /* Build the .glink lazy link call stubs.  */
12030       indx = 0;
12031       while (p < htab->glink->contents + htab->glink->size)
12032         {
12033           if (indx < 0x8000)
12034             {
12035               bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
12036               p += 4;
12037             }
12038           else
12039             {
12040               bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
12041               p += 4;
12042               bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
12043               p += 4;
12044             }
12045           bfd_put_32 (htab->glink->owner,
12046                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
12047           indx++;
12048           p += 4;
12049         }
12050       htab->glink->rawsize = p - htab->glink->contents;
12051     }
12052
12053   if (htab->brlt->size != 0)
12054     {
12055       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
12056                                          htab->brlt->size);
12057       if (htab->brlt->contents == NULL)
12058         return FALSE;
12059     }
12060   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
12061     {
12062       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
12063                                             htab->relbrlt->size);
12064       if (htab->relbrlt->contents == NULL)
12065         return FALSE;
12066     }
12067
12068   if (htab->glink_eh_frame != NULL
12069       && htab->glink_eh_frame->size != 0)
12070     {
12071       bfd_vma val;
12072       bfd_byte *last_fde;
12073       size_t last_fde_len, size, align, pad;
12074
12075       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12076       if (p == NULL)
12077         return FALSE;
12078       htab->glink_eh_frame->contents = p;
12079       last_fde = p;
12080
12081       htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12082
12083       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12084       /* CIE length (rewrite in case little-endian).  */
12085       last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12086       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12087       p += sizeof (glink_eh_frame_cie);
12088
12089       for (stub_sec = htab->stub_bfd->sections;
12090            stub_sec != NULL;
12091            stub_sec = stub_sec->next)
12092         if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12093           {
12094             last_fde = p;
12095             last_fde_len = 16;
12096             /* FDE length.  */
12097             bfd_put_32 (htab->elf.dynobj, 16, p);
12098             p += 4;
12099             /* CIE pointer.  */
12100             val = p - htab->glink_eh_frame->contents;
12101             bfd_put_32 (htab->elf.dynobj, val, p);
12102             p += 4;
12103             /* Offset to stub section.  */
12104             val = (stub_sec->output_section->vma
12105                    + stub_sec->output_offset);
12106             val -= (htab->glink_eh_frame->output_section->vma
12107                     + htab->glink_eh_frame->output_offset);
12108             val -= p - htab->glink_eh_frame->contents;
12109             if (val + 0x80000000 > 0xffffffff)
12110               {
12111                 info->callbacks->einfo
12112                   (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
12113                    stub_sec->name);
12114                 return FALSE;
12115               }
12116             bfd_put_32 (htab->elf.dynobj, val, p);
12117             p += 4;
12118             /* stub section size.  */
12119             bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
12120             p += 4;
12121             /* Augmentation.  */
12122             p += 1;
12123             /* Pad.  */
12124             p += 3;
12125           }
12126       if (htab->glink != NULL && htab->glink->size != 0)
12127         {
12128           last_fde = p;
12129           last_fde_len = 20;
12130           /* FDE length.  */
12131           bfd_put_32 (htab->elf.dynobj, 20, p);
12132           p += 4;
12133           /* CIE pointer.  */
12134           val = p - htab->glink_eh_frame->contents;
12135           bfd_put_32 (htab->elf.dynobj, val, p);
12136           p += 4;
12137           /* Offset to .glink.  */
12138           val = (htab->glink->output_section->vma
12139                  + htab->glink->output_offset
12140                  + 8);
12141           val -= (htab->glink_eh_frame->output_section->vma
12142                   + htab->glink_eh_frame->output_offset);
12143           val -= p - htab->glink_eh_frame->contents;
12144           if (val + 0x80000000 > 0xffffffff)
12145             {
12146               info->callbacks->einfo
12147                 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
12148                  htab->glink->name);
12149               return FALSE;
12150             }
12151           bfd_put_32 (htab->elf.dynobj, val, p);
12152           p += 4;
12153           /* .glink size.  */
12154           bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
12155           p += 4;
12156           /* Augmentation.  */
12157           p += 1;
12158
12159           *p++ = DW_CFA_advance_loc + 1;
12160           *p++ = DW_CFA_register;
12161           *p++ = 65;
12162           *p++ = 12;
12163           *p++ = DW_CFA_advance_loc + 4;
12164           *p++ = DW_CFA_restore_extended;
12165           *p++ = 65;
12166         }
12167       /* Subsume any padding into the last FDE if user .eh_frame
12168          sections are aligned more than glink_eh_frame.  Otherwise any
12169          zero padding will be seen as a terminator.  */
12170       size = p - htab->glink_eh_frame->contents;
12171       align = 1;
12172       align <<= htab->glink_eh_frame->output_section->alignment_power;
12173       align -= 1;
12174       pad = ((size + align) & ~align) - size;
12175       htab->glink_eh_frame->size = size + pad;
12176       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12177     }
12178
12179   /* Build the stubs as directed by the stub hash table.  */
12180   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
12181
12182   if (htab->relbrlt != NULL)
12183     htab->relbrlt->reloc_count = 0;
12184
12185   if (htab->plt_stub_align != 0)
12186     for (stub_sec = htab->stub_bfd->sections;
12187          stub_sec != NULL;
12188          stub_sec = stub_sec->next)
12189       if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12190         stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
12191                           & (-1 << htab->plt_stub_align));
12192
12193   for (stub_sec = htab->stub_bfd->sections;
12194        stub_sec != NULL;
12195        stub_sec = stub_sec->next)
12196     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12197       {
12198         stub_sec_count += 1;
12199         if (stub_sec->rawsize != stub_sec->size)
12200           break;
12201       }
12202
12203   if (stub_sec != NULL
12204       || htab->glink->rawsize != htab->glink->size
12205       || (htab->glink_eh_frame != NULL
12206           && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
12207     {
12208       htab->stub_error = TRUE;
12209       info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
12210     }
12211
12212   if (htab->stub_error)
12213     return FALSE;
12214
12215   if (stats != NULL)
12216     {
12217       *stats = bfd_malloc (500);
12218       if (*stats == NULL)
12219         return FALSE;
12220
12221       sprintf (*stats, _("linker stubs in %u group%s\n"
12222                          "  branch       %lu\n"
12223                          "  toc adjust   %lu\n"
12224                          "  long branch  %lu\n"
12225                          "  long toc adj %lu\n"
12226                          "  plt call     %lu\n"
12227                          "  plt call toc %lu"),
12228                stub_sec_count,
12229                stub_sec_count == 1 ? "" : "s",
12230                htab->stub_count[ppc_stub_long_branch - 1],
12231                htab->stub_count[ppc_stub_long_branch_r2off - 1],
12232                htab->stub_count[ppc_stub_plt_branch - 1],
12233                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
12234                htab->stub_count[ppc_stub_plt_call - 1],
12235                htab->stub_count[ppc_stub_plt_call_r2save - 1]);
12236     }
12237   return TRUE;
12238 }
12239
12240 /* This function undoes the changes made by add_symbol_adjust.  */
12241
12242 static bfd_boolean
12243 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12244 {
12245   struct ppc_link_hash_entry *eh;
12246
12247   if (h->root.type == bfd_link_hash_indirect)
12248     return TRUE;
12249
12250   eh = (struct ppc_link_hash_entry *) h;
12251   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
12252     return TRUE;
12253
12254   eh->elf.root.type = bfd_link_hash_undefined;
12255   return TRUE;
12256 }
12257
12258 void
12259 ppc64_elf_restore_symbols (struct bfd_link_info *info)
12260 {
12261   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12262
12263   if (htab != NULL)
12264     elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
12265 }
12266
12267 /* What to do when ld finds relocations against symbols defined in
12268    discarded sections.  */
12269
12270 static unsigned int
12271 ppc64_elf_action_discarded (asection *sec)
12272 {
12273   if (strcmp (".opd", sec->name) == 0)
12274     return 0;
12275
12276   if (strcmp (".toc", sec->name) == 0)
12277     return 0;
12278
12279   if (strcmp (".toc1", sec->name) == 0)
12280     return 0;
12281
12282   return _bfd_elf_default_action_discarded (sec);
12283 }
12284
12285 /* The RELOCATE_SECTION function is called by the ELF backend linker
12286    to handle the relocations for a section.
12287
12288    The relocs are always passed as Rela structures; if the section
12289    actually uses Rel structures, the r_addend field will always be
12290    zero.
12291
12292    This function is responsible for adjust the section contents as
12293    necessary, and (if using Rela relocs and generating a
12294    relocatable output file) adjusting the reloc addend as
12295    necessary.
12296
12297    This function does not have to worry about setting the reloc
12298    address or the reloc symbol index.
12299
12300    LOCAL_SYMS is a pointer to the swapped in local symbols.
12301
12302    LOCAL_SECTIONS is an array giving the section in the input file
12303    corresponding to the st_shndx field of each local symbol.
12304
12305    The global hash table entry for the global symbols can be found
12306    via elf_sym_hashes (input_bfd).
12307
12308    When generating relocatable output, this function must handle
12309    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
12310    going to be the section symbol corresponding to the output
12311    section, which means that the addend must be adjusted
12312    accordingly.  */
12313
12314 static bfd_boolean
12315 ppc64_elf_relocate_section (bfd *output_bfd,
12316                             struct bfd_link_info *info,
12317                             bfd *input_bfd,
12318                             asection *input_section,
12319                             bfd_byte *contents,
12320                             Elf_Internal_Rela *relocs,
12321                             Elf_Internal_Sym *local_syms,
12322                             asection **local_sections)
12323 {
12324   struct ppc_link_hash_table *htab;
12325   Elf_Internal_Shdr *symtab_hdr;
12326   struct elf_link_hash_entry **sym_hashes;
12327   Elf_Internal_Rela *rel;
12328   Elf_Internal_Rela *relend;
12329   Elf_Internal_Rela outrel;
12330   bfd_byte *loc;
12331   struct got_entry **local_got_ents;
12332   bfd_vma TOCstart;
12333   bfd_boolean ret = TRUE;
12334   bfd_boolean is_opd;
12335   /* Assume 'at' branch hints.  */
12336   bfd_boolean is_isa_v2 = TRUE;
12337   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
12338
12339   /* Initialize howto table if needed.  */
12340   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
12341     ppc_howto_init ();
12342
12343   htab = ppc_hash_table (info);
12344   if (htab == NULL)
12345     return FALSE;
12346
12347   /* Don't relocate stub sections.  */
12348   if (input_section->owner == htab->stub_bfd)
12349     return TRUE;
12350
12351   BFD_ASSERT (is_ppc64_elf (input_bfd));
12352
12353   local_got_ents = elf_local_got_ents (input_bfd);
12354   TOCstart = elf_gp (output_bfd);
12355   symtab_hdr = &elf_symtab_hdr (input_bfd);
12356   sym_hashes = elf_sym_hashes (input_bfd);
12357   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
12358
12359   rel = relocs;
12360   relend = relocs + input_section->reloc_count;
12361   for (; rel < relend; rel++)
12362     {
12363       enum elf_ppc64_reloc_type r_type;
12364       bfd_vma addend;
12365       bfd_reloc_status_type r;
12366       Elf_Internal_Sym *sym;
12367       asection *sec;
12368       struct elf_link_hash_entry *h_elf;
12369       struct ppc_link_hash_entry *h;
12370       struct ppc_link_hash_entry *fdh;
12371       const char *sym_name;
12372       unsigned long r_symndx, toc_symndx;
12373       bfd_vma toc_addend;
12374       unsigned char tls_mask, tls_gd, tls_type;
12375       unsigned char sym_type;
12376       bfd_vma relocation;
12377       bfd_boolean unresolved_reloc;
12378       bfd_boolean warned;
12379       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
12380       unsigned int insn;
12381       unsigned int mask;
12382       struct ppc_stub_hash_entry *stub_entry;
12383       bfd_vma max_br_offset;
12384       bfd_vma from;
12385       const Elf_Internal_Rela orig_rel = *rel;
12386
12387       r_type = ELF64_R_TYPE (rel->r_info);
12388       r_symndx = ELF64_R_SYM (rel->r_info);
12389
12390       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
12391          symbol of the previous ADDR64 reloc.  The symbol gives us the
12392          proper TOC base to use.  */
12393       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
12394           && rel != relocs
12395           && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
12396           && is_opd)
12397         r_symndx = ELF64_R_SYM (rel[-1].r_info);
12398
12399       sym = NULL;
12400       sec = NULL;
12401       h_elf = NULL;
12402       sym_name = NULL;
12403       unresolved_reloc = FALSE;
12404       warned = FALSE;
12405
12406       if (r_symndx < symtab_hdr->sh_info)
12407         {
12408           /* It's a local symbol.  */
12409           struct _opd_sec_data *opd;
12410
12411           sym = local_syms + r_symndx;
12412           sec = local_sections[r_symndx];
12413           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
12414           sym_type = ELF64_ST_TYPE (sym->st_info);
12415           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
12416           opd = get_opd_info (sec);
12417           if (opd != NULL && opd->adjust != NULL)
12418             {
12419               long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
12420               if (adjust == -1)
12421                 relocation = 0;
12422               else
12423                 {
12424                   /* If this is a relocation against the opd section sym
12425                      and we have edited .opd, adjust the reloc addend so
12426                      that ld -r and ld --emit-relocs output is correct.
12427                      If it is a reloc against some other .opd symbol,
12428                      then the symbol value will be adjusted later.  */
12429                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
12430                     rel->r_addend += adjust;
12431                   else
12432                     relocation += adjust;
12433                 }
12434             }
12435         }
12436       else
12437         {
12438           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
12439                                    r_symndx, symtab_hdr, sym_hashes,
12440                                    h_elf, sec, relocation,
12441                                    unresolved_reloc, warned);
12442           sym_name = h_elf->root.root.string;
12443           sym_type = h_elf->type;
12444           if (sec != NULL
12445               && sec->owner == output_bfd
12446               && strcmp (sec->name, ".opd") == 0)
12447             {
12448               /* This is a symbol defined in a linker script.  All
12449                  such are defined in output sections, even those
12450                  defined by simple assignment from a symbol defined in
12451                  an input section.  Transfer the symbol to an
12452                  appropriate input .opd section, so that a branch to
12453                  this symbol will be mapped to the location specified
12454                  by the opd entry.  */
12455               struct bfd_link_order *lo;
12456               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
12457                 if (lo->type == bfd_indirect_link_order)
12458                   {
12459                     asection *isec = lo->u.indirect.section;
12460                     if (h_elf->root.u.def.value >= isec->output_offset
12461                         && h_elf->root.u.def.value < (isec->output_offset
12462                                                       + isec->size))
12463                       {
12464                         h_elf->root.u.def.value -= isec->output_offset;
12465                         h_elf->root.u.def.section = isec;
12466                         sec = isec;
12467                         break;
12468                       }
12469                   }
12470             }
12471         }
12472       h = (struct ppc_link_hash_entry *) h_elf;
12473
12474       if (sec != NULL && discarded_section (sec))
12475         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
12476                                          rel, 1, relend,
12477                                          ppc64_elf_howto_table[r_type], 0,
12478                                          contents);
12479
12480       if (info->relocatable)
12481         continue;
12482
12483       if (h != NULL && &h->elf == htab->elf.hgot)
12484         {
12485           relocation = (TOCstart
12486                         + htab->stub_group[input_section->id].toc_off);
12487           sec = bfd_abs_section_ptr;
12488           unresolved_reloc = FALSE;
12489         }
12490
12491       /* TLS optimizations.  Replace instruction sequences and relocs
12492          based on information we collected in tls_optimize.  We edit
12493          RELOCS so that --emit-relocs will output something sensible
12494          for the final instruction stream.  */
12495       tls_mask = 0;
12496       tls_gd = 0;
12497       toc_symndx = 0;
12498       if (h != NULL)
12499         tls_mask = h->tls_mask;
12500       else if (local_got_ents != NULL)
12501         {
12502           struct plt_entry **local_plt = (struct plt_entry **)
12503             (local_got_ents + symtab_hdr->sh_info);
12504           unsigned char *lgot_masks = (unsigned char *)
12505             (local_plt + symtab_hdr->sh_info);
12506           tls_mask = lgot_masks[r_symndx];
12507         }
12508       if (tls_mask == 0
12509           && (r_type == R_PPC64_TLS
12510               || r_type == R_PPC64_TLSGD
12511               || r_type == R_PPC64_TLSLD))
12512         {
12513           /* Check for toc tls entries.  */
12514           unsigned char *toc_tls;
12515
12516           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12517                              &local_syms, rel, input_bfd))
12518             return FALSE;
12519
12520           if (toc_tls)
12521             tls_mask = *toc_tls;
12522         }
12523
12524       /* Check that tls relocs are used with tls syms, and non-tls
12525          relocs are used with non-tls syms.  */
12526       if (r_symndx != STN_UNDEF
12527           && r_type != R_PPC64_NONE
12528           && (h == NULL
12529               || h->elf.root.type == bfd_link_hash_defined
12530               || h->elf.root.type == bfd_link_hash_defweak)
12531           && (IS_PPC64_TLS_RELOC (r_type)
12532               != (sym_type == STT_TLS
12533                   || (sym_type == STT_SECTION
12534                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
12535         {
12536           if (tls_mask != 0
12537               && (r_type == R_PPC64_TLS
12538                   || r_type == R_PPC64_TLSGD
12539                   || r_type == R_PPC64_TLSLD))
12540             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
12541             ;
12542           else
12543             info->callbacks->einfo
12544               (!IS_PPC64_TLS_RELOC (r_type)
12545                ? _("%P: %H: %s used with TLS symbol `%T'\n")
12546                : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
12547                input_bfd, input_section, rel->r_offset,
12548                ppc64_elf_howto_table[r_type]->name,
12549                sym_name);
12550         }
12551
12552       /* Ensure reloc mapping code below stays sane.  */
12553       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
12554           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
12555           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
12556           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
12557           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
12558           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
12559           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
12560           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
12561           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
12562           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
12563         abort ();
12564
12565       switch (r_type)
12566         {
12567         default:
12568           break;
12569
12570         case R_PPC64_LO_DS_OPT:
12571           insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12572           if ((insn & (0x3f << 26)) != 58u << 26)
12573             abort ();
12574           insn += (14u << 26) - (58u << 26);
12575           bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12576           r_type = R_PPC64_TOC16_LO;
12577           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12578           break;
12579
12580         case R_PPC64_TOC16:
12581         case R_PPC64_TOC16_LO:
12582         case R_PPC64_TOC16_DS:
12583         case R_PPC64_TOC16_LO_DS:
12584           {
12585             /* Check for toc tls entries.  */
12586             unsigned char *toc_tls;
12587             int retval;
12588
12589             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12590                                    &local_syms, rel, input_bfd);
12591             if (retval == 0)
12592               return FALSE;
12593
12594             if (toc_tls)
12595               {
12596                 tls_mask = *toc_tls;
12597                 if (r_type == R_PPC64_TOC16_DS
12598                     || r_type == R_PPC64_TOC16_LO_DS)
12599                   {
12600                     if (tls_mask != 0
12601                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
12602                       goto toctprel;
12603                   }
12604                 else
12605                   {
12606                     /* If we found a GD reloc pair, then we might be
12607                        doing a GD->IE transition.  */
12608                     if (retval == 2)
12609                       {
12610                         tls_gd = TLS_TPRELGD;
12611                         if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12612                           goto tls_ldgd_opt;
12613                       }
12614                     else if (retval == 3)
12615                       {
12616                         if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12617                           goto tls_ldgd_opt;
12618                       }
12619                   }
12620               }
12621           }
12622           break;
12623
12624         case R_PPC64_GOT_TPREL16_HI:
12625         case R_PPC64_GOT_TPREL16_HA:
12626           if (tls_mask != 0
12627               && (tls_mask & TLS_TPREL) == 0)
12628             {
12629               rel->r_offset -= d_offset;
12630               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12631               r_type = R_PPC64_NONE;
12632               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12633             }
12634           break;
12635
12636         case R_PPC64_GOT_TPREL16_DS:
12637         case R_PPC64_GOT_TPREL16_LO_DS:
12638           if (tls_mask != 0
12639               && (tls_mask & TLS_TPREL) == 0)
12640             {
12641             toctprel:
12642               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12643               insn &= 31 << 21;
12644               insn |= 0x3c0d0000;       /* addis 0,13,0 */
12645               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12646               r_type = R_PPC64_TPREL16_HA;
12647               if (toc_symndx != 0)
12648                 {
12649                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
12650                   rel->r_addend = toc_addend;
12651                   /* We changed the symbol.  Start over in order to
12652                      get h, sym, sec etc. right.  */
12653                   rel--;
12654                   continue;
12655                 }
12656               else
12657                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12658             }
12659           break;
12660
12661         case R_PPC64_TLS:
12662           if (tls_mask != 0
12663               && (tls_mask & TLS_TPREL) == 0)
12664             {
12665               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
12666               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
12667               if (insn == 0)
12668                 abort ();
12669               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
12670               /* Was PPC64_TLS which sits on insn boundary, now
12671                  PPC64_TPREL16_LO which is at low-order half-word.  */
12672               rel->r_offset += d_offset;
12673               r_type = R_PPC64_TPREL16_LO;
12674               if (toc_symndx != 0)
12675                 {
12676                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
12677                   rel->r_addend = toc_addend;
12678                   /* We changed the symbol.  Start over in order to
12679                      get h, sym, sec etc. right.  */
12680                   rel--;
12681                   continue;
12682                 }
12683               else
12684                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12685             }
12686           break;
12687
12688         case R_PPC64_GOT_TLSGD16_HI:
12689         case R_PPC64_GOT_TLSGD16_HA:
12690           tls_gd = TLS_TPRELGD;
12691           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12692             goto tls_gdld_hi;
12693           break;
12694
12695         case R_PPC64_GOT_TLSLD16_HI:
12696         case R_PPC64_GOT_TLSLD16_HA:
12697           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12698             {
12699             tls_gdld_hi:
12700               if ((tls_mask & tls_gd) != 0)
12701                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12702                           + R_PPC64_GOT_TPREL16_DS);
12703               else
12704                 {
12705                   rel->r_offset -= d_offset;
12706                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12707                   r_type = R_PPC64_NONE;
12708                 }
12709               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12710             }
12711           break;
12712
12713         case R_PPC64_GOT_TLSGD16:
12714         case R_PPC64_GOT_TLSGD16_LO:
12715           tls_gd = TLS_TPRELGD;
12716           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12717             goto tls_ldgd_opt;
12718           break;
12719
12720         case R_PPC64_GOT_TLSLD16:
12721         case R_PPC64_GOT_TLSLD16_LO:
12722           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12723             {
12724               unsigned int insn1, insn2, insn3;
12725               bfd_vma offset;
12726
12727             tls_ldgd_opt:
12728               offset = (bfd_vma) -1;
12729               /* If not using the newer R_PPC64_TLSGD/LD to mark
12730                  __tls_get_addr calls, we must trust that the call
12731                  stays with its arg setup insns, ie. that the next
12732                  reloc is the __tls_get_addr call associated with
12733                  the current reloc.  Edit both insns.  */
12734               if (input_section->has_tls_get_addr_call
12735                   && rel + 1 < relend
12736                   && branch_reloc_hash_match (input_bfd, rel + 1,
12737                                               htab->tls_get_addr,
12738                                               htab->tls_get_addr_fd))
12739                 offset = rel[1].r_offset;
12740               if ((tls_mask & tls_gd) != 0)
12741                 {
12742                   /* IE */
12743                   insn1 = bfd_get_32 (output_bfd,
12744                                       contents + rel->r_offset - d_offset);
12745                   insn1 &= (1 << 26) - (1 << 2);
12746                   insn1 |= 58 << 26;    /* ld */
12747                   insn2 = 0x7c636a14;   /* add 3,3,13 */
12748                   if (offset != (bfd_vma) -1)
12749                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12750                   if ((tls_mask & TLS_EXPLICIT) == 0)
12751                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12752                               + R_PPC64_GOT_TPREL16_DS);
12753                   else
12754                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
12755                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12756                 }
12757               else
12758                 {
12759                   /* LE */
12760                   insn1 = 0x3c6d0000;   /* addis 3,13,0 */
12761                   insn2 = 0x38630000;   /* addi 3,3,0 */
12762                   if (tls_gd == 0)
12763                     {
12764                       /* Was an LD reloc.  */
12765                       if (toc_symndx)
12766                         sec = local_sections[toc_symndx];
12767                       for (r_symndx = 0;
12768                            r_symndx < symtab_hdr->sh_info;
12769                            r_symndx++)
12770                         if (local_sections[r_symndx] == sec)
12771                           break;
12772                       if (r_symndx >= symtab_hdr->sh_info)
12773                         r_symndx = STN_UNDEF;
12774                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12775                       if (r_symndx != STN_UNDEF)
12776                         rel->r_addend -= (local_syms[r_symndx].st_value
12777                                           + sec->output_offset
12778                                           + sec->output_section->vma);
12779                     }
12780                   else if (toc_symndx != 0)
12781                     {
12782                       r_symndx = toc_symndx;
12783                       rel->r_addend = toc_addend;
12784                     }
12785                   r_type = R_PPC64_TPREL16_HA;
12786                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12787                   if (offset != (bfd_vma) -1)
12788                     {
12789                       rel[1].r_info = ELF64_R_INFO (r_symndx,
12790                                                     R_PPC64_TPREL16_LO);
12791                       rel[1].r_offset = offset + d_offset;
12792                       rel[1].r_addend = rel->r_addend;
12793                     }
12794                 }
12795               bfd_put_32 (output_bfd, insn1,
12796                           contents + rel->r_offset - d_offset);
12797               if (offset != (bfd_vma) -1)
12798                 {
12799                   insn3 = bfd_get_32 (output_bfd,
12800                                       contents + offset + 4);
12801                   if (insn3 == NOP
12802                       || insn3 == CROR_151515 || insn3 == CROR_313131)
12803                     {
12804                       rel[1].r_offset += 4;
12805                       bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12806                       insn2 = NOP;
12807                     }
12808                   bfd_put_32 (output_bfd, insn2, contents + offset);
12809                 }
12810               if ((tls_mask & tls_gd) == 0
12811                   && (tls_gd == 0 || toc_symndx != 0))
12812                 {
12813                   /* We changed the symbol.  Start over in order
12814                      to get h, sym, sec etc. right.  */
12815                   rel--;
12816                   continue;
12817                 }
12818             }
12819           break;
12820
12821         case R_PPC64_TLSGD:
12822           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12823             {
12824               unsigned int insn2, insn3;
12825               bfd_vma offset = rel->r_offset;
12826
12827               if ((tls_mask & TLS_TPRELGD) != 0)
12828                 {
12829                   /* IE */
12830                   r_type = R_PPC64_NONE;
12831                   insn2 = 0x7c636a14;   /* add 3,3,13 */
12832                 }
12833               else
12834                 {
12835                   /* LE */
12836                   if (toc_symndx != 0)
12837                     {
12838                       r_symndx = toc_symndx;
12839                       rel->r_addend = toc_addend;
12840                     }
12841                   r_type = R_PPC64_TPREL16_LO;
12842                   rel->r_offset = offset + d_offset;
12843                   insn2 = 0x38630000;   /* addi 3,3,0 */
12844                 }
12845               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12846               /* Zap the reloc on the _tls_get_addr call too.  */
12847               BFD_ASSERT (offset == rel[1].r_offset);
12848               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12849               insn3 = bfd_get_32 (output_bfd,
12850                                   contents + offset + 4);
12851               if (insn3 == NOP
12852                   || insn3 == CROR_151515 || insn3 == CROR_313131)
12853                 {
12854                   rel->r_offset += 4;
12855                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12856                   insn2 = NOP;
12857                 }
12858               bfd_put_32 (output_bfd, insn2, contents + offset);
12859               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
12860                 {
12861                   rel--;
12862                   continue;
12863                 }
12864             }
12865           break;
12866
12867         case R_PPC64_TLSLD:
12868           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12869             {
12870               unsigned int insn2, insn3;
12871               bfd_vma offset = rel->r_offset;
12872
12873               if (toc_symndx)
12874                 sec = local_sections[toc_symndx];
12875               for (r_symndx = 0;
12876                    r_symndx < symtab_hdr->sh_info;
12877                    r_symndx++)
12878                 if (local_sections[r_symndx] == sec)
12879                   break;
12880               if (r_symndx >= symtab_hdr->sh_info)
12881                 r_symndx = STN_UNDEF;
12882               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12883               if (r_symndx != STN_UNDEF)
12884                 rel->r_addend -= (local_syms[r_symndx].st_value
12885                                   + sec->output_offset
12886                                   + sec->output_section->vma);
12887
12888               r_type = R_PPC64_TPREL16_LO;
12889               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12890               rel->r_offset = offset + d_offset;
12891               /* Zap the reloc on the _tls_get_addr call too.  */
12892               BFD_ASSERT (offset == rel[1].r_offset);
12893               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12894               insn2 = 0x38630000;       /* addi 3,3,0 */
12895               insn3 = bfd_get_32 (output_bfd,
12896                                   contents + offset + 4);
12897               if (insn3 == NOP
12898                   || insn3 == CROR_151515 || insn3 == CROR_313131)
12899                 {
12900                   rel->r_offset += 4;
12901                   bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12902                   insn2 = NOP;
12903                 }
12904               bfd_put_32 (output_bfd, insn2, contents + offset);
12905               rel--;
12906               continue;
12907             }
12908           break;
12909
12910         case R_PPC64_DTPMOD64:
12911           if (rel + 1 < relend
12912               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
12913               && rel[1].r_offset == rel->r_offset + 8)
12914             {
12915               if ((tls_mask & TLS_GD) == 0)
12916                 {
12917                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
12918                   if ((tls_mask & TLS_TPRELGD) != 0)
12919                     r_type = R_PPC64_TPREL64;
12920                   else
12921                     {
12922                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12923                       r_type = R_PPC64_NONE;
12924                     }
12925                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12926                 }
12927             }
12928           else
12929             {
12930               if ((tls_mask & TLS_LD) == 0)
12931                 {
12932                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12933                   r_type = R_PPC64_NONE;
12934                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12935                 }
12936             }
12937           break;
12938
12939         case R_PPC64_TPREL64:
12940           if ((tls_mask & TLS_TPREL) == 0)
12941             {
12942               r_type = R_PPC64_NONE;
12943               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12944             }
12945           break;
12946         }
12947
12948       /* Handle other relocations that tweak non-addend part of insn.  */
12949       insn = 0;
12950       max_br_offset = 1 << 25;
12951       addend = rel->r_addend;
12952       reloc_dest = DEST_NORMAL;
12953       switch (r_type)
12954         {
12955         default:
12956           break;
12957
12958         case R_PPC64_TOCSAVE:
12959           if (relocation + addend == (rel->r_offset
12960                                       + input_section->output_offset
12961                                       + input_section->output_section->vma)
12962               && tocsave_find (htab, NO_INSERT,
12963                                &local_syms, rel, input_bfd))
12964             {
12965               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
12966               if (insn == NOP
12967                   || insn == CROR_151515 || insn == CROR_313131)
12968                 bfd_put_32 (input_bfd, STD_R2_40R1,
12969                             contents + rel->r_offset);
12970             }
12971           break;
12972
12973           /* Branch taken prediction relocations.  */
12974         case R_PPC64_ADDR14_BRTAKEN:
12975         case R_PPC64_REL14_BRTAKEN:
12976           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
12977           /* Fall thru.  */
12978
12979           /* Branch not taken prediction relocations.  */
12980         case R_PPC64_ADDR14_BRNTAKEN:
12981         case R_PPC64_REL14_BRNTAKEN:
12982           insn |= bfd_get_32 (output_bfd,
12983                               contents + rel->r_offset) & ~(0x01 << 21);
12984           /* Fall thru.  */
12985
12986         case R_PPC64_REL14:
12987           max_br_offset = 1 << 15;
12988           /* Fall thru.  */
12989
12990         case R_PPC64_REL24:
12991           /* Calls to functions with a different TOC, such as calls to
12992              shared objects, need to alter the TOC pointer.  This is
12993              done using a linkage stub.  A REL24 branching to these
12994              linkage stubs needs to be followed by a nop, as the nop
12995              will be replaced with an instruction to restore the TOC
12996              base pointer.  */
12997           fdh = h;
12998           if (h != NULL
12999               && h->oh != NULL
13000               && h->oh->is_func_descriptor)
13001             fdh = ppc_follow_link (h->oh);
13002           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13003                                            htab);
13004           if (stub_entry != NULL
13005               && (stub_entry->stub_type == ppc_stub_plt_call
13006                   || stub_entry->stub_type == ppc_stub_plt_call_r2save
13007                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13008                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
13009             {
13010               bfd_boolean can_plt_call = FALSE;
13011
13012               /* All of these stubs will modify r2, so there must be a
13013                  branch and link followed by a nop.  The nop is
13014                  replaced by an insn to restore r2.  */
13015               if (rel->r_offset + 8 <= input_section->size)
13016                 {
13017                   unsigned long br;
13018
13019                   br = bfd_get_32 (input_bfd,
13020                                    contents + rel->r_offset);
13021                   if ((br & 1) != 0)
13022                     {
13023                       unsigned long nop;
13024
13025                       nop = bfd_get_32 (input_bfd,
13026                                         contents + rel->r_offset + 4);
13027                       if (nop == NOP
13028                           || nop == CROR_151515 || nop == CROR_313131)
13029                         {
13030                           if (h != NULL
13031                               && (h == htab->tls_get_addr_fd
13032                                   || h == htab->tls_get_addr)
13033                               && !htab->no_tls_get_addr_opt)
13034                             {
13035                               /* Special stub used, leave nop alone.  */
13036                             }
13037                           else
13038                             bfd_put_32 (input_bfd, LD_R2_40R1,
13039                                         contents + rel->r_offset + 4);
13040                           can_plt_call = TRUE;
13041                         }
13042                     }
13043                 }
13044
13045               if (!can_plt_call && h != NULL)
13046                 {
13047                   const char *name = h->elf.root.root.string;
13048
13049                   if (*name == '.')
13050                     ++name;
13051
13052                   if (strncmp (name, "__libc_start_main", 17) == 0
13053                       && (name[17] == 0 || name[17] == '@'))
13054                     {
13055                       /* Allow crt1 branch to go via a toc adjusting
13056                          stub.  Other calls that never return could do
13057                          the same, if we could detect such.  */
13058                       can_plt_call = TRUE;
13059                     }
13060                 }
13061
13062               if (!can_plt_call)
13063                 {
13064                   /* g++ as of 20130507 emits self-calls without a
13065                      following nop.  This is arguably wrong since we
13066                      have conflicting information.  On the one hand a
13067                      global symbol and on the other a local call
13068                      sequence, but don't error for this special case.
13069                      It isn't possible to cheaply verify we have
13070                      exactly such a call.  Allow all calls to the same
13071                      section.  */
13072                   asection *code_sec = sec;
13073
13074                   if (get_opd_info (sec) != NULL)
13075                     {
13076                       bfd_vma off = (relocation + addend
13077                                      - sec->output_section->vma
13078                                      - sec->output_offset);
13079
13080                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
13081                     }
13082                   if (code_sec == input_section)
13083                     can_plt_call = TRUE;
13084                 }
13085
13086               if (!can_plt_call)
13087                 {
13088                   info->callbacks->einfo
13089                     (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13090                        "recompile with -fPIC"),
13091                      input_bfd, input_section, rel->r_offset, sym_name);
13092
13093                   bfd_set_error (bfd_error_bad_value);
13094                   ret = FALSE;
13095                 }
13096
13097               if (can_plt_call
13098                   && (stub_entry->stub_type == ppc_stub_plt_call
13099                       || stub_entry->stub_type == ppc_stub_plt_call_r2save))
13100                 unresolved_reloc = FALSE;
13101             }
13102
13103           if ((stub_entry == NULL
13104                || stub_entry->stub_type == ppc_stub_long_branch
13105                || stub_entry->stub_type == ppc_stub_plt_branch)
13106               && get_opd_info (sec) != NULL)
13107             {
13108               /* The branch destination is the value of the opd entry. */
13109               bfd_vma off = (relocation + addend
13110                              - sec->output_section->vma
13111                              - sec->output_offset);
13112               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
13113               if (dest != (bfd_vma) -1)
13114                 {
13115                   relocation = dest;
13116                   addend = 0;
13117                   reloc_dest = DEST_OPD;
13118                 }
13119             }
13120
13121           /* If the branch is out of reach we ought to have a long
13122              branch stub.  */
13123           from = (rel->r_offset
13124                   + input_section->output_offset
13125                   + input_section->output_section->vma);
13126
13127           if (stub_entry != NULL
13128               && (stub_entry->stub_type == ppc_stub_long_branch
13129                   || stub_entry->stub_type == ppc_stub_plt_branch)
13130               && (r_type == R_PPC64_ADDR14_BRTAKEN
13131                   || r_type == R_PPC64_ADDR14_BRNTAKEN
13132                   || (relocation + addend - from + max_br_offset
13133                       < 2 * max_br_offset)))
13134             /* Don't use the stub if this branch is in range.  */
13135             stub_entry = NULL;
13136
13137           if (stub_entry != NULL)
13138             {
13139               /* Munge up the value and addend so that we call the stub
13140                  rather than the procedure directly.  */
13141               relocation = (stub_entry->stub_offset
13142                             + stub_entry->stub_sec->output_offset
13143                             + stub_entry->stub_sec->output_section->vma);
13144               addend = 0;
13145               reloc_dest = DEST_STUB;
13146
13147               if ((stub_entry->stub_type == ppc_stub_plt_call
13148                    || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13149                   && (ALWAYS_EMIT_R2SAVE
13150                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13151                   && rel + 1 < relend
13152                   && rel[1].r_offset == rel->r_offset + 4
13153                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
13154                 relocation += 4;
13155             }
13156
13157           if (insn != 0)
13158             {
13159               if (is_isa_v2)
13160                 {
13161                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
13162                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
13163                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
13164                   if ((insn & (0x14 << 21)) == (0x04 << 21))
13165                     insn |= 0x02 << 21;
13166                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
13167                     insn |= 0x08 << 21;
13168                   else
13169                     break;
13170                 }
13171               else
13172                 {
13173                   /* Invert 'y' bit if not the default.  */
13174                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
13175                     insn ^= 0x01 << 21;
13176                 }
13177
13178               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
13179             }
13180
13181           /* NOP out calls to undefined weak functions.
13182              We can thus call a weak function without first
13183              checking whether the function is defined.  */
13184           else if (h != NULL
13185                    && h->elf.root.type == bfd_link_hash_undefweak
13186                    && h->elf.dynindx == -1
13187                    && r_type == R_PPC64_REL24
13188                    && relocation == 0
13189                    && addend == 0)
13190             {
13191               bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13192               continue;
13193             }
13194           break;
13195         }
13196
13197       /* Set `addend'.  */
13198       tls_type = 0;
13199       switch (r_type)
13200         {
13201         default:
13202           info->callbacks->einfo
13203             (_("%P: %B: unknown relocation type %d for `%T'\n"),
13204              input_bfd, (int) r_type, sym_name);
13205
13206           bfd_set_error (bfd_error_bad_value);
13207           ret = FALSE;
13208           continue;
13209
13210         case R_PPC64_NONE:
13211         case R_PPC64_TLS:
13212         case R_PPC64_TLSGD:
13213         case R_PPC64_TLSLD:
13214         case R_PPC64_TOCSAVE:
13215         case R_PPC64_GNU_VTINHERIT:
13216         case R_PPC64_GNU_VTENTRY:
13217           continue;
13218
13219           /* GOT16 relocations.  Like an ADDR16 using the symbol's
13220              address in the GOT as relocation value instead of the
13221              symbol's value itself.  Also, create a GOT entry for the
13222              symbol and put the symbol value there.  */
13223         case R_PPC64_GOT_TLSGD16:
13224         case R_PPC64_GOT_TLSGD16_LO:
13225         case R_PPC64_GOT_TLSGD16_HI:
13226         case R_PPC64_GOT_TLSGD16_HA:
13227           tls_type = TLS_TLS | TLS_GD;
13228           goto dogot;
13229
13230         case R_PPC64_GOT_TLSLD16:
13231         case R_PPC64_GOT_TLSLD16_LO:
13232         case R_PPC64_GOT_TLSLD16_HI:
13233         case R_PPC64_GOT_TLSLD16_HA:
13234           tls_type = TLS_TLS | TLS_LD;
13235           goto dogot;
13236
13237         case R_PPC64_GOT_TPREL16_DS:
13238         case R_PPC64_GOT_TPREL16_LO_DS:
13239         case R_PPC64_GOT_TPREL16_HI:
13240         case R_PPC64_GOT_TPREL16_HA:
13241           tls_type = TLS_TLS | TLS_TPREL;
13242           goto dogot;
13243
13244         case R_PPC64_GOT_DTPREL16_DS:
13245         case R_PPC64_GOT_DTPREL16_LO_DS:
13246         case R_PPC64_GOT_DTPREL16_HI:
13247         case R_PPC64_GOT_DTPREL16_HA:
13248           tls_type = TLS_TLS | TLS_DTPREL;
13249           goto dogot;
13250
13251         case R_PPC64_GOT16:
13252         case R_PPC64_GOT16_LO:
13253         case R_PPC64_GOT16_HI:
13254         case R_PPC64_GOT16_HA:
13255         case R_PPC64_GOT16_DS:
13256         case R_PPC64_GOT16_LO_DS:
13257         dogot:
13258           {
13259             /* Relocation is to the entry for this symbol in the global
13260                offset table.  */
13261             asection *got;
13262             bfd_vma *offp;
13263             bfd_vma off;
13264             unsigned long indx = 0;
13265             struct got_entry *ent;
13266
13267             if (tls_type == (TLS_TLS | TLS_LD)
13268                 && (h == NULL
13269                     || !h->elf.def_dynamic))
13270               ent = ppc64_tlsld_got (input_bfd);
13271             else
13272               {
13273
13274                 if (h != NULL)
13275                   {
13276                     bfd_boolean dyn = htab->elf.dynamic_sections_created;
13277                     if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
13278                                                           &h->elf)
13279                         || (info->shared
13280                             && SYMBOL_CALLS_LOCAL (info, &h->elf)))
13281                       /* This is actually a static link, or it is a
13282                          -Bsymbolic link and the symbol is defined
13283                          locally, or the symbol was forced to be local
13284                          because of a version file.  */
13285                       ;
13286                     else
13287                       {
13288                         BFD_ASSERT (h->elf.dynindx != -1);
13289                         indx = h->elf.dynindx;
13290                         unresolved_reloc = FALSE;
13291                       }
13292                     ent = h->elf.got.glist;
13293                   }
13294                 else
13295                   {
13296                     if (local_got_ents == NULL)
13297                       abort ();
13298                     ent = local_got_ents[r_symndx];
13299                   }
13300
13301                 for (; ent != NULL; ent = ent->next)
13302                   if (ent->addend == orig_rel.r_addend
13303                       && ent->owner == input_bfd
13304                       && ent->tls_type == tls_type)
13305                     break;
13306               }
13307
13308             if (ent == NULL)
13309               abort ();
13310             if (ent->is_indirect)
13311               ent = ent->got.ent;
13312             offp = &ent->got.offset;
13313             got = ppc64_elf_tdata (ent->owner)->got;
13314             if (got == NULL)
13315               abort ();
13316
13317             /* The offset must always be a multiple of 8.  We use the
13318                least significant bit to record whether we have already
13319                processed this entry.  */
13320             off = *offp;
13321             if ((off & 1) != 0)
13322               off &= ~1;
13323             else
13324               {
13325                 /* Generate relocs for the dynamic linker, except in
13326                    the case of TLSLD where we'll use one entry per
13327                    module.  */
13328                 asection *relgot;
13329                 bfd_boolean ifunc;
13330
13331                 *offp = off | 1;
13332                 relgot = NULL;
13333                 ifunc = (h != NULL
13334                          ? h->elf.type == STT_GNU_IFUNC
13335                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
13336                 if (ifunc)
13337                   relgot = htab->reliplt;
13338                 else if ((info->shared || indx != 0)
13339                          && (h == NULL
13340                              || (tls_type == (TLS_TLS | TLS_LD)
13341                                  && !h->elf.def_dynamic)
13342                              || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13343                              || h->elf.root.type != bfd_link_hash_undefweak))
13344                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
13345                 if (relgot != NULL)
13346                   {
13347                     outrel.r_offset = (got->output_section->vma
13348                                        + got->output_offset
13349                                        + off);
13350                     outrel.r_addend = addend;
13351                     if (tls_type & (TLS_LD | TLS_GD))
13352                       {
13353                         outrel.r_addend = 0;
13354                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
13355                         if (tls_type == (TLS_TLS | TLS_GD))
13356                           {
13357                             loc = relgot->contents;
13358                             loc += (relgot->reloc_count++
13359                                     * sizeof (Elf64_External_Rela));
13360                             bfd_elf64_swap_reloca_out (output_bfd,
13361                                                        &outrel, loc);
13362                             outrel.r_offset += 8;
13363                             outrel.r_addend = addend;
13364                             outrel.r_info
13365                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
13366                           }
13367                       }
13368                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
13369                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
13370                     else if (tls_type == (TLS_TLS | TLS_TPREL))
13371                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
13372                     else if (indx != 0)
13373                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
13374                     else
13375                       {
13376                         if (ifunc)
13377                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13378                         else
13379                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13380
13381                         /* Write the .got section contents for the sake
13382                            of prelink.  */
13383                         loc = got->contents + off;
13384                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
13385                                     loc);
13386                       }
13387
13388                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
13389                       {
13390                         outrel.r_addend += relocation;
13391                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
13392                           outrel.r_addend -= htab->elf.tls_sec->vma;
13393                       }
13394                     loc = relgot->contents;
13395                     loc += (relgot->reloc_count++
13396                             * sizeof (Elf64_External_Rela));
13397                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13398                   }
13399
13400                 /* Init the .got section contents here if we're not
13401                    emitting a reloc.  */
13402                 else
13403                   {
13404                     relocation += addend;
13405                     if (tls_type == (TLS_TLS | TLS_LD))
13406                       relocation = 1;
13407                     else if (tls_type != 0)
13408                       {
13409                         relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
13410                         if (tls_type == (TLS_TLS | TLS_TPREL))
13411                           relocation += DTP_OFFSET - TP_OFFSET;
13412
13413                         if (tls_type == (TLS_TLS | TLS_GD))
13414                           {
13415                             bfd_put_64 (output_bfd, relocation,
13416                                         got->contents + off + 8);
13417                             relocation = 1;
13418                           }
13419                       }
13420
13421                     bfd_put_64 (output_bfd, relocation,
13422                                 got->contents + off);
13423                   }
13424               }
13425
13426             if (off >= (bfd_vma) -2)
13427               abort ();
13428
13429             relocation = got->output_section->vma + got->output_offset + off;
13430             addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
13431           }
13432           break;
13433
13434         case R_PPC64_PLT16_HA:
13435         case R_PPC64_PLT16_HI:
13436         case R_PPC64_PLT16_LO:
13437         case R_PPC64_PLT32:
13438         case R_PPC64_PLT64:
13439           /* Relocation is to the entry for this symbol in the
13440              procedure linkage table.  */
13441
13442           /* Resolve a PLT reloc against a local symbol directly,
13443              without using the procedure linkage table.  */
13444           if (h == NULL)
13445             break;
13446
13447           /* It's possible that we didn't make a PLT entry for this
13448              symbol.  This happens when statically linking PIC code,
13449              or when using -Bsymbolic.  Go find a match if there is a
13450              PLT entry.  */
13451           if (htab->plt != NULL)
13452             {
13453               struct plt_entry *ent;
13454               for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
13455                 if (ent->addend == orig_rel.r_addend
13456                     && ent->plt.offset != (bfd_vma) -1)
13457                   {
13458                     relocation = (htab->plt->output_section->vma
13459                                   + htab->plt->output_offset
13460                                   + ent->plt.offset);
13461                     unresolved_reloc = FALSE;
13462                   }
13463             }
13464           break;
13465
13466         case R_PPC64_TOC:
13467           /* Relocation value is TOC base.  */
13468           relocation = TOCstart;
13469           if (r_symndx == STN_UNDEF)
13470             relocation += htab->stub_group[input_section->id].toc_off;
13471           else if (unresolved_reloc)
13472             ;
13473           else if (sec != NULL && sec->id <= htab->top_id)
13474             relocation += htab->stub_group[sec->id].toc_off;
13475           else
13476             unresolved_reloc = TRUE;
13477           goto dodyn;
13478
13479           /* TOC16 relocs.  We want the offset relative to the TOC base,
13480              which is the address of the start of the TOC plus 0x8000.
13481              The TOC consists of sections .got, .toc, .tocbss, and .plt,
13482              in this order.  */
13483         case R_PPC64_TOC16:
13484         case R_PPC64_TOC16_LO:
13485         case R_PPC64_TOC16_HI:
13486         case R_PPC64_TOC16_DS:
13487         case R_PPC64_TOC16_LO_DS:
13488         case R_PPC64_TOC16_HA:
13489           addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
13490           break;
13491
13492           /* Relocate against the beginning of the section.  */
13493         case R_PPC64_SECTOFF:
13494         case R_PPC64_SECTOFF_LO:
13495         case R_PPC64_SECTOFF_HI:
13496         case R_PPC64_SECTOFF_DS:
13497         case R_PPC64_SECTOFF_LO_DS:
13498         case R_PPC64_SECTOFF_HA:
13499           if (sec != NULL)
13500             addend -= sec->output_section->vma;
13501           break;
13502
13503         case R_PPC64_REL16:
13504         case R_PPC64_REL16_LO:
13505         case R_PPC64_REL16_HI:
13506         case R_PPC64_REL16_HA:
13507           break;
13508
13509         case R_PPC64_REL14:
13510         case R_PPC64_REL14_BRNTAKEN:
13511         case R_PPC64_REL14_BRTAKEN:
13512         case R_PPC64_REL24:
13513           break;
13514
13515         case R_PPC64_TPREL16:
13516         case R_PPC64_TPREL16_LO:
13517         case R_PPC64_TPREL16_HI:
13518         case R_PPC64_TPREL16_HA:
13519         case R_PPC64_TPREL16_DS:
13520         case R_PPC64_TPREL16_LO_DS:
13521         case R_PPC64_TPREL16_HIGHER:
13522         case R_PPC64_TPREL16_HIGHERA:
13523         case R_PPC64_TPREL16_HIGHEST:
13524         case R_PPC64_TPREL16_HIGHESTA:
13525           if (h != NULL
13526               && h->elf.root.type == bfd_link_hash_undefweak
13527               && h->elf.dynindx == -1)
13528             {
13529               /* Make this relocation against an undefined weak symbol
13530                  resolve to zero.  This is really just a tweak, since
13531                  code using weak externs ought to check that they are
13532                  defined before using them.  */
13533               bfd_byte *p = contents + rel->r_offset - d_offset;
13534
13535               insn = bfd_get_32 (output_bfd, p);
13536               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
13537               if (insn != 0)
13538                 bfd_put_32 (output_bfd, insn, p);
13539               break;
13540             }
13541           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13542           if (info->shared)
13543             /* The TPREL16 relocs shouldn't really be used in shared
13544                libs as they will result in DT_TEXTREL being set, but
13545                support them anyway.  */
13546             goto dodyn;
13547           break;
13548
13549         case R_PPC64_DTPREL16:
13550         case R_PPC64_DTPREL16_LO:
13551         case R_PPC64_DTPREL16_HI:
13552         case R_PPC64_DTPREL16_HA:
13553         case R_PPC64_DTPREL16_DS:
13554         case R_PPC64_DTPREL16_LO_DS:
13555         case R_PPC64_DTPREL16_HIGHER:
13556         case R_PPC64_DTPREL16_HIGHERA:
13557         case R_PPC64_DTPREL16_HIGHEST:
13558         case R_PPC64_DTPREL16_HIGHESTA:
13559           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13560           break;
13561
13562         case R_PPC64_DTPMOD64:
13563           relocation = 1;
13564           addend = 0;
13565           goto dodyn;
13566
13567         case R_PPC64_TPREL64:
13568           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13569           goto dodyn;
13570
13571         case R_PPC64_DTPREL64:
13572           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13573           /* Fall thru */
13574
13575           /* Relocations that may need to be propagated if this is a
13576              dynamic object.  */
13577         case R_PPC64_REL30:
13578         case R_PPC64_REL32:
13579         case R_PPC64_REL64:
13580         case R_PPC64_ADDR14:
13581         case R_PPC64_ADDR14_BRNTAKEN:
13582         case R_PPC64_ADDR14_BRTAKEN:
13583         case R_PPC64_ADDR16:
13584         case R_PPC64_ADDR16_DS:
13585         case R_PPC64_ADDR16_HA:
13586         case R_PPC64_ADDR16_HI:
13587         case R_PPC64_ADDR16_HIGHER:
13588         case R_PPC64_ADDR16_HIGHERA:
13589         case R_PPC64_ADDR16_HIGHEST:
13590         case R_PPC64_ADDR16_HIGHESTA:
13591         case R_PPC64_ADDR16_LO:
13592         case R_PPC64_ADDR16_LO_DS:
13593         case R_PPC64_ADDR24:
13594         case R_PPC64_ADDR32:
13595         case R_PPC64_ADDR64:
13596         case R_PPC64_UADDR16:
13597         case R_PPC64_UADDR32:
13598         case R_PPC64_UADDR64:
13599         dodyn:
13600           if ((input_section->flags & SEC_ALLOC) == 0)
13601             break;
13602
13603           if (NO_OPD_RELOCS && is_opd)
13604             break;
13605
13606           if ((info->shared
13607                && (h == NULL
13608                    || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13609                    || h->elf.root.type != bfd_link_hash_undefweak)
13610                && (must_be_dyn_reloc (info, r_type)
13611                    || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
13612               || (ELIMINATE_COPY_RELOCS
13613                   && !info->shared
13614                   && h != NULL
13615                   && h->elf.dynindx != -1
13616                   && !h->elf.non_got_ref
13617                   && !h->elf.def_regular)
13618               || (!info->shared
13619                   && (h != NULL
13620                       ? h->elf.type == STT_GNU_IFUNC
13621                       : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
13622             {
13623               bfd_boolean skip, relocate;
13624               asection *sreloc;
13625               bfd_vma out_off;
13626
13627               /* When generating a dynamic object, these relocations
13628                  are copied into the output file to be resolved at run
13629                  time.  */
13630
13631               skip = FALSE;
13632               relocate = FALSE;
13633
13634               out_off = _bfd_elf_section_offset (output_bfd, info,
13635                                                  input_section, rel->r_offset);
13636               if (out_off == (bfd_vma) -1)
13637                 skip = TRUE;
13638               else if (out_off == (bfd_vma) -2)
13639                 skip = TRUE, relocate = TRUE;
13640               out_off += (input_section->output_section->vma
13641                           + input_section->output_offset);
13642               outrel.r_offset = out_off;
13643               outrel.r_addend = rel->r_addend;
13644
13645               /* Optimize unaligned reloc use.  */
13646               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
13647                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
13648                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
13649               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
13650                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
13651                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
13652               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
13653                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
13654                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
13655
13656               if (skip)
13657                 memset (&outrel, 0, sizeof outrel);
13658               else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
13659                        && !is_opd
13660                        && r_type != R_PPC64_TOC)
13661                 {
13662                   BFD_ASSERT (h->elf.dynindx != -1);
13663                   outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
13664                 }
13665               else
13666                 {
13667                   /* This symbol is local, or marked to become local,
13668                      or this is an opd section reloc which must point
13669                      at a local function.  */
13670                   outrel.r_addend += relocation;
13671                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13672                     {
13673                       if (is_opd && h != NULL)
13674                         {
13675                           /* Lie about opd entries.  This case occurs
13676                              when building shared libraries and we
13677                              reference a function in another shared
13678                              lib.  The same thing happens for a weak
13679                              definition in an application that's
13680                              overridden by a strong definition in a
13681                              shared lib.  (I believe this is a generic
13682                              bug in binutils handling of weak syms.)
13683                              In these cases we won't use the opd
13684                              entry in this lib.  */
13685                           unresolved_reloc = FALSE;
13686                         }
13687                       if (!is_opd
13688                           && r_type == R_PPC64_ADDR64
13689                           && (h != NULL
13690                               ? h->elf.type == STT_GNU_IFUNC
13691                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
13692                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13693                       else
13694                         {
13695                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13696
13697                           /* We need to relocate .opd contents for ld.so.
13698                              Prelink also wants simple and consistent rules
13699                              for relocs.  This make all RELATIVE relocs have
13700                              *r_offset equal to r_addend.  */
13701                           relocate = TRUE;
13702                         }
13703                     }
13704                   else
13705                     {
13706                       long indx = 0;
13707
13708                       if (h != NULL
13709                           ? h->elf.type == STT_GNU_IFUNC
13710                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13711                         {
13712                           info->callbacks->einfo
13713                             (_("%P: %H: %s for indirect "
13714                                "function `%T' unsupported\n"),
13715                              input_bfd, input_section, rel->r_offset,
13716                              ppc64_elf_howto_table[r_type]->name,
13717                              sym_name);
13718                           ret = FALSE;
13719                         }
13720                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
13721                         ;
13722                       else if (sec == NULL || sec->owner == NULL)
13723                         {
13724                           bfd_set_error (bfd_error_bad_value);
13725                           return FALSE;
13726                         }
13727                       else
13728                         {
13729                           asection *osec;
13730
13731                           osec = sec->output_section;
13732                           indx = elf_section_data (osec)->dynindx;
13733
13734                           if (indx == 0)
13735                             {
13736                               if ((osec->flags & SEC_READONLY) == 0
13737                                   && htab->elf.data_index_section != NULL)
13738                                 osec = htab->elf.data_index_section;
13739                               else
13740                                 osec = htab->elf.text_index_section;
13741                               indx = elf_section_data (osec)->dynindx;
13742                             }
13743                           BFD_ASSERT (indx != 0);
13744
13745                           /* We are turning this relocation into one
13746                              against a section symbol, so subtract out
13747                              the output section's address but not the
13748                              offset of the input section in the output
13749                              section.  */
13750                           outrel.r_addend -= osec->vma;
13751                         }
13752
13753                       outrel.r_info = ELF64_R_INFO (indx, r_type);
13754                     }
13755                 }
13756
13757               sreloc = elf_section_data (input_section)->sreloc;
13758               if (h != NULL
13759                   ? h->elf.type == STT_GNU_IFUNC
13760                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13761                 sreloc = htab->reliplt;
13762               if (sreloc == NULL)
13763                 abort ();
13764
13765               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
13766                   >= sreloc->size)
13767                 abort ();
13768               loc = sreloc->contents;
13769               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
13770               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13771
13772               /* If this reloc is against an external symbol, it will
13773                  be computed at runtime, so there's no need to do
13774                  anything now.  However, for the sake of prelink ensure
13775                  that the section contents are a known value.  */
13776               if (! relocate)
13777                 {
13778                   unresolved_reloc = FALSE;
13779                   /* The value chosen here is quite arbitrary as ld.so
13780                      ignores section contents except for the special
13781                      case of .opd where the contents might be accessed
13782                      before relocation.  Choose zero, as that won't
13783                      cause reloc overflow.  */
13784                   relocation = 0;
13785                   addend = 0;
13786                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
13787                      to improve backward compatibility with older
13788                      versions of ld.  */
13789                   if (r_type == R_PPC64_ADDR64)
13790                     addend = outrel.r_addend;
13791                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
13792                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
13793                     addend = (input_section->output_section->vma
13794                               + input_section->output_offset
13795                               + rel->r_offset);
13796                 }
13797             }
13798           break;
13799
13800         case R_PPC64_COPY:
13801         case R_PPC64_GLOB_DAT:
13802         case R_PPC64_JMP_SLOT:
13803         case R_PPC64_JMP_IREL:
13804         case R_PPC64_RELATIVE:
13805           /* We shouldn't ever see these dynamic relocs in relocatable
13806              files.  */
13807           /* Fall through.  */
13808
13809         case R_PPC64_PLTGOT16:
13810         case R_PPC64_PLTGOT16_DS:
13811         case R_PPC64_PLTGOT16_HA:
13812         case R_PPC64_PLTGOT16_HI:
13813         case R_PPC64_PLTGOT16_LO:
13814         case R_PPC64_PLTGOT16_LO_DS:
13815         case R_PPC64_PLTREL32:
13816         case R_PPC64_PLTREL64:
13817           /* These ones haven't been implemented yet.  */
13818
13819           info->callbacks->einfo
13820             (_("%P: %B: %s is not supported for `%T'\n"),
13821              input_bfd,
13822              ppc64_elf_howto_table[r_type]->name, sym_name);
13823
13824           bfd_set_error (bfd_error_invalid_operation);
13825           ret = FALSE;
13826           continue;
13827         }
13828
13829       /* Multi-instruction sequences that access the TOC can be
13830          optimized, eg. addis ra,r2,0; addi rb,ra,x;
13831          to             nop;           addi rb,r2,x;  */
13832       switch (r_type)
13833         {
13834         default:
13835           break;
13836
13837         case R_PPC64_GOT_TLSLD16_HI:
13838         case R_PPC64_GOT_TLSGD16_HI:
13839         case R_PPC64_GOT_TPREL16_HI:
13840         case R_PPC64_GOT_DTPREL16_HI:
13841         case R_PPC64_GOT16_HI:
13842         case R_PPC64_TOC16_HI:
13843           /* These relocs would only be useful if building up an
13844              offset to later add to r2, perhaps in an indexed
13845              addressing mode instruction.  Don't try to optimize.
13846              Unfortunately, the possibility of someone building up an
13847              offset like this or even with the HA relocs, means that
13848              we need to check the high insn when optimizing the low
13849              insn.  */
13850           break;
13851
13852         case R_PPC64_GOT_TLSLD16_HA:
13853         case R_PPC64_GOT_TLSGD16_HA:
13854         case R_PPC64_GOT_TPREL16_HA:
13855         case R_PPC64_GOT_DTPREL16_HA:
13856         case R_PPC64_GOT16_HA:
13857         case R_PPC64_TOC16_HA:
13858           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
13859               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
13860             {
13861               bfd_byte *p = contents + (rel->r_offset & ~3);
13862               bfd_put_32 (input_bfd, NOP, p);
13863             }
13864           break;
13865
13866         case R_PPC64_GOT_TLSLD16_LO:
13867         case R_PPC64_GOT_TLSGD16_LO:
13868         case R_PPC64_GOT_TPREL16_LO_DS:
13869         case R_PPC64_GOT_DTPREL16_LO_DS:
13870         case R_PPC64_GOT16_LO:
13871         case R_PPC64_GOT16_LO_DS:
13872         case R_PPC64_TOC16_LO:
13873         case R_PPC64_TOC16_LO_DS:
13874           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
13875               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
13876             {
13877               bfd_byte *p = contents + (rel->r_offset & ~3);
13878               insn = bfd_get_32 (input_bfd, p);
13879               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
13880                 {
13881                   /* Transform addic to addi when we change reg.  */
13882                   insn &= ~((0x3f << 26) | (0x1f << 16));
13883                   insn |= (14u << 26) | (2 << 16);
13884                 }
13885               else
13886                 {
13887                   insn &= ~(0x1f << 16);
13888                   insn |= 2 << 16;
13889                 }
13890               bfd_put_32 (input_bfd, insn, p);
13891             }
13892           break;
13893         }
13894
13895       /* Do any further special processing.  */
13896       switch (r_type)
13897         {
13898         default:
13899           break;
13900
13901         case R_PPC64_ADDR16_HA:
13902         case R_PPC64_REL16_HA:
13903         case R_PPC64_ADDR16_HIGHERA:
13904         case R_PPC64_ADDR16_HIGHESTA:
13905         case R_PPC64_TOC16_HA:
13906         case R_PPC64_SECTOFF_HA:
13907         case R_PPC64_TPREL16_HA:
13908         case R_PPC64_DTPREL16_HA:
13909         case R_PPC64_TPREL16_HIGHER:
13910         case R_PPC64_TPREL16_HIGHERA:
13911         case R_PPC64_TPREL16_HIGHEST:
13912         case R_PPC64_TPREL16_HIGHESTA:
13913         case R_PPC64_DTPREL16_HIGHER:
13914         case R_PPC64_DTPREL16_HIGHERA:
13915         case R_PPC64_DTPREL16_HIGHEST:
13916         case R_PPC64_DTPREL16_HIGHESTA:
13917           /* It's just possible that this symbol is a weak symbol
13918              that's not actually defined anywhere. In that case,
13919              'sec' would be NULL, and we should leave the symbol
13920              alone (it will be set to zero elsewhere in the link).  */
13921           if (sec == NULL)
13922             break;
13923           /* Fall thru */
13924
13925         case R_PPC64_GOT16_HA:
13926         case R_PPC64_PLTGOT16_HA:
13927         case R_PPC64_PLT16_HA:
13928         case R_PPC64_GOT_TLSGD16_HA:
13929         case R_PPC64_GOT_TLSLD16_HA:
13930         case R_PPC64_GOT_TPREL16_HA:
13931         case R_PPC64_GOT_DTPREL16_HA:
13932           /* Add 0x10000 if sign bit in 0:15 is set.
13933              Bits 0:15 are not used.  */
13934           addend += 0x8000;
13935           break;
13936
13937         case R_PPC64_ADDR16_DS:
13938         case R_PPC64_ADDR16_LO_DS:
13939         case R_PPC64_GOT16_DS:
13940         case R_PPC64_GOT16_LO_DS:
13941         case R_PPC64_PLT16_LO_DS:
13942         case R_PPC64_SECTOFF_DS:
13943         case R_PPC64_SECTOFF_LO_DS:
13944         case R_PPC64_TOC16_DS:
13945         case R_PPC64_TOC16_LO_DS:
13946         case R_PPC64_PLTGOT16_DS:
13947         case R_PPC64_PLTGOT16_LO_DS:
13948         case R_PPC64_GOT_TPREL16_DS:
13949         case R_PPC64_GOT_TPREL16_LO_DS:
13950         case R_PPC64_GOT_DTPREL16_DS:
13951         case R_PPC64_GOT_DTPREL16_LO_DS:
13952         case R_PPC64_TPREL16_DS:
13953         case R_PPC64_TPREL16_LO_DS:
13954         case R_PPC64_DTPREL16_DS:
13955         case R_PPC64_DTPREL16_LO_DS:
13956           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
13957           mask = 3;
13958           /* If this reloc is against an lq insn, then the value must be
13959              a multiple of 16.  This is somewhat of a hack, but the
13960              "correct" way to do this by defining _DQ forms of all the
13961              _DS relocs bloats all reloc switches in this file.  It
13962              doesn't seem to make much sense to use any of these relocs
13963              in data, so testing the insn should be safe.  */
13964           if ((insn & (0x3f << 26)) == (56u << 26))
13965             mask = 15;
13966           if (((relocation + addend) & mask) != 0)
13967             {
13968               info->callbacks->einfo
13969                 (_("%P: %H: error: %s not a multiple of %u\n"),
13970                  input_bfd, input_section, rel->r_offset,
13971                  ppc64_elf_howto_table[r_type]->name,
13972                  mask + 1);
13973               bfd_set_error (bfd_error_bad_value);
13974               ret = FALSE;
13975               continue;
13976             }
13977           break;
13978         }
13979
13980       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13981          because such sections are not SEC_ALLOC and thus ld.so will
13982          not process them.  */
13983       if (unresolved_reloc
13984           && !((input_section->flags & SEC_DEBUGGING) != 0
13985                && h->elf.def_dynamic)
13986           && _bfd_elf_section_offset (output_bfd, info, input_section,
13987                                       rel->r_offset) != (bfd_vma) -1)
13988         {
13989           info->callbacks->einfo
13990             (_("%P: %H: unresolvable %s against `%T'\n"),
13991              input_bfd, input_section, rel->r_offset,
13992              ppc64_elf_howto_table[(int) r_type]->name,
13993              h->elf.root.root.string);
13994           ret = FALSE;
13995         }
13996
13997       r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
13998                                     input_bfd,
13999                                     input_section,
14000                                     contents,
14001                                     rel->r_offset,
14002                                     relocation,
14003                                     addend);
14004
14005       if (r != bfd_reloc_ok)
14006         {
14007           char *more_info = NULL;
14008           const char *reloc_name = ppc64_elf_howto_table[r_type]->name;
14009
14010           if (reloc_dest != DEST_NORMAL)
14011             {
14012               more_info = bfd_malloc (strlen (reloc_name) + 8);
14013               if (more_info != NULL)
14014                 {
14015                   strcpy (more_info, reloc_name);
14016                   strcat (more_info, (reloc_dest == DEST_OPD
14017                                       ? " (OPD)" : " (stub)"));
14018                   reloc_name = more_info;
14019                 }
14020             }
14021
14022           if (r == bfd_reloc_overflow)
14023             {
14024               if (warned)
14025                 continue;
14026               if (h != NULL
14027                   && h->elf.root.type == bfd_link_hash_undefweak
14028                   && ppc64_elf_howto_table[r_type]->pc_relative)
14029                 {
14030                   /* Assume this is a call protected by other code that
14031                      detects the symbol is undefined.  If this is the case,
14032                      we can safely ignore the overflow.  If not, the
14033                      program is hosed anyway, and a little warning isn't
14034                      going to help.  */
14035
14036                   continue;
14037                 }
14038
14039               if (!((*info->callbacks->reloc_overflow)
14040                     (info, &h->elf.root, sym_name,
14041                      reloc_name, orig_rel.r_addend,
14042                      input_bfd, input_section, rel->r_offset)))
14043                 return FALSE;
14044             }
14045           else
14046             {
14047               info->callbacks->einfo
14048                 (_("%P: %H: %s against `%T': error %d\n"),
14049                  input_bfd, input_section, rel->r_offset,
14050                  reloc_name, sym_name, (int) r);
14051               ret = FALSE;
14052             }
14053           if (more_info != NULL)
14054             free (more_info);
14055         }
14056     }
14057
14058   /* If we're emitting relocations, then shortly after this function
14059      returns, reloc offsets and addends for this section will be
14060      adjusted.  Worse, reloc symbol indices will be for the output
14061      file rather than the input.  Save a copy of the relocs for
14062      opd_entry_value.  */
14063   if (is_opd && (info->emitrelocations || info->relocatable))
14064     {
14065       bfd_size_type amt;
14066       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
14067       rel = bfd_alloc (input_bfd, amt);
14068       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
14069       ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
14070       if (rel == NULL)
14071         return FALSE;
14072       memcpy (rel, relocs, amt);
14073     }
14074   return ret;
14075 }
14076
14077 /* Adjust the value of any local symbols in opd sections.  */
14078
14079 static int
14080 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
14081                               const char *name ATTRIBUTE_UNUSED,
14082                               Elf_Internal_Sym *elfsym,
14083                               asection *input_sec,
14084                               struct elf_link_hash_entry *h)
14085 {
14086   struct _opd_sec_data *opd;
14087   long adjust;
14088   bfd_vma value;
14089
14090   if (h != NULL)
14091     return 1;
14092
14093   opd = get_opd_info (input_sec);
14094   if (opd == NULL || opd->adjust == NULL)
14095     return 1;
14096
14097   value = elfsym->st_value - input_sec->output_offset;
14098   if (!info->relocatable)
14099     value -= input_sec->output_section->vma;
14100
14101   adjust = opd->adjust[value / 8];
14102   if (adjust == -1)
14103     return 2;
14104
14105   elfsym->st_value += adjust;
14106   return 1;
14107 }
14108
14109 /* Finish up dynamic symbol handling.  We set the contents of various
14110    dynamic sections here.  */
14111
14112 static bfd_boolean
14113 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
14114                                  struct bfd_link_info *info,
14115                                  struct elf_link_hash_entry *h,
14116                                  Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
14117 {
14118   struct ppc_link_hash_table *htab;
14119   struct plt_entry *ent;
14120   Elf_Internal_Rela rela;
14121   bfd_byte *loc;
14122
14123   htab = ppc_hash_table (info);
14124   if (htab == NULL)
14125     return FALSE;
14126
14127   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14128     if (ent->plt.offset != (bfd_vma) -1)
14129       {
14130         /* This symbol has an entry in the procedure linkage
14131            table.  Set it up.  */
14132         if (!htab->elf.dynamic_sections_created
14133             || h->dynindx == -1)
14134           {
14135             BFD_ASSERT (h->type == STT_GNU_IFUNC
14136                         && h->def_regular
14137                         && (h->root.type == bfd_link_hash_defined
14138                             || h->root.type == bfd_link_hash_defweak));
14139             rela.r_offset = (htab->iplt->output_section->vma
14140                              + htab->iplt->output_offset
14141                              + ent->plt.offset);
14142             rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14143             rela.r_addend = (h->root.u.def.value
14144                              + h->root.u.def.section->output_offset
14145                              + h->root.u.def.section->output_section->vma
14146                              + ent->addend);
14147             loc = (htab->reliplt->contents
14148                    + (htab->reliplt->reloc_count++
14149                       * sizeof (Elf64_External_Rela)));
14150           }
14151         else
14152           {
14153             rela.r_offset = (htab->plt->output_section->vma
14154                              + htab->plt->output_offset
14155                              + ent->plt.offset);
14156             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14157             rela.r_addend = ent->addend;
14158             loc = (htab->relplt->contents
14159                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
14160                       / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
14161           }
14162         bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
14163       }
14164
14165   if (h->needs_copy)
14166     {
14167       /* This symbol needs a copy reloc.  Set it up.  */
14168
14169       if (h->dynindx == -1
14170           || (h->root.type != bfd_link_hash_defined
14171               && h->root.type != bfd_link_hash_defweak)
14172           || htab->relbss == NULL)
14173         abort ();
14174
14175       rela.r_offset = (h->root.u.def.value
14176                        + h->root.u.def.section->output_section->vma
14177                        + h->root.u.def.section->output_offset);
14178       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
14179       rela.r_addend = 0;
14180       loc = htab->relbss->contents;
14181       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
14182       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
14183     }
14184
14185   return TRUE;
14186 }
14187
14188 /* Used to decide how to sort relocs in an optimal manner for the
14189    dynamic linker, before writing them out.  */
14190
14191 static enum elf_reloc_type_class
14192 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
14193                             const asection *rel_sec,
14194                             const Elf_Internal_Rela *rela)
14195 {
14196   enum elf_ppc64_reloc_type r_type;
14197   struct ppc_link_hash_table *htab = ppc_hash_table (info);
14198
14199   if (rel_sec == htab->reliplt)
14200     return reloc_class_ifunc;
14201
14202   r_type = ELF64_R_TYPE (rela->r_info);
14203   switch (r_type)
14204     {
14205     case R_PPC64_RELATIVE:
14206       return reloc_class_relative;
14207     case R_PPC64_JMP_SLOT:
14208       return reloc_class_plt;
14209     case R_PPC64_COPY:
14210       return reloc_class_copy;
14211     default:
14212       return reloc_class_normal;
14213     }
14214 }
14215
14216 /* Finish up the dynamic sections.  */
14217
14218 static bfd_boolean
14219 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
14220                                    struct bfd_link_info *info)
14221 {
14222   struct ppc_link_hash_table *htab;
14223   bfd *dynobj;
14224   asection *sdyn;
14225
14226   htab = ppc_hash_table (info);
14227   if (htab == NULL)
14228     return FALSE;
14229
14230   dynobj = htab->elf.dynobj;
14231   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
14232
14233   if (htab->elf.dynamic_sections_created)
14234     {
14235       Elf64_External_Dyn *dyncon, *dynconend;
14236
14237       if (sdyn == NULL || htab->got == NULL)
14238         abort ();
14239
14240       dyncon = (Elf64_External_Dyn *) sdyn->contents;
14241       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
14242       for (; dyncon < dynconend; dyncon++)
14243         {
14244           Elf_Internal_Dyn dyn;
14245           asection *s;
14246
14247           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
14248
14249           switch (dyn.d_tag)
14250             {
14251             default:
14252               continue;
14253
14254             case DT_PPC64_GLINK:
14255               s = htab->glink;
14256               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14257               /* We stupidly defined DT_PPC64_GLINK to be the start
14258                  of glink rather than the first entry point, which is
14259                  what ld.so needs, and now have a bigger stub to
14260                  support automatic multiple TOCs.  */
14261               dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
14262               break;
14263
14264             case DT_PPC64_OPD:
14265               s = bfd_get_section_by_name (output_bfd, ".opd");
14266               if (s == NULL)
14267                 continue;
14268               dyn.d_un.d_ptr = s->vma;
14269               break;
14270
14271             case DT_PPC64_OPDSZ:
14272               s = bfd_get_section_by_name (output_bfd, ".opd");
14273               if (s == NULL)
14274                 continue;
14275               dyn.d_un.d_val = s->size;
14276               break;
14277
14278             case DT_PLTGOT:
14279               s = htab->plt;
14280               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14281               break;
14282
14283             case DT_JMPREL:
14284               s = htab->relplt;
14285               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14286               break;
14287
14288             case DT_PLTRELSZ:
14289               dyn.d_un.d_val = htab->relplt->size;
14290               break;
14291
14292             case DT_RELASZ:
14293               /* Don't count procedure linkage table relocs in the
14294                  overall reloc count.  */
14295               s = htab->relplt;
14296               if (s == NULL)
14297                 continue;
14298               dyn.d_un.d_val -= s->size;
14299               break;
14300
14301             case DT_RELA:
14302               /* We may not be using the standard ELF linker script.
14303                  If .rela.plt is the first .rela section, we adjust
14304                  DT_RELA to not include it.  */
14305               s = htab->relplt;
14306               if (s == NULL)
14307                 continue;
14308               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
14309                 continue;
14310               dyn.d_un.d_ptr += s->size;
14311               break;
14312             }
14313
14314           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
14315         }
14316     }
14317
14318   if (htab->got != NULL && htab->got->size != 0)
14319     {
14320       /* Fill in the first entry in the global offset table.
14321          We use it to hold the link-time TOCbase.  */
14322       bfd_put_64 (output_bfd,
14323                   elf_gp (output_bfd) + TOC_BASE_OFF,
14324                   htab->got->contents);
14325
14326       /* Set .got entry size.  */
14327       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
14328     }
14329
14330   if (htab->plt != NULL && htab->plt->size != 0)
14331     {
14332       /* Set .plt entry size.  */
14333       elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
14334         = PLT_ENTRY_SIZE;
14335     }
14336
14337   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
14338      brlt ourselves if emitrelocations.  */
14339   if (htab->brlt != NULL
14340       && htab->brlt->reloc_count != 0
14341       && !_bfd_elf_link_output_relocs (output_bfd,
14342                                        htab->brlt,
14343                                        elf_section_data (htab->brlt)->rela.hdr,
14344                                        elf_section_data (htab->brlt)->relocs,
14345                                        NULL))
14346     return FALSE;
14347
14348   if (htab->glink != NULL
14349       && htab->glink->reloc_count != 0
14350       && !_bfd_elf_link_output_relocs (output_bfd,
14351                                        htab->glink,
14352                                        elf_section_data (htab->glink)->rela.hdr,
14353                                        elf_section_data (htab->glink)->relocs,
14354                                        NULL))
14355     return FALSE;
14356
14357
14358   if (htab->glink_eh_frame != NULL
14359       && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
14360       && !_bfd_elf_write_section_eh_frame (output_bfd, info,
14361                                            htab->glink_eh_frame,
14362                                            htab->glink_eh_frame->contents))
14363     return FALSE;
14364
14365   /* We need to handle writing out multiple GOT sections ourselves,
14366      since we didn't add them to DYNOBJ.  We know dynobj is the first
14367      bfd.  */
14368   while ((dynobj = dynobj->link_next) != NULL)
14369     {
14370       asection *s;
14371
14372       if (!is_ppc64_elf (dynobj))
14373         continue;
14374
14375       s = ppc64_elf_tdata (dynobj)->got;
14376       if (s != NULL
14377           && s->size != 0
14378           && s->output_section != bfd_abs_section_ptr
14379           && !bfd_set_section_contents (output_bfd, s->output_section,
14380                                         s->contents, s->output_offset,
14381                                         s->size))
14382         return FALSE;
14383       s = ppc64_elf_tdata (dynobj)->relgot;
14384       if (s != NULL
14385           && s->size != 0
14386           && s->output_section != bfd_abs_section_ptr
14387           && !bfd_set_section_contents (output_bfd, s->output_section,
14388                                         s->contents, s->output_offset,
14389                                         s->size))
14390         return FALSE;
14391     }
14392
14393   return TRUE;
14394 }
14395
14396 #include "elf64-target.h"
14397
14398 /* FreeBSD support */
14399
14400 #undef  TARGET_LITTLE_SYM
14401 #undef  TARGET_LITTLE_NAME
14402
14403 #undef  TARGET_BIG_SYM
14404 #define TARGET_BIG_SYM  bfd_elf64_powerpc_freebsd_vec
14405 #undef  TARGET_BIG_NAME
14406 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
14407
14408 #undef  ELF_OSABI
14409 #define ELF_OSABI       ELFOSABI_FREEBSD
14410
14411 #undef  elf64_bed
14412 #define elf64_bed       elf64_powerpc_fbsd_bed
14413
14414 #include "elf64-target.h"
14415